Replies: 3 comments 4 replies
-
For me personally having a component with a clear discrete API is much more readable and reliable than using a directive with many options to configure. Directives don't have any contracts and maintaining complex APIs would be much harder without any contracts. I actually think that custom directives should be replaced with components in almost all cases, since it's just much easier to maintain and especially debug. To compare with your example: <div
v-motion:initial="{ x: -100, opacity: 0 }"
v-motion:enter="{ x: 0, opacity: 1}"
v-motion:exit="{ x: +100, opacity: 0 }"
/> The component-based variant reads much better to me: <motion
:initial="{ x: -100, opacity: 0 }"
:enter="{ x: 0, opacity: 1}"
:exit="{ x: +100, opacity: 0 }"
>
<div />
</motion> And I think a wrapper component is a reasonable price to pay for significant increase in reliability and readability. |
Beta Was this translation helpful? Give feedback.
-
How would this work with other directives that you can apply multiple times? For example, I have an idea, based on composables: Define the directive in a scope (e.g. a file) that it uses as a closure, the same as a composable can be used as an app-wide store. An object or array is defined in this closure, which will store data. N.B. Written in a hurry before I go out for the day. May be slightly flawed. Will add code example. and maybe try it out. |
Beta Was this translation helpful? Give feedback.
-
I think that maybe these solutions will suite for your idea:
<v-motion
:v-motion-initial="{ ... }"
/> Web Components use that convention with custom element names to avoid the possibility to overlap native HTML tags, but being a convention means that it's not mandatory.
And in the HTML should look like like this: <v-motion
:v-motion[initial]="{ ... }"
:exit="{ ... }"
/> |
Beta Was this translation helpful? Give feedback.
-
Just starting a conversation to gather ideas.
Directives can vary in complexity.
The most basic may take no value at all:
v-autofocus
.Many take just one value:
v-show="false"
.And a directive that takes a few (maybe optional) parameters usually uses an object literal:
v-tooltip="{ message: "Help!", position: "top" }"
.Growing further in complexity, for example having many properties or taking complex objects as parameters, is not great.
A real example of such a directive is
vue-motion
(from Tahul, not the one from Posva).Things that work and are not hacks:
Not pretty at all.
Obscures what parameters are used and what is their source data when reading the template. May warrant an additional
computed
.Much more powerful, but adds layers of wrappers in the template, doesn't always feel like the right solution.
A hack that works (what
vue-motion
does):Put your state in attributes and sniff their values in the directive itself.
For example
vue-motion
puts objects that describe theinitial
,enter
,exit
variants (and several others) each in their own bound attribute:That is clever but it has some drawbacks:
active
could lead to clashes between libs.(Can be fixed by using more specific names, e.g.
motion-initial
).Idea:
Maybe Vue could help split state of heavy-duty directives, e.g. by using the namespace syntax:
And each of those could be made available together (maybe in one object with those keys) to the
v-motion
directive (and that directive only).Note that in the case of
vue-motion
that got rid of the emptyv-motion
directive, since now the presence of the directive is already implied by the various arguments.I think that syntax is actually not too bad even for "basic" directives that take some modifier with a value.
For example you could consider:
Beta Was this translation helpful? Give feedback.
All reactions