Collision of nested css variables when using v-bind in css #7735
Replies: 5 comments 5 replies
-
According to docs, you are using an incorrect syntax. You have to wrap JS expression in quotes margin-bottom: v-bind('props.space'); |
Beta Was this translation helpful? Give feedback.
-
Yes, you are right. I simplified the component to make the example short, and made few typos by doing that. However, even with your fix, that does not solve the problem of css variables being overwritten. |
Beta Was this translation helpful? Give feedback.
-
From what I can tell, you are using the same component recursively. That is an expected behavior because according to the HTML output, they don't have anything that makes it different from another. However, when you pass the variable "space" with a valid size value, it should add a Here https://stackblitz.com/edit/vitejs-vite-eemlts?file=src/App.vue Output HTML <div data-v-2069ff04="" class="stack" style="--2069ff04-props\.space: 10px">
<div data-v-2069ff04="" class="stack" style="--2069ff04-props\.space: 20px">
<div
data-v-2069ff04=""
class="stack"
style="--2069ff04-props\.space: 30px"
>
<div
data-v-2069ff04=""
class="stack"
style="--2069ff04-props\.space: 40px"
>
<div
data-v-2069ff04=""
class="stack"
style="--2069ff04-props\.space: 50px"
></div>
</div>
</div>
</div>
</div> |
Beta Was this translation helpful? Give feedback.
-
See this example: https://stackblitz.com/edit/vitejs-vite-d2ffto?file=src%2FApp.vue,src%2FStack.vue The goal is to have two stacks with 100px space between them. Each stack item contains another stack that should have items 10px apart. But the result is that everything is just 10px apart. |
Beta Was this translation helpful? Give feedback.
-
It shouldn't matter that they have the same generated name. In fact, you want them to. The CSS for the component should be static i.e. with the same generated variable names. In contrast, the values should be dynamic, which is what the style injection does. If it's not doing what you want, it's not because of the v-bind behavior. |
Beta Was this translation helpful? Give feedback.
-
Hi,
I am building a component library in Vue 3, and I am utilizing v-bind in CSS. I have an issue when I nest the same component.
Example: I have a component, Stack. It is a layout component whose only purpose is to set a distance between the “stack” of elements. The simplified version would look like this:
The problem is, that when nested, all components have the same ID and therefore the CSS variable is being overwritten:
As you can see above, both nested divs are using the same CSS variable name --0e1a8256 so it is overwritten. Is there a way how to prevent this from happening? Can I somehow force Vue to use a different ID for every new component instance? Or is there a better way?
Thank you for any advice
Beta Was this translation helpful? Give feedback.
All reactions