Differentiating between suffixes and unique-ids in the directive names #62720
Replies: 3 comments 6 replies
-
It's a difficult task to put a lot of data into strings. There's risk of them becoming inscrutable or ambiguous. I do think a clear specification on exactly what the parts of directive attributes signify and how they'll be processed will be helpful for the Interactivity API and users. If we're adding another part to the directive, why not use something other than more dashes? I don't think we're limited to just ascii lowercase and dashes, underscores and more characters should be perfectly fine. I looked at this recently:
What about a different character like
https://core.trac.wordpress.org/ticket/61052 and WordPress/wordpress-develop#6429 contain some related work. I'm worried that the term "namespace" is overloaded. Are these attached to Interactivity API store namespaces in some way? For many of the directives, their only purpose to to make the attributes unique, right? This might also be called a unique ID or key. |
Beta Was this translation helpful? Give feedback.
-
There's an important case to consider where style directives are used with CSS custom properties to get a suffix that starts with
We might wind up with |
Beta Was this translation helpful? Give feedback.
-
An alternative to having to add unique ID suffixes for all of the directives would be to allow the directive values to be comma-delimited. So instead of: <div
data-wp-on-click--foo="foo::actions.handleClick"
data-wp-on-click--bar="bar::actions.handleClick"
>
</div> It could instead be: <div
data-wp-on-click="foo::actions.handleClick, bar::actions.handleClick"
>
</div> This is how AMP addressed the issue of associating multiple actions with a directive. For augmentation then, adding a new directive would be a matter of seeing if the directive already exists, and if so and then appending $directives = $p->get_attribute( 'data-wp-on-click' );
if ( $directives ) {
$directives .= ',';
} else {
$directives = '';
}
$directives .= 'myPlugin::action.handleClick';
$p->set_attribute( 'data-wp-on-click', $directives ); or $directives = $p->get_attribute( 'data-wp-on-click' ) . ',' . 'myPlugin::action.handleClick';
$p->set_attribute( 'data-wp-on-click', ltrim( $directives, ',' ) ); or $directives = (array) $p->get_attribute( 'data-wp-on-click' );
$directives[] = 'myPlugin::action.handleClick';
$p->set_attribute( 'data-wp-on-click', implode( ', ', array_filter( $directives ) ) ) |
Beta Was this translation helpful? Give feedback.
-
When attempting to augment an existing block with interactivity, all directives should include a unique-id to prevent collisions.
This is @westonruter from a conversation in Slack:
That means all directives should support unique-ids, maybe even by default.
As of today, only some directives support unique-ids but they use the same syntax as directives that use suffixes. For example:
data-wp-on--click
<-click
is a suffixdata-wp-watch--my-plugin
<-my-plugin
is a unique-idSo far, this has worked well.
What we've done in the directives with suffixes is to add the unique-ids at the end:
data-wp-on--click--my-plugin
However, when the suffix is optional and we also want to add unique-ids, a conflict arises.
Imagine we want to add unique-ids to
data-wp-class
, but we also want it to be usable without a suffix (passing it an object of classes that can be true or false):data-wp-class--is-red="state.isRed"
<- This works fine.data-wp-class="state.classes"
<- This works fine.data-wp-class--my-plugin="state.classes"
<- This doesn't work as expected, becausemy-plugin
would be the name of the class (suffix), not the unique-id.The same would happen if we want to use a suffix in
data-wp-context
to facilitate the writing of contexts with a single property:data-wp-context--is-open="false"
<- This would be the equivalent ofdata-wp-context='{ "isOpen": false }'
data-wp-context--my-plugin='{ "isOpen": false }'
<- This won't work as expected becausemy-plugin
would be themyPlugin
property, not the unique-id.So maybe we should standardize the syntax of the unique-ids and differentiate it from the suffixes.
The first idea that comes to mind is to use a triple dash:
---
.data-wp-directive--suffix
data-wp-directive---unique-id
data-wp-directive--suffix---unique-id
So, there would no longer be a conflict to differentiate between suffix and unique-ids in directives where the suffix is optional:
data-wp-class--is-red="state.isRed"
<- This works fine.data-wp-class---my-plugin="state.classes"
<- This works fine.data-wp-context--is-open="false"
<- This works fine.data-wp-context---my-plugin='{ "isOpen": false }'
<- This works fine.We would need to check if we can make the change in a way that is backward compatible with the directives that are currently using unique-ids (and keep also the
--
), like:data-wp-watch--unique-id
->data-wp-watch---unique-id
data-wp-init--unique-id
->data-wp-init---unique-id
data-wp-on--suffix--unique-id
->data-wp-on--suffix---unique-id
Thoughts? Do you see any impediments? Any alternative syntax?
Beta Was this translation helpful? Give feedback.
All reactions