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

Docs: Add code snippet to addons panel #29253

Merged
merged 15 commits into from
Dec 13, 2024
Merged

Conversation

larsrickert
Copy link
Contributor

@larsrickert larsrickert commented Oct 1, 2024

Closes #26691

What I did

Add a new "Code" panel for the docs addon so the generated code snippet can also be viewed/copied in single story mode and is no longer limited to the docs page / view mode.

image

Open questions:

  • Since the code snippet generation is way more advanced for Vue than for other frameworks (when using vue-component-meta docgen), should be provide a preview option to hide/disable the "Code" panel?
  • Should we remove the top/bottom margin of the source code?

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

  1. Run a sandbox for template, e.g. yarn task --task sandbox --start-from auto --template vue3-vite/default-ts
  2. Open Storybook in your browser
  3. Access any single story, e.g. http://localhost:6006/?path=/story/stories-renderers-vue3-vue3-vite-default-ts-sourcecode--default
  4. Click on "Code" panel at the bottom of the screen

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 77.7 MB 77.7 MB 0 B 0.58 0%
initSize 133 MB 133 MB 3.35 kB 0.68 0%
diffSize 55.3 MB 55.3 MB 3.35 kB 0.68 0%
buildSize 6.87 MB 7.24 MB 363 kB 1448.14 🔺5%
buildSbAddonsSize 1.51 MB 1.88 MB 363 kB 1435.12 🔺19.3%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 1.86 MB 1.86 MB 75 B 2.34 0%
buildSbPreviewSize 0 B 0 B 0 B - -
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 3.57 MB 3.93 MB 363 kB 1459.31 🔺9.2%
buildPreviewSize 3.3 MB 3.3 MB 168 B 7.78 0%
testBuildSize 0 B 0 B 0 B - -
testBuildSbAddonsSize 0 B 0 B 0 B - -
testBuildSbCommonSize 0 B 0 B 0 B - -
testBuildSbManagerSize 0 B 0 B 0 B - -
testBuildSbPreviewSize 0 B 0 B 0 B - -
testBuildStaticSize 0 B 0 B 0 B - -
testBuildPrebuildSize 0 B 0 B 0 B - -
testBuildPreviewSize 0 B 0 B 0 B - -
name before after diff z %
createTime 21s 6.6s -14s -439ms -0.97 -218.1%
generateTime 20.1s 21.4s 1.2s -0.26 6%
initTime 14.3s 12.6s -1s -681ms -0.84 -13.3%
buildTime 9.9s 9.7s -131ms 1.16 -1.3%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 4.4s 4.3s -72ms -1.13 -1.6%
devManagerResponsive 3.2s 3.3s 109ms -1.03 3.3%
devManagerHeaderVisible 488ms 514ms 26ms -0.69 5.1%
devManagerIndexVisible 571ms 590ms 19ms -0.61 3.2%
devStoryVisibleUncached 1.1s 1.1s 64ms -1.99 🔺5.3%
devStoryVisible 571ms 543ms -28ms -1.02 -5.2%
devAutodocsVisible 473ms 445ms -28ms -0.79 -6.3%
devMDXVisible 436ms 434ms -2ms -0.82 -0.5%
buildManagerHeaderVisible 483ms 493ms 10ms -0.9 2%
buildManagerIndexVisible 570ms 582ms 12ms -0.91 2.1%
buildStoryVisible 448ms 459ms 11ms -0.8 2.4%
buildAutodocsVisible 368ms 418ms 50ms -0.43 12%
buildMDXVisible 378ms 385ms 7ms -0.72 1.8%

Greptile Summary

This PR adds a new 'Code' panel to the Storybook UI for displaying and copying generated code snippets in single story view mode, focusing on improvements for the docs addon and Vue3 renderer.

  • Implemented new 'Code' panel in code/addons/docs/src/manager.tsx for viewing generated code snippets
  • Enhanced Vue3 source code generation in code/renderers/vue3/src/docs/sourceDecorator.ts, improving handling of complex props and slots
  • Updated code/addons/essentials/package.json and related files to integrate the new 'Code' panel functionality
  • Reordered addons in code/addons/essentials/src/index.ts, potentially affecting addon initialization order
  • Removed view mode check in Vue3 sourceDecorator, allowing source code generation for non-docs view modes

