No flash on load
A classic script in the head, or a small inline snippet for SSR, writes the theme before the first paint.
The stored theme is on the page before it paints, follows the system while visitors want it to, and stays in sync across tabs and controls.
Install it, try the live preview, then read the full reference.
npm install @samline/themeAbout 1.8 KB gzipZero dependenciesMIT licensed
Pick a theme, then reload
The choice is remembered. On System, changing your operating system's appearance updates this box live.
ONE SMALL SCRIPT. RIGHT AT HOME IN YOUR STACK.
A classic script in the head, or a small inline snippet for SSR, writes the theme before the first paint.
Buttons get aria-pressed, radios are checked and selects show the mode, with no extra code.
In system mode, a change in the operating system appearance updates the page at once.
A choice made in one tab reaches the others, and broken storage never throws.
Any number of named themes, as data-theme or a class, with color-scheme and theme-color set for you.
Transitions pause while the theme changes, so the page swaps at once instead of fading every element.
Your CSS stays in charge: the package only writes the theme name.
Package
createTheme and themeScript as ESM or CommonJS.
Controller
mode, theme, set, toggle, subscribe and destroy.
SSR head
An inline snippet for your server-rendered head.
HTML / CDN
A classic script for the head that applies at once.
Controls
Buttons, radios, toggles and selects, without code.
Styling
Style [data-theme] or a class; no stylesheet ships.
Add a remembered dark mode to any page, without a flash.
Read the installation guideInstall
npm
npm install @samline/themepnpm
pnpm add @samline/themeyarn
yarn add @samline/themebun
bun add @samline/themeHTML / CDN
<script src="https://cdn.jsdelivr.net/npm/@samline/theme@1.1.0/dist/browser/theme.global.js"></script>Add a control
A data-theme-toggle button switches dark mode and reports it with aria-pressed. Inline themeScript() in the head too, so the theme is on the page before it paints.
<button type="button" data-theme-toggle>Dark mode</button>Start it
import { createTheme } from '@samline/theme'
// Style [data-theme='dark'] in your CSS.
createTheme()Continue with every option, custom themes and server rendering.
Explore the optionsThree common jobs, without touching storage or media queries yourself.
<button type="button" data-theme-set="light">Light</button>
<button type="button" data-theme-set="system">System</button>
<button type="button" data-theme-set="dark">Dark</button>import { themeScript } from '@samline/theme'
const head = `<script>${themeScript()}</script>`import { createTheme } from '@samline/theme'
const theme = createTheme()
theme.subscribe(({ theme }) => chart.setTheme(theme))Try the examples or follow the first steps.
Guides, the complete API and release notes, in one place.
Read the documentation