Skip to content

Commit 23f76e3

Browse files
shilmanstorybook-bot
authored andcommitted
Merge pull request #27147 from storybookjs/jeppe/27139-bug-unable-to-use-subtitle-doc-block-in-mdx-without-a-story
Docs: Fix Subtitle block when no `of` prop passed (cherry picked from commit 1ba1a7e)
1 parent 94f2fc8 commit 23f76e3

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

code/ui/blocks/src/blocks/Subtitle.stories.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,6 @@ export const OfStringMetaAttached: Story = {
9999
parameters: { relativeCsfPaths: ['../examples/Button.stories'], attached: true },
100100
};
101101
export const Children: Story = {
102-
parameters: { relativeCsfPaths: ['../examples/Button.stories'], attached: true },
102+
parameters: { relativeCsfPaths: ['../examples/Button.stories'], attached: false },
103103
render: () => <Subtitle>This subtitle is a string passed as a children</Subtitle>,
104104
};

code/ui/blocks/src/blocks/Subtitle.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ export const Subtitle: FunctionComponent<SubtitleProps> = (props) => {
2525
throw new Error('Unexpected `of={undefined}`, did you mistype a CSF file reference?');
2626
}
2727

28-
const { preparedMeta } = useOf(of || 'meta', ['meta']);
29-
const { componentSubtitle, docs } = preparedMeta.parameters || {};
28+
let preparedMeta;
29+
try {
30+
preparedMeta = useOf(of || 'meta', ['meta']).preparedMeta;
31+
} catch (error) {
32+
if (children && !error.message.includes('did you forget to use <Meta of={} />?')) {
33+
// ignore error about unattached CSF since we can still render children
34+
throw error;
35+
}
36+
}
37+
38+
const { componentSubtitle, docs } = preparedMeta?.parameters || {};
3039

3140
if (componentSubtitle) {
3241
deprecate(

code/ui/blocks/src/blocks/Title.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const Title: FunctionComponent<TitleProps> = (props) => {
4242
}
4343
}
4444

45-
const content = children || extractTitle(preparedMeta.title);
45+
const content = children || extractTitle(preparedMeta?.title);
4646

4747
return content ? <PureTitle className="sbdocs-title sb-unstyled">{content}</PureTitle> : null;
4848
};

0 commit comments

Comments
 (0)