Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto generate Controls from Typescript types #456

Open
matthewwolfe opened this issue Jul 11, 2023 · 4 comments
Open

Auto generate Controls from Typescript types #456

matthewwolfe opened this issue Jul 11, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@matthewwolfe
Copy link
Contributor

matthewwolfe commented Jul 11, 2023

Is your feature request related to a problem? Please describe.
When using Typescript and args/argTypes it is very repetitive to specify the props as a Typescript type and then repeat the same structure again in the story file as args and argTypes.

Describe the solution you'd like
The ability to auto generate Controls, ie:

interface Props {
  variant: 'primary' | 'secondary';
}

Props are translated into a select/radio with the options

Describe alternatives you've considered
react-docgen

Additional context
N/A

@matthewwolfe matthewwolfe added the needs triage needs to be reviewed label Jul 11, 2023
@tajo tajo added enhancement New feature or request and removed needs triage needs to be reviewed labels Jul 12, 2023
@matthewwolfe
Copy link
Contributor Author

matthewwolfe commented Jul 17, 2023

I did a little bit of discovery on the viability of this, and it looks like react-docgen can reliably generate values depending on how the props are typed in the stories file.

❌ Does not generate type information

const Card: Story<Props> = ({ label }) => <p>Label: {label}</p>;

✅ Generates type information

const Card: Story<Props> = ({ label }: Props) => <p>Label: {label}</p>;

Considerations

  • react-docgen is not a heavy dependency, but it is also not super lightweight, optional flag to enable?
  • Must work with hot reload
  • Types that do not map cleanly to a control

@tajo do you have any opinion on if this is something that you'd like implemented? If so, could you provide a bit of direction on how you'd like this structured? Any info would be appreciated if you are interested in this, and I'd be happy to take a stab at an implementation.

@tajo
Copy link
Owner

tajo commented Jul 18, 2023

It would nice to have this as an opt-in feature through config.mjs. I guess you don't want to automatically add control everywhere just because you use TypeScript, so users need to consent to it. This way we can make react-docgen dependency optional. It can be still in dependencies but should be used dynamically (or just make sure it doesn't get bundled by default) and added into optimizeDeps.

Also, I assume, when enabled and args/argTypes are defined at the same time, args/argTypes should take the precedence.

@d9k
Copy link

d9k commented Oct 16, 2023

Found https://github.com/lukifer/ladlescoop enthusiast project for that:

Automatically generate Ladle stories from existing React components, extracting Props definitions and default values, and automatically creating controls from types.

@d9k
Copy link

d9k commented Oct 18, 2023

Ladlescoop doesn't support generation controls from inherited props yet

Please consider controls autogeneration feature as top 🔝 priority.

⚡ Instant startup time is cool but syncing ladle controls manually on each component change is a boring 🥵 tedious task which outweights ⚖️ all developer's time saved by ladle optimisations. It breaks single-source-of-truth principle.

Because of this can't switch from Storybook to Ladle yet and can't advice switching to friends/colleagues 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants