Chrome 135 संस्करण से बटनों में `command` और `commandfor` पेश
(developer.chrome.com)- dynamic web applications बनाने के लिए बटन अनिवार्य हैं। इनका उपयोग menu खोलने, actions switch करने और form submit करने में होता है
- Chrome 135 में नए
commandऔरcommandforattributes, पुरानेpopovertargetactionऔरpopovertargetattributes को बेहतर बनाते हैं और उनकी जगह लेते हैं - पहले button behavior implement करते समय आने वाली समस्याएँ:
- HTML के
onclickhandler पर security policy (CSP) के कारण वास्तविक code में उपयोग की पाबंदियाँ हो सकती हैं - बटन और दूसरे elements की state sync करनी पड़ती है, और accessibility बनाए रखते हुए state manage करने वाला code जटिल हो जाता है
- React, AlpineJS, Svelte आदि में भी state और event handling जटिल रहती है
- HTML के
command और commandfor पैटर्न
commandऔरcommandforattributes का उपयोग करने पर बटन दूसरे elements पर declarative तरीके से काम कर सकते हैं। इससे framework जैसी सुविधा मिलती है, जबकि flexibility भी बनी रहती हैcommandforबटन ID का उपयोग करता है (forattribute की तरह), औरcommandbuilt-in values लेता है, जिससे तरीका अधिक intuitive बनता है- उदाहरण: menu खोलने वाला बटन implement करना
aria-expandedया अतिरिक्त JavaScript की ज़रूरत नहीं
<button commandfor="my-menu" command="show-popover"> Open Menu </button> <div popover id="my-menu"> <!-- ... --> </div>
command और commandfor बनाम popovertargetaction और popovertarget
- अगर आपने
popoverइस्तेमाल किया है, तो आपpopovertargetऔरpopovertargetactionattributes से परिचित हो सकते हैं - ये
commandforऔरcommandकी तरह काम करते हैं, लेकिन खास तौर पर popover के लिए बने हैं - नए attributes पुराने attributes को पूरी तरह replace करते हैं और अतिरिक्त functionality भी देते हैं
built-in commands
commandattribute में ऐसे actions built-in हैं जो अलग-अलग APIs से map होते हैंshow-popover:el.showPopover()से map होता हैhide-popover:el.hidePopover()से map होता हैtoggle-popover:el.togglePopover()से map होता हैshow-modal:dialogEl.showModal()से map होता हैclose:dialogEl.close()से map होता है
- उदाहरण: delete confirmation dialog implement करना
- JavaScript के बिना state और accessibility manage की जा सकती है
<button commandfor="confirm-dialog" command="show-modal"> Delete Record </button> <dialog id="confirm-dialog"> <header> <h1>Delete Record?</h1> <button commandfor="confirm-dialog" command="close" aria-label="Close"> <img role="none" src="/close-icon.svg"> </button> </header> <p>Are you sure? This action cannot be undone</p> <footer> <button commandfor="confirm-dialog" command="close" value="cancel"> Cancel </button> <button commandfor="confirm-dialog" command="close" value="delete"> Delete </button> </footer> </dialog>- result handling code: dialog के close event में return value को process किया जा सकता है
dialog.addEventListener("close", (event) => { if (event.target.returnValue === "cancel") { console.log("Cancel was clicked"); } else if (event.target.returnValue === "delete") { console.log("Delete was clicked"); } });
custom commands
- built-in commands के अलावा
--prefix का उपयोग करके custom commands define किए जा सकते हैं - custom commands target element पर
"command"event trigger करते हैं, लेकिन कोई अतिरिक्त logic execute नहीं करते - उदाहरण: image rotation command implement करना
<button commandfor="the-image" command="--rotate-landscape"> Landscape </button> <button commandfor="the-image" command="--rotate-portrait"> Portrait </button> <img id="the-image" src="photo.jpg"> <script type="module"> const image = document.getElementById("the-image"); image.addEventListener("command", (event) => { if (event.command === "--rotate-landscape") { image.style.rotate = "-90deg"; } else if (event.command === "--rotate-portrait") { image.style.rotate = "0deg"; } }); </script>
Shadow DOM में command handling
- Shadow DOM में
commandforID के आधार पर काम करता है, इसलिए ये सीमाएँ हैं:- अलग-अलग Shadow DOMs के बीच element reference नहीं किया जा सकता
- ऐसे मामले में JavaScript API का उपयोग करके
.commandForElementproperty set की जा सकती है
- उदाहरण: Shadow DOM में command connect करना
<my-element> <template shadowrootmode="open"> <button command="show-popover">Show popover</button> <slot></slot> </template> <div popover><!-- ... --></div> </my-element> <script> customElements.define("my-element", class extends HTMLElement { connectedCallback() { const popover = this.querySelector('[popover]'); this.shadowRoot.querySelector('button').commandForElement = popover; } }); </script>
आगे की योजना
- Chrome आगे और built-in commands जोड़ने की योजना बना रहा है:
<details>element को खोलना और बंद करना<input>और<select>में"show-picker"command support<video>और<audio>playback commands- elements से text copy करने की सुविधा
1 टिप्पणियां
Hacker News टिप्पणियाँ
प्रोग्रामिंग भाषा सिद्धांतकार 80 के दशक से
gotoके एक अधिक शक्तिशाली संस्करणcomefromके बारे में अटकलें लगाते रहे हैं। इसे केवल intercal में लागू किया गया था। intercal, C जैसी भाषाओं की तुलना में सुरक्षा, प्रदर्शन और एर्गोनॉमिक्स के मामले में बेहतर है, लेकिन व्यावसायिक बाज़ार में प्रवेश करने में इसे कठिनाई हुई है। यह देखना दिलचस्प है कि javascript, intercal की इस सुविधा को अपना रहा है। उम्मीद है कि इससे शिष्ट प्रोग्रामिंग में उछाल आएगा, जैसे javascript के closure-आधारित objects ने functional programming को मुख्यधारा में पहुँचाया थाInvokers केवल Chrome की चीज़ नहीं हैं। Firefox nightly में भी ये पहले से उपलब्ध हैं
JS के बिना declarative UI behavior लागू करने का विचार आकर्षक है
aria-expandedको manipulate करने की ज़रूरत नहीं)show-modalजैसे built-in commands accessibility को markup में एकीकृत करते हैं--rotate-landscape) components को HTML के ज़रिए API expose करने देते हैंसवाल:
.commandForElementसेट करने के लिए JS चाहिए। यह आधा-हल हुई समस्या जैसा लगता हैshow-picker,toggle-details) जोड़ता है, तो क्या platform niche syntax से फूला हुआ हो जाएगा?spec:
commandforattributecommandattributeक्या यह वही action/messaging pattern है जिसका इस्तेमाल Next, Be, Apple आदि लगभग 30 साल पहले करते थे, या मैं कुछ मिस कर रहा हूँ
Netscape के शुरुआती Java UI toolkit (IFC) ने action elements को wire up करने की सुविधा दी थी
नए
commandऔरcommandforattributes,popovertargetactionऔरpopovertargetattributes को बेहतर बनाते हैं और उनका स्थान लेते हैंstrings के ज़रिए programming करने से मुझे पूरी तरह एलर्जी है। accessibility के फ़ायदे समझ में आते हैं, लेकिन web app behavior की एक और layer के लिए element IDs का इस्तेमाल मुझे खास उत्साहित नहीं करता
इसे पूरे API के बिना लागू नहीं करना चाहिए था। लगभग 5 commands की बजाय, ऐसा लगता है जैसे HTML के ज़रिए JavaScript की सारी functionality लागू की जा सकती है। इसका मतलब हज़ारों commands हो सकता है
HTML में
command and conquerको लेकर उम्मीद थीHTML को बेहतर और विस्तारित करना अच्छी बात है, लेकिन अभी बहुत लंबा रास्ता बाकी है। HTMX टीम के पास कुछ अच्छे ideas हैं