All Projects → josefaidt → svelte-themer

josefaidt / svelte-themer

Licence: MIT License
A theming engine for your Svelte apps using CSS Variables, persisted.

Programming Languages

javascript
184084 projects - #8 most used programming language
Svelte
593 projects

Projects that are alternatives of or similar to svelte-themer

theme-change
Change CSS theme with toggle, buttons or select using CSS custom properties and localStorage
Stars: ✭ 283 (+221.59%)
Mutual labels:  theming, css-variables
Svelte Css Vars
✨ Ever wanted to have reactive css variables in svelte? What if I told you there's a way?
Stars: ✭ 150 (+70.45%)
Mutual labels:  svelte, css-variables
Theming Demo
https://codesandbox.io/s/github/juliaqiuxy/theming-demo/tree/master/?from-embed
Stars: ✭ 59 (-32.95%)
Mutual labels:  theming, css-variables
svelte-style-directive
A custom Svelte preprocessor to add support for style directive.
Stars: ✭ 19 (-78.41%)
Mutual labels:  svelte, css-variables
svelte-electron-boilerplate
🧬 Create a desktop app with this user-friendly Svelte boilerplate for electron
Stars: ✭ 70 (-20.45%)
Mutual labels:  svelte
microzord
Simple and powerful Micro Frontends framework
Stars: ✭ 81 (-7.95%)
Mutual labels:  svelte
tabler-icons-svelte
A library of SVG Svelte components for Tabler Icons.
Stars: ✭ 50 (-43.18%)
Mutual labels:  svelte
svelte-persistent-store
A Svelte store that keep its value through pages and reloads
Stars: ✭ 111 (+26.14%)
Mutual labels:  svelte
svelte-interview-questions
Concepts and Questions related to Svelte - Part of official Svelte resources list
Stars: ✭ 18 (-79.55%)
Mutual labels:  svelte
svelte-search
Accessible, customizable Svelte search component
Stars: ✭ 17 (-80.68%)
Mutual labels:  svelte
template
Elder.js template project. It is part template, part tutorial. Dive in!
Stars: ✭ 101 (+14.77%)
Mutual labels:  svelte
svelte-previous
A Svelte store that remembers previous values
Stars: ✭ 46 (-47.73%)
Mutual labels:  svelte
104-ignore-not-interesting
過濾 104人力銀行 搜尋結果中你不感興趣的職缺 | Ignore jobs you are not interested in on 104
Stars: ✭ 25 (-71.59%)
Mutual labels:  svelte
pushu.ps
A simple Svelte app that let's you track your daily push-ups routine
Stars: ✭ 41 (-53.41%)
Mutual labels:  svelte
svelte-algolia
Svelte plugin for keeping Algolia indices in sync with custom data fetching functions.
Stars: ✭ 17 (-80.68%)
Mutual labels:  svelte
svelte-form
JSON Schema form for Svelte v3
Stars: ✭ 47 (-46.59%)
Mutual labels:  svelte
markushatvan.com
Personal website and blog written from scratch with SvelteKit and TailwindCSS.
Stars: ✭ 82 (-6.82%)
Mutual labels:  svelte
rollup-plugin-svelte-hot
Fork of official rollup-plugin-svelte with added HMR support (for both Nollup or Rollup)
Stars: ✭ 49 (-44.32%)
Mutual labels:  svelte
svelte-google-analytics
Google Analytics component for Svelte
Stars: ✭ 41 (-53.41%)
Mutual labels:  svelte
svelte-typescript-rollup
Svelte + Typescript + Rollup
Stars: ✭ 79 (-10.23%)
Mutual labels:  svelte

svelte-themer

A theming engine for your Svelte apps using CSS Variables, persisted.

<script>
  import { ThemeWrapper, ThemeToggle } from 'svelte-themer'
</script>

<ThemeWrapper>
  <main>
    <h1>svelte themer</h1>
    <ThemeToggle />
  </main>
</ThemeWrapper>

<style>
  :global(html) {
    background-color: var(--theme-colors-background, initial);
    color: var(--theme-colors-text, initial);
  }
</style>

CSS Variables

CSS variables are created for app-wide consumption using the nomenclature --[prefix]-[property!]

For example:

  • --theme-text by default where property = 'text'
  • --base-text where prefix = 'base' and property = 'text'
  • --text where prefix = null || undefined and property = 'text'

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

const lightTheme = {
  light: {
    colors: {
      text: '#282230',
      background: {
        _: '#f1f1f1',
        contrast: '#b1b1b1',
      },
      primary: '#01796f',
      primary_dark: '#016159',
      secondary: '#562931',
    },
  },
}

Turns into

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

[theme='light'],
.theme--light {
  --theme-colors-text: var(--theme-light-colors-text);
  --theme-colors-background: var(--theme-light-colors-background);
  --theme-colors-background-contrast: --var(
    theme-light-colors-background-contrast
  );
  --theme-colors-primary: var(--theme-light-colors-primary);
  --theme-colors-primary_dark: var(--theme-light-colors-primary_dark);
  --theme-colors-secondary: var(--theme-light-colors-secondary);
}

Getting Started

Use the preset themes supplied by svelte-themer or create your own! Theme names are specified by the key, and all properties are transformed into CSS Variables.

NOTE: svelte-themer is preset with 3 themes to showcase the flexible functionality of toggle()

// src/themes.js
export const themes = {
  light: {
    colors: {
      text: '#282230',
      background: {
        _: '#f1f1f1',
        contrast: '#b1b1b1',
      },
      primary: '#01796f',
      primary_dark: '#016159',
      secondary: '#562931',
    },
  },
  dark: {
    colors: {
      text: '#f1f1f1',
      background: {
        _: '#27323a',
        contrast: '#0d1215',
      },
      primary: '#01978b',
      primary_dark: '#00887c',
      secondary: '#fe8690',
    },
  },
}

Components

With svelte-themer there are two components: a wrapper component, and a button for toggling themes. The provided button is more for convenience as the function used to toggle themes is exposed to the theme context.

ThemeWrapper

<!-- 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. To disabled persistence, provide key={null}.

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

Theme Loading Order

ThemeWrapper will load a theme on first visit based on the following order:

  1. User-provided - The value specified in the theme prop.
  2. Saved - User's stored choice (from localStorage)
  3. Prefers - User's Operating System settings (via prefers-color-scheme)
  4. Fallback - First theme in themes specified (from presets, light)

By default, the "prefers" step will choose a theme based on OS settings, however this can be modified to directly choose "light" or "dark" by leveraging the mode prop:

<ThemeWrapper mode="auto|light|dark">
  <!--  -->
</ThemeWrapper>

Accessing Theme Context

Described below is the pattern used for accessing theme context to create your own toggle button.

<!-- src/MyToggleButton.svelte -->
<script>
  import { getContext } from 'svelte'
  let { toggle, current, theme } = getContext('theme')
</script>

<button on:click="{toggle}">
  <slot>{$current}</slot>
</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>

Actions

use:theme

<script>
  import { theme } from 'svelte-themer/use'
  export let myTheme = {
    text: 'red',
  }
</script>

<div use:theme="{myTheme}">
  <p>Hello, World!</p>
</div>

<style>
  p {
    color: var(--text);
  }
</style>

use:stylesheet

<script>
  import { stylesheet } from 'svelte-themer/use'
  export let myTheme = {
    text: 'red',
  }
</script>

<div use:stylesheet="{myTheme}">
  <p>Hello, World!</p>
</div>

<style>
  p {
    color: var(--text);
  }
</style>

Contributing

Refer to the contributing guidelines.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].