-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat: create toc component #8560
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 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8032308
feat: create toc component
araujogui 250e22e
refactor(ui-components): review
araujogui 67dd1c9
fix: custom anchor component
araujogui 30ab5f4
refactor: review
araujogui 0528249
feat: i18n prop
araujogui 17c7036
choire: patch version
araujogui ab45404
review(ui-components): some nits
araujogui 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
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
packages/ui-components/src/Common/TableOfContents/index.module.css
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,39 @@ | ||
| @reference "../../styles/index.css"; | ||
|
|
||
| .details { | ||
| @apply my-2 | ||
| block | ||
| rounded-md | ||
| bg-neutral-200 | ||
| lg:hidden | ||
| dark:bg-neutral-900; | ||
|
|
||
| .summary { | ||
| @apply px-4 | ||
| py-2; | ||
| } | ||
|
|
||
| .list { | ||
| @apply space-y-1 | ||
| px-4 | ||
| pb-2; | ||
| } | ||
|
|
||
| .link { | ||
| @apply text-sm | ||
| font-semibold | ||
| text-neutral-900 | ||
| underline | ||
| hover:text-neutral-700 | ||
| dark:text-white | ||
| dark:hover:text-neutral-500; | ||
| } | ||
|
|
||
| .depthThree { | ||
| @apply pl-2; | ||
| } | ||
|
|
||
| .depthFour { | ||
| @apply pl-4; | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
packages/ui-components/src/Common/TableOfContents/index.stories.tsx
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,65 @@ | ||
| import TableOfContents from '#ui/Common/TableOfContents'; | ||
|
|
||
| import type { Meta as MetaObj, StoryObj } from '@storybook/react-webpack5'; | ||
|
|
||
| type Story = StoryObj<typeof TableOfContents>; | ||
| type Meta = MetaObj<typeof TableOfContents>; | ||
|
|
||
| export const Default: Story = {}; | ||
|
|
||
| export const CustomDepth: Story = { | ||
| args: { | ||
| minDepth: 1, | ||
| maxDepth: 6, | ||
| }, | ||
| }; | ||
|
|
||
| export default { | ||
| component: TableOfContents, | ||
| args: { | ||
| ariaLabel: 'Table of Contents', | ||
| summaryTitle: 'On this page', | ||
| headings: [ | ||
| { | ||
| value: 'OpenSSL update assessment, and Node.js project plans', | ||
| depth: 1, | ||
| data: { id: 'heading-1' }, | ||
| }, | ||
| { | ||
| value: 'Summary', | ||
| depth: 2, | ||
| data: { id: 'summary' }, | ||
| }, | ||
| { | ||
| value: 'Analysis', | ||
| depth: 2, | ||
| data: { id: 'analysis' }, | ||
| }, | ||
| { | ||
| value: 'The c_rehash script allows command injection (CVE-2022-2068)', | ||
| depth: 3, | ||
| data: { id: 'the_c_rehash' }, | ||
| }, | ||
| { | ||
| value: 'Contact and future updates', | ||
| depth: 3, | ||
| data: { id: 'contact_and_future_updates' }, | ||
| }, | ||
| { | ||
| value: 'Email', | ||
| depth: 4, | ||
| data: { id: 'email' }, | ||
| }, | ||
| { | ||
| value: 'Slack', | ||
| depth: 4, | ||
| data: { id: 'slack' }, | ||
| }, | ||
| { | ||
| value: '#node-website', | ||
| depth: 5, // h5s do not get shown | ||
| data: { id: 'node-website' }, | ||
| }, | ||
| ], | ||
| }, | ||
| } as Meta; |
54 changes: 54 additions & 0 deletions
54
packages/ui-components/src/Common/TableOfContents/index.tsx
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,54 @@ | ||
| import classNames from 'classnames'; | ||
|
|
||
| import { LinkLike } from '#ui/types'; | ||
|
|
||
| import type { Heading } from '@vcarl/remark-headings'; | ||
| import type { FC } from 'react'; | ||
|
|
||
| import styles from './index.module.css'; | ||
|
|
||
| type TableOfContentsProps = { | ||
| headings: Array<Heading>; | ||
| ariaLabel: string; | ||
| summaryTitle: string; | ||
| minDepth?: number; | ||
| maxDepth?: number; | ||
| as?: LinkLike; | ||
| }; | ||
|
|
||
| const TableOfContents: FC<TableOfContentsProps> = ({ | ||
| headings, | ||
| ariaLabel, | ||
| summaryTitle, | ||
| minDepth = 2, | ||
| maxDepth = 4, | ||
| as: Component = 'a', | ||
| }) => { | ||
| const filteredHeadings = headings.filter( | ||
| ({ depth }) => depth >= minDepth && depth <= maxDepth | ||
| ); | ||
|
|
||
| return ( | ||
| <details className={styles.details} aria-label={ariaLabel}> | ||
| <summary className={styles.summary}>{summaryTitle}</summary> | ||
| <ul className={styles.list}> | ||
| {filteredHeadings.map((head, index) => ( | ||
| <li key={head.data?.id ?? index}> | ||
| <Component | ||
| href={head.data?.id && `#${head.data.id}`} | ||
| className={classNames( | ||
| styles.link, | ||
| head.depth === 3 && styles.depthThree, | ||
| head.depth === 4 && styles.depthFour | ||
|
araujogui marked this conversation as resolved.
Outdated
|
||
| )} | ||
|
araujogui marked this conversation as resolved.
Outdated
|
||
| > | ||
| {head.value} | ||
| </Component> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </details> | ||
| ); | ||
| }; | ||
|
|
||
| export default TableOfContents; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.