0.5.5 • Published 2 years ago

svelte-themer v0.5.5

Weekly downloads
458
License
MIT
Repository
github
Last release
2 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'

Now supports adding all theme colors as theme-specific CSS variables:

{
  name: 'light',
  colors: {
    text: '#282230',
    background: '#f1f1f1',
    primary: '#01796f',
    primary_dark: '#016159',
    secondary: '#562931',
  },
},

Turns into

:root {
  --theme-light-text: #282230;
  --theme-light-background: #f1f1f1;
  --theme-light-primary: #01796f;
  --theme-light-primary_dark: #016159;
  --theme-light-secondary: #562931;
}

Getting Started

You can use the preset themes supplied by svelte-themer or create your own! Ensure each theme object has the necessary keys.

// src/themes.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 './themes.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 key prop.

<ThemeWrapper key="my-svelte-app__theme">
  <!--  -->
</ThemeWrapper>

Accessing Theme Context

<script>
  import { getContext } from 'svelte'
  let { toggle, current, colors } = getContext('theme')
</script>

<button on:click="{toggle}">{$current}</button>

Provided Theme Toggle

<!-- src/App.svelte -->
<script>
  import { ThemeWrapper, ThemeToggle } from 'svelte-themer'
  import themes from './themes.js'
</script>

<ThemeWrapper themes="{themes}">
  <main>
    <h1>My Svelte App</h1>
    <ThemeToggle />
  </main>
</ThemeWrapper>
0.5.5-next.1

2 years ago

0.5.4-next.1

2 years ago

0.5.3-next.1

2 years ago

0.5.5-next.0

2 years ago

0.5.4-next.2

2 years ago

0.5.3-next.0

2 years ago

0.5.5-next.2

2 years ago

0.5.6-next.0

2 years ago

0.5.4-next.4

2 years ago

0.5.4

2 years ago

0.5.3

2 years ago

0.5.5

2 years ago

0.5.0

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0-next.2

3 years ago

0.5.0-next.1

3 years ago

0.5.0-next.4

3 years ago

0.5.0-next.3

3 years ago

0.4.13

3 years ago

0.4.13-next.2

3 years ago

0.4.13-next.0

3 years ago

0.4.12

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.9-next.0

3 years ago

0.4.7-next.1

3 years ago

0.4.7-next.2

3 years ago

0.4.7-next.3

3 years ago

0.4.9-next.1

3 years ago

0.4.7-next.4

3 years ago

0.4.10

3 years ago

0.4.11-next.6

3 years ago

0.4.11

3 years ago

0.4.11-next.2

3 years ago

0.4.12-next.1

3 years ago

0.4.11-next.3

3 years ago

0.4.12-next.0

3 years ago

0.4.11-next.4

3 years ago

0.4.7

3 years ago

0.4.11-next.5

3 years ago

0.4.6

3 years ago

0.4.11-next.0

3 years ago

0.4.11-next.1

3 years ago

0.4.6-next.9

3 years ago

0.4.6-next.8

3 years ago

0.4.6-next.7

3 years ago

0.4.6-next.6

3 years ago

0.4.6-next.4

3 years ago

0.4.6-rc.3

3 years ago

0.4.6-rc.2

3 years ago

0.4.6-rc.1

3 years ago

0.4.4-rc.3

3 years ago

0.4.4-rc.2

3 years ago

0.4.5

3 years ago

0.4.4-rc.1

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.4.0-rc.6

3 years ago

0.4.0-rc.5

3 years ago

0.4.0-rc.4

3 years ago

0.4.0-rc.2

3 years ago

0.4.0-rc.1

3 years ago

0.3.5

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.0

3 years ago

0.3.1

3 years ago

0.2.1

3 years ago

0.2.0

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago