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 #23728 from Sidnioulz/feat/22490-Title
Addon-Docs: Title blocks to support `of` prop
- Loading branch information
Showing
4 changed files
with
99 additions
and
6 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
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,55 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { Title } from './Title'; | ||
import * as DefaultButtonStories from '../examples/Button.stories'; | ||
|
||
const meta: Meta<typeof Title> = { | ||
component: Title, | ||
title: 'Blocks/Title', | ||
parameters: { | ||
controls: { | ||
include: [], | ||
hideNoControlsWarning: true, | ||
}, | ||
// workaround for https://github.com/storybookjs/storybook/issues/20505 | ||
docs: { source: { type: 'code' } }, | ||
attached: false, | ||
docsStyles: true, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
export const OfCSFFile: Story = { | ||
args: { | ||
of: DefaultButtonStories, | ||
}, | ||
parameters: { relativeCsfPaths: ['../examples/Button.stories'] }, | ||
}; | ||
|
||
export const OfMeta: Story = { | ||
args: { | ||
of: DefaultButtonStories, | ||
}, | ||
parameters: { relativeCsfPaths: ['../examples/Button.stories'] }, | ||
}; | ||
|
||
export const OfStringMetaAttached: Story = { | ||
name: 'Of attached "meta"', | ||
args: { | ||
of: 'meta', | ||
}, | ||
parameters: { relativeCsfPaths: ['../examples/Button.stories'], attached: true }, | ||
}; | ||
|
||
export const Children: Story = { | ||
args: { | ||
children: 'Title as children', | ||
}, | ||
parameters: { relativeCsfPaths: ['../examples/Button.stories'], attached: false }, | ||
}; | ||
|
||
export const DefaultAttached: Story = { | ||
args: {}, | ||
parameters: { relativeCsfPaths: ['../examples/Button.stories'], attached: true }, | ||
}; |
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,23 +1,48 @@ | ||
import type { ComponentTitle } from '@storybook/types'; | ||
import type { FunctionComponent, ReactNode } from 'react'; | ||
import React, { useContext } from 'react'; | ||
import React from 'react'; | ||
import { Title as PureTitle } from '../components'; | ||
import { DocsContext } from './DocsContext'; | ||
import type { Of } from './useOf'; | ||
import { useOf } from './useOf'; | ||
|
||
interface TitleProps { | ||
/** | ||
* Specify where to get the title from. Must be a CSF file's default export. | ||
* If not specified, the title will be read from children, or extracted from the meta of the attached CSF file. | ||
*/ | ||
of?: Of; | ||
|
||
/** | ||
* Specify content to display as the title. | ||
*/ | ||
children?: ReactNode; | ||
} | ||
|
||
const STORY_KIND_PATH_SEPARATOR = /\s*\/\s*/; | ||
|
||
export const extractTitle = (title: ComponentTitle) => { | ||
const groups = title.trim().split(STORY_KIND_PATH_SEPARATOR); | ||
return (groups && groups[groups.length - 1]) || title; | ||
return groups?.[groups?.length - 1] || title; | ||
}; | ||
|
||
export const Title: FunctionComponent<TitleProps> = ({ children }) => { | ||
const context = useContext(DocsContext); | ||
const content = children || extractTitle(context.storyById().title); | ||
export const Title: FunctionComponent<TitleProps> = (props) => { | ||
const { children, of } = props; | ||
|
||
if ('of' in props && of === undefined) { | ||
throw new Error('Unexpected `of={undefined}`, did you mistype a CSF file reference?'); | ||
} | ||
|
||
let preparedMeta; | ||
try { | ||
preparedMeta = useOf(of || 'meta', ['meta']).preparedMeta; | ||
} catch (error) { | ||
if (children && !error.message.includes('did you forget to use <Meta of={} />?')) { | ||
// ignore error about unattached CSF since we can still render children | ||
throw error; | ||
} | ||
} | ||
|
||
const content = children || extractTitle(preparedMeta.title); | ||
|
||
return content ? <PureTitle className="sbdocs-title sb-unstyled">{content}</PureTitle> : null; | ||
}; |
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