ModeProvider
A React Native provider that holds the light, dark or system choice and exposes it through a useMode hook.
Holds the light, dark or system choice for the whole app. Read it anywhere below with useMode(). "system" follows the device and updates when the device changes. The provider keeps the choice in memory only; save it from onModeChange and pass it back as initialMode to remember it. Works in React and in React Native with no extra packages.
Installation
CLI
npx @modus-ui/cli add mode-providerManual
- Copy the code from the code tab, using the React or React Native switch for your project.
- Create a file at
components/ui/mode-provider.tsx. - Paste the code.
The CLI installs the React Native version in an Expo or React Native app and the React version everywhere else. In a project that targets both, keep the React file mode-provider.tsx and the React Native file mode-provider.native.tsx side by side and import ./mode-provider; Metro picks the .native file on its own.
Usage
Props
| Prop | Type | Default | Description |
|---|---|---|---|
initialMode | Mode | "system" | The mode to start in. |
onModeChange | (mode: Mode) => void | - | Called whenever the mode changes, so you can save it. |
useMode() returns mode, the resolved scheme, setMode(mode) and toggle(), and throws outside a ModeProvider.
Comments