addDarkSubscriber((dark) => { console.log("Dark mode changed: ", dark); }); setDark(true); // will trigger the subscriber
Use with React
A React hook is provided in the pure-react package
to get the dark mode state from React components.
Use setDark to change the dark mode state from React compoenents like you would from anywhere else.
Persisting to localStorage
You can persist the dark mode preference to by passing persist: true to initDark.
This will make initDark also load the preference from localStorage.
import { initDark } from"@pistonite/pure/dark";
initDark({ persist:true });
Setting color-scheme CSS property
The color-scheme property handles dark mode for native components like buttons
and scrollbars. By default, initDark will handle setting this property for the :root selector.
You can override this by passing a selector option.
import { initDark } from"@pistonite/pure/dark";
// will set `.my-app { color-scheme: dark }` initDark({ selector:".my-app" });
Init Dark mode wrappers
Detect user preference
User preference is detected with
matchMediaAPI, if available.Global dark mode state
initDarkinitializes the dark mode state.Use with React
A React hook is provided in the
pure-reactpackage to get the dark mode state from React components.Use
setDarkto change the dark mode state from React compoenents like you would from anywhere else.Persisting to localStorage
You can persist the dark mode preference to by passing
persist: truetoinitDark. This will makeinitDarkalso load the preference from localStorage.Setting
color-schemeCSS propertyThe
color-schemeproperty handles dark mode for native components like buttons and scrollbars. By default,initDarkwill handle setting this property for the:rootselector. You can override this by passing aselectoroption.