#SB7: useGlobals() missing

1 messages · Page 1 of 1 (latest)

stuck heron
#

Hi, I wanted to know if there is a way of changing the globals from Stories?

Example.
The defaultValue for the global theme is set to Dark , but one story needs the light theme to be the default one for it.

I used to use import { useGlobals } from '@storybook/client-api'; but there isn't one there anymore.

Can you help me with it?

young narwhal
stuck heron
#

When I try to import from manager-api this is the error I get.

young narwhal
#

Can you share what your code looks like please?

stuck heron
#

Yes, this is in preview.tsx

    const { title } = context;
    const [globals, updateGlobals] = useGlobals();

    useEffect(() => {
        if (!title.includes('Components')) {
            updateGlobals({ theme: 'light' });
        } else {
            updateGlobals({ theme: 'both' });
        }
    }, [title]);

    if (['light', 'dark'].includes(globals.theme)) {
        return (
            <Theme theme={globals.theme}>
                <Story {...context} />
            </Theme>
        );
    }

    return (
        <BothThemes>
            <Story {...context} />
        </BothThemes>
    );
};
#

If the title includes Components then have the theme set as both, if not set it as light. That is the logic behind the updating globals I am trying to have

young narwhal
#

Hmm... can you try this:

import { useGlobals } from '@storybook/preview-api';

The docs say it's in manager-api, but your code pertains to the Preview (where stories are rendered), not the Manager (the Storybook UI). So I'm curious if it's available in preview-api.

stuck heron
#

Yes, that worked! But there is some rendering issue with the Docs now, Playground works, but autodocs are broken. I might disable them just for now, but you might know the root of the issue?

young narwhal
#

Hmm... no, that's a new one for me. Does it happen for every story?

stuck heron
#

Yes, for every story. I tried restarting it, and it still happens.

young narwhal
#

Hmm... I'm really not sure, sorry.

stuck heron
#

No worries, any tips on how could I change the theme inside Stories or Preview?

young narwhal
#

I think that's the only API available to do it. The flickering you're seeing is a bug. If you can create a simplified reproduction, submitting an issue would be appreciated.