Copy link

nx-cloud bot commented Oct 1, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit f2a8af8. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

6 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

@chakAs3
Copy link
Contributor

chakAs3 commented Oct 5, 2024

Hey @larsrickert, I like how you used the same code snippet on the SourceDecorator. However, this could ideally be an addon, similar to the existing StorySource addon. Though StorySource serves a different purpose—it only displays the actual story code from the file.

@larsrickert
Copy link
Contributor Author

Hey @larsrickert, I like how you used the same code snippet on the SourceDecorator. However, this could ideally be an addon, similar to the existing StorySource addon. Though StorySource serves a different purpose—it only displays the actual story code from the file.

@chakAs3 Thanks for the feedback! Imo the code generation / snippets are responsibility of the docs addon which also already shows the snippets on the "Docs" page. So this PR/feature "just" also shows the snippets on the single story pages.
So its more like adding a missing feature / extending an existing feature of the docs addon instead of creating a new addon/feature.

@shilman shilman self-assigned this Oct 7, 2024
@larsrickert larsrickert requested a review from chakAs3 October 11, 2024 06:42
@larsrickert
Copy link
Contributor Author

@shilman Hey :) Would it be possible for you to take a look at this PR?

@shilman
Copy link
Member

shilman commented Oct 30, 2024

@larsrickert Sorry I missed this one. Will look later today!

Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @larsrickert ! Thanks for your patience on the review.

This looks great: a low-cost improvement to Storybook, eliminating the need for the Storysource addon, which is broken/outdated.

My original thought was to create a standalone addon for this, @storybook/addon-source, which would co-exist with addon-docs the same way that @storybook/addon-controls current does. What do you think about that direction?

I realize this is a much simpler change, and I appreciate that.

I want to discuss this with the core team on Monday and get back to you. But I like this direction and appreciate your PR!!!

@shilman
Copy link
Member

shilman commented Nov 4, 2024

@larsrickert I haven't had a formal discussion with the team but initial sentiment from talking to a few people is that we should move controls & source handling into the core, and keep docs as its own addon. I will keep you posted on whether we're serious about that change & how it might get done.

In the meantime, I'm inclined to merge this PR since it's useful as is. I will try to have some resolution this week. Thanks for your patience.

@larsrickert larsrickert force-pushed the larsrickert/1898-story-code-panel branch from 14f6a7a to 7b4d91f Compare November 4, 2024 07:35
@larsrickert
Copy link
Contributor Author

@shilman Thanks for your insights and thoughts! In general, I appreciate the separation of concerns by moving features to dedicated addons. In the case of the docs addon / source generation I am open to both options. Since the source code generation is already part of the docs addon (and its already displayed on the "Docs" page for each story), the new "Code" panel would also fit perfectly in the current docs addon in my opinion.

I like your idea about merging this feature already in the docs addon and then discuss if it should be split up into a source addon in the future. But since this would be a bigger refactoring and probably also other parts of the docs addon would be moved, I'd still merge this PR already as feature for Storybook 8.x. 👍

@shilman
Copy link
Member

shilman commented Nov 11, 2024

Hi @larsrickert, can you please add the following flag:

parameters: {
  docs: {
    source: {
      addonPanel: false,
    },
  },
}

This way users can opt out of the new behavior if they want. Once that's ready and CI is green, I'll update MIGRATION.md to describe the new behavior and will merge the PR.

@larsrickert larsrickert force-pushed the larsrickert/1898-story-code-panel branch from 7b4d91f to ddef097 Compare November 11, 2024 13:59
@storybook-pr-benchmarking
Copy link

storybook-pr-benchmarking bot commented Nov 11, 2024

Package Benchmarks

Commit: f2a8af8, ran on 13 December 2024 at 12:53:42 UTC

No significant changes detected, all good. 👏

@larsrickert
Copy link
Contributor Author

Hi @larsrickert, can you please add the following flag:

parameters: {
  docs: {
    source: {
      addonPanel: false,
    },
  },
}

This way users can opt out of the new behavior if they want. Once that's ready and CI is green, I'll update MIGRATION.md to describe the new behavior and will merge the PR.

