Skip to content

Commit 6093af9

Browse files
Visually differentiate table of content heading levels (#7385)
* propose heading indentation within metabar toc * adds test for heading visibility
1 parent baae0f1 commit 6093af9

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { render } from '@testing-library/react';
2+
3+
import MetaBar from '..';
4+
5+
describe('MetaBar', () => {
6+
it('does not render h5s in the table of contents', () => {
7+
const { queryByText, getByText } = render(
8+
<MetaBar
9+
items={{}}
10+
headings={{
11+
items: [
12+
{
13+
value: 'Heading Level 1',
14+
depth: 1,
15+
data: { id: 'heading-1' },
16+
},
17+
{
18+
value: 'Heading Level 2',
19+
depth: 2,
20+
data: { id: 'heading-2' },
21+
},
22+
{
23+
value: 'Heading Level 3',
24+
depth: 3,
25+
data: { id: 'heading-3' },
26+
},
27+
{
28+
value: 'Heading Level 4',
29+
depth: 4,
30+
data: { id: 'heading-4' },
31+
},
32+
{
33+
value: 'Heading Level 5',
34+
depth: 5,
35+
data: { id: 'heading-5' },
36+
},
37+
{
38+
value: 'Heading Level 6',
39+
depth: 6,
40+
data: { id: 'heading-6' },
41+
},
42+
],
43+
}}
44+
/>
45+
);
46+
47+
const h1Element = queryByText('Heading Level 1');
48+
expect(h1Element).not.toBeInTheDocument();
49+
50+
getByText('Heading Level 2');
51+
getByText('Heading Level 3');
52+
getByText('Heading Level 4');
53+
54+
const h5Element = queryByText('Heading Level 5');
55+
expect(h5Element).not.toBeInTheDocument();
56+
57+
const h6Element = queryByText('Heading Level 6');
58+
expect(h6Element).not.toBeInTheDocument();
59+
});
60+
});

apps/site/components/Containers/MetaBar/index.stories.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ export const Default: Story = {
7878
depth: 3,
7979
data: { id: 'contact_and_future_updates' },
8080
},
81+
{
82+
value: 'Email',
83+
depth: 4,
84+
data: { id: 'email' },
85+
},
86+
{
87+
value: 'Slack',
88+
depth: 4,
89+
data: { id: 'slack' },
90+
},
91+
{
92+
value: '#node-website',
93+
depth: 5, // h5s do not get shown
94+
data: { id: 'node-website' },
95+
},
8196
],
8297
},
8398
},

apps/site/components/Containers/MetaBar/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ const MetaBar: FC<MetaBarProps> = ({ items, headings }) => {
4444
<dd>
4545
<ol>
4646
{heading.map(head => (
47-
<li key={head.value}>
48-
<Link href={`#${head?.data?.id}`}>{head.value}</Link>
47+
<li
48+
key={head.value}
49+
className={
50+
head.depth === 3 ? 'pl-2' : head.depth === 4 ? 'pl-4' : ''
51+
}
52+
>
53+
<Link href={`#${head?.data?.id}`}> {head.value}</Link>
4954
</li>
5055
))}
5156
</ol>

0 commit comments

Comments
 (0)