Skip to content

Commit

Permalink
Visually differentiate table of content heading levels (#7385)
Browse files Browse the repository at this point in the history
* propose heading indentation within metabar toc

* adds test for heading visibility
  • Loading branch information
bmuenzenmeyer authored Jan 6, 2025
1 parent baae0f1 commit 6093af9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
60 changes: 60 additions & 0 deletions apps/site/components/Containers/MetaBar/__tests__/index.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { render } from '@testing-library/react';

import MetaBar from '..';

describe('MetaBar', () => {
it('does not render h5s in the table of contents', () => {
const { queryByText, getByText } = render(
<MetaBar
items={{}}
headings={{
items: [
{
value: 'Heading Level 1',
depth: 1,
data: { id: 'heading-1' },
},
{
value: 'Heading Level 2',
depth: 2,
data: { id: 'heading-2' },
},
{
value: 'Heading Level 3',
depth: 3,
data: { id: 'heading-3' },
},
{
value: 'Heading Level 4',
depth: 4,
data: { id: 'heading-4' },
},
{
value: 'Heading Level 5',
depth: 5,
data: { id: 'heading-5' },
},
{
value: 'Heading Level 6',
depth: 6,
data: { id: 'heading-6' },
},
],
}}
/>
);

const h1Element = queryByText('Heading Level 1');
expect(h1Element).not.toBeInTheDocument();

getByText('Heading Level 2');
getByText('Heading Level 3');
getByText('Heading Level 4');

const h5Element = queryByText('Heading Level 5');
expect(h5Element).not.toBeInTheDocument();

const h6Element = queryByText('Heading Level 6');
expect(h6Element).not.toBeInTheDocument();
});
});
15 changes: 15 additions & 0 deletions apps/site/components/Containers/MetaBar/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ export const Default: Story = {
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' },
},
],
},
},
Expand Down
9 changes: 7 additions & 2 deletions apps/site/components/Containers/MetaBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ const MetaBar: FC<MetaBarProps> = ({ items, headings }) => {
<dd>
<ol>
{heading.map(head => (
<li key={head.value}>
<Link href={`#${head?.data?.id}`}>{head.value}</Link>
<li
key={head.value}
className={
head.depth === 3 ? 'pl-2' : head.depth === 4 ? 'pl-4' : ''
}
>
<Link href={`#${head?.data?.id}`}> {head.value}</Link>
</li>
))}
</ol>
Expand Down

0 comments on commit 6093af9

Please sign in to comment.