Skip to content

Commit

Permalink
Merge pull request #17638 from LucaCras/17596-document-csf3.0-auto-title
Browse files Browse the repository at this point in the history
Update the documentation with info on auto generating titles
  • Loading branch information
jonniebigodes authored Mar 14, 2022
2 parents 9022de6 + e1ecf24 commit a0f1dc1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
34 changes: 30 additions & 4 deletions docs/configure/sidebar-and-urls.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,47 @@ If you’d prefer to show top-level nodes as folders rather than roots, you can

<!-- prettier-ignore-end -->

## Generating titles based on `__dirname`
## Automatically generate titles

As a CSF file is a JavaScript file, all exports (including the default export) can be generated dynamically. In particular you can use the `__dirname` variable to create the title based on the path name (this example uses the paths.macro):
With CSF 3.0 files, you can choose to leave out a title and let it be inferred from the story's path on disk instead:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'common/component-story-dynamic-title.js.mdx',
'common/component-story-auto-title.js.mdx',
]}
/>

<!-- prettier-ignore-end -->

If you need, you can also generate automatic titles for all your stories using a configuration object. See the [story loading](./overview.md#with-a-configuration-object) documentation to learn how you can use this feature.
This is controlled by the way your stories are configured:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'common/component-story-configuration.js.mdx',
]}
/>

<!-- prettier-ignore-end -->

Given this configuration, the stories file `../src/components/MyComponent.stories.tsx` will get the title `components/MyComponent`.

You can further customize the generated title by modifying the stories configuration.

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'common/component-story-configuration-custom.js.mdx',
]}
/>

<!-- prettier-ignore-end -->

This configuration would match a custom file pattern, and add a custom prefix of Foo to each generated title.

## Permalinking to stories

Expand Down
4 changes: 4 additions & 0 deletions docs/snippets/common/component-story-auto-title.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```js
// CSF 3.0 - MyComponent.stories.js|jsx|ts|tsx
export default { component: MyComponent }
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
```js
// ./storybook/main.js
module.exports = {
stories: [
{ directory: '../src', files: '*.story.tsx', titlePrefix: 'Foo' }
]
};
```
6 changes: 6 additions & 0 deletions docs/snippets/common/component-story-configuration.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```js
// ./storybook/main.js
module.exports = {
stories: ['../src']
};
```
9 changes: 0 additions & 9 deletions docs/snippets/common/component-story-dynamic-title.js.mdx

This file was deleted.

0 comments on commit a0f1dc1

Please sign in to comment.