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

Docs: Fix MDX Stories block tag-filtering behavior #27144

Merged
merged 1 commit into from
May 15, 2024
Merged
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
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',
},
};
Loading