Skip to content

Commit

Permalink
Merge pull request #27144 from storybookjs/shilman/27143-fix-stories-…
Browse files Browse the repository at this point in the history
…block-in-mdx

Docs: Fix MDX Stories block tag-filtering behavior
  • Loading branch information
shilman committed May 15, 2024
2 parents 78bbb6c + babad72 commit ce3d0d5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
4 changes: 1 addition & 3 deletions code/addons/docs/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export const parameters: any = {
filter: (story: PreparedStory) => {
const tags = story.tags || [];
return (
tags.includes('autodocs') &&
tags.filter((tag) => excludeTags[tag]).length === 0 &&
!story.parameters.docs?.disable
tags.filter((tag) => excludeTags[tag]).length === 0 && !story.parameters.docs?.disable
);
},
},
Expand Down
10 changes: 10 additions & 0 deletions code/ui/blocks/src/blocks/Stories.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ export const DifferentToolbars: Story = {
relativeCsfPaths: ['../examples/StoriesParameters.stories'],
},
};
export const NoAutodocs: Story = {
parameters: {
relativeCsfPaths: ['../examples/ButtonNoAutodocs.stories'],
},
};
export const SomeAutodocs: Story = {
parameters: {
relativeCsfPaths: ['../examples/ButtonSomeAutodocs.stories'],
},
};
11 changes: 11 additions & 0 deletions code/ui/blocks/src/blocks/Stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ export const Stories: FC<StoriesProps> = ({ title = 'Stories', includePrimary =
if (filter) {
stories = stories.filter((story) => filter(story, getStoryContext(story)));
}
// NOTE: this should be part of the default filter function. However, there is currently
// no way to distinguish a Stories block in an autodocs page from Stories in an MDX file
// making https://github.com/storybookjs/storybook/pull/26634 an unintentional breaking change.
//
// The new behavior here is that if NONE of the stories in the autodocs page are tagged
// with 'autodocs', we show all stories. If ANY of the stories have autodocs then we use
// the new behavior.
const hasAutodocsTaggedStory = stories.some((story) => story.tags?.includes('autodocs'));
if (hasAutodocsTaggedStory) {
stories = stories.filter((story) => story.tags?.includes('autodocs'));
}

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

Expand Down
29 changes: 29 additions & 0 deletions code/ui/blocks/src/examples/ButtonNoAutodocs.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Meta, StoryObj } from '@storybook/react';
import { Button } from './Button';

const meta = {
title: 'examples/ButtonNoAutodocs',
component: Button,
argTypes: {
backgroundColor: { control: 'color' },
},
parameters: {
chromatic: { disable: true },
},
} satisfies Meta<typeof Button>;

export default meta;
type Story = StoryObj<typeof meta>;

export const Primary: Story = {
args: {
primary: true,
label: 'Button',
},
};

export const Secondary: Story = {
args: {
label: 'Button',
},
};
30 changes: 30 additions & 0 deletions code/ui/blocks/src/examples/ButtonSomeAutodocs.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { Meta, StoryObj } from '@storybook/react';
import { Button } from './Button';

const meta = {
title: 'examples/ButtonSomeAutodocs',
component: Button,
argTypes: {
backgroundColor: { control: 'color' },
},
parameters: {
chromatic: { disable: true },
},
} satisfies Meta<typeof Button>;

export default meta;
type Story = StoryObj<typeof meta>;

export const Primary: Story = {
args: {
primary: true,
label: 'Button',
},
};

export const Secondary: Story = {
tags: ['autodocs'],
args: {
label: 'Button',
},
};

0 comments on commit ce3d0d5

Please sign in to comment.