storybook-addon-root-attribute icon indicating copy to clipboard operation
storybook-addon-root-attribute copied to clipboard

Access current value within the template

Open colorfulfool opened this issue 3 years ago • 4 comments

Hi,

Is there a way to get the currently selected value of root attribute from within a template?

Something like:

...
  rootAttribute: {
    defaultState: {
      name: "Dark",
      value: "dark"
    },
    states: [
      {
        name: "Light",
        value: null
      }
    ]
  },
...

const Template: Story = (state, context) => (
  context.rootAttribute // returns "dark"
);

colorfulfool avatar May 15 '22 13:05 colorfulfool

@colorfulfool how about read it from DOM?

le0pard avatar May 15 '22 13:05 le0pard

Should be possible with a mutation observer, I guess. But it the app itself the theme (dark/light) is placed in Redux, from where it's synced to DOM. Appearance is mostly influenced by CSS that the DOM attribute triggers, but some components access the theme directly. So it would make sense to be able to inject the current value into Redux?

colorfulfool avatar May 16 '22 01:05 colorfulfool

So it would make sense to be able to inject the current value into Redux?

Yes, it make sense. Also some devs using context to pass "theme" to components

le0pard avatar May 16 '22 21:05 le0pard

So, is it possible?

To inject the value into story's Redux provider, I need to access the value from the story somehow.

colorfulfool avatar May 21 '22 19:05 colorfulfool