@centroculturadigital-mx/svelte-themer
v0.0.37
Published
Styling your Svelte apps with CSS Variables, persisted.
Downloads
2
Readme
svelte-themer
Styling your Svelte apps with CSS Variables, persisted.
<script>
import { ThemeWrapper, ThemeToggle } from 'svelte-themer'
</script>
<ThemeWrapper>
<main>
<h1>svelte themer</h1>
<ThemeToggle />
</main>
</ThemeWrapper>
CSS Variables
CSS variables are created for app-wide consumption using the nomenclature --theme-[prefix]-[property!]
For example:
--theme-base-text
whereprefix = 'base'
andproperty = 'text'
--theme-text
whereprefix = null || undefined
andproperty = 'text'
Getting Started
Use the following as a base for your custom themes:
// src/theme.js
export const themes = [
{
name: 'light',
colors: {
text: '#282230',
background: '#f1f1f1',
},
},
{
name: 'dark',
colors: {
text: '#f1f1f1',
background: '#27323a',
},
},
]
ThemeWrapper
Then, provide the new themes to the ThemeWrapper
component
<!-- src/App.svelte -->
<script>
import { ThemeWrapper } from 'svelte-themer'
import { themes } from './theme.js'
</script>
<ThemeWrapper themes="{themes}">
<main>
<h1>My Svelte App</h1>
</main>
</ThemeWrapper>
This allows any components nested to access the theme Context which wraps a writeable Theme
store
Theme Persistence
By default svelte-themer persists the chosen theme with localStorage
, and can be modified via the storageKey
prop.
<ThemeWrapper storageKey="my-svelte-app__theme">
<!-- -->
</ThemeWrapper>
Accessing Theme Context
<script>
import { getContext } from 'svelte'
let { toggle, theme } = getContext('theme')
</script>
<button on:click="{toggle}">
{$theme.name}
</button>
Provided Theme Toggle
<!-- src/App.svelte -->
<script>
import { ThemeWrapper, ThemeToggle } from 'svelte-themer'
import { themes } from './theme.js'
</script>
<ThemeWrapper themes="{themes}">
<main>
<h1>My Svelte App</h1>
<ThemeToggle />
</main>
</ThemeWrapper>