-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add the ability to add classes to slots #4443
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
Comments
@tiddle The current way to achieve this is with <MyComponent>
<ul slot="list" let:class={className} class={className}>
...
</ul>
</MyComponent> I think this limitation makes sense because the component doesn't know anything about the child slots. It's up to the component user to figure out what to do with the slot props. |
@jesseskinner I think you don't understand the OP's problem. Just like mine, I don't know what child component will be passed. Regardless of the passed child component inserted into the slot, I need to style it. E.g. I can't add special props and keywords to every single component I have and will ever create for this to work. |
@GitGangGuy Sure, you want to be able to control the element used for the slot from the parent. Right now the syntax I used above is valid, where slot props become available through the My concern is that this new feature would not be backwards compatible, because it would break my example code above. Would |
@jesseskinner I understand your concerns and I don't have a solution, but another use case for passing attributes through, would be transitions. Currently, you can't do |
@tiddle True.. actually maybe that could be a constraint of the new feature, that only directives get passed down to the slot element. That'd work for class too: instead of |
Named slots do currently always have a single root element, but this is a limitation of the current syntax and not a design feature. (Default slots, by contrast, already do not necessarily have a single root element.) We don't want to rely on there being a single root element - or prevent ourselves from ever implementing named slots without a single root node - and so there's not anything to apply the class to or the transition to. It wasn't popular when treating |
@Conduitry I don’t think that the possibility of multiple root elements is a problem here. In my case, if there are multiple root elements, I would like to apply the class to all those root elements with which the |
Instead of using a class one can also use the css attribute selector with the slot name in combination with the :global() modifier. Example: <MyComponent>
<p slot="mySlot">world</p>
</MyComponent> MyComponent.svelte <div>
Hello
<slot name="mySlot" />
</div>
<style>
:global( [slot="mySlot"] ) { ... }
</style> This makes the additional wrapper around the slot obsolete |
@jesseskinner This seems to do the job. Just wrap the slot in a div with the desired class. <div class="my-list-style">
<slot name="list" >
<p>No items found</p>
</slot>
</div> |
this isn't possible tho when using named slots, which only allow for one subelement to be passed |
@gianmarco27 but what is your point, mate? He is actually passing a single element to the named slot in the provided example. It might not be useful for every single scenario, but in my case it solved my problem. Being able to only pass a single element to a named slot is not a limitation of the solution he suggested, but of Svelte itself. EDIT: I just remembered that in those cases where you need to add more that one element to a named slot but don’t want an additional element wrapping them up, you can use a <svelte:fragment name="my-slot">
<span>Element 1</span>
<span>Element 2</span>
<span>Element 3</span>
</svelte:fragment> |
Is your feature request related to a problem? Please describe.
I want to be able to add classes to slots so that i can style them directly.
Describe the solution you'd like
App.svelte
MyComponent.svelte
Describe alternatives you've considered
This alternative works, but i still can't style the
ul
in my example correctly and it feels like it renders the fallback slot functionality useless.How important is this feature to you?
There are work arounds, but nothing clean. I just feel like this should be functionality that should be part of the
slot
feature.The text was updated successfully, but these errors were encountered: