Skip to content

Commit

Permalink
Vue: Consider custom code snippet in story code panel
Browse files Browse the repository at this point in the history
  • Loading branch information
larsrickert committed Jan 3, 2025
1 parent fbf01bd commit 7486245
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
38 changes: 28 additions & 10 deletions code/addons/docs/src/manager.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';
import React, { useState } 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 } 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 Down Expand Up @@ -33,14 +34,15 @@ addons.register(ADDON_ID, (api) => {
},
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,
});

const [codeSnippet, setSourceCode] = useState<{
source?: string;
format?: SyntaxHighlighterFormatTypes;
}>({});

useChannel({
[SNIPPET_RENDERED]: ({ source, format }) => {
setSourceCode({ source, format });
Expand All @@ -49,9 +51,25 @@ addons.register(ADDON_ID, (api) => {

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}
dark
/>
</SourceStyles>
</AddonPanel>
);
},
});
});

const SourceStyles = styled.div(() => ({
height: '100%',
[`> :first-child${ignoreSsrWarning}`]: {
margin: 0,
height: '100%',
boxShadow: 'none',
},
}));
29 changes: 29 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,29 @@
export default {
component: globalThis.Components.Button,
tags: ['autodocs'],
parameters: {
chromatic: { disable: true },
},
};

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

export const CustomCode = {
args: { label: 'Custom code' },
parameters: {
docs: {
source: {
code: '<button>Custom code</button>',
},
},
},
};

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

0 comments on commit 7486245

Please sign in to comment.