How to use the @storybook/api.useAddonState function in @storybook/api

To help you get started, we’ve selected a few @storybook/api examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github storybookjs / storybook / addons / coverage / src / CoveragePanel.tsx View on Github external
export const CoveragePanel: React.FC<{}> = () => {
  const [coverageSummary, setCoverageSummary] = useAddonState(ADDON_ID, null);
  const [coverageDetail, setCoverageDetail] = useAddonState(ADDON_ID, null);
  useChannel({
    [EVENTS.COVERAGE_DETAIL]: (detail: CoverageDetail) => setCoverageDetail(detail),
  });
  // console.log('panel', { coverage });
  // return coverageSummary ?  : <div>No coverage</div>;
  return coverageDetail ?  : <div>No coverage</div>;
};
github storybookjs / storybook / dev-kits / addon-roundtrip / src / panel.tsx View on Github external
export const Panel = () =&gt; {
  const [results, setState] = useAddonState(ADDON_ID, []);
  const emit = useChannel({
    [EVENTS.RESULT]: (newResults: Results) =&gt; setState(newResults),
  });

  return (
    
      <content>
       emit(EVENTS.REQUEST) },
          { title: 'setState', onClick: () =&gt; setState(['foo']) },
          {
            title: 'setState with options',
            onClick: () =&gt; setState(['bar'], { persistence: 'session' }),
          },</content>
github bcgov / range-web / .storybook / roles-addon / Tool.js View on Github external
const ViewportTool = () =&gt; {
  const [role, setRole] = useAddonState(ADDON_ID, 'myra_range_officer')

  const emit = useChannel({
    [STORY_RENDERED]: () =&gt; {
      setTimeout(() =&gt; {
        emit('role/change', role)
      }, 100)
    }
  })

  const handleChange = e =&gt; {
    setRole(e.target.value)
    emit('role/change', e.target.value)
  }

  return (
github storybookjs / storybook / addons / viewport / src / Tool.tsx View on Github external
withTheme(({ theme }: { theme: Theme }) =&gt; {
    const {
      viewports = MINIMAL_VIEWPORTS,
      defaultViewport = responsiveViewport.id,
      disable,
    } = useParameter(PARAM_KEY, {});
    const [state, setState] = useAddonState(ADDON_ID, {
      selected: defaultViewport,
      isRotated: false,
    });
    const list = toList(viewports);

    useEffect(() =&gt; {
      setState({
        selected:
          defaultViewport || (viewports[state.selected] ? state.selected : responsiveViewport.id),
        isRotated: state.isRotated,
      });
    }, [defaultViewport]);

    const { selected, isRotated } = state;
    const item =
      list.find(i =&gt; i.id === selected) ||
github storybookjs / storybook / addons / viewport / src / Tool.tsx View on Github external
withTheme(({ theme }: { theme: Theme }) =&gt; {
    const { viewports, defaultViewport, disable } = useParameter(
      PARAM_KEY,
      {
        viewports: {},
        defaultViewport: responsiveViewport.id,
      }
    );
    const [state, setState] = useAddonState(ADDON_ID, {
      selected: defaultViewport || responsiveViewport.id,
      isRotated: false,
    });
    const list = toList(viewports);

    const { selected, isRotated } = state;
    const item =
      list.find(i =&gt; i.id === selected) ||
      list.find(i =&gt; i.id === defaultViewport) ||
      list.find(i =&gt; i.default) ||
      responsiveViewport;

    const ref = useRef();

    const styles = getStyles(ref.current, item.styles, isRotated);
github storybookjs / storybook / addons / design-assets / src / panel.tsx View on Github external
export const Panel = () =&gt; {
  const results = useParameter(PARAM_KEY, []);
  const [selected, setSelected] = useAddonState(ADDON_ID, 0);
  const { storyId } = useStorybookState();

  return useMemo(() =&gt; {
    if (results.length === 0) {
      return null;
    }

    if (results.length &amp;&amp; !results[selected]) {
      setSelected(0);
      return null;
    }
    const url = getUrl(results[selected]).replace('{id}', storyId);
    return (
      
        
        {results.length &gt; 1 ? (
github storybookjs / storybook / addons / live-edit / src / useEditor.ts View on Github external
export const useEditor = () =&gt; {
  const [addonState, setAddonState] = useAddonState(ADDON_NAME, {});

  const setAddonStateFunc = (newSource: string, storyId: string) =&gt; {
    setAddonState({
      ...addonState,
      [storyId]: newSource.trimLeft(),
    });
  };

  return [addonState, setAddonStateFunc] as [
    Record,
    (newSource: string, storyId: string) =&gt; void
  ];
};

@storybook/api

Storybook Manager API (facade)

MIT
Latest version published 9 months ago

Package Health Score

91 / 100
Full package analysis