All Projects → saadeghi → theme-change

saadeghi / theme-change

Licence: other
Change CSS theme with toggle, buttons or select using CSS custom properties and localStorage

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to theme-change

Theming Demo
https://codesandbox.io/s/github/juliaqiuxy/theming-demo/tree/master/?from-embed
Stars: ✭ 59 (-79.15%)
Mutual labels:  theming, css-variables
Halfmoon
Front-end framework with a built-in dark mode and full customizability using CSS variables; great for building dashboards and tools.
Stars: ✭ 2,583 (+812.72%)
Mutual labels:  css-custom-properties, css-variables
svelte-themer
A theming engine for your Svelte apps using CSS Variables, persisted.
Stars: ✭ 88 (-68.9%)
Mutual labels:  theming, css-variables
postcss-variable-compress
Minifies css variable names
Stars: ✭ 18 (-93.64%)
Mutual labels:  css-custom-properties, css-variables
Theme Builder
The theming system helps you in building a theme of your choice and apply it to test live. Why wait? Just give it a try.
Stars: ✭ 82 (-71.02%)
Mutual labels:  localstorage, theming
v-helper
A shorter SCSS access to CSS custom properties.
Stars: ✭ 14 (-95.05%)
Mutual labels:  css-custom-properties, css-variables
fylgja
The modular highly customisable CSS framework. Powered by CSS Components, Utilities and Props for building your Web UI.
Stars: ✭ 65 (-77.03%)
Mutual labels:  css-custom-properties, css-variables
swatch
A lightweight, modern theming library based on CSS variables and the setter/getter pattern.
Stars: ✭ 14 (-95.05%)
Mutual labels:  theming, css-custom-properties
web-session-counter
Utility to count a user's web sessions based on the definition GA uses.
Stars: ✭ 22 (-92.23%)
Mutual labels:  localstorage
Holi
Holi is a lightweight Jetpack Compose library of colors, gradients and cool utility functions for all your palette needs!
Stars: ✭ 160 (-43.46%)
Mutual labels:  theming
elm-storage
Unified interface for accessing and modifying LocalStorage, SessionStorage and Cookies
Stars: ✭ 13 (-95.41%)
Mutual labels:  localstorage
client-persist
Offline storage for your web client. Supports IndexedDB, WebSQL, localStorage and sessionStorage with an easy to crawl with API.
Stars: ✭ 14 (-95.05%)
Mutual labels:  localstorage
Theming-Android
Examples and tips on how to support multiple themes within your app
Stars: ✭ 40 (-85.87%)
Mutual labels:  theming
stoor
Storage wrapper with support for namespacing, timeouts and multi get/set and remove.
Stars: ✭ 26 (-90.81%)
Mutual labels:  localstorage
Materialstartpage
Save your favorite sites with stylish using Local Storage
Stars: ✭ 12 (-95.76%)
Mutual labels:  localstorage
react-nodejs-mongodb-crud
👨‍💻 Fullstack web app built with MongoDB, NodeJs, React and Redux. Features: Protected routes client/server side, MaterialUI layout
Stars: ✭ 91 (-67.84%)
Mutual labels:  localstorage
base16.nix
Quickly theme programs in your favourite base16 colorscheme
Stars: ✭ 61 (-78.45%)
Mutual labels:  theming
host-webfonts-locally
OMGF automagically caches the Google Fonts used by your theme/plugins locally. No configuration (or brains) required!
Stars: ✭ 13 (-95.41%)
Mutual labels:  localstorage
async
A lightweight and high performance async CSS and script loader for frontend optimization.
Stars: ✭ 17 (-93.99%)
Mutual labels:  localstorage
WebReader
基于HTML5开发的WebApp阅读器
Stars: ✭ 19 (-93.29%)
Mutual labels:  localstorage

🎨 CSS Theme Change

  • A tiny JS script to handle CSS themes
  • Change CSS theme using button, toggle or a <select>
  • It saves chosen theme in browser and uses it again when page reloads


🖥 Demo

image

💿 Use

JS

Use CDN:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/index.js"></script>
Or use NPM:

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

import { themeChange } from 'theme-change'
themeChange()
or if it's a React project:

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

import { useEffect } from 'react'
import { themeChange } from 'theme-change'

useEffect(() => {
  themeChange(false)
  // 👆 false parameter is required for react project
}, [])
or if it's a Vue 3 project:

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

import { onMounted, onUpdated, onUnmounted } from 'vue'
import { themeChange } from 'theme-change'

export default {
  setup() {
    onMounted(() => {
      themeChange(false)
    })
  },
}
or if it's a Vue 2 project:

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

import { themeChange } from 'theme-change'

export default {
  mounted: function () {
    themeChange(false)
  },
}
or if it's a Svelte project:

Install: npm i theme-change --save and use it in your svelte component that uses one theme-change attributes:

import { onMount } from 'svelte'
import { themeChange } from 'theme-change'

// NOTE: the element that is using one of the theme attributes must be in the DOM on mount
onMount(() => {
  themeChange(false)
  // 👆 false parameter is required for svelte
})

CSS

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

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

then use your variables on any element

body {
  background-color: var(--my-color);
}

HTML

There are 3 options:

  • Using buttons to set a theme

    btn

    Clicking on these buttons, sets the chosen theme and also adds the ACTIVECLASS to the chosen button

    <button data-set-theme="" data-act-class="ACTIVECLASS"></button>
    <button data-set-theme="dark" data-act-class="ACTIVECLASS"></button>
    <button data-set-theme="pink" data-act-class="ACTIVECLASS"></button>
    
  • Toggle between two themes

    toggle

    Clicking on this element, toggles between dark and light theme and also adds the ACTIVECLASS to the element

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

    select

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

Advance use

Set theme based on OS color-scheme
@media (prefers-color-scheme: dark){
  :root{
    --my-color: #252b30;
  }
}
Use with PurgeCSS

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

  • Safelist [data-theme] on postcss config

    module.exports = {
      purge: {
        options: {
          safelist: [/data-theme$/],
        },
      },
    }
  • Safelist inside CSS file

    /*! purgecss start ignore */
    
    [data-theme='dark'] {
      --my-color: #252b30;
    }
    
    /*! purgecss end ignore */

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].