Giter Club home page Giter Club logo

themes.js's Introduction

themes.js

A super lightweight and fast Theme library with auto system color scheme detection in JavaScript.

Latest Release Package Size MIT License

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 themes.js

yarn add themes.js

pnpm i themes.js

CDN

<script src="https://unpkg.com/themes.js"></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 'themes.js'
const themes = new Themes()

CDN

<script src="https://unpkg.com/themes.js"></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]

Configs

You can pass theme, storageKey or sync config when initialize.

const themes = new Themes({
  theme: 'dark', // (optional) default: null
  storageKey: 'theme', // (optional) default: 'theme'
  sync: true, // (optional) default: false
})

Events

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

Development

pnpm i

Playground

pnpm dev

Inspiration

next-themes

LICENSE

MIT

themes.js's People

Contributors

dependabot[bot] avatar itsezc avatar serkodev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

itsezc

themes.js's Issues

Feature request: Add a custom storageKey option

Currently the storageKey (for localStorage) is hard coded to theme, and this might not be flexible enough for apps where state management solutions and other scripts are often used in conjuction with themes.js.

I propose a new config setup for the initalization:

From (this will still work)

const themes = new Themes('ocean');

To

const themes = new Themes({
  theme: 'ocean',
  storageKey: 'theme',
});

Where the storageKey can be provided as an option for the constructor, or it defaults to theme, however if a string is provided to the constructor, then existing behaviour is to be expected.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.