Skip to content

Should class and style should be included in $attrs? #6144

@mrcsmcln

Description

@mrcsmcln

What 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions