0.0.37 • Published 4 years ago

@centroculturadigital-mx/svelte-themer v0.0.37

Weekly downloads
1
License
-
Repository
-
Last release
4 years ago

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 where prefix = 'base' and property = 'text'
  • --theme-text where prefix = null || undefined and property = '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>
0.0.37

4 years ago

0.0.36

4 years ago

0.0.34

4 years ago

0.0.35

4 years ago

0.0.33

4 years ago

0.0.32

4 years ago

0.0.31

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.0

4 years ago