Try it out on the Master CSS documentation site.
Vanilla, Next, React, Vue, Svelte, and Master CSS are available:
- β‘οΈ Ultra-lightweight ~1.2KB
- π Switch between
light
,dark
, andsystem
- π Sync with system theme preferences
- πΎ Store the user's preference in
localStorage
- π« Access theme preferences and modes through context
- 𧩠Built-in
"use client"
directive
The prefers-color-scheme
cannot force override to the specified color mode. Once you want to switch themes, you cannot use @media (prefers-color-scheme: dark)
.
https://stackoverflow.com/questions/56300132/how-to-override-css-prefers-color-scheme-setting
This package automatically switches themes using class=""
and color-scheme:
; that's it.
<html class="dark" style="color-scheme: dark">
<body>
<h1 class="bg:black@dark bg:white@light">Hello World</h1>
</body>
</html>
To view the source code examples:
- React: https://github.com/master-co/theme-mode/tree/main/examples/react
- Vue: https://github.com/master-co/theme-mode/tree/main/examples/vue
- Svelte: https://github.com/master-co/theme-mode/tree/main/examples/svelte
Install the package depending on your framework.
npm install theme-mode
import ThemeMode from 'theme-mode'
const themeMode = new ThemeMode().init()
// Set `preference` anywhere to switch theme modes.
themeMode.preference = 'dark'
npm install @master/theme-mode.react
import ThemeModeProvider from '@master/theme-mode.react'
export default function App({ children }) {
return (
<ThemeModeProvider preference='system'>
{children}
</ThemeModeProvider>
)
}
npm install @master/theme-mode.vue
<script setup lang="ts">
import ThemeModeProvider from '@master/theme-mode.vue'
</script>
<template>
<ThemeModeProvider preference="system">
<slot></slot>
</ThemeModeProvider>
</template>
npm install @master/theme-mode.svelte
<script lang="ts">
import ThemeModeProvider from '@master/theme-mode.svelte';
</script>
<ThemeModeProvider preference="system">
...
</ThemeModeProvider>
You can set the default theme mode when the user has not set a theme preference, such as common light
or dark
mode.
<ThemeModeProvider preference="dark">...</ThemeModeProvider>
Rendered as:
<html class="dark" style="color-scheme: dark">β¦</html>
Automatically switches modes based on the user's system preference.
<ThemeModeProvider preference="system">...</ThemeModeProvider>
Rendered as:
<html class="light" style="color-scheme: light">β¦</html>
<!-- or -->
<html class="dark" style="color-scheme: dark">β¦</html>
Note: CSS only supports light and dark modes for system preferences.
By default options.store
is set to 'theme-preference'
, which uses this key to set local storage when the preference is changed.
In this way, the theme preference set last time will be applied when the user visits or refreshes the website again.
To disable local storage, set it to false
.
<ThemeModeProvider store={false}>...</ThemeModeProvider>
You can now create selector-driven CSS themes using tools like Master CSS.
<html class="light" style="color-scheme: light">
<body>
<div class="block@dark" hidden>Dark</div>
<div class="block@light" hidden>Light</div>
<div class="block@christmas" hidden>Christmas</div>
</body>
</html>
import { useThemeMode } from '@master/theme-mode.react'
export default function ThemeModeSelect() {
const themeMode = useThemeMode()
return (
<button>
{themeMode.value === 'dark' ? 'π' : 'βοΈ'} {themeMode.preference}
<select className="abs full inset:0 opacity:0"
value={themeMode.preference}
onChange={(event) => themeMode.preference = event.target.value}>
<option value="light">βοΈ Light</option>
<option value="dark">π Dark</option>
<option value="system">System</option>
</select>
</button>
)
}
<script setup lang="ts">
import { inject } from 'vue'
const themeMode = inject<any>('theme-mode')
</script>
<template>
<button class="px:5x r:2x font:18 h:48 bg:slate-10@light bg:gray-80@dark fg:strong rel">
{{ themeMode.value === 'dark' ? 'π' : 'βοΈ' }} {{ themeMode.preference }}
<select class="abs full inset:0 opacity:0" v-model="themeMode.preference">
<option value="light">βοΈ Light</option>
<option value="dark">π Dark</option>
<option value="system">System</option>
</select>
</button>
</template>
<script lang="ts">
import { getThemeMode } from "@master/theme-mode.svelte";
const themeMode = getThemeMode();
</script>
<span id="value">{$themeMode.value}</span>
<span id="preference">{$themeMode.preference}</span>
<select class="abs full inset:0 opacity:0" bind:value={$themeMode.preference}>
<option value="light">βοΈ Light</option>
<option value="dark">π Dark</option>
<option value="system">System</option>
</select>
If you've pre-rendered your CSS styles to the page to improve the page loading and first-render experience, it's crucial to initialize the theme mode in advance.
By default, three modules of minified advanced initial scripts for different default themes are exported:
theme-mode/pre-init
: https://github.com/master-co/theme-mode/tree/main/packages/core/src/pre-init.iife.min.tstheme-mode/pre-init-light
: https://github.com/master-co/theme-mode/tree/main/packages/core/src/pre-init-light.iife.min.tstheme-mode/pre-init-dark
: https://github.com/master-co/theme-mode/tree/main/packages/core/src/pre-init-dark.iife.min.ts
You have to use the build tool to inject these original scripts into HTML <head>
, taking Next.js as an example:
import PRE_INIT_THEME_MODE_SCRIPT from '!!raw-loader!theme-mode/pre-init';
export default async function RootLayout({ children }: {
children: JSX.Element
}) {
return (
<html suppressHydrationWarning>
<head>
<script dangerouslySetInnerHTML={{ __html: PRE_INIT_THEME_MODE_SCRIPT }}></script>
...
</head>
...
</html>
)
}
Or copy them directly:
const preference = localStorage.getItem('theme-preference') || 'system';
const value = preference === 'system'
? matchMedia('(prefers-color-scheme:dark)').matches ? 'dark' : 'light'
: preference;
document.documentElement.classList.add(value);
if (['dark', 'light'].includes(value)) document.documentElement.style.colorScheme = value;
Those JS resources cannot be loaded from external because this is a critical script for the first painting of the page.
Specify the default theme preference.
- Default:
undefined
- Value:
'dark'
|'light'
|'system'
|string
Enable local storage and specify the key for localStorage
.
- Default:
'theme-preference'
- Value:
'theme-preference'
|string
|false
Set or get the current theme preference.
- Default:
undefined
- Value:
'dark'
|'light'
|'system'
|string
Set or get the current theme mode.
- Default:
undefined
- Value:
'dark'
|'light'
|string
Get the currently stored theme preference.
- Default:
undefined
- Value:
'dark'
|'light'
|string
Get the theme mode of the current system
- Default:
undefined
- Value:
'dark'
|'light'
|string
Initialize the default theme mode. This is usually performed after the DOM has been initialized.
Destroy the theme mode, including removing media query listeners.
The Master community can be found here:
- Discuss on GitHub - Ask questions, voice ideas, and do any other discussion
- Join our Discord Server - Casually chat with other people using the language β δΈζ
Our γ Code of Conduct γ applies to all Master community channels.
Please see our CONTRIBUTING for workflow.