@molecule/app-theme-nativewind

Provider bond · theme · App (browser) · v1.1.0 · Apache-2.0

NativeWind theme provider for molecule.dev: the web theme palette and light/dark mode on React Native

npm install @molecule/app-theme-nativewind

npm · Source on GitHub · Implements @molecule/app-theme

How it works

@molecule/app-theme-nativewind is a provider bond on the app (browser) side: it implements the theme core interface (@molecule/app-theme) with a concrete vendor or library behind it.

Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.

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)

Works with: @molecule/app-storage, @molecule/app-theme, @molecule/app-theme-css-variables, @molecule/app-ui-tailwind

Reference

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.ts JSDoc, 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-native

API

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 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): NativeWindThemeProvider
  • config — 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: Theme

darkTheme

The default dark theme (the same palette as @molecule/app-theme-css-variables).

const darkTheme: Theme

Core 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.0
  • nativewind ^4.2.0
  • react-native >=0.72.0

Runtime Dependencies

  • @molecule/app-storage

  • @molecule/app-theme

  • @molecule/app-theme-css-variables

  • @molecule/app-ui-tailwind

  • nativewind

  • react-native

  • Needs the Tailwind preset from @molecule/app-ui-nativewind (presets: [require('nativewind/preset'), require('@molecule/app-ui-nativewind/preset')] in tailwind.config.js). It defines the colour classes as these variables and sets darkMode: 'class'. Without it, bg-primary has no rule and paints nothing.

  • systemDefault: true starts on the theme matching the device setting and follows it until the person picks a theme. Set "userInterfaceStyle": "automatic" in app.json, or iOS always reports light.

  • persistKey saves the chosen theme through the bonded @molecule/app-storage provider (or storage). Device storage is asynchronous, so the app starts on the default theme and switches once the saved one is read; await themeProvider.ready behind a splash screen to avoid the switch.

  • On Expo web the variables are set on <html> and .dark is toggled there.

  • React Navigation's headers and screen backgrounds are not ClassMap classes: pass useNavigationTheme(…) from @molecule/app-routing-react-navigation to <NavigationContainer theme={…}> so they follow this provider too.