Hey @shilman Sure, no problem :) Is there some example/docs how to implement this or someone that could guide me here? I already tried to achieve it but in addons.add() I can only hide the panel content by returning null, but the "Code" panel itself is still displayed. I guess we need to check the parameter before calling addons.add?

@shilman
Copy link
Member

shilman commented Nov 13, 2024

Oof. I'm actually not too familiar with that aspect of things. @ndelangen can you please help?

Copy link
Contributor

@kasperpeulen kasperpeulen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, and I agree with @shilman request to be able to turn it off.

@larsrickert larsrickert force-pushed the larsrickert/1898-story-code-panel branch from ddef097 to 413f2d4 Compare November 25, 2024 08:48
@larsrickert
Copy link
Contributor Author

@shilman @kasperpeulen Thanks for the feedback! I added the parameter as requested :)
Does someone of you have time to review if this is the correct way to do this?

@larsrickert
Copy link
Contributor Author

@larsrickert Tested and it's working well. I'll take another pass at things tomorrow and try to get it merged. Great job!!

@shilman Great, thanks for testing! Do you already have a rough release plan for this?

@larsrickert larsrickert force-pushed the larsrickert/1898-story-code-panel branch from fb132e5 to 9f35083 Compare December 3, 2024 10:11
@shilman
Copy link
Member

shilman commented Dec 3, 2024

@larsrickert not so comfortable with the current parameter implementation. I have a cleaner solution with a worse user API. Need to figure that out, sorry for the delays. I'll update again later today

@shilman
Copy link
Member

shilman commented Dec 3, 2024

@larsrickert i discussed with @yannbf and @ndelangen and the current parameter approach won't cut it. i'm going to reimplement it now with a worse API and then get it merged. thanks again for the contribution and for your patience on this.

@shilman
Copy link
Member

shilman commented Dec 3, 2024

@larsrickert I've opened a PR on top of this one for your review. Please merge if you're OK with it, or we can discuss the changes there.

#29791

I've also found some issues with the PR in its current form.

  • The default snippet is messed up for React out of the box (see sandbox)
  • The panel should show up at the end so as not to disrupt existing setups

I've also reached out in Discord in case it's more efficient to discuss it there.

code/addons/docs/src/manager.tsx Outdated Show resolved Hide resolved
@larsrickert larsrickert force-pushed the larsrickert/1898-story-code-panel branch from 78ee7df to db146c3 Compare December 6, 2024 06:52
@shilman shilman changed the title Docs: Add code snippet panel to single story view mode Docs: Add code snippet to addons panel Dec 9, 2024
Copy link
Contributor

@JReinhold JReinhold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great addition!

Don't we want to document this too?

MIGRATION.md Outdated
```js
export default {
parameters: {
docsSourcePanel: { disable: true },
Copy link
Contributor

@JReinhold JReinhold Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I think this is the right API.

We already have parameters.docs.source.X: https://storybook.js.org/docs/api/doc-blocks/doc-block-source

Wouldn't it make sense to keep in line with that, and have it as parameters.docs.source.panel = bool instead?

Potentially, if we want to leave room for other panel-specific configs later, it could be parameters.docs.source.panel.enable = bool

cc @shilman

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that this has already been discussed, before.

Personally, I highly disagree with the direction.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just think that we can change internal hacks whenever, but we can only change the API in majors, so I'd rather merge this with hacks than an API we dislike.

It looks like we can add something here if we want: https://github.com/storybookjs/storybook/blob/next/code/core/src/manager/container/Panel.tsx/#L32

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JReinhold — But as you (implicitly) point out, parameters.docs.source.X are props of the Source doc block. That wouldn't be the case here.

Copy link
Contributor

@kylegach kylegach Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe parameters.docs.sourcePanel: boolean would be a nice middle ground?

Although if the panel title is Code, maybe it should be codePanel?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JReinhold @kylegach Thanks for your input!
I updated the implementation to support a new parameters.docs.codePanel parameter so the code panel can be disabled by setting:

export default {
  parameters: {
    docs: {
      codePanel: false,
    },
  },
};

Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Will add some documentation in a followup PR 😬

@shilman shilman merged commit b98a73b into next Dec 13, 2024
58 checks passed
@shilman shilman deleted the larsrickert/1898-story-code-panel branch December 13, 2024 13:29
@github-actions github-actions bot mentioned this pull request Dec 13, 2024
17 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants