-
-
Notifications
You must be signed in to change notification settings - Fork 432
How can I create multi theme sapper app? #562
Comments
I think I found good solution, but it will require changes in sapper or svelte. I can add theme style to the component like that:
And we can change body class for quick switch theme. To put all the styles of all the themes into a separate file, we can use the npm modules: svelte-preprocess + postcss-import. And in each component there will be only one additional line:
The Sapper will remove all the styles of other components and leave only styles that is necessary in the current component. But the Sapper do not allow to add such theme styles:
What is strange: |
If output size is not a concern, you can wrap all your theme related styles in
|
Output size is a concern. Is it possible to remove selectors that are not used and not wrapped in
|
I came up with a solution close to good. The downside is that each component must have an identifier. theme_dark.scss
themes.scss
Svelte component:
|
Try this: _layout.html <svelte:head>
<link rel="stylesheet" type="text/css" href="{theme}-theme.css">
</svelte:head>
<script>
export default {
preload () {
// Read user session or cookie or url param or ...
return { theme: session.theme || 'dark' }
}
}
</script> dark-theme.css :root {
--primary-text: white;
}
body {
background: black;
} Svelte component: <h1>Hello world</h1>
<style>
h1 {
color: var(--primary-text);
}
</style> |
I think it is not good idea - to use CSS variables. It is suitable only in cases where the themes differ mainly in the colors and text sizes. I am trying to improve my idea above. To identify components, we can use the path to the component file and use the svelte CSS preprocessor. Maybe all this can be arranged as a plugin later: rollup.config.js
theme_dark.scss
|
I made a plugin for working with themes in svelte: |
There is ongoing discussion about how to best support this in Svelte: sveltejs/svelte#1550 and sveltejs/rfcs#13 I'm going to close this since there's not much for Sapper to do. Please feel free to contribute to the issues in Svelte |
Is the sapper designed to create applications with multiple themes? If so, how can I do this?
I see that the styles of each component are loaded at the end of the head tag with the generated class names. To add a theme I need, somehow, to put a file with a theme after the head tag and add readable classes inside the components. Of course - this is a bad decision, but I still do not see others.
Ideally, I would like the theme of the application to be divided between the components and will load in parts, but so that it is in one place in the project. And so that the user can switch themes without restarting the application. Is it possible at all?
Thanks.
The text was updated successfully, but these errors were encountered: