-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25328 from storybookjs/shilman/autodocs-filter
UI: Add configurable tags-based exclusion from sidebar/autodocs
- Loading branch information
Showing
27 changed files
with
215 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,27 @@ | ||
import type { PreparedStory } from '@storybook/types'; | ||
import { global } from '@storybook/global'; | ||
|
||
const excludeTags = Object.entries(global.TAGS_OPTIONS ?? {}).reduce((acc, entry) => { | ||
const [tag, option] = entry; | ||
if ((option as any).excludeFromDocsStories) { | ||
acc[tag] = true; | ||
} | ||
return acc; | ||
}, {} as Record<string, boolean>); | ||
|
||
export const parameters: any = { | ||
docs: { | ||
renderer: async () => { | ||
const { DocsRenderer } = (await import('./DocsRenderer')) as any; | ||
return new DocsRenderer(); | ||
}, | ||
stories: { | ||
filter: (story: PreparedStory) => { | ||
const tags = story.tags || []; | ||
return ( | ||
tags.filter((tag) => excludeTags[tag]).length === 0 && !story.parameters.docs?.disable | ||
); | ||
}, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { test, expect } from '@playwright/test'; | ||
import { SbPage } from './util'; | ||
|
||
const storybookUrl = process.env.STORYBOOK_URL || 'http://localhost:8001'; | ||
|
||
test.describe('tags', () => { | ||
test.beforeEach(async ({ page }) => { | ||
await page.goto(storybookUrl); | ||
await new SbPage(page).waitUntilLoaded(); | ||
}); | ||
|
||
test('should correctly filter dev-only, docs-only, test-only stories', async ({ page }) => { | ||
const sbPage = new SbPage(page); | ||
|
||
await sbPage.navigateToStory('lib/preview-api/tags', 'docs'); | ||
|
||
// Sidebar should include dev-only and exclude docs-only and test-only | ||
const devOnlyEntry = await page.locator('#lib-preview-api-tags--dev-only').all(); | ||
expect(devOnlyEntry.length).toBe(1); | ||
|
||
const docsOnlyEntry = await page.locator('#lib-preview-api-tags--docs-only').all(); | ||
expect(docsOnlyEntry.length).toBe(0); | ||
|
||
const testOnlyEntry = await page.locator('#lib-preview-api-tags--test-only').all(); | ||
expect(testOnlyEntry.length).toBe(0); | ||
|
||
// Autodocs should include docs-only and exclude dev-only and test-only | ||
const root = sbPage.previewRoot(); | ||
|
||
const devOnlyAnchor = await root.locator('#anchor--lib-preview-api-tags--dev-only').all(); | ||
expect(devOnlyAnchor.length).toBe(0); | ||
|
||
const docsOnlyAnchor = await root.locator('#anchor--lib-preview-api-tags--docs-only').all(); | ||
expect(docsOnlyAnchor.length).toBe(1); | ||
|
||
const testOnlyAnchor = await root.locator('#anchor--lib-preview-api-tags--test-only').all(); | ||
expect(testOnlyAnchor.length).toBe(0); | ||
}); | ||
|
||
test('should correctly filter out test-only autodocs pages', async ({ page }) => { | ||
const sbPage = new SbPage(page); | ||
|
||
await sbPage.selectToolbar('#lib-preview-api'); | ||
|
||
// Sidebar should exclude test-only stories and their docs | ||
const componentEntry = await page.locator('#lib-preview-api-test-only-tag').all(); | ||
expect(componentEntry.length).toBe(0); | ||
|
||
// Even though test-only autodocs not sidebar, it is still in the preview | ||
// Even though the test-only story is filtered out of the stories, it is still the primary story (should it be?) | ||
await sbPage.deepLinkToStory(storybookUrl, 'lib/preview-api/test-only-tag', 'docs'); | ||
await sbPage.waitUntilLoaded(); | ||
const docsButton = await sbPage.previewRoot().locator('button', { hasText: 'Button' }); | ||
await expect(docsButton).toBeVisible(); | ||
|
||
// Even though test-only story not sidebar, it is still in the preview | ||
await sbPage.deepLinkToStory(storybookUrl, 'lib/preview-api/test-only-tag', 'default'); | ||
await sbPage.waitUntilLoaded(); | ||
const storyButton = await sbPage.previewRoot().locator('button', { hasText: 'Button' }); | ||
await expect(storyButton).toBeVisible(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { addons } from '@storybook/manager-api'; | ||
import { global } from '@storybook/global'; | ||
|
||
const STATIC_FILTER = 'static-filter'; | ||
|
||
addons.register(STATIC_FILTER, (api) => { | ||
// FIXME: this ensures the filter is applied after the first render | ||
// to avoid a strange race condition in Webkit only. | ||
const excludeTags = Object.entries(global.TAGS_OPTIONS ?? {}).reduce((acc, entry) => { | ||
const [tag, option] = entry; | ||
if ((option as any).excludeFromSidebar) { | ||
acc[tag] = true; | ||
} | ||
return acc; | ||
}, {} as Record<string, boolean>); | ||
|
||
api.experimental_setFilter(STATIC_FILTER, (item) => { | ||
const tags = item.tags || []; | ||
return tags.filter((tag) => excludeTags[tag]).length === 0; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.