-
-
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
Injecting styles into components #12802
Comments
Not every component has a single wrapper around them so in a component like this <label></label>
<input /> on which element does the style goes? However maintainers are kinda thinking about some solution that could help with this situation but definitely for svelte 5.x |
Here's the translation of your Korean text into English, formatted in a more readable Markdown style: Injecting Styles into ComponentsThere are a few different cases to consider: Idea when returning a single element: Styles are always injected into the top-level element. You cannot inject styles into other elements because
<Border><nav>~~</nav></Border>
<style>
$Border {position:absolute;}
</style> You can specify it in this format.
<script>
const { tag = 'div', classes } = $props();
</script>
<div class="outer">
<svelte:element this={tag} class="inner" class={ classes } >
{@render children()}
</svelte:element>
</div>
<Border tag='nav'>~~</Border>
<style>
$Border {position:absolute;}
$Border nav {width:200px; height: 100lvh;} // You need to know that 'nav' exists inside through the usage of the Border component.
</style>
<script>
const { tag = 'div', classes, icon } = $props();
</script>
<div class="outer">
<svg>
{icon ?? <text>no-icon</text>}
</svg>
<svelte:element this={tag} class="inner" class={ classes } >
{@render children()}
</svelte:element>
</div>
<Border tag='nav'>~~</Border>
<style>
$Border {position:absolute;}
$Border > *:last-child {{width:200px; height: 100lvh;} // 'nav' will be styled.
</style>
<div>
header
</div>
<div class='inner'>
{@render children()}
</div>
<div>
footer
</div>
<Border>
<BorderElement>ul~li~~</BorderElement>
</Border>
<style>
$BorderElement{ color : pink } // all style
$BorderElement:first-child { color: red; }
$BorderElement:last-child { color: green; }
$BorderElement:nth-child(2) { color:blue }
</style> However, the current approach seems a bit awkward. It needs to be supplemented with better syntax... |
Describe the problem
Hello,
I always style my components with a no-JS environment in mind. I only use scripts to control the on/off state of styles when absolutely necessary.
As a result, I end up using
:global
quite a lot when applying styles to componentized elements.Border.svelte
+page.svelte
Describe the proposed solution
If components were designed to allow for style injection, the use of
:global
could be minimized, and the code would become more robust against errors since styles wouldn't rely on text literals for selection.Border.svelte
+page.svelte
Importance
nice to have
The text was updated successfully, but these errors were encountered: