Patterns for passing dynamic options to directives #52973
Replies: 1 comment
-
I like the idea of allowing dynamic options for directives. 😄 Regarding the usage of <a data-wp-navigation-link='{ "prefetch": "hover", "scroll": true }'> ... </a>
<a data-wp-navigation-link='{ "prefetch": "hover", "scrollPayload": "selectors.namespace.scrollPayload" }'> ... </a>
<a data-wp-navigation-link='{ "prefetch": "hover", "scrollCallback": "actions.namespace.scrollToSomeElement" }'> ... </a> If you really need to allow passing different types for the same prop, I'd avoid encouraging the usage of special prefixes. A more straightforward way would be to check if the string starts with <a data-wp-navigation-link='{ "prefetch": "hover", "scroll": true }'> ... </a>
<a data-wp-navigation-link='{ "prefetch": "hover", "scroll": "selectors.namespace.scrollPayload" }'> ... </a>
<a data-wp-navigation-link='{ "prefetch": "hover", "scroll": "actions.namespace.scrollToSomeElement" }'> ... </a> |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Directives that instead of a reference receive a list of options could still accept a reference that returns those options when they need to be modified dynamically.
For example, a directive
data-wp-example
that has a couple of options, like this:Could also accept a reference that returns those options dynamically:
This pattern could also be applied per option.
Whenever it makes sense, instead of the value of the option (or apart from it), the functionality itself could be delegated. For example, imagine this directive that does client-side navigation with options for
prefetch
andscroll
:Instead of passing the option to
scroll
dynamically, the reference could handle the scroll logic itself.The
ref:
prefix is not enforced, and of course, all this logic needs to be implemented inside the directive itself, but if these use cases start to arise, the use of this pattern could become a standard.Beta Was this translation helpful? Give feedback.
All reactions