@@ -2,13 +2,24 @@ import type { Preview } from '@storybook/react-native';
22import { Theme , ThemeProvider } from '@metamask/design-system-twrnc-preset' ;
33import { darkTheme , lightTheme } from '@metamask/design-tokens' ;
44import React , { type PropsWithChildren } from 'react' ;
5- import { useColorScheme } from 'react-native' ;
65import { GestureHandlerRootView } from 'react-native-gesture-handler' ;
76import { 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
2637const 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
3661export default preview ;
0 commit comments