Function: onPrefersColorSchemeChange()
ts
function onPrefersColorSchemeChange(callback): () => void;Subscribes to changes of the user's preferred color scheme, as reported by the prefers-color-scheme CSS media feature.
Does not fire in non-browser environments; returns a no-op unsubscribe.
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | (scheme) => void | Called with the new color scheme whenever it changes. |
Returns
A cleanup function that removes the listener.
() => void
Example
ts
const off = onPrefersColorSchemeChange((scheme) => {
console.log(scheme) // 'light' | 'dark'
})
off() // unsubscribeSince
1.3.0