storybookjs / addon-kit

Everything you need to build a Storybook addon
https://storybook.js.org/docs/react/addons/writing-addons
MIT License
142 stars 35 forks source link

[Bug] - useArgs error in custom addon #56

Closed lukastanisic99 closed 1 year ago

lukastanisic99 commented 1 year ago

Describe the bug

I've cloned the repo. I've installed dependencies with yarn. Yarn start doesn't work and it throws an error 'unknown quite flag', but removing the flag works. I've tried a simple modification to the PanelContent.tsx by import { useArgs } from "@storybook/manager-api"; and later const [args, updateArgs,resetArgs] = useArgs(); which throws an error in the browser - "Uncaught TypeError: Cannot read properties of undefined (reading 'type') at useArgs (chunk-QQ57T6RI.mjs:53:34107)"

I've taken a look at the controls addon - specifically (https://github.com/storybookjs/storybook/blob/next/code/addons/controls/src/ControlsPanel.tsx) and I use the useArgs method in the same way. I'm attaching a screenshot of the error message and code for PanelContent.tsx.

Modified PanelContent.tsx from this repo: ` import React, { Fragment,useEffect, useState } from "react"; import { styled, themes, convert } from "@storybook/theming"; import { TabsState, Placeholder, Button } from "@storybook/components"; import { useArgs } from "@storybook/manager-api"; import { List } from "./List";

export const RequestDataButton = styled(Button)({ marginTop: "1rem", });

type Results = { danger: any[]; warning: any[]; };

interface PanelContentProps { results: Results; fetchData: () => void; clearData: () => void; }

/**

<div id="danger" title={${results.danger.length} Danger} color={convert(themes.normal).color.negative}

<div id="warning" title={${results.warning.length} Warning} color={convert(themes.normal).color.warning}

) };

`

image

Steps to reproduce the behavior

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior

A clear and concise description of what you expected to happen.

Screenshots and/or logs

If applicable, add screenshots and/or logs to help explain your problem.

Environment

Additional context

Add any other context about the problem here.

Sidnioulz commented 1 year ago

Haven't read through all of this, and also not a SB maintainer, so I may be wrong, but it seems to me you want to call useArgs in a manager context. IIRC, useArgs is exposed by the preview API, not the manager API, so it should not be available with the import you're using and it also should not work outside a story context.

lukastanisic99 commented 1 year ago

Thanks for the reply! How is this different than the control addon? If you have the time take a look at this - https://github.com/storybookjs/storybook/blob/next/code/addons/controls/src/ControlsPanel.tsx

winkerVSbecks commented 1 year ago

The import is fine. You're importing from the manager-api where the panel code runs.

The issue is, and I learnt it today, that getCurrentStoryData might not be ready in time. In the control panel example you shared, there's a check for this. https://github.com/storybookjs/storybook/blob/next/code/addons/controls/src/manager.tsx#L29

winkerVSbecks commented 1 year ago

You'll also need to:

  • rename manager.ts to manager.tsx
  • add import React from "react"; to manager.tsx
  • Update entry: ["src/index.ts", "src/preview.ts", "src/manager.tsx"], in tsup.config.ts