Theme examples.
Live previews of distinct scenarios, each with independently copyable files. The same examples appear in the documentation.
Each preview runs the real package. The copied files load the published browser bundle, so they run as they are.
One button for dark mode
A data-theme-toggle button switches between light and dark from the theme in effect and reports its state with aria-pressed. The choice is remembered.
This box remembers its own theme. Reload the page: it stays.
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>One button for dark mode</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<button type="button" class="toggle" data-theme-toggle>
<svg
width="1em"
height="1em"
viewBox="0 0 24 24"
aria-hidden="true"
style="vertical-align: -0.125em"
>
<circle cx="12" cy="12" r="9" fill="none" stroke="currentColor" stroke-width="2" />
<path d="M12 3a9 9 0 0 1 0 18z" fill="currentColor" />
</svg>
Dark mode
</button>
<div class="card">
<h1>Hello</h1>
<p>
Reload the page: your choice stays. Change your system theme: nothing moves until you go
back to it.
</p>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/theme@1.1.0/dist/browser/theme.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
:root {
--bg: #ffffff;
--fg: #18181b;
--card: #f4f4f5;
--accent: #0e7490;
}
[data-theme='dark'] {
--bg: #18181b;
--fg: #f4f4f5;
--card: #27272a;
--accent: #22d3ee;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
font-family: system-ui, sans-serif;
}
main {
display: grid;
gap: 1rem;
max-width: 32rem;
margin: 0 auto;
padding: 2rem 1rem;
}
.card {
padding: 1rem;
border-radius: 0.75rem;
background: var(--card);
}
.toggle {
justify-self: start;
padding: 0.5rem 1rem;
border: 1px solid currentColor;
border-radius: 999px;
background: none;
color: inherit;
font: inherit;
cursor: pointer;
}
.toggle[aria-pressed='true'] {
background: var(--accent);
border-color: var(--accent);
color: var(--bg);
}
JavaScript
// Nothing to call: the script applies the theme and handles [data-theme-toggle].
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>One button for dark mode</title>
<style>
:root {
--bg: #ffffff;
--fg: #18181b;
--card: #f4f4f5;
--accent: #0e7490;
}
[data-theme='dark'] {
--bg: #18181b;
--fg: #f4f4f5;
--card: #27272a;
--accent: #22d3ee;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
font-family: system-ui, sans-serif;
}
main {
display: grid;
gap: 1rem;
max-width: 32rem;
margin: 0 auto;
padding: 2rem 1rem;
}
.card {
padding: 1rem;
border-radius: 0.75rem;
background: var(--card);
}
.toggle {
justify-self: start;
padding: 0.5rem 1rem;
border: 1px solid currentColor;
border-radius: 999px;
background: none;
color: inherit;
font: inherit;
cursor: pointer;
}
.toggle[aria-pressed='true'] {
background: var(--accent);
border-color: var(--accent);
color: var(--bg);
}
</style>
</head>
<body>
<main>
<button type="button" class="toggle" data-theme-toggle>
<svg
width="1em"
height="1em"
viewBox="0 0 24 24"
aria-hidden="true"
style="vertical-align: -0.125em"
>
<circle cx="12" cy="12" r="9" fill="none" stroke="currentColor" stroke-width="2" />
<path d="M12 3a9 9 0 0 1 0 18z" fill="currentColor" />
</svg>
Dark mode
</button>
<div class="card">
<h1>Hello</h1>
<p>
Reload the page: your choice stays. Change your system theme: nothing moves until you go
back to it.
</p>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/theme@1.1.0/dist/browser/theme.global.js"></script>
<script>
// Nothing to call: the script applies the theme and handles [data-theme-toggle].
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>One button for dark mode</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<button type="button" class="toggle" data-theme-toggle>
<svg
width="1em"
height="1em"
viewBox="0 0 24 24"
aria-hidden="true"
style="vertical-align: -0.125em"
>
<circle cx="12" cy="12" r="9" fill="none" stroke="currentColor" stroke-width="2" />
<path d="M12 3a9 9 0 0 1 0 18z" fill="currentColor" />
</svg>
Dark mode
</button>
<div class="card">
<h1>Hello</h1>
<p>
Reload the page: your choice stays. Change your system theme: nothing moves until you go
back to it.
</p>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
:root {
--bg: #ffffff;
--fg: #18181b;
--card: #f4f4f5;
--accent: #0e7490;
}
[data-theme='dark'] {
--bg: #18181b;
--fg: #f4f4f5;
--card: #27272a;
--accent: #22d3ee;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
font-family: system-ui, sans-serif;
}
main {
display: grid;
gap: 1rem;
max-width: 32rem;
margin: 0 auto;
padding: 2rem 1rem;
}
.card {
padding: 1rem;
border-radius: 0.75rem;
background: var(--card);
}
.toggle {
justify-self: start;
padding: 0.5rem 1rem;
border: 1px solid currentColor;
border-radius: 999px;
background: none;
color: inherit;
font: inherit;
cursor: pointer;
}
.toggle[aria-pressed='true'] {
background: var(--accent);
border-color: var(--accent);
color: var(--bg);
}
JavaScript
import { createTheme } from '@samline/theme'
createTheme()
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>One button for dark mode</title>
<style>
:root {
--bg: #ffffff;
--fg: #18181b;
--card: #f4f4f5;
--accent: #0e7490;
}
[data-theme='dark'] {
--bg: #18181b;
--fg: #f4f4f5;
--card: #27272a;
--accent: #22d3ee;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
font-family: system-ui, sans-serif;
}
main {
display: grid;
gap: 1rem;
max-width: 32rem;
margin: 0 auto;
padding: 2rem 1rem;
}
.card {
padding: 1rem;
border-radius: 0.75rem;
background: var(--card);
}
.toggle {
justify-self: start;
padding: 0.5rem 1rem;
border: 1px solid currentColor;
border-radius: 999px;
background: none;
color: inherit;
font: inherit;
cursor: pointer;
}
.toggle[aria-pressed='true'] {
background: var(--accent);
border-color: var(--accent);
color: var(--bg);
}
</style>
</head>
<body>
<main>
<button type="button" class="toggle" data-theme-toggle>
<svg
width="1em"
height="1em"
viewBox="0 0 24 24"
aria-hidden="true"
style="vertical-align: -0.125em"
>
<circle cx="12" cy="12" r="9" fill="none" stroke="currentColor" stroke-width="2" />
<path d="M12 3a9 9 0 0 1 0 18z" fill="currentColor" />
</svg>
Dark mode
</button>
<div class="card">
<h1>Hello</h1>
<p>
Reload the page: your choice stays. Change your system theme: nothing moves until you go
back to it.
</p>
</div>
</main>
<script type="module">
import { createTheme } from '@samline/theme'
createTheme()
</script>
</body>
</html>
Let visitors follow their system
Buttons with data-theme-set offer light, dark and system, a select offers the same, and both stay in sync. A themechange event reports every change.
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Let visitors follow their system</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="group" role="group" aria-label="Theme">
<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>
</div>
<label>
Theme
<select data-theme-select>
<option value="system">System</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<p class="card" id="status" aria-live="polite"></p>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/theme@1.1.0/dist/browser/theme.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
:root {
--bg: #ffffff;
--fg: #18181b;
--card: #f4f4f5;
--accent: #0e7490;
}
[data-theme='dark'] {
--bg: #18181b;
--fg: #f4f4f5;
--card: #27272a;
--accent: #22d3ee;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
font-family: system-ui, sans-serif;
}
main {
display: grid;
gap: 1rem;
max-width: 32rem;
margin: 0 auto;
padding: 2rem 1rem;
}
.card {
padding: 1rem;
border-radius: 0.75rem;
background: var(--card);
}
.group {
display: inline-flex;
justify-self: start;
border: 1px solid currentColor;
border-radius: 999px;
overflow: hidden;
}
.group button {
padding: 0.5rem 1rem;
border: 0;
background: none;
color: inherit;
font: inherit;
cursor: pointer;
}
.group [aria-pressed='true'] {
background: var(--accent);
color: var(--bg);
}
select {
margin-left: 0.5rem;
font: inherit;
}
JavaScript
const status = document.querySelector('#status')
const show = ({ mode, theme }) => {
status.textContent = mode === 'system' ? `Following the system: ${theme}` : `Chosen: ${theme}`
}
show(Theme.controller)
document.addEventListener('themechange', event => show(event.detail))
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Let visitors follow their system</title>
<style>
:root {
--bg: #ffffff;
--fg: #18181b;
--card: #f4f4f5;
--accent: #0e7490;
}
[data-theme='dark'] {
--bg: #18181b;
--fg: #f4f4f5;
--card: #27272a;
--accent: #22d3ee;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
font-family: system-ui, sans-serif;
}
main {
display: grid;
gap: 1rem;
max-width: 32rem;
margin: 0 auto;
padding: 2rem 1rem;
}
.card {
padding: 1rem;
border-radius: 0.75rem;
background: var(--card);
}
.group {
display: inline-flex;
justify-self: start;
border: 1px solid currentColor;
border-radius: 999px;
overflow: hidden;
}
.group button {
padding: 0.5rem 1rem;
border: 0;
background: none;
color: inherit;
font: inherit;
cursor: pointer;
}
.group [aria-pressed='true'] {
background: var(--accent);
color: var(--bg);
}
select {
margin-left: 0.5rem;
font: inherit;
}
</style>
</head>
<body>
<main>
<div class="group" role="group" aria-label="Theme">
<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>
</div>
<label>
Theme
<select data-theme-select>
<option value="system">System</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<p class="card" id="status" aria-live="polite"></p>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/theme@1.1.0/dist/browser/theme.global.js"></script>
<script>
const status = document.querySelector('#status')
const show = ({ mode, theme }) => {
status.textContent =
mode === 'system' ? `Following the system: ${theme}` : `Chosen: ${theme}`
}
show(Theme.controller)
document.addEventListener('themechange', event => show(event.detail))
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Let visitors follow their system</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="group" role="group" aria-label="Theme">
<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>
</div>
<label>
Theme
<select data-theme-select>
<option value="system">System</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<p class="card" id="status" aria-live="polite"></p>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
:root {
--bg: #ffffff;
--fg: #18181b;
--card: #f4f4f5;
--accent: #0e7490;
}
[data-theme='dark'] {
--bg: #18181b;
--fg: #f4f4f5;
--card: #27272a;
--accent: #22d3ee;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
font-family: system-ui, sans-serif;
}
main {
display: grid;
gap: 1rem;
max-width: 32rem;
margin: 0 auto;
padding: 2rem 1rem;
}
.card {
padding: 1rem;
border-radius: 0.75rem;
background: var(--card);
}
.group {
display: inline-flex;
justify-self: start;
border: 1px solid currentColor;
border-radius: 999px;
overflow: hidden;
}
.group button {
padding: 0.5rem 1rem;
border: 0;
background: none;
color: inherit;
font: inherit;
cursor: pointer;
}
.group [aria-pressed='true'] {
background: var(--accent);
color: var(--bg);
}
select {
margin-left: 0.5rem;
font: inherit;
}
JavaScript
import { createTheme } from '@samline/theme'
const controller = createTheme()
const status = document.querySelector('#status')
const show = ({ mode, theme }) => {
status.textContent = mode === 'system' ? `Following the system: ${theme}` : `Chosen: ${theme}`
}
show(controller)
document.addEventListener('themechange', event => show(event.detail))
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Let visitors follow their system</title>
<style>
:root {
--bg: #ffffff;
--fg: #18181b;
--card: #f4f4f5;
--accent: #0e7490;
}
[data-theme='dark'] {
--bg: #18181b;
--fg: #f4f4f5;
--card: #27272a;
--accent: #22d3ee;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
font-family: system-ui, sans-serif;
}
main {
display: grid;
gap: 1rem;
max-width: 32rem;
margin: 0 auto;
padding: 2rem 1rem;
}
.card {
padding: 1rem;
border-radius: 0.75rem;
background: var(--card);
}
.group {
display: inline-flex;
justify-self: start;
border: 1px solid currentColor;
border-radius: 999px;
overflow: hidden;
}
.group button {
padding: 0.5rem 1rem;
border: 0;
background: none;
color: inherit;
font: inherit;
cursor: pointer;
}
.group [aria-pressed='true'] {
background: var(--accent);
color: var(--bg);
}
select {
margin-left: 0.5rem;
font: inherit;
}
</style>
</head>
<body>
<main>
<div class="group" role="group" aria-label="Theme">
<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>
</div>
<label>
Theme
<select data-theme-select>
<option value="system">System</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<p class="card" id="status" aria-live="polite"></p>
</main>
<script type="module">
import { createTheme } from '@samline/theme'
const controller = createTheme()
const status = document.querySelector('#status')
const show = ({ mode, theme }) => {
status.textContent =
mode === 'system' ? `Following the system: ${theme}` : `Chosen: ${theme}`
}
show(controller)
document.addEventListener('themechange', event => show(event.detail))
</script>
</body>
</html>
More than light and dark
Any number of named themes, written as a class. The script tag names them so a stored choice applies before the first paint, and Theme.auto adds a browser UI color for each one. color-scheme is set for light and dark only.
Written as a class, with a palette of its own for each theme.
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>More than light and dark</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<fieldset>
<legend>Theme</legend>
<label><input type="radio" name="theme" data-theme-set="light" /> Light</label>
<label><input type="radio" name="theme" data-theme-set="dark" /> Dark</label>
<label><input type="radio" name="theme" data-theme-set="sepia" /> Sepia</label>
</fieldset>
<div class="card">
<p>Radios are checked for you, and the address bar matches on mobile.</p>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/theme@1.1.0/dist/browser/theme.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
:root {
--bg: #ffffff;
--fg: #18181b;
--card: #f4f4f5;
}
.dark {
--bg: #18181b;
--fg: #f4f4f5;
--card: #27272a;
}
.sepia {
--bg: #f4ecd8;
--fg: #433422;
--card: #eadfc4;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
font-family: Georgia, serif;
}
main {
display: grid;
gap: 1rem;
max-width: 32rem;
margin: 0 auto;
padding: 2rem 1rem;
}
fieldset {
display: flex;
gap: 1rem;
border-radius: 0.75rem;
}
.card {
padding: 1rem;
border-radius: 0.75rem;
background: var(--card);
}
JavaScript
// Restart with the options a script tag cannot hold. The data-* options
// on the script tag still apply, so the theme on the page does not change.
Theme.auto({
themeColor: { light: '#ffffff', dark: '#18181b', sepia: '#f4ecd8' },
})
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>More than light and dark</title>
<style>
:root {
--bg: #ffffff;
--fg: #18181b;
--card: #f4f4f5;
}
.dark {
--bg: #18181b;
--fg: #f4f4f5;
--card: #27272a;
}
.sepia {
--bg: #f4ecd8;
--fg: #433422;
--card: #eadfc4;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
font-family: Georgia, serif;
}
main {
display: grid;
gap: 1rem;
max-width: 32rem;
margin: 0 auto;
padding: 2rem 1rem;
}
fieldset {
display: flex;
gap: 1rem;
border-radius: 0.75rem;
}
.card {
padding: 1rem;
border-radius: 0.75rem;
background: var(--card);
}
</style>
</head>
<body>
<main>
<fieldset>
<legend>Theme</legend>
<label><input type="radio" name="theme" data-theme-set="light" /> Light</label>
<label><input type="radio" name="theme" data-theme-set="dark" /> Dark</label>
<label><input type="radio" name="theme" data-theme-set="sepia" /> Sepia</label>
</fieldset>
<div class="card">
<p>Radios are checked for you, and the address bar matches on mobile.</p>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/theme@1.1.0/dist/browser/theme.global.js"></script>
<script>
// Restart with the options a script tag cannot hold. The data-* options
// on the script tag still apply, so the theme on the page does not change.
Theme.auto({
themeColor: { light: '#ffffff', dark: '#18181b', sepia: '#f4ecd8' },
})
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>More than light and dark</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<fieldset>
<legend>Theme</legend>
<label><input type="radio" name="theme" data-theme-set="light" /> Light</label>
<label><input type="radio" name="theme" data-theme-set="dark" /> Dark</label>
<label><input type="radio" name="theme" data-theme-set="sepia" /> Sepia</label>
</fieldset>
<div class="card">
<p>Radios are checked for you, and the address bar matches on mobile.</p>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
:root {
--bg: #ffffff;
--fg: #18181b;
--card: #f4f4f5;
}
.dark {
--bg: #18181b;
--fg: #f4f4f5;
--card: #27272a;
}
.sepia {
--bg: #f4ecd8;
--fg: #433422;
--card: #eadfc4;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
font-family: Georgia, serif;
}
main {
display: grid;
gap: 1rem;
max-width: 32rem;
margin: 0 auto;
padding: 2rem 1rem;
}
fieldset {
display: flex;
gap: 1rem;
border-radius: 0.75rem;
}
.card {
padding: 1rem;
border-radius: 0.75rem;
background: var(--card);
}
JavaScript
import { createTheme } from '@samline/theme'
createTheme({
themes: ['light', 'dark', 'sepia'],
defaultMode: 'light',
attribute: 'class',
themeColor: { light: '#ffffff', dark: '#18181b', sepia: '#f4ecd8' },
})
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>More than light and dark</title>
<style>
:root {
--bg: #ffffff;
--fg: #18181b;
--card: #f4f4f5;
}
.dark {
--bg: #18181b;
--fg: #f4f4f5;
--card: #27272a;
}
.sepia {
--bg: #f4ecd8;
--fg: #433422;
--card: #eadfc4;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
font-family: Georgia, serif;
}
main {
display: grid;
gap: 1rem;
max-width: 32rem;
margin: 0 auto;
padding: 2rem 1rem;
}
fieldset {
display: flex;
gap: 1rem;
border-radius: 0.75rem;
}
.card {
padding: 1rem;
border-radius: 0.75rem;
background: var(--card);
}
</style>
</head>
<body>
<main>
<fieldset>
<legend>Theme</legend>
<label><input type="radio" name="theme" data-theme-set="light" /> Light</label>
<label><input type="radio" name="theme" data-theme-set="dark" /> Dark</label>
<label><input type="radio" name="theme" data-theme-set="sepia" /> Sepia</label>
</fieldset>
<div class="card">
<p>Radios are checked for you, and the address bar matches on mobile.</p>
</div>
</main>
<script type="module">
import { createTheme } from '@samline/theme'
createTheme({
themes: ['light', 'dark', 'sepia'],
defaultMode: 'light',
attribute: 'class',
themeColor: { light: '#ffffff', dark: '#18181b', sepia: '#f4ecd8' },
})
</script>
</body>
</html>
These examples show a few possibilities. The next ideas are yours: combine the package’s capabilities with your own content, design and interactions to create the experience your project needs.
Read the example guideDownload the Theme skill for AI agents