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
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<h1>Migration</h1>

- [From version 8.4.x to 8.5.x](#from-version-84x-to-85x)
- [Added source code panel to docs](#added-source-code-panel-to-docs)
- [From version 8.2.x to 8.3.x](#from-version-82x-to-83x)
- [Removed `experimental_SIDEBAR_BOTTOM` and deprecated `experimental_SIDEBAR_TOP` addon types](#removed-experimental_sidebar_bottom-and-deprecated-experimental_sidebar_top-addon-types)
- [New parameters format for addon backgrounds](#new-parameters-format-for-addon-backgrounds)
Expand Down Expand Up @@ -167,7 +169,7 @@
- [Angular: Drop support for calling Storybook directly](#angular-drop-support-for-calling-storybook-directly)
- [Angular: Application providers and ModuleWithProviders](#angular-application-providers-and-modulewithproviders)
- [Angular: Removed legacy renderer](#angular-removed-legacy-renderer)
- [Angular: initializer functions](#angular-initializer-functions)
- [Angular: Initializer functions](#angular-initializer-functions)
- [Next.js: use the `@storybook/nextjs` framework](#nextjs-use-the-storybooknextjs-framework)
- [SvelteKit: needs the `@storybook/sveltekit` framework](#sveltekit-needs-the-storybooksveltekit-framework)
- [Vue3: replaced app export with setup](#vue3-replaced-app-export-with-setup)
Expand Down Expand Up @@ -419,6 +421,22 @@
- [Packages renaming](#packages-renaming)
- [Deprecated embedded addons](#deprecated-embedded-addons)

## From version 8.4.x to 8.5.x

### Added source code panel to docs

Starting in 8.5, Storybook Docs (`@storybook/addon-docs`) automatically adds a new addon panel to stories that displays a source snippet beneath each story. This works similarly to the existing [source snippet doc block](https://storybook.js.org/docs/writing-docs/doc-blocks#source), but in the story view. It is intended to replace the [Storysource addon](https://storybook.js.org/addons/@storybook/addon-storysource).

If you wish to disable this panel globally, add the following line to your `.storybook/preview.js` project configuration. You can also selectively disable/enable at the story level.

```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,
    },
  },
};

},
};
```

## From version 8.2.x to 8.3.x

### Removed `experimental_SIDEBAR_BOTTOM` and deprecated `experimental_SIDEBAR_TOP` addon types
Expand Down
13 changes: 11 additions & 2 deletions code/addons/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@
"./angular": "./angular/index.js",
"./angular/index.js": "./angular/index.js",
"./web-components/index.js": "./web-components/index.js",
"./package.json": "./package.json"
"./package.json": "./package.json",
"./manager": {
"types": "./dist/manager.d.ts",
"import": "./dist/manager.mjs",
"require": "./dist/manager.js"
}
},
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -129,7 +134,11 @@
"./src/preview.ts",
"./src/blocks.ts",
"./src/shims/mdx-react-shim.ts",
"./src/mdx-loader.ts"
"./src/mdx-loader.ts",
"./src/manager.tsx"
],
"managerEntries": [
"./src/manager.tsx"
]
},
"gitHead": "e6a7fd8a655c69780bc20b9749c2699e44beae16",
Expand Down
38 changes: 38 additions & 0 deletions code/addons/docs/src/manager.tsx
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 code/addons/docs/template/stories/sourcePanel/index.stories.tsx
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 } },
};
5 changes: 5 additions & 0 deletions code/addons/essentials/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"./backgrounds/manager": "./dist/backgrounds/manager.js",
"./controls/manager": "./dist/controls/manager.js",
"./docs/manager": "./dist/docs/manager.js",
"./docs/preview": {
"types": "./dist/docs/preview.d.ts",
"import": "./dist/docs/preview.mjs",
Expand Down Expand Up @@ -114,10 +115,14 @@
"./src/docs/preset.ts",
"./src/docs/mdx-react-shim.ts"
],
"entries": [
"./src/docs/manager.ts"
],
"managerEntries": [
"./src/actions/manager.ts",
"./src/backgrounds/manager.ts",
"./src/controls/manager.ts",
"./src/docs/manager.ts",
"./src/measure/manager.ts",
"./src/outline/manager.ts",
"./src/toolbars/manager.ts",
Expand Down
2 changes: 2 additions & 0 deletions code/addons/essentials/src/docs/manager.ts
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';
2 changes: 1 addition & 1 deletion code/addons/essentials/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ export function addons(options: PresetOptions) {

// NOTE: The order of these addons is important.
return [
'docs',
'controls',
'actions',
'docs',
larsrickert marked this conversation as resolved.
Show resolved Hide resolved
'backgrounds',
'viewport',
'toolbars',
Expand Down
6 changes: 1 addition & 5 deletions code/renderers/vue3/src/docs/sourceDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ ${template}`;
* Checks if the source code generation should be skipped for the given Story context. Will be true
* if one of the following is true:
*
* - View mode is not "docs"
* - Story is no arg story
* - Story has set custom source code via parameters.docs.source.code
* - Story has set source type to "code" via parameters.docs.source.type
Expand All @@ -120,13 +119,10 @@ export const shouldSkipSourceCodeGeneration = (context: StoryContext): boolean =
}

const isArgsStory = context?.parameters.__isArgsStory;
const isDocsViewMode = context?.viewMode === 'docs';

// never render if the user is forcing the block to render code, or
// if the user provides code, or if it's not an args story.
return (
!isDocsViewMode || !isArgsStory || sourceParams?.code || sourceParams?.type === SourceType.CODE
larsrickert marked this conversation as resolved.
Show resolved Hide resolved
);
return !isArgsStory || sourceParams?.code || sourceParams?.type === SourceType.CODE;
};

/**
Expand Down
Loading