Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to capture the current selected background value #372

Open
cmnstmntmn opened this issue Jul 19, 2022 · 2 comments
Open

Not able to capture the current selected background value #372

cmnstmntmn opened this issue Jul 19, 2022 · 2 comments

Comments

@cmnstmntmn
Copy link

Describe the bug
I'm having

//.ondevice/preview.js

export const decorators = [withBackgrounds, ThemeDecorator];
export const parameters = {
  actions: { argTypesRegex: '^on[A-Z].*' },
  backgrounds: [
    {name: 'Light', value: '#000000', default: true},
    {name: 'Dark', value: '#ffffff'},
  ],
};

Then, in my theme decorator i'm looking for the selected value inside the context object,
but it doesn't seem to be there.

export const ThemeDecorator = (Story, context) => {
  const theme =  ...'; //i'm looking for the selected background value inside the context object

  return (
    <ThemeProvider dark={theme === 'dark'}>
      <Story />
    </ThemeProvider>
  );
};

System:
sb 6.0.1-beta.7

@dannyhw
Copy link
Member

dannyhw commented Jul 19, 2022

@cmnstmntmn
I just looked at it and I think that currently it would be difficult to do this, the only viable way would be to listen for the set background event and maintain a copy of the background colour state. However using the addons channel for events is not generally recommended for end users so use with caution.

import addons from '@storybook/addons';
export const BackgroundDecorator = (StoryFn, context) => {
  const [background, setBackground] = useState(
    context.parameters.backgrounds?.find((b) => b.default).value || ''
  );
  const channel = addons.getChannel();
  useEffect(() => {
    channel.on('storybook-addon-background:update', setBackground);
    return () => {
      channel.removeListener('storybook-addon-background:update', setBackground);
    };
  }, [channel]);
  console.log({ background });
  return (
    <>
      <StoryFn />
    </>
  );
};

@dannyhw
Copy link
Member

dannyhw commented Jul 19, 2022

Ideally we should update the backgrounds addon to copy the updated behaviour on the web.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants