Skip to content

Allow class as valid component prop #11132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fastfedora opened this issue Feb 20, 2020 · 3 comments
Closed

Allow class as valid component prop #11132

fastfedora opened this issue Feb 20, 2020 · 3 comments

Comments

@fastfedora
Copy link

What problem does this feature solve?

It allows for proper encapsulation of components. Currently the class and style props cannot be intercepted or prevented from being injected into the root element of a component. This prevents the ability to refactor components without requiring the calling code to be changed.

For example, assume a Logo component which is used with a class attribute:

<my-logo class="app-logo" />

With the template:

<template>
   <img src="my-logo.png" />
</template>

Due to the merging of the class attribute the rendered tag will be:

   <img class="app-logo" src="my-logo.png" />

Now, let's assume we want to refactor this component so it lazily loads the image. Our new template is:

<template>
   <div>
      <my-loader v-if="loading" />
      <img v-else src="my-logo.png" />
   </div>
</template>

Now the class attribute is put on the div, which may not be appropriate. With good encapsulation, there would be a way to define class as a prop to intercept the value being passed in and render it on a different element than the root element, such as:

<template>
   <div> a
      <my-loader v-if="loading" />
      <img v-else :class="class" src="my-logo.png" />
   </div>
</template>

Currently, attempting to define class as a prop displays this warning on the console:

[Vue warn]: "class" is a reserved attribute and cannot be used as component prop.

While inheritAttrs can be set to false to disable most props from being passed through to the root element, it does not apply to class or style.

What does the proposed API look like?

Ideally simply allow class and style as valid Vue props that can be used to intercept the values passed in (that is, they don't pass through to the root element when these are defined as props).

If that is not possible for backward-compatibility reasons, add a flag similar to inheritAttrs that enables this functionality (e.g. enableClassAndStyleProps).

@posva
Copy link
Member

posva commented Feb 20, 2020

This would be a breaking change for Vue 2 so it won't happen, but it already works that way on Vue 3 when declaring a prop named class.
I personally don't think it is worth adding yet, another option that goes with inheritAttrs to prevent class and style from falling through, but let's see what other people feedback is

@geongeorge
Copy link

You can make the other case of having to inherit class and style props to the component root element as a feature.
This is not worth breaking the code

@fastfedora
Copy link
Author

Since it works correctly in Vue 3, we can just wait until that version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants