@molecule/app-theme-nativewind
v1.1.0
Published
NativeWind theme provider for molecule.dev: the web theme palette and light/dark mode on React Native
Maintainers
Readme
@molecule/app-theme-nativewind
NativeWind theme provider for molecule.dev: the web's theme palette and light/dark mode on React Native (iOS, Android and Expo web).
It implements @molecule/app-theme, so useTheme(), toggleMode() and theme
switchers work as they do on the web. Every colour token of
@molecule/app-ui-tailwind's base.css, the derived ones included
(surface-container, on-primary, …), is computed from the active theme and
set as one of NativeWind's root variables, so every mounted component repaints.
dark: utilities follow the theme, not only the device setting.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { setProvider } from '@molecule/app-theme'
import {
createNativeWindThemeProvider,
darkTheme,
lightTheme,
} from '@molecule/app-theme-nativewind'
export const themeProvider = createNativeWindThemeProvider({
themes: [lightTheme, darkTheme],
defaultTheme: 'light',
systemDefault: true,
persistKey: 'molecule-theme',
})
setProvider(themeProvider)Type
provider
Installation
npm install @molecule/app-theme-nativewind @molecule/app-storage @molecule/app-theme @molecule/app-theme-css-variables @molecule/app-ui-tailwind nativewind react-nativeAPI
Interfaces
NativeWindThemeStorage
Where a saved theme name is kept. Either call may be asynchronous: device
storage is. @molecule/app-storage satisfies it.
interface NativeWindThemeStorage {
/** Reads the saved theme name (`null` when there is none). */
get(key: string): Promise<unknown> | unknown
/** Saves the chosen theme name. */
set(key: string, value: string): Promise<void> | void
}NativeWindThemeConfig
Configuration for {@link createNativeWindThemeProvider}.
interface NativeWindThemeConfig {
/** The themes the app offers. The first theme of each mode fills that mode until another is chosen. */
themes: Theme[]
/** The theme to start on (a name; default: the first theme). */
defaultTheme?: string
/**
* With no saved choice, start on the theme whose mode matches the device's
* appearance setting, and follow it when it changes, until the person picks a
* theme. Default: false.
*/
systemDefault?: boolean
/** Saves the chosen theme under this key and restores it on the next launch. */
persistKey?: string
/**
* Where `persistKey` is kept. Default: the `@molecule/app-storage` provider
* bonded at startup (read once it is bonded, so the bond order does not matter).
*/
storage?: NativeWindThemeStorage
}NativeWindThemeProvider
A ThemeProvider for NativeWind, with a promise that settles once a saved
theme has been read and applied.
interface NativeWindThemeProvider extends ThemeProvider {
/** Settles once a saved theme (if any) has been restored: splash screens, tests. */
ready: Promise<void>
}Functions
createNativeWindThemeProvider(config)
Creates a theme provider for a NativeWind app (iOS, Android and Expo web).
Pair it with @molecule/app-ui-nativewind's Tailwind preset, which gives every
colour class of the web ClassMap a variable this provider sets.
function createNativeWindThemeProvider(config: NativeWindThemeConfig): NativeWindThemeProviderconfig— The themes, the starting theme, OS-following and persistence.
Returns: The provider.
Constants
lightTheme
The default light theme (the same palette as @molecule/app-theme-css-variables).
const lightTheme: ThemedarkTheme
The default dark theme (the same palette as @molecule/app-theme-css-variables).
const darkTheme: ThemeCore Interface
Implements @molecule/app-theme interface.
Injection Notes
Requirements
Peer dependencies:
@molecule/app-storage^1.1.1@molecule/app-theme^1.0.4@molecule/app-theme-css-variables^1.0.4@molecule/app-ui-tailwind^1.4.0nativewind^4.2.0react-native>=0.72.0
Runtime Dependencies
@molecule/app-storage@molecule/app-theme@molecule/app-theme-css-variables@molecule/app-ui-tailwindnativewindreact-nativeNeeds the Tailwind preset from
@molecule/app-ui-nativewind(presets: [require('nativewind/preset'), require('@molecule/app-ui-nativewind/preset')]intailwind.config.js). It defines the colour classes as these variables and setsdarkMode: 'class'. Without it,bg-primaryhas no rule and paints nothing.systemDefault: truestarts on the theme matching the device setting and follows it until the person picks a theme. Set"userInterfaceStyle": "automatic"inapp.json, or iOS always reports light.persistKeysaves the chosen theme through the bonded@molecule/app-storageprovider (orstorage). Device storage is asynchronous, so the app starts on the default theme and switches once the saved one is read; awaitthemeProvider.readybehind a splash screen to avoid the switch.On Expo web the variables are set on
<html>and.darkis toggled there.React Navigation's headers and screen backgrounds are not ClassMap classes: pass
useNavigationTheme(…)from@molecule/app-routing-react-navigationto<NavigationContainer theme={…}>so they follow this provider too.
