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

[Storybook] Add story code snippets #7716

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

mgadewoll
Copy link
Contributor

@mgadewoll mgadewoll commented Apr 26, 2024

Summary

🚧 This PR is still being updated and annotated 🚧

πŸ“’ This PR adds code snippets to our stories. πŸ¦„ πŸ‘©β€πŸ’»

The goal of this is to improve devX by providing dynamically updated code snippets for the stories. The code snippet is generated on story load as well as when any args change (e.g. when controls in the controls table are being updated)

This new functionality for code snippets is provided in an additional story panel next to the available panels for "Controls", "Actions" and "Interactions".

Screenshot 2024-04-26 at 19 29 10

The basis for the code snippet generation is heavily inspired (copied and enhanced) on Storybooks Source block. The internal jsxDecorator file was copied and then adjusted to our specific needs. The main functionality to generate a string from react elements comes from the react-element-to-jsx-string package that is used.

The overall setup

We run our custom jsxDecorator on every story as a decorator via preview.tsx. This decorator generates the code snippet as string and sends it via Storybooks Channel events to our newly added custom addon panel which outputs the code string using Storybooks SyntaxHighlighter copmponent.

Screenshot 2024-04-26 at 18 59 57

The main updates to the jsxDecorator on our side are to ensure the code outputs clean and relevant code snippets.
What was considered:

  • rename Emotion wrappers to the actual component name (whenever we use css on a component in a story it will be an Emotion component)
  • rename stateful wrappers that start with the wording Stateful (requires us to follow an agreed convention)
  • remove obsolete fragment wrappers (but keeps needed ones)
  • remove story specific wrappers (e.g. layout or styling)
  • keep related wrappers (parent & subcomponent or related by name)
  • resolve any other unexpected wrapper we might add to structure complex stories
  • rename internal component names (starting with _ underscore, <_Component> is changed to <Component>)
  • resolve css object to style attribute
  • ensure boolean props are output in a meaningful way (generally as shorthand but keep specifically defined false values where false has meaning)
  • ensure a nice formatting

The updates happen in different stages in jsxDecorator:

  1. pre-conversion: determine what react element should be passed to react-element-to-jsx-string and with which options
  2. conversion: pass react elements to react-element-to-jsx-string
  3. post-conversion: do additional replacements on the returned string
  4. formatting: format the result using prettier

Options

Currently there are two addon specific parameter options added with this PR that can be used under the key codeSnippet in the parameters config key.

// meta or story config
const meta = {
  title: 'Navigation/EuiButton',
  component: EuiButton,
  parameters: {
    codeSnippet: {
       // will skip code snippet generation for the component or story
      disable: true,
      // useful for complex story composition wrappers (using the story component as nested child)
      // it will resolve the outer wrapper and return the code snippet for it's children
      // see the story for `EuiHeader/Multiple Fixed Headers`. `EuiHeaderAlert/Flyout Example` or `EuiHeaderAlert/Popover Example` as examples
      resolveChildren: true,
    }
  }
}

ℹ️ Stories will also be skipped whenever an anonymous function is returned as story function, e.g. from render().

Screenshots

skipped code snippet generation
Screenshot 2024-04-26 at 20 03 24

Screenshot 2024-04-26 at 19 59 01

Screenshot 2024-04-26 at 20 04 18

Disclaimer

⚠️ This PR currently does not add tests for the jsxDecorator functionality yet.

QA

  • review all stories and their generated code snippets
    • Are there unexpected outputs?
    • Are there opportunities to improve what we output?
    • Does everything run as expected?

@mgadewoll mgadewoll added documentation Issues or PRs that only affect documentation - will not need changelog entries skip-changelog labels Apr 26, 2024
+ adds addon panel for outputting code snippet with Storybooks SyntaxHighlighter

+ registers code-snippet addon panel in Storybook manager
- ensure naming convention for stateful wrapper to start with Stateful... for filters to work

- ensure args are passed along when using decorators

- ensure no anonymous render functions are used as they would otherwise be skipped
- ensures resolving wrapper by using specific parameter

- updates FLyoutExample to ensure conditional code is initially available for the code generation by using a storybook only prop
ensures the prop is added as expected and that tokens are not resolved
@mgadewoll mgadewoll force-pushed the spacetime/storybook-code-snippets branch from 6c1c416 to 01bfb67 Compare May 10, 2024 15:11
@kibanamachine
Copy link

Preview staging links for this PR:

@elasticmachine
Copy link
Collaborator

πŸ’š Build Succeeded

History

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Issues or PRs that only affect documentation - will not need changelog entries skip-changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants