-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
+127
−12
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a91b0ba
feat: add code snippet panel to single story view mode
larsrickert 107c40b
remove console log
larsrickert a283cea
use dark theme
larsrickert 1fa0e7d
feat: support `docs.source.addonPanel` parameter
larsrickert 01e11e5
Addon-docs: Fix and document source panel disabling
shilman 2be1769
fix typos
larsrickert 33ce8f9
move docs source panel order
larsrickert db146c3
fix react code snippet format
larsrickert ad65bda
Merge branch 'next' into larsrickert/1898-story-code-panel
shilman 75f235a
Fix merge conflict remnants
shilman 8408f76
Merge branch 'next' into larsrickert/1898-story-code-panel
yannbf ef170fd
Merge branch 'next' into larsrickert/1898-story-code-panel
larsrickert d749986
refactor: update parameter to disable code panel
larsrickert 46bf7b6
Merge branch 'next' into larsrickert/1898-story-code-panel
shilman f2a8af8
Fix typechecks
shilman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react'; | ||
|
||
import { AddonPanel, type SyntaxHighlighterFormatTypes } from 'storybook/internal/components'; | ||
import { ADDON_ID, PANEL_ID, SNIPPET_RENDERED } from 'storybook/internal/docs-tools'; | ||
import { addons, types, useAddonState, useChannel } from 'storybook/internal/manager-api'; | ||
|
||
import { Source } from '@storybook/blocks'; | ||
|
||
addons.register(ADDON_ID, (api) => { | ||
addons.add(PANEL_ID, { | ||
title: 'Code', | ||
type: types.PANEL, | ||
// disable this with `docsSourcePanel: { disable: true }` | ||
paramKey: 'docsSourcePanel', | ||
larsrickert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
match: ({ viewMode }) => viewMode === 'story', | ||
render: ({ active }) => { | ||
const [codeSnippet, setSourceCode] = useAddonState<{ | ||
source: string; | ||
format: SyntaxHighlighterFormatTypes; | ||
}>(ADDON_ID, { | ||
source: '', | ||
format: 'html', | ||
}); | ||
|
||
useChannel({ | ||
[SNIPPET_RENDERED]: ({ source, format }) => { | ||
setSourceCode({ source, format }); | ||
}, | ||
}); | ||
|
||
return ( | ||
<AddonPanel active={!!active}> | ||
<Source code={codeSnippet.source} format={codeSnippet.format} dark /> | ||
</AddonPanel> | ||
); | ||
}, | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
code/addons/docs/template/stories/sourcePanel/index.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export default { | ||
component: globalThis.Components.Button, | ||
tags: ['autodocs'], | ||
parameters: { | ||
chromatic: { disable: true }, | ||
docsSourcePanel: { disable: true }, | ||
}, | ||
}; | ||
|
||
export const One = { args: { label: 'One' } }; | ||
export const Two = { args: { label: 'Two' } }; | ||
export const WithSource = { | ||
args: { label: 'Three' }, | ||
parameters: { docsSourcePanel: { disable: false } }, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// @ts-expect-error (no types needed for this) | ||
export * from '@storybook/addon-docs/manager'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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-sourceWouldn'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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 becodePanel
?There was a problem hiding this comment.
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: