-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Reusable components for different projects #3000
Comments
Have you seen #1550 and the discussion there? |
Yes, it's not about theming, it's about reusable and light components |
@elite174 I have faced a similar issue and I solved it like this: Button.svelte<script>
export let theme;
</script>
<style>
button[theme="primary"] {
background: tomato;
}
button[theme="text"] {
background: blue;
}
</style>
<button theme={theme}>Button</button> App.svelte<script>
import Button from "./Button.svelte";
</script>
<Button theme="primary" />
<Button theme="text" /> |
@s0kil |
Svelte will automatically remove unused styles. Let's say in one component I use |
Omg |
@elite174 I was wrong, when the attribute is set dynamically, all the styles get included. Only on static attributes, unused CSS is removed. <style>
.primary {
background: tomato;
}
.text {
background: blue;
}
</style>
<!-- `.primary` won't be included in bundle -->
<button class="text">Button</button> |
Aha! So my point is that it would be great if there is a mechanism to use only necessary things, because without this features you won't be able to use svelte in a big company that has many projects and one repo with components for all the projects, because the components will be too heavy. |
So, look at the BEM. I can easily turn my components into "building blocks" and use only necessary things. Svelte doesn't allow to do that. |
Here is another option, you could use css-in-js, Here is a blog post by Rich, and using babel-plugin-emotion will remove dead code. |
I agree, I don't like JS runtime for CSS. |
Ok, we are talking about css, but there is also functionality issue. For instance, I have basic component and I want to enhance it. I also want several modifications that can be used in composition. Is it possible in svelte? |
|
I think in simplistic way what you can use is:
Choose the theme you want by including the CSS theme file. |
@sahajre That is a decent solution, unless you have a large application, and you actually care about performance. |
What do you mean? Just curious. Are browsers dumb and try to download (and parse) same file multiple times? I thought I read that Chrome attempts to avoid redundant parsing at least (WICG/webcomponents#282 (comment)).
Care to elaborate? There is a lot of overlap between theming, styling, and reusable components. |
I've found using functional/atomic classes work better than BEM with the dynamic nature of svelte. E.g., classes like .bg-primary .radius-sm .shadow-md can be defined globally and then dynamically set with something like .bg-{variant} anywhere you need it without even including a style block per component. You can also set overriding styles on svelte:head or spread an object of conditional classnames pretty easily when needed. |
Styling custom components is surprisingly complicated, especially if trying to avoid global styles with naming conventions and instead leverage the shadow DOM. Maybe it’s not worth it? First I've got default and core style inside component style block (some of which should not be overridden). Next component is theme-able so needs to respond to a theme stylesheet, which I guess is going to do so via CSS vars since ::part and ::theme aren’t browser ready yet. Finally my components are for a highly configurable user dashboard so a user might override a particular component’s style (say set a specific component background red to call it out). These overrides are probably stored in a database and fed into component as a JSON object and transformed to inline styles on html element style attribute (maybe Constructable Stylesheet can be used instead of JSON / string input once support improves). |
Check out https://tailwindcss.com/ |
It's why I am trying to avoid making components for "buttons" and use global styles for common things. |
There is an RFC for a solution to this here: sveltejs/rfcs#13 In the interim, you can use a global stylesheet as you would with any other project. It has been the conclusion of various discussions that there are no plans to provide unscoped styling from within a component. I suggest we take discussion of component styling there so that we can draw it to some sort of a conclusion. |
Good enough? |
I have interesting question about components reusability.
For instance, I have several projects and I also have Button component. Button can be yellow, green, white etc. In one project I need to have green Button, but in another project I need to have yellow Button. Now I can set different styles in Button component (i.e. I can write different styles in <style> tag and then choose the theme of the Button using props
, however in this case the Button component becomes heavier than I need (On the page there will be unusable styles). How can I organize the component so that I can have only necessary code (Like modifiers in BEM methodology https://github.com/bem/bem-react/tree/master/packages/core )?
The text was updated successfully, but these errors were encountered: