-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Class directives for components #4749
Conversation
I played around with the tests a bit in your branch. It looks like this specifically alters the That is, if I remove edit: also thank you for implementing this. I'll definitely find it useful :) |
Yes, that's right. It gives the consuming component freedom of what to actually do with the classes. |
Thank you for your PR - but I'm a bit confused about the purpose of this PR. Couldn't you just implement the same thing by yourself with a prop called "class"? I'm not sure this PR is the right solution for a number of reasons, but feel free to correct me:
|
My take is that it's essentially an shortcut to let you write The "when does this apply" question does present some challenges. On the one hand, it's an unofficial but widely-used practice to do There are also challenges in changing this feature to support components that don't export a In my mind, the primary argument for this is that it's a very common thing to need and cleaner than the current alternative of string manipulation or wrapping the child component in a Having thought through a lot of this as I write this comment. I like the convenience of this shorthand and the consistency of being able to use |
I've added code that warn's in dev mode if a class directive is used on a component that doesn't export a class property. This will erroneously warn if the the component spreads props instead of explicitly passing the class but I'm not sure if not warning when @antony I've tried my best to answer your concerns and better explain my thought process.
Yes and no. This implementation already does require a class prop. Technically I could have the same functionality by preforming the string manipulation myself user side but it's not as clean and can have a surprising number of edge cases when implemented naively. The primary motivation for this change is to have the same behavior between dom elements and wrapper components. Class directives are extremely convenient but that convenience is lost when a section of code needs to be converted to a component. The nice thing with this implementation is that it doesn't have any overhead or modified behavior for code that doesn't use the feature. If you don't like, no harm done, you can just ignore it.
I don't know I would say that the behavior is any different. The implementation is different sure, but the user facing functionality is the same: conditionally modifying the class "attribute". Yes components handle that class differently than dom elements but from the users perspective the directive is still doing the same thing. Additionally in most cases where the class prop is applied to the root dom element than the end result is also the same.
You're not trying to pass a class to a dom element. You're passing a class to a component. It's up to the component to define what that means for the components use case. In most cases it would be passed to a dom element. From the components point of view it's no different than if I were to explicitly pass a class prop.
Any side-effects this causes would have to be documented by the component but again this is no different than the current situation.
Warning when a component doesn't export a class property should help with this. It still doesn't guarantee implementation but it would no longer silently fail. It sounds like most of the objections revolve around the idea that this might not work exactly the same for all components. The thing is, this is true for any framework feature a component uses. The framework gives component authors the tools to write components, and to write the api to consume those components, it's up to the authors themselves to use those tools to the fullest and implement best practices. Passing the class prop to the root dom element is already a wide spread practice and I think a section in the tutorial encouraging component authors to do so as well and good documentation of this feature would make this a very useful feature to have. |
Adding my 2 cents to the discussion. Adding a class prop to a component doesn't necessarily mean it should apply the style to the root element, but it makes sense that it should apply it the main element visually. Let's take a modal component as an example.
When a user passes a class prop, I think we all agree that the normal behavior is to apply it to the And normally, accepting a class prop is documented in the component's props, so passing a class prop to a component that doesn't utilize it, and see no effect is not surprising at all, in the sense that passing any undocumented prop normally mean that it will have no effect. |
I'm a bit against this. Svelte currently has no opinion as to what you store in an exported Also, what if I want to name my I'd be less hesitant about component class directives if either:
This might be nit-picky, but I think a valid concern. |
Sorry to let this PR hang around for so long, but I'm going to close this. I don't think we want to do anything to make people think we have special handling for the |
This PR implements
class:
directives for components. This is especially useful for components that are a simple wrapper around a dom element.