Skip to content

Commit 225051d

Browse files
committed
storybook(native): add backgrounds options and sync ThemeProvider to selected background; align with web preview
1 parent f4a33d5 commit 225051d

1 file changed

Lines changed: 34 additions & 9 deletions

File tree

apps/storybook-react-native/.rnstorybook/preview.tsx

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@ import type { Preview } from '@storybook/react-native';
22
import { Theme, ThemeProvider } from '@metamask/design-system-twrnc-preset';
33
import { darkTheme, lightTheme } from '@metamask/design-tokens';
44
import React, { type PropsWithChildren } from 'react';
5-
import { useColorScheme } from 'react-native';
65
import { GestureHandlerRootView } from 'react-native-gesture-handler';
76
import { SafeAreaProvider } from 'react-native-safe-area-context';
87

9-
const ThemeDecorator = ({ children }: PropsWithChildren) => {
10-
const colorScheme = useColorScheme();
11-
const theme = colorScheme === 'dark' ? Theme.Dark : Theme.Light;
8+
// Background options keyed by name so on-device backgrounds toolbar
9+
// and context.globals.backgrounds.value (which returns the key) both work.
10+
const backgroundOptions = {
11+
light: { name: 'light', value: lightTheme.colors.background.default },
12+
dark: { name: 'dark', value: darkTheme.colors.background.default },
13+
};
14+
15+
function themeFromKey(key?: string): Theme {
16+
return key === 'dark' ? Theme.Dark : Theme.Light;
17+
}
18+
19+
type ThemeDecoratorProps = PropsWithChildren<{ selectedKey?: string }>;
20+
21+
const ThemeDecorator = ({ children, selectedKey }: ThemeDecoratorProps) => {
22+
const theme = themeFromKey(selectedKey);
1223
const backgroundColor =
1324
theme === Theme.Dark
1425
? darkTheme.colors.background.default
@@ -25,12 +36,26 @@ const ThemeDecorator = ({ children }: PropsWithChildren) => {
2536

2637
const preview: Preview = {
2738
decorators: [
28-
(Story: React.ComponentType) => (
29-
<ThemeDecorator>
30-
<Story />
31-
</ThemeDecorator>
32-
),
39+
(Story: React.ComponentType, context: any) => {
40+
const selectedKey = context.globals?.backgrounds?.value as
41+
| string
42+
| undefined;
43+
return (
44+
<ThemeDecorator selectedKey={selectedKey}>
45+
<Story />
46+
</ThemeDecorator>
47+
);
48+
},
3349
],
50+
parameters: {
51+
backgrounds: {
52+
options: backgroundOptions,
53+
},
54+
layout: 'fullscreen',
55+
},
56+
initialGlobals: {
57+
backgrounds: { value: 'light' },
58+
},
3459
};
3560

3661
export default preview;

0 commit comments

Comments
 (0)