SvelteKit styling and CSS classes #2092
Replies: 1 comment 6 replies
|
The trick is to stop thinking of these as component-scoped styles and lean into global classes for anything you want to reuse — which works cleanly because Bits UI forwards Reusable CSS classes without the utility spamPut the classes you want to reuse in a global stylesheet ( <Accordion.Trigger class="accordion-trigger" />/* app.css — global, so never scoped away, never tree-shaken */
.accordion-trigger { /* ... */ }
.accordion-trigger[data-state="open"] { /* ... */ }This is exactly the "traditional web dev" model you're after. The reason your instinct to use component- Reach for a one-off scoped class only when it's genuinely one-off; for that case either use a utility ( <Dialog.Overlay class="overlay" />
<style>
:global(.overlay) { /* applies even though .overlay lands on a child element */ }
</style>PortalsPortalled content (Dialog/Popover/Tooltip content, etc.) is moved to
Either way the class ends up on the real portalled node (Bits UI forwards it), and because the rule is global it applies regardless of where in the DOM the portal is mounted. So portals aren't a special case in your mental model — they're just the clearest reason the reusable-styling layer needs to be global rather than component-scoped. Net: global stylesheet for the reusable stuff, |
Uh oh!
There was an error while loading. Please reload this page.
Under Svelte, CSS classes are scoped to the elements they are applied to, and CSS selectors that don't utilize a scoped class are hidden from the build. Controversially, components have no way to signal to Svelte a CSS class is in use, so any selectors using classes passed to a component via a property will not be processed. Not here to debate on that in particular, just looking for answers and providing background.
Recommendations around both Bits-UI and Svelte make it clear the options available:
All the basic examples in Bits-UI documentation seem to expect the latter of the three. Which is fine, but I really don't like the look of huge identical class lists when repeating the same component over and over. I could compartmentalize the utility classes into custom components, but I really don't want to have to basically duplicate a component with no other changes than to apply a preset list of styles, maybe with built-in variants determined by a prop.
So my question is, what's the best way forward here to achieve the most favorable result, given my criteria—simple project structure with no extraneous components or utility class spam? What I'd really like to do is to use utility classes for one-off instances, and make CSS classes to handle the rest, like traditional web development. This may not be very "Sveltey", but I can live with that.
Also, how should I handle out-of-scope components, like portals? They are removed from the flow of the page and parented directly to the
body, so styles have to be re-defined for them, and they can't have styles composed under the main component scoped class. How does that fit into all of this? While I think I get why it was done this way, I can't say it isn't annoying.All reactions