diff --git a/examples/expo-example/.rnstorybook/preview.tsx b/examples/expo-example/.rnstorybook/preview.tsx index f38f0e25b6..665a77a1a8 100644 --- a/examples/expo-example/.rnstorybook/preview.tsx +++ b/examples/expo-example/.rnstorybook/preview.tsx @@ -29,15 +29,29 @@ const preview: Preview = { hideFullScreenButton: false, noSafeArea: false, my_param: 'anything', + // backgrounds: { + // default: Appearance.getColorScheme() === 'dark' ? 'dark' : 'plain', + // values: [ + // { name: 'plain', value: 'white' }, + // { name: 'dark', value: '#333' }, + // { name: 'app', value: '#eeeeee' }, + // ], + // }, backgrounds: { - default: Appearance.getColorScheme() === 'dark' ? 'dark' : 'plain', - values: [ - { name: 'plain', value: 'white' }, - { name: 'dark', value: '#333' }, - { name: 'app', value: '#eeeeee' }, - ], + options: { + // 👇 Default options + dark: { name: 'Dark', value: '#333' }, + light: { name: 'Light', value: '#F7F9F2' }, + // 👇 Add your own + maroon: { name: 'Maroon', value: '#400' }, + }, }, }, + + initialGlobals: { + // 👇 Set the initial background color + backgrounds: { value: 'light' }, + }, }; export default preview; diff --git a/packages/ondevice-backgrounds/src/BackgroundPanel.tsx b/packages/ondevice-backgrounds/src/BackgroundPanel.tsx index 2a48e57abb..da64cb95d6 100644 --- a/packages/ondevice-backgrounds/src/BackgroundPanel.tsx +++ b/packages/ondevice-backgrounds/src/BackgroundPanel.tsx @@ -1,9 +1,9 @@ -import type { AddonStore, API } from 'storybook/internal/manager-api'; import { StyleSheet, Text, View } from 'react-native'; +import type { AddonStore, API } from 'storybook/internal/manager-api'; +import { useGlobals } from 'storybook/internal/preview-api'; import Swatch from './Swatch'; -import BackgroundEvents, { PARAM_KEY } from './constants'; -import { Background } from './index'; +import { PARAM_KEY } from './constants'; const codeSample = ` import React from 'react'; @@ -24,13 +24,14 @@ const BackgroundMeta: ComponentMeta = { component: Background, decorators: [withBackgrounds], parameters: { - backgrounds: { - default: 'plain', - values: [ - { name: 'plain', value: 'white' }, - { name: 'warm', value: 'hotpink' }, - { name: 'cool', value: 'deepskyblue' }, - ], + backgrounds: { + options: { + // 👇 Default options + dark: { name: 'Dark', value: '#333' }, + light: { name: 'Light', value: '#F7F9F2' }, + // 👇 Add your own + maroon: { name: 'Maroon', value: '#400' }, + }, }, }, }; @@ -60,27 +61,38 @@ const Instructions = () => ( export type Channel = ReturnType; interface BackgroundPanelProps { - channel: Channel; api: API; active: boolean; } -const BackgroundPanel = ({ active, api, channel }: BackgroundPanelProps) => { - if (!active) { - return null; - } +interface BackgroundOptions { + [key: string]: { + name: string; + value: string; + }; +} +const BackgroundPanel = ({ active, api }: BackgroundPanelProps) => { + const [, updateGlobals] = useGlobals(); const store = api.store(); const storyId = store.getSelection().storyId; const story = store.fromId(storyId); - const backgrounds: { default?: string; values: Background[] } = story.parameters[PARAM_KEY]; + + if (!active) { + return null; + } + + const backgrounds: BackgroundOptions = story.parameters[PARAM_KEY]?.options; const setBackgroundFromSwatch = (background: string) => { - channel.emit(BackgroundEvents.UPDATE_BACKGROUND, background); + updateGlobals({ + [PARAM_KEY]: { value: background }, + }); }; + return ( {backgrounds ? ( - backgrounds.values.map(({ value, name }) => ( + Object.entries(backgrounds).map(([name, { value }]) => ( diff --git a/packages/ondevice-backgrounds/src/constants.ts b/packages/ondevice-backgrounds/src/constants.ts index d28430f5e0..bf217eb0f1 100644 --- a/packages/ondevice-backgrounds/src/constants.ts +++ b/packages/ondevice-backgrounds/src/constants.ts @@ -2,8 +2,8 @@ export const PARAM_KEY = 'backgrounds'; export const ADDON_ID = 'storybook-addon-background'; export const PANEL_ID = `${ADDON_ID}/background-panel`; -export default { - SET: `${ADDON_ID}:set`, - UNSET: `${ADDON_ID}:unset`, - UPDATE_BACKGROUND: `${ADDON_ID}:update`, -}; +// export default { +// SET: `${ADDON_ID}:set`, +// UNSET: `${ADDON_ID}:unset`, +// UPDATE_BACKGROUND: `${ADDON_ID}:update`, +// }; diff --git a/packages/ondevice-backgrounds/src/container.tsx b/packages/ondevice-backgrounds/src/container.tsx index c6a03eb0f1..575bb1ceff 100644 --- a/packages/ondevice-backgrounds/src/container.tsx +++ b/packages/ondevice-backgrounds/src/container.tsx @@ -1,23 +1,16 @@ -import React, { ReactNode, useState, useEffect } from 'react'; +import { ReactNode } from 'react'; import { StyleSheet, View } from 'react-native'; -import Constants from './constants'; -import { Channel } from './BackgroundPanel'; +import { PARAM_KEY } from './constants'; + +import { useGlobals } from 'storybook/internal/preview-api'; interface ContainerProps { - initialBackground: string; - channel: Channel; children: ReactNode; } -const Container = ({ initialBackground, channel, children }: ContainerProps) => { - const [background, setBackground] = useState(initialBackground || ''); - - useEffect(() => { - channel.on(Constants.UPDATE_BACKGROUND, setBackground); - return () => { - channel.removeListener(Constants.UPDATE_BACKGROUND, setBackground); - }; - }, [channel]); +const Container = ({ children }: ContainerProps) => { + const [globals] = useGlobals(); + const background = globals[PARAM_KEY]?.value; return ( { - const data = (parameters || options || { values: [] }) as { - default?: string; - values: Background[]; - }; - const backgrounds: Background[] = data.values; - - let background = 'transparent'; - if (backgrounds.length !== 0) { - addons.getChannel().emit(Events.SET, backgrounds); - const defaultValue = data.default - ? backgrounds.find((b) => b.name === data.default) - : undefined; - const defaultOrFirst = defaultValue ? defaultValue : backgrounds[0]; - - if (defaultOrFirst) { - background = defaultOrFirst.value; - } - } - - return ( - - {getStory(context) as React.ReactNode} - - ); - }, -}); +export const withBackgrounds: DecoratorFunction = (Story) => ( + + + +); diff --git a/packages/ondevice-backgrounds/src/register.tsx b/packages/ondevice-backgrounds/src/register.tsx index 58919323db..7ce460d3c9 100644 --- a/packages/ondevice-backgrounds/src/register.tsx +++ b/packages/ondevice-backgrounds/src/register.tsx @@ -3,11 +3,10 @@ import BackgroundPanel from './BackgroundPanel'; import { ADDON_ID, PANEL_ID, PARAM_KEY } from './constants'; addons.register(ADDON_ID, (api) => { - const channel = addons.getChannel(); addons.add(PANEL_ID, { type: types.PANEL, title: 'Backgrounds', - render: ({ active }) => , + render: ({ active }) => , paramKey: PARAM_KEY, }); });