Should `class` and `style` should be included in `$attrs`?
See original GitHub issueWhat problem does this feature solve?
What’s the rationale for excluding class
and style
bindings from $attrs
? How else would you bind these attributes to wrapped elements?
What does the proposed API look like?
Consider this field
component:
<template>
<div class="field">
<input v-bind="$attrs" class="field__input">
</div>
</template>
<script>
export default {
name: 'field'
}
</script>
Say I’d like to bind the field__input_error
class to the <input>
element when hasError
is true. I’d expect to be able to do it like this:
<field :class="{ field__input_error: hasError }"></field>
But if you wanted to go with the current behavior, you could do this instead:
<template>
<div v-bind="divAttrs" class="field">
<input v-bind="inputAttrs" class="field__input">
</div>
</template>
<script>
export default {
name: 'field',
computed: {
divAttrs() {
return this.$attrs.class
},
inputAttrs() {
const { _, ...attrs } = this.$attrs
return attrs
}
}
}
</script>
I would suggest that we don’t overwrite existing class
and style
attributes as is currently done with other attributes. Also, inheritAttrs
would ideally prevent class
and style
attributes from being passed to the root element when set to false
.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:12 (3 by maintainers)
Top Results From Across the Web
$attrs includes class & style - Vue 3 Migration Guide
Attributes in $attrs are no longer automatically added to the root element, leaving it to the developer to decide where to add them....
Read more >Why would I ever use a class over an attribute in HTML and ...
I noticed that a lot of help online uses classes but I have generally used attributes (like the query selector [my-attr]) to style...
Read more >$attrs includes class & style - Vue.js
$attrs now contains all attributes passed to a component, including class and style . # 2.x Behavior. class and style attributes get some ......
Read more >attrs by Example - attrs 22.2.0 documentation
attrs offers two ways to define validators for each attribute and it's up to you to choose which one suits your style and...
Read more >Using Style Classes and IDs in HTML and CSS - ThoughtCo
Class and ID Attributes ... Designers sometimes must apply a style on only some of the elements in a document, but not all...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Can this be reopened? If we can have the freedom to attach
$attrs
to the element we want, why can’t we be given the same freedom for$styles
?This has been causing me grief too, I’m having to use props with un-obvious names to pass class attributes down to my reusable components.
yyx990803 you stated “When authoring components, I don’t think it’s a good idea to encourage the user to directly style the component via classes or inline styles.” But in my mind the whole point of making reusable components that are flexible is that they can be styled by the container, thus making them easy to use without refactoring every time you want to change say the background colour or text size.
Just the name of the property ‘inheritAttrs’ should imply exactly that, maybe it should be renamed ‘inheritMostAttrs’ that way new users would get a clue!
Having to write computed properties in 90% of my components to deal with applying classes and styles to the inner elements is quite frankly very annoying and wastes time when you forget.
Personally I think the default behaviour should be changed to exclude all attributes if the flag is false, however this would now be a breaking change so how about a related property ‘inheritStylingInAttrs’ which would default to false and not include them, setting it to true would force the behaviour we are all looking for and would have expected in the first place.