Socket
Socket
Sign inDemoInstall

theme-change

Package Overview
Dependencies
0
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    theme-change

Change CSS theme with toggle, buttons or select using CSS Variables and localStorage


Version published
Weekly downloads
8.4K
increased by5.35%
Maintainers
1
Install size
14.1 kB
Created
Weekly downloads
 

Changelog

Source

2.4.0 (2023-03-02)

Bug Fixes

  • empty theme value btn bug (689d752)

Readme

Source

🎨 CSS Theme Change

A very very small script to handle CSS theming


Change CSS theme with toggle, buttons or select using CSS Variables (CSS custom properties) and localStorage.
It saves the chosen theme in brower localStorage and loads it again when page loads.
You only need to define your theme in CSS

Demo

image

👨‍💻 Use


1️⃣ Change theme with buttons

btn

Using data-set-theme Clicking on these buttons, sets the chosen theme and also adds the ACTIVECLASS to the chosen button

HTML

<button data-act-class="ACTIVECLASS" data-set-theme=""></button>
<button data-act-class="ACTIVECLASS" data-set-theme="dark"></button>
<button data-act-class="ACTIVECLASS" data-set-theme="pink"></button>

JS

Use CDN

<script src="https://unpkg.com/theme-change@latest/dist/btn.js"></script>

Or use NPM:
Install: npm i theme-change --save and use it in your js file:

import {themeBtn} from "theme-change"
themeBtn()

2️⃣ Toggle between 2 themes

toggle

Using data-toggle-theme Clicking on this element, toggles between the 2 values and applies the ACTIVECLASS when dark theme is active

HTML

<button data-act-class="ACTIVECLASS" data-toggle-theme="dark,light"></button>

JS

Use CDN

<script src="https://unpkg.com/theme-change@latest/dist/toggle.js"></script>

Or use NPM:
Install: npm i theme-change --save and use it in your js file:

import {themeToggle} from "theme-change"
themeToggle()

3️⃣ Using a <select> to choose theme

select

Using data-choose-theme set theme when <select> changes

HTML

<select data-choose-theme>
  <option value="">Default</option>
  <option value="dark">Dark</option>
  <option value="pink">Pink</option>
</select>

JS

Use CDN

<script src="https://unpkg.com/theme-change@latest/dist/select.js"></script>

Or use NPM:
Install: npm i theme-change --save and use it in your js file:

import {themeSelect} from "theme-change"
themeSelect()

Set up your CSS

Set your themeable style as custom properties in CSS like this:

:root {
  --color-default: #fff;
  /* or any other variables */
}
[data-theme='dark'] {
  --color-default: #252b30;
}
[data-theme='pink'] {
  --color-default: #ffabc8;
}

then use your variables on any element

body {
  background-color: var(--color-default);
}
Set default theme for dark mode users

You can also define a default theme when user is using a dark theme on their OS

@media (prefers-color-scheme: dark){
  --color-default: #252b30;
}
Fixing PurgeCSS issue

⚠️ If you're using Purge CSS, you might need to safe list your CSS using the comments below because your secondary themes will be purged

/*! purgecss start ignore */

[data-theme='dark'] {
  --color-default: #252b30;
}

/*! purgecss end ignore */

Keywords

FAQs

Last updated on 04 Mar 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc