-
Notifications
You must be signed in to change notification settings - Fork 0
docs: add prev/next navigation to guide doc pages #14
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d7f01cd
docs: add prev/next navigation to guide docs pages
shijistar cc7fbd7
docs: add bottom previous/next navigator
shijistar 5cf8d49
docs: update navigation links and translations for documentation
shijistar 359b4df
docs: optimize story paths retrieval with useMemo for performance
shijistar 5a748ab
docs: adjust table cell styles for improved readability
shijistar 4be89db
docs: update changelog navigation link with language parameter
shijistar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,128 @@ | ||
| import { useMemo } from 'react'; | ||
| import { Controls, Markdown, Primary, Subtitle, Title, useOf } from '@storybook/addon-docs/blocks'; | ||
| import LinkTo from '@storybook/addon-links/react'; | ||
| import type { ResolvedModuleExportFromType } from 'storybook/internal/types'; | ||
| import { Flex } from 'antd'; | ||
| import { useStoryT } from '../locales'; | ||
| import { processDescription } from '../utils/description'; | ||
|
|
||
| function StoryDocPage() { | ||
| const t = useStoryT(); | ||
| const currentStory = useMemo(() => { | ||
| const path = new URLSearchParams(top?.location.search).get('path'); | ||
| const matches = path?.match(/\/docs\/(components|hooks|utils)-(\w+?)--api/); | ||
| if (matches && matches.length > 2) { | ||
| return `./stories/${matches[1]}/${matches[2]}/index.stories.tsx`.toLowerCase(); | ||
| } | ||
| return null; | ||
| }, []); | ||
| const componentPaths = useMemo(() => Object.keys(import.meta.glob(`./stories/components/*/index.stories.tsx`)), []); | ||
| const hookPaths = useMemo(() => Object.keys(import.meta.glob(`./stories/hooks/*/index.stories.tsx`)), []); | ||
| const utilPaths = useMemo(() => Object.keys(import.meta.glob(`./stories/utils/*/index.stories.tsx`)), []); | ||
| const allStories = useMemo( | ||
| () => | ||
| [...componentPaths, ...hookPaths, ...utilPaths].map((path) => { | ||
| const parts = path.split('/'); | ||
| return { | ||
| path: path.toLowerCase(), | ||
| name: parts[3], | ||
| url: `${parts[2]}/${parts[3]}`, | ||
| }; | ||
| }), | ||
| [componentPaths, hookPaths, utilPaths], | ||
| ); | ||
| const index = useMemo(() => allStories.findIndex((story) => story.path === currentStory), [allStories, currentStory]); | ||
|
|
||
| return ( | ||
| <> | ||
| <Title /> | ||
| <Subtitle /> | ||
| <CustomComponentDescription /> | ||
| <h2>{t('storybook.stories.demo')}</h2> | ||
| <Primary /> | ||
| <Controls /> | ||
| <Flex justify="space-between"> | ||
| <p>{t('storybook.stories.nav.previous')}</p> | ||
| <p>{t('storybook.stories.nav.next')}</p> | ||
| </Flex> | ||
| <Flex justify="space-between"> | ||
| {index > 0 ? ( | ||
| <LinkTo kind={allStories[index - 1].url} story="api"> | ||
| <span style={{ fontSize: 18, fontWeight: 600 }}>← {allStories[index - 1].name}</span> | ||
| </LinkTo> | ||
| ) : ( | ||
| <LinkTo kind="get-started" story="api"> | ||
| <span style={{ fontSize: 18, fontWeight: 600 }}>← {t('storybook.stories.nav.getStarted')}</span> | ||
| </LinkTo> | ||
| )} | ||
| {index < allStories.length - 1 ? ( | ||
| <LinkTo kind={allStories[index + 1].url} story="api"> | ||
| <span style={{ fontSize: 18, fontWeight: 600 }}>{allStories[index + 1].name} →</span> | ||
| </LinkTo> | ||
| ) : ( | ||
| <p style={{ margin: 0 }}>{t('storybook.stories.nav.nothing')}</p> | ||
| )} | ||
| </Flex> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| function CustomComponentDescription() { | ||
| const resolvedOfMeta = useOf<'meta'>('meta'); | ||
| let resolvedOfComponent: ResolvedModuleExportFromType<'component'> | undefined; | ||
| try { | ||
| // eslint-disable-next-line @tiny-codes/react-hooks/rules-of-hooks | ||
| resolvedOfComponent = useOf<'component'>('component'); | ||
| } catch { | ||
| // Ignore error | ||
| } | ||
| const descriptionOfMeta = getDescriptionFromResolvedOf(resolvedOfMeta); | ||
| const descriptionOfComponent = getDescriptionFromResolvedOf(resolvedOfComponent); | ||
| const next = useMemo(() => { | ||
| const description = descriptionOfMeta || descriptionOfComponent || ''; | ||
| return processDescription(description); | ||
| }, [descriptionOfMeta, descriptionOfComponent]); | ||
|
|
||
| if (!next) return null; | ||
| return <Markdown>{next}</Markdown>; | ||
| } | ||
|
|
||
| function getDescriptionFromResolvedOf(resolvedOf: ReturnType<typeof useOf> | undefined): string | null { | ||
| if (!resolvedOf) return null; | ||
| switch (resolvedOf.type) { | ||
| case 'story': { | ||
| return resolvedOf.story.parameters.docs?.description?.story || null; | ||
| } | ||
| case 'meta': { | ||
| const { parameters, component } = resolvedOf.preparedMeta; | ||
| const metaDescription = parameters.docs?.description?.component; | ||
| if (metaDescription) { | ||
| return metaDescription; | ||
| } | ||
| return ( | ||
| parameters.docs?.extractComponentDescription?.(component, { | ||
| component, | ||
| parameters, | ||
| }) || null | ||
| ); | ||
| } | ||
| case 'component': { | ||
| const { | ||
| component, | ||
| projectAnnotations: { parameters }, | ||
| } = resolvedOf; | ||
| return ( | ||
| parameters?.docs?.extractComponentDescription?.(component, { | ||
| component, | ||
| parameters, | ||
| }) || null | ||
| ); | ||
| } | ||
| default: { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| throw new Error(`Unrecognized module type resolved from 'useOf', got: ${(resolvedOf as any).type}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default StoryDocPage; |
This file contains hidden or 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,10 @@ | ||
| import type { PropsWithChildren } from 'react'; | ||
| import type { DocsContainerProps } from '@storybook/addon-docs/blocks'; | ||
| import { DocsContainer } from '@storybook/addon-docs/blocks'; | ||
| import { themes } from 'storybook/theming'; | ||
|
|
||
| function ThemedDocsContainer({ isDark, ...props }: PropsWithChildren<DocsContainerProps> & { isDark: boolean }) { | ||
| return <DocsContainer {...props} theme={isDark ? themes.dark : themes.light} />; | ||
| } | ||
|
|
||
| export default ThemedDocsContainer; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,6 +1,14 @@ | ||
| import { Meta, Markdown } from '@storybook/addon-docs/blocks'; | ||
| import { Markdown, Meta } from '@storybook/addon-docs/blocks'; | ||
| import content from '../../CHANGELOG.md?raw'; | ||
|
|
||
| <Meta title="Changelog" /> | ||
|
|
||
| <Markdown>{content}</Markdown> | ||
|
|
||
| --- | ||
|
|
||
| {/* docs-prev-next-nav */} | ||
|
|
||
| | Previous | Next | | ||
| | ---------------------------------------------------------------- | ----- | | ||
| | [← Get Started](?path=/docs/get-started--api&globals=lang:en-US) | \- \- | |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Link Get Started to Changelog.
Both next links bypass the final guide page and open the BreakLines component story. This breaks the declared sequence: Introduce, Install, Get Started, Changelog.
.storybook/docs-localized/GetStarted.en-US.mdx#L61-L63: replace the BreakLines target with?path=/docs/changelog--api&globals=lang:en-US..storybook/docs-localized/GetStarted.zh-CN.mdx#L61-L63: replace the BreakLines target with?path=/docs/changelog--api&globals=lang:zh-CN.📍 Affects 2 files
.storybook/docs-localized/GetStarted.en-US.mdx#L61-L63(this comment).storybook/docs-localized/GetStarted.zh-CN.mdx#L61-L63🤖 Prompt for AI Agents