addon-backgrounds: global context not initially updated with color
See original GitHub issueDescribe the bug
When using @storybook/addons-backgrounds
in a react project with a custom Decorator
, the initial/default background value is not passed to the context.globals
.
I have a project performing text contrast logic where I need the initial value to pass to javascript.
To Reproduce
- Pull down and run this repro here.
- Go to the primary button story.
- Notice the
console.log
of thecontext.globals
shows and empty object. - Click on a different background color, notice the
context.globals
now includes the selected value.
System
Environment Info:
System:
OS: macOS 11.4
CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Binaries:
Node: 14.17.3 - ~/.nvm/versions/node/v14.17.3/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 6.14.13 - ~/.nvm/versions/node/v14.17.3/bin/npm
Browsers:
Chrome: 91.0.4472.114
Firefox: 90.0.1
Safari: 14.1.1
npmPackages:
@storybook/addon-backgrounds: ^6.3.4 => 6.3.4
@storybook/react: ^6.3.4 => 6.3.4
Additional context Could be related to #14846
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
@storybook/addon-backgrounds | Yarn - Package Manager
Storybook Addon Backgrounds can be used to change background colors inside the preview in Storybook. ... Ember: Remove global Ember usage #17843 ...
Read more >Storybook Addon Background Toggle
Globals are currently not correctly initialized by storybook, meaning they always return {} as the initial value. To correct this, we update globals...
Read more >React context not updating - Stack Overflow
Think about React context just like you would a component, if you want to update a value and ... [setPageContext]) return <p>Hello, World!...
Read more >@storybook/addon-backgrounds: Versions | Openbase
Bug Fixes. Core: Catch and do nothing to avoid triggering unhandled exception problems #20177; Controls: Fix color control not resetting when initial value ......
Read more >The Perfect Color is in the Context, not the Theory - Bootcamp
Don't let the perceived technicality or complexity of all these color theories slow you down or keep you from moving forward. For new...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
this looks like a bug with
addon-backgrounds
or with core, depending on how you see it cc @yannbf @tmeasday @ghengeveldalso cc @stevensacks since you’re using the same pattern in your i18n work
AFAICT what’s going on here is that the globals code in core assumes that the globals are declared statically, either by the end user or by addons. what that would look like:
If you don’t declare a
globalType
, it will infer the type from the data inglobals
. So the final results union these two.addon-backgrounds
uses a different pattern where the user declares global values as parameters, and then the addon callsupdateGlobals
on story render. I’m not a fan of this pattern, but it’s what we have for now.Given this pattern, we could fix the problem:
I think the first fix is closer to the design intent of globals. WDYT?
@bytrangle the issue is that it does not capture the initial default value in
preview.js
here, and pass it to the decorators.The whole
globalTypes
vsglobals
is really confusing when to use one or the other or both. As@shilman
saidIs this not true? If it is I’d think the demo would work as is.
All in all how do you make the demo work so that it passes the initial default value to the decorators? In my custom background addon I have to call
setGlobals
for it to capture the initial value.