Skip to content

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 ​

ParameterTypeDescription
callback(scheme) => voidCalled 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() // unsubscribe

Since ​

1.3.0

Released under the MIT License.