Is it possible and valid to modify context parameters in decorator? #24462
Replies: 2 comments 2 replies
-
Hey mate, have you figured out something about this approach? |
Beta Was this translation helpful? Give feedback.
-
I don't know the actual answer to the question, but I think that assumption is mostly correct. Decorators are called when a story is preparing to render or a change has happened that could have caused args to be different and a new render or updating of args needs to happen. By the time a decorator is called, the parameters have already been parsed. The docs define parameters as "Parameters are a set of static, named metadata about a story, typically used to control the behavior of Storybook features and addons.", so I think the intention is that they are not able to be dynamically changed, but depending on the addon you may can come up with a workaround. I think the Viewports addon tracks the current viewport selection in globals, so you may be able to use the useGlobals hook to update the current selection. |
Beta Was this translation helpful? Give feedback.
-
I have some stories that are only shown for mobile browsers. My setup requires both providing a context and setting
parameters.viewport.defaultViewport
to one of mobile viewports so it opens in mobile view.Since decorators are meant to provide some sort of context wrapping, following decorator would work:
I provide custom tag to story that should be rendered as mobile. And it works as expected, mobile context wraps the story.
Next I am trying to add some context modification to decorator:
And it also "works", but not as I expect. If I write minimal story:
console.log
will printMyMobileViewport
, but default viewport will not be selected. Moreover, opening another story after this one will actually applyMyMobileViewport
to default viewport this time.My previous workaround with Storybook 6.5 was HOC-based per story, but now it is harder to type with Storybook 7 StoryObj and having a lot of StoryFn stories that I do not want to migrate to StoryObj right now.
So the question is: is it possible and valid to modify context in decorator? Am I doing it wrong? Or is it only for wrapping render part of a story, but not for modifying context?
Beta Was this translation helpful? Give feedback.
All reactions