diff --git a/.changeset/color-mode-hook.md b/.changeset/color-mode-hook.md new file mode 100644 index 0000000..6d70ad3 --- /dev/null +++ b/.changeset/color-mode-hook.md @@ -0,0 +1,8 @@ +--- +'@maxcwolf/ui-react-native': patch +--- + +Theme switching is now `useColorMode()`, which returns `{ mode, setMode, name, names, theme }`. +It replaces `useThemeSwitcher()` (`preference`, `setPreference`). `ThemeProvider`'s props are now +`mode`, `defaultMode` and `onModeChange`, and the `ThemePreference` type is `ColorMode`. +`useTheme()` is unchanged. diff --git a/README.md b/README.md index 5575a6a..631092e 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,8 @@ export default function App() { Switch theme anywhere below the provider: ```tsx -const { setPreference } = useThemeSwitcher(); -setPreference('dark'); // or 'light', 'system', or any theme you registered +const { setMode } = useColorMode(); +setMode('dark'); // or 'light', 'system', or any theme you registered ``` ## Components diff --git a/apps/storybook/src/decorators.tsx b/apps/storybook/src/decorators.tsx index 323f8ba..923c318 100644 --- a/apps/storybook/src/decorators.tsx +++ b/apps/storybook/src/decorators.tsx @@ -1,14 +1,10 @@ -import { Box, ThemeProvider, type ThemePreference } from '@maxcwolf/ui-react-native'; +import { Box, ThemeProvider, type ColorMode } from '@maxcwolf/ui-react-native'; import type { Decorator } from '@storybook/react'; /** The themes the toolbar (web) and captures can pick. `system` follows the device. */ -export const themeOptions = [ - 'system', - 'light', - 'dark', -] as const satisfies readonly ThemePreference[]; +export const themeOptions = ['system', 'light', 'dark'] as const satisfies readonly ColorMode[]; -const toPreference = (value: unknown): ThemePreference => +const toMode = (value: unknown): ColorMode => themeOptions.find((option) => option === value) ?? 'system'; /** @@ -17,11 +13,11 @@ const toPreference = (value: unknown): ThemePreference => * and tested in every theme; on a device, `system` follows the simulator's appearance. */ export const withTheme: Decorator = (Story, context) => { - const preference = toPreference(context.globals.theme); + const mode = toMode(context.globals.theme); return ( - + {/* e2e waits for this id, so axe and screenshots never see the previous theme. */} - + diff --git a/docs/adr/0005-theme-api.md b/docs/adr/0005-theme-api.md index 71143d3..d471231 100644 --- a/docs/adr/0005-theme-api.md +++ b/docs/adr/0005-theme-api.md @@ -1,6 +1,6 @@ # 0005. Theme API: semantic colors, named themes, switching in the provider -- Status: accepted +- Status: accepted (amended 2026-11-08: the switcher is `useColorMode` with `mode`/`setMode`) - Date: 2026-11-07 ## Context @@ -13,8 +13,11 @@ impossible for a component to opt out of. - Components use semantic color names from one `ThemeColors` type; every theme defines all of them (a missing one is a type error). - `ThemeProvider` takes any number of named themes (`themes={{ light, dark, ...rest }}`) and a - preference (`'system'` or a name), controlled or uncontrolled; `useThemeSwitcher()` reads and - changes it at runtime. `'system'` follows `useColorScheme()`. + color mode (`'system'` or a theme name), controlled (`mode`) or uncontrolled (`defaultMode`); + `useColorMode()` reads and changes it at runtime. `'system'` follows `useColorScheme()`. +- Switching is a separate hook from `useTheme()`, as in MUI (`useColorScheme`) and Chakra + (`useColorMode`). `useTheme()` returns only the theme's tokens, which is all components need, so + a component cannot change the app's theme and the theme object stays the plain `Theme` type. - `createWolfTheme(overrides, base)` builds a theme from partial overrides with the same type. - `auditThemeContrast(theme)` checks a theme against every pairing the components render, and is public so apps can test their own themes. @@ -23,9 +26,10 @@ impossible for a component to opt out of. `fontSize`/`fontWeight`/`fontFamily` props. **Guards:** the `ThemeColors` type, `theme.test.ts` (both themes pass the audit and have the same -shape), ESLint `no-restricted-imports` and `wolf/no-raw-color`, `TextProps`. +shape), ESLint `no-restricted-imports` (Restyle's hooks outside `src/theme/`, `useColorMode` in +components and primitives) and `wolf/no-raw-color`, `TextProps`. ## Consequences Theme state lives in one place. Persisting the user's choice is the app's job -(`onPreferenceChange`), so the library needs no storage dependency. +(`onModeChange`), so the library needs no storage dependency. diff --git a/docs/theming.md b/docs/theming.md index c7e41da..515201f 100644 --- a/docs/theming.md +++ b/docs/theming.md @@ -25,26 +25,29 @@ changes their appearance setting. On Android, Expo apps need `expo-system-ui` an ## Let the user choose ```tsx -import { useThemeSwitcher } from '@maxcwolf/ui-react-native'; +import { useColorMode } from '@maxcwolf/ui-react-native'; function AppearanceSetting() { - const { name, preference, names, setPreference } = useThemeSwitcher(); - // name: the theme showing now ('light'); preference: what was chosen ('system') + const { mode, setMode, name, names } = useColorMode(); + // mode: what was chosen ('system'); name: the theme showing now ('light') // names: every theme the provider has (['light', 'dark']) - return ; + return ; } ``` +`useColorMode()` is for app code such as a settings screen. Components read the active theme's +tokens with `useTheme()` and never change the mode. + To remember the choice across launches, persist it yourself and hand it back: ```tsx - + ``` -Or control it completely with `preference`, for example from your own settings store: +Or control it completely with `mode`, for example from your own settings store: ```tsx - + ``` ## Make a brand theme @@ -74,8 +77,8 @@ variant is merged, so `{ heading1: { fontSize: 40 } }` keeps its weight and line `themes` takes any names. `light` and `dark` back `'system'`; the rest are chosen by name. ```tsx - -// later: setPreference('highContrast') + +// later: setMode('highContrast') ``` ## Check your theme's contrast diff --git a/e2e/__screenshots__/desktop/foundations-theme--switching--dark.png b/e2e/__screenshots__/desktop/foundations-theme--switching--dark.png index d66da9e..d6d518e 100644 Binary files a/e2e/__screenshots__/desktop/foundations-theme--switching--dark.png and b/e2e/__screenshots__/desktop/foundations-theme--switching--dark.png differ diff --git a/e2e/__screenshots__/desktop/foundations-theme--switching--light.png b/e2e/__screenshots__/desktop/foundations-theme--switching--light.png index 12cd767..d3389f2 100644 Binary files a/e2e/__screenshots__/desktop/foundations-theme--switching--light.png and b/e2e/__screenshots__/desktop/foundations-theme--switching--light.png differ diff --git a/e2e/__screenshots__/mobile/foundations-theme--switching--dark.png b/e2e/__screenshots__/mobile/foundations-theme--switching--dark.png index 9654b02..bb3823d 100644 Binary files a/e2e/__screenshots__/mobile/foundations-theme--switching--dark.png and b/e2e/__screenshots__/mobile/foundations-theme--switching--dark.png differ diff --git a/e2e/__screenshots__/mobile/foundations-theme--switching--light.png b/e2e/__screenshots__/mobile/foundations-theme--switching--light.png index cc3a841..280e984 100644 Binary files a/e2e/__screenshots__/mobile/foundations-theme--switching--light.png and b/e2e/__screenshots__/mobile/foundations-theme--switching--light.png differ diff --git a/e2e/__transcripts__/foundations-theme--switching.txt b/e2e/__transcripts__/foundations-theme--switching.txt index e6360dd..c514dfe 100644 --- a/e2e/__transcripts__/foundations-theme--switching.txt +++ b/e2e/__transcripts__/foundations-theme--switching.txt @@ -1,5 +1,5 @@ heading, Appearance, level 1 -Showing "light" (preference: light) +Showing "light" (mode: light) button, system button, light button, dark diff --git a/e2e/tests/keyboard.spec.ts b/e2e/tests/keyboard.spec.ts index e0201be..43be34d 100644 --- a/e2e/tests/keyboard.spec.ts +++ b/e2e/tests/keyboard.spec.ts @@ -22,7 +22,7 @@ test('a button takes keyboard focus with a visible ring, and Enter presses it', await page.keyboard.press('Tab'); await page.keyboard.press('Tab'); await page.keyboard.press('Enter'); - await expect(page.getByText('Showing "dark" (preference: dark)')).toBeVisible(); + await expect(page.getByText('Showing "dark" (mode: dark)')).toBeVisible(); }); test('a mouse click does not leave a focus ring behind', async ({ page }) => { diff --git a/eslint.config.mjs b/eslint.config.mjs index 5045183..89a3b99 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -17,6 +17,13 @@ const restyleOnlyInTheme = { 'Use useTheme / ThemeProvider from the theme layer (src/theme), which types the theme and adds theme switching (ADR 0005).', }; +const colorModeOnlyInApps = { + group: ['**/theme', '**/theme/*'], + importNames: ['useColorMode'], + message: + 'Components read tokens with useTheme(); changing the color mode belongs to the app (ADR 0005).', +}; + const testingLibraryRender = { name: '@testing-library/react-native', importNames: ['render'], @@ -142,7 +149,19 @@ export default tseslint.config( rules: { 'no-restricted-imports': [ 'error', - { paths: [restyleOnlyInTheme, reactNativeViewsInComponents] }, + { + paths: [restyleOnlyInTheme, reactNativeViewsInComponents], + patterns: [colorModeOnlyInApps], + }, + ], + }, + }, + { + files: ['packages/ui/src/primitives/**/*.{ts,tsx}'], + rules: { + 'no-restricted-imports': [ + 'error', + { paths: [restyleOnlyInTheme], patterns: [colorModeOnlyInApps] }, ], }, }, diff --git a/packages/ui/src/__contracts__/__snapshots__/public-api.test.ts.snap b/packages/ui/src/__contracts__/__snapshots__/public-api.test.ts.snap index 9a88a5e..56f701f 100644 --- a/packages/ui/src/__contracts__/__snapshots__/public-api.test.ts.snap +++ b/packages/ui/src/__contracts__/__snapshots__/public-api.test.ts.snap @@ -20,7 +20,7 @@ exports[`exports exactly the public API 1`] = ` "iconNames", "lightColors", "lightTheme", + "useColorMode", "useTheme", - "useThemeSwitcher", ] `; diff --git a/packages/ui/src/test-utils/render.tsx b/packages/ui/src/test-utils/render.tsx index 36174aa..dfde357 100644 --- a/packages/ui/src/test-utils/render.tsx +++ b/packages/ui/src/test-utils/render.tsx @@ -4,11 +4,11 @@ import { type RenderResult, } from '@testing-library/react-native'; import type { ReactElement } from 'react'; -import { ThemeProvider, type ThemePreference, type ThemeSet } from '../theme'; +import { ThemeProvider, type ColorMode, type ThemeSet } from '../theme'; interface Options extends Omit { /** The theme to render under. Defaults to `'light'`, so a test never depends on the device. */ - theme?: ThemePreference; + theme?: ColorMode; themes?: ThemeSet; } @@ -20,7 +20,7 @@ export function render( return rtlRender(ui, { ...options, wrapper: ({ children }) => ( - + {children} ), diff --git a/packages/ui/src/theme/Theme.stories.tsx b/packages/ui/src/theme/Theme.stories.tsx index e8dfb5a..a1ecf0e 100644 --- a/packages/ui/src/theme/Theme.stories.tsx +++ b/packages/ui/src/theme/Theme.stories.tsx @@ -7,7 +7,7 @@ import { Box, Text } from '../primitives'; import type { ColorName } from './colors'; import { createWolfTheme } from './create-theme'; import { darkTheme, lightTheme, type TextVariant } from './theme'; -import { ThemeProvider, useThemeSwitcher } from './ThemeProvider'; +import { ThemeProvider, useColorMode } from './ThemeProvider'; import { useTheme } from './use-theme'; const meta = { title: 'Foundations/Theme' } satisfies Meta; @@ -83,21 +83,21 @@ const brandDark = createWolfTheme( ); function ThemeSettings() { - const { name, preference, names, setPreference } = useThemeSwitcher(); + const { name, mode, names, setMode } = useColorMode(); return ( Appearance - {`Showing "${name}" (preference: ${preference})`} + {`Showing "${name}" (mode: ${mode})`} {['system', ...names].map((option) => (