This package has been deprecated

Author message:

WARNING: This project has been renamed to themes.js. Install using themes.js instead.

vanilla-themes
TypeScript icon, indicating that this package has built-in type declarations

0.0.7 • Public • Published

vanilla-themes

Theme library with auto system color scheme detection in JavaScript.

Features

  • Auto detect Dark / Light mode by JavaScript
  • Support custom theme
  • Support localStorage save and sync with other windows
  • Use class instead of @media prefers-color-scheme

Install

Package Manager

npm i vanilla-themes

yarn add vanilla-themes

pnpm i vanilla-themes

CDN

<script src="https://unpkg.com/vanilla-themes"></script>

Quick start

Prepare CSS

.light body {
  background-color: white;
}
.dark body {
  background-color: black;
}

Auto Dark / Light and auto localStorage sync theme by default

ESM

import Themes from 'vanilla-themes'
const themes = new Themes()

CDN

<script src="https://unpkg.com/vanilla-themes"></script>
<script>new window.Themes()</script>

Usage

Custom theme

const themes = new Themes('ocean')
.ocean body {
  background-color: blue;
}

Dynamic force change theme

const themes = new Themes()
themes.theme = 'dark'

// sync to localStorage if needed
themes.commit()

Sync with localStorage

By default, localStorage sync will auto enable If you calling new Themes() with empty arguments.

const themes = new Themes()
// sync localStorage is enabled by default

If you passing init theme (like new Themes('dark')), the localStorage sync would be disable by default.

const themes = new Themes('dark')
// sync localStorage is disabled by default

You can switch on and off sync after init Themes by setting .sync.

themes.sync = true

Notice: after calling commit(), localStorage sync will auto set to enable, you can turn off the sync manuelly if needed.

themes.commit()
// themes.sync = true [auto]

Events

// theme on change
themes.onChange = (theme) => {
  console.log('theme changed', theme)
}

Development

pnpm i

Playground

pnpm --filter ./playground dev

Inspiration

next-themes

LICENSE

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i vanilla-themes

Weekly Downloads

0

Version

0.0.7

License

MIT

Unpacked Size

14.8 kB

Total Files

6

Last publish

Collaborators

  • serkodev