Replies: 3 comments 13 replies
-
How would this work with the idea of a "default"? For example, the viewports addon has If I'm reading right, |
Beta Was this translation helpful? Give feedback.
-
I have a PR that implements the As part of that PR, @shilman and I were discussing removing the existing parameter-driven behaviour for the same effect from the viewport and backgrounds addon. There are couple of things to consider here: Viewports
When this happens it doesn't change the global, meaning when you change stories it'll "revert" to the old global value (like Backgrounds You can set a list of allowed backgrounds via a parameter. If the current value isn't in that list, it'll use the So:
There's two ways we can go here:
|
Beta Was this translation helpful? Give feedback.
-
Here is an alternative proposal that combines @tmeasday 's proposed UI behavior with an Let's consider the following // preview.js
export default {
globals: { theme: 'light' },
} There are a few cases to consider: UI mode
Test mode
Current behaviorSemantics
UI mode
Test mode
Proposed behaviorSemantics
UI mode
Test mode
ArgumentThe semantics has changed so technically this is a breaking change. However:
To mitigate the impact, we could also add a new annotation |
Beta Was this translation helpful? Give feedback.
-
Status: in-review; championed by @shilman
Summary
We propose a new field for stories,
globals
, that allow you to set a context value such as a theme or locale or viewport for that story.Problem Statement
Storybook globals are the recommended way to store global state such as theme, locale, or viewport. Globals are typically modified by toolbar menus in the UI and they persist across stories.
The problem is that it's not easy to make a story that renders with a specific configuration of globals. For example, if you want a story that always renders in the dark theme to show off a special "dark mode" look or behavior.
You can do this by hand by navigating to the story and selecting "dark" theme via the toolbar menu. However, there is no direct way to do this programmatically.
Non-goals
There are addons, such as Backgrounds and React-intl that use a complex workaround and should be updated to use the new feature.
There are addons, such as Viewport, that don't use globals at all, but should.
All of these addons should be updated, but that's out of scope for this project.
Implementation
To set the globals automatically for a specific story, we propose a new field called
globals
:This is equivalent to manually selecting the dark theme in the toolbar UI BEFORE navigating to the
DarkPrimary
story.It does not change any other aspect of globals behavior. So, for example, if you are in
light
theme and navigate to theDarkPrimary
story, it will set the theme todark
. The theme will staydark
until you set the global manually in the UI, or until you set it programmatically to another value in another story.Prior Art
Some addons use parameters to achieve this behavior. But this is a convoluted workaround for not having a direct way to set globals in stories.
Examples:
Deliverables
globals
field to@storybook/csf
typesglobals
fieldRisks
No response
Unresolved Questions
globals
inpreview.js
which stores the values for globals in a Storybook ONCE on startup. This behavior is inconsistent with the new proposed Story globals field, which stores the values before navigating to a story which specifiesglobals
. How should we resolve this, if at all?globals
behaved in a "storybook-like" way, we would also be able to setglobals
at the component-level and these values would cascade at the story/component/project likeparameters
do. Do we want this?Alternatives considered / Abandoned Ideas
No response
Beta Was this translation helpful? Give feedback.
All reactions