Skip to content

Commit 94f2fc8

Browse files
shilmanstorybook-bot
authored andcommitted
Merge pull request #27144 from storybookjs/shilman/27143-fix-stories-block-in-mdx
Docs: Fix MDX Stories block tag-filtering behavior (cherry picked from commit ce3d0d5)
1 parent bbfa3e0 commit 94f2fc8

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

code/addons/docs/src/preview.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ export const parameters: any = {
2222
filter: (story: PreparedStory) => {
2323
const tags = story.tags || [];
2424
return (
25-
tags.includes('autodocs') &&
26-
tags.filter((tag) => excludeTags[tag]).length === 0 &&
27-
!story.parameters.docs?.disable
25+
tags.filter((tag) => excludeTags[tag]).length === 0 && !story.parameters.docs?.disable
2826
);
2927
},
3028
},

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,13 @@ export const DifferentToolbars: Story = {
2626
relativeCsfPaths: ['../examples/StoriesParameters.stories'],
2727
},
2828
};
29+
export const NoAutodocs: Story = {
30+
parameters: {
31+
relativeCsfPaths: ['../examples/ButtonNoAutodocs.stories'],
32+
},
33+
};
34+
export const SomeAutodocs: Story = {
35+
parameters: {
36+
relativeCsfPaths: ['../examples/ButtonSomeAutodocs.stories'],
37+
},
38+
};

code/ui/blocks/src/blocks/Stories.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ export const Stories: FC<StoriesProps> = ({ title = 'Stories', includePrimary =
3434
if (filter) {
3535
stories = stories.filter((story) => filter(story, getStoryContext(story)));
3636
}
37+
// NOTE: this should be part of the default filter function. However, there is currently
38+
// no way to distinguish a Stories block in an autodocs page from Stories in an MDX file
39+
// making https://github.com/storybookjs/storybook/pull/26634 an unintentional breaking change.
40+
//
41+
// The new behavior here is that if NONE of the stories in the autodocs page are tagged
42+
// with 'autodocs', we show all stories. If ANY of the stories have autodocs then we use
43+
// the new behavior.
44+
const hasAutodocsTaggedStory = stories.some((story) => story.tags?.includes('autodocs'));
45+
if (hasAutodocsTaggedStory) {
46+
stories = stories.filter((story) => story.tags?.includes('autodocs'));
47+
}
3748

3849
if (!includePrimary) stories = stories.slice(1);
3950

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Meta, StoryObj } from '@storybook/react';
2+
import { Button } from './Button';
3+
4+
const meta = {
5+
title: 'examples/ButtonNoAutodocs',
6+
component: Button,
7+
argTypes: {
8+
backgroundColor: { control: 'color' },
9+
},
10+
parameters: {
11+
chromatic: { disable: true },
12+
},
13+
} satisfies Meta<typeof Button>;
14+
15+
export default meta;
16+
type Story = StoryObj<typeof meta>;
17+
18+
export const Primary: Story = {
19+
args: {
20+
primary: true,
21+
label: 'Button',
22+
},
23+
};
24+
25+
export const Secondary: Story = {
26+
args: {
27+
label: 'Button',
28+
},
29+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { Meta, StoryObj } from '@storybook/react';
2+
import { Button } from './Button';
3+
4+
const meta = {
5+
title: 'examples/ButtonSomeAutodocs',
6+
component: Button,
7+
argTypes: {
8+
backgroundColor: { control: 'color' },
9+
},
10+
parameters: {
11+
chromatic: { disable: true },
12+
},
13+
} satisfies Meta<typeof Button>;
14+
15+
export default meta;
16+
type Story = StoryObj<typeof meta>;
17+
18+
export const Primary: Story = {
19+
args: {
20+
primary: true,
21+
label: 'Button',
22+
},
23+
};
24+
25+
export const Secondary: Story = {
26+
tags: ['autodocs'],
27+
args: {
28+
label: 'Button',
29+
},
30+
};

0 commit comments

Comments
 (0)