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

Vue: Consider custom code snippet in story code panel and update styles #30179

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
40 changes: 31 additions & 9 deletions code/addons/docs/src/manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import React from 'react';

import { AddonPanel, type SyntaxHighlighterFormatTypes } from 'storybook/internal/components';
import { ADDON_ID, PANEL_ID, PARAM_KEY, SNIPPET_RENDERED } from 'storybook/internal/docs-tools';
import { addons, types, useAddonState, useChannel } from 'storybook/internal/manager-api';
import { addons, types, useChannel, useParameter } from 'storybook/internal/manager-api';
import { ignoreSsrWarning, styled, useTheme } from 'storybook/internal/theming';

import { Source } from '@storybook/blocks';
import { Source, type SourceParameters } from '@storybook/blocks';

addons.register(ADDON_ID, (api) => {
addons.add(PANEL_ID, {
Expand All @@ -27,25 +28,46 @@ addons.register(ADDON_ID, (api) => {
disabled: (parameters) => !parameters?.docs?.codePanel,
match: ({ viewMode }) => viewMode === 'story',
render: ({ active }) => {
const [codeSnippet, setSourceCode] = useAddonState<{
source: string;
format: SyntaxHighlighterFormatTypes;
}>(ADDON_ID, {
source: '',
format: 'html',
const parameter = useParameter(PARAM_KEY, {
source: { code: '' } as SourceParameters,
theme: 'dark',
});

const [codeSnippet, setSourceCode] = React.useState<{
source?: string;
format?: SyntaxHighlighterFormatTypes;
}>({});
larsrickert marked this conversation as resolved.
Show resolved Hide resolved

useChannel({
[SNIPPET_RENDERED]: ({ source, format }) => {
setSourceCode({ source, format });
},
});

const theme = useTheme();
const isDark = theme.base !== 'light';

return (
<AddonPanel active={!!active}>
<Source code={codeSnippet.source} format={codeSnippet.format} dark />
<SourceStyles>
<Source
{...parameter.source}
code={parameter.source.code || codeSnippet.source}
format={parameter.source.format || codeSnippet.format}
larsrickert marked this conversation as resolved.
Show resolved Hide resolved
dark={isDark}
/>
</SourceStyles>
</AddonPanel>
);
},
});
});

const SourceStyles = styled.div(() => ({
height: '100%',
[`> :first-child${ignoreSsrWarning}`]: {
margin: 0,
height: '100%',
boxShadow: 'none',
},
}));
32 changes: 32 additions & 0 deletions code/addons/docs/template/stories/codePanel/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export default {
component: globalThis.Components.Button,
larsrickert marked this conversation as resolved.
Show resolved Hide resolved
tags: ['autodocs'],
parameters: {
chromatic: { disable: true },
docs: {
codePanel: true,
},
},
};

export const Default = { args: { label: 'Default' } };

export const CustomCode = {
args: { label: 'Custom code' },
parameters: {
docs: {
source: {
code: '<button>Custom code</button>',
larsrickert marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
};

export const WithoutPanel = {
args: { label: 'Without panel' },
parameters: {
docs: {
codePanel: false,
},
},
};
23 changes: 0 additions & 23 deletions code/addons/docs/template/stories/sourcePanel/index.stories.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion code/lib/blocks/src/blocks/Source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { DocsContext } from './DocsContext';
import type { SourceContextProps, SourceItem } from './SourceContainer';
import { SourceContext, UNKNOWN_ARGS_HASH, argsHash } from './SourceContainer';

type SourceParameters = SourceCodeProps & {
export type SourceParameters = SourceCodeProps & {
/** Where to read the source code from, see `SourceType` */
type?: SourceType;
/** Transform the detected source for display */
Expand Down
Loading