Files
livewire-material/resources/js/theme.js
T
Andreas Reinhold / reiniandClaude Opus 5 b48e879254
tests / lint (push) Successful in 1m0s
tests / feature (8.4) (push) Successful in 1m0s
tests / feature (8.5) (push) Successful in 1m1s
tests / browser (safari, webkit) (push) Successful in 1m59s
tests / browser (chrome, chromium) (push) Successful in 1m49s
tests / browser (firefox, firefox) (push) Successful in 1m54s
Add the Material 3 Expressive foundation
Colour, shape, type, elevation and motion as tokens and Tailwind
utilities; `php artisan material:scheme`, which generates an app's colour
roles with Google's material-color-utilities (spec 2025); the theme head
script with light, dark and system and its Alpine store; Google Sans Flex;
every Material Symbol (4,135, outlined and filled) drawn by <x-icon>
without blade-icons; all 35 M3 Expressive shapes, ported from androidx, as
<x-shape>; the x-figure directive; the Toasts concern; DesignGuard for
applications' tests; and a showcase with every token, both themes side by
side and an icon search.

Colour utilities are `@theme inline`, so a section with its own
data-theme repaints; without it they resolve once on :root.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V9NnLxnPp8vaaurb3Z1MFy
2026-09-13 05:24:11 +02:00

63 lines
2.1 KiB
JavaScript

/**
* The theme, as one piece of state every toggle on the page shares.
*
* <x-theme-script> has already decided the theme before this runs; the store starts from
* what it wrote on <html> (data-theme-choice, data-theme, data-theme-key). `choice` is what
* the visitor picked — light, dark or system — and `resolved` is what is showing.
*
* `value` is an accessor, so binding a control with `x-model="$store.theme.value"` goes
* through the same write as `set()` and `toggle()`: the attributes, then localStorage.
*/
document.addEventListener('alpine:init', () => {
const root = document.documentElement
const media = window.matchMedia('(prefers-color-scheme: dark)')
const choices = ['light', 'dark', 'system']
const resolve = (choice) => (choice === 'system' ? (media.matches ? 'dark' : 'light') : choice)
window.Alpine.store('theme', {
choice: choices.includes(root.dataset.themeChoice) ? root.dataset.themeChoice : 'system',
resolved: root.dataset.theme === 'dark' ? 'dark' : 'light',
get value() {
return this.choice
},
set value(choice) {
this.set(choice)
},
set(choice) {
if (!choices.includes(choice)) {
return
}
this.choice = choice
this.resolved = resolve(choice)
root.dataset.themeChoice = choice
root.dataset.theme = this.resolved
try {
localStorage.setItem(root.dataset.themeKey || 'material-theme', choice)
} catch {
// Blocked storage: the page still switches, it just will not remember.
}
},
toggle() {
this.set(this.resolved === 'dark' ? 'light' : 'dark')
},
})
// The head script repaints on an OS change while the choice is `system`; this keeps the
// store's `resolved` — which a toggle's icon reads — in step with it.
media.addEventListener('change', () => {
const theme = window.Alpine.store('theme')
if (theme.choice === 'system') {
theme.resolved = resolve('system')
}
})
})