Skip to content

Conversation

@allmarkedup
Copy link
Owner

@allmarkedup allmarkedup commented Aug 10, 2025

This PR introduces an alternative, more concise binding syntax that can optionally be used instead of the standard binding descriptor data-bind-attr style, as suggested by the proposal here.

This alternative style takes the format sx-[attributeName]="identifier#propertyName". In contrast to the regular syntax, the name of the attribute to bind to the value is part of the descriptor attribute name, rather than being specified as part of the attribute value.

<!-- 'regular' attribute binding syntax -->
<div data-bind-attr="hidden~my-controller#isHidden"></div>

<!-- new, more concise attribute binding syntax -->
<div sx-hidden="my-controller#isHidden"></div>

Similarly, text content and inner HTML bindings can be applied using sx-text and sx-html attributes in place of the standard data-bind-text and data-bind-html attributes, if desired.

 <!-- text content binding -->
<h1 sx-text="my-controller#title"></h1>

 <!-- HTML binding -->
<article sx-html="my-controller#articleContent"></article>

Both syntaxes are always supported on all elements. There is no configuration required to switch between the two styles.

Customising the attribute prefix

The attribute prefix (by default sx-) can be customised when initialising StimulusX if required.

StimulusX.init(Stimulus, { shorthandAttributePrefix: "data-sx-" }); 
 <!-- binding attributes now prefixed by `data-sx-` instead of `sx-` -->
<div data-sx-hidden="my-controller#isHidden"></div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants