Mackenzie's Site (http://159.223.118.101/blog/2022/11/23/idea-declarative-alternative-to-js.txt) Ideas: Cascading Behavior Sheets, a declarative alternative to JS ================================================================= I have had the idea for some time that the web ought to have a declarative format to define behavior on elements like it does for styles (CSS). It would be an alternative to JavaScript (JS) that would be as robust as CSS, simplifying adding and defining common behaviors. There are a lot of things sites do frequently that can take a fair amount of work for a new person to implement, as well as require a payload sent over the wire. For people who don't need complications beyond standard, this could be provided by the browser with some configuration in a simple sheet. I think there should be a Cascading Behavior Sheet (CBS) standard for the web. Potential advantages: - robust forward and backward compatibility like CSS - simpler, easier to learn format than JS - little to write or think about for common functionality - little to send over wire for common functionality - more performant native implementation possible - declarative - familiar syntax to CSS devs - simple to connect behavior broadly to chosen selectors - cascade, `@media`, `@support`, etc to limit which and when behaviors apply - automatic handling of attaching and removing behaviors when they apply / don't, including new DOM elements - maintain separation of concerns that keeping JS and CSS separate provides I have had various ideas of how this could be implemented over the years, but finding and reading [Tab Atkin's Cascading Attribute Sheets idea](https://www.xanthir.com/b4K_0) and [Dave Rupert's recent post on that](https://daverupert.com/2022/10/use-case-for-cascading-attribute-sheets/) got me thinking about the behavior sheets again and gave me some ideas. It pushed me to finally write out my thoughts. The documents would look and operate very similarly to CSS, with one or more selectors defining what elements a declaration applies to, and one or more properties defining what behavior those elements would get. It would use the same cascade rules as CSS to determine which behavior wins when there are conflicts. It would support media queries, etc. to limit when certain declarations apply. It would allow attaching common built in behavior as well as custom behavior. Built-in custom behavior properties --- Commonly implemented behaviors for elements could be implemented in browsers and enabled and configured with CBS properties. Drag and drop, autocomplete, table sorting, and service workers are some examples that would be good candidates for CBS because they are implemented frequently and the most common implementations are similar enough to be largely handled by an on off switch plus some settings. The browser would implement this behavior as it sees fit and how it believes will best serve its user, taking into account the properties set by the developer in the CBS. ### Dragondrop One common UI example could be drag and drop behavior. In a possible implementation, one could set a `draggable` property to `true` to enable dragging behavior on a set of elements. Related properties could define characteristics of this dragging, like what happens when the user releases the item, if it can be dragged to certain drop targets, a class to add while dragging, and if it will be constrained to a container. An example might look like: ``` css .draggable{ draggable: true; draggable-class: 'draggable-dragging'; draggable-container: '.drag-container'; draggable-end: goback; draggable-target: '.droppable'; } ``` ### Autocomplete Another commonly implemented UI behavior might be an autocomplete widget from a search input. That might look like: ``` css .siteHeader input[type="search"]{ autocomplete: '/search.json'; autocomplete-minlength: 3; } ``` where results would be fetched from the URL '/search.json' once the entered string reaches 3 characters. An `autocomplete` value like `window.searchResults` might tell it to use a JS global variable for results. The JSON result would likely have to be in a standard format. If an array, the value would be used as the display and submit value. If an object map of key-value pairs, the key would be used for display and the value would be used for submit. There might be some property to define a different mapping. If the response is HTML, it would likely be a collection of `