Skip to content

Commit 37d3bde

Browse files
Peter Bengtssonrsese
andauthored
simplify Table of Contents (#28833)
* simplify table-of-contents * key * fix rendering tests * Not so much top margin * Map topic ToC links are spans * use p tag * Back to span for the article title * Update comment to match markup * remove hack * use h2 instead * fix tests * fix use of key * use regular className instead Co-authored-by: Robert Sese <[email protected]>
1 parent 7b81003 commit 37d3bde

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed
Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { useRouter } from 'next/router'
21
import cx from 'classnames'
32

43
import { ActionList } from '@primer/react'
54
import { Link } from 'components/Link'
6-
import { BumpLink } from 'components/ui/BumpLink'
75
import type { TocItem } from 'components/context/ProductLandingContext'
86

97
type Props = {
@@ -12,22 +10,43 @@ type Props = {
1210
}
1311
export const TableOfContents = (props: Props) => {
1412
const { items, variant = 'expanded' } = props
15-
const router = useRouter()
13+
14+
const actionItems = (items || []).filter((item) => typeof item !== 'undefined')
1615

1716
return (
1817
<ul
1918
data-testid="table-of-contents"
20-
className={cx(variant === 'compact' ? 'list-style-outside pl-2' : 'list-style-none')}
19+
className={cx(variant === 'compact' ? 'list-style-outside pl-2' : '')}
2120
>
22-
<ActionList variant="full">
23-
{(items || [])
24-
.filter((item) => typeof item !== 'undefined')
25-
.map((item) => {
26-
const { fullPath: href, title, intro, childTocItems } = item
27-
const isActive = router.pathname === href
28-
return variant === 'compact' ? (
21+
{variant === 'expanded' &&
22+
actionItems.map((item) => {
23+
const { fullPath: href, title, intro } = item
24+
25+
return (
26+
<li
27+
key={href}
28+
data-testid="expanded-item"
29+
className="pt-4 pb-3 f4 d-list-item width-full list-style-none border-bottom"
30+
>
31+
<h2 className="py-1 h4">
32+
<Link href={href} className="color-fg-accent">
33+
{title}
34+
</Link>
35+
</h2>
36+
{intro && (
37+
<p className="f4 color-fg-muted" dangerouslySetInnerHTML={{ __html: intro }} />
38+
)}
39+
</li>
40+
)
41+
})}
42+
43+
{variant === 'compact' && (
44+
<ActionList>
45+
{actionItems.map((item) => {
46+
const { fullPath: href, title, childTocItems } = item
47+
return (
2948
<ActionList.Item key={href}>
30-
<div className="f4 d-list-item width-full list-style-none">
49+
<li className="f4 d-list-item width-full list-style-none">
3150
<Link className="d-block width-full text-underline" href={href}>
3251
{title}
3352
</Link>
@@ -54,24 +73,12 @@ export const TableOfContents = (props: Props) => {
5473
})}
5574
</ul>
5675
)}
57-
</div>
58-
</ActionList.Item>
59-
) : (
60-
<ActionList.Item key={href} className={cx('border-bottom')}>
61-
<div className={cx('mt-2', isActive && 'color-fg-muted')}>
62-
<BumpLink as={Link} href={href} title={<h2 className="py-1 h4">{title}</h2>}>
63-
{intro && (
64-
<p
65-
className="f4 color-fg-muted"
66-
dangerouslySetInnerHTML={{ __html: intro }}
67-
/>
68-
)}
69-
</BumpLink>
70-
</div>
76+
</li>
7177
</ActionList.Item>
7278
)
7379
})}
74-
</ActionList>
80+
</ActionList>
81+
)}
7582
</ul>
7683
)
7784
}

components/landing/TocLanding.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const TocLanding = () => {
7878

7979
<div className="border-bottom border-xl-0 pb-4 mb-5 pb-xl-2 mb-xl-2" />
8080

81-
<div className={variant === 'expanded' ? 'mt-7' : 'mt-2'}>
81+
<div className={variant === 'expanded' ? 'mt-5' : 'mt-2'}>
8282
{featuredLinks.gettingStarted && featuredLinks.popular && (
8383
<div className="pb-8 container-xl">
8484
<div className="gutter gutter-xl-spacious clearfix">

tests/rendering/server.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -718,13 +718,13 @@ describe('server', () => {
718718
expect($('[data-testid=table-of-contents] ul li a').length).toBeGreaterThan(5)
719719
})
720720

721-
test('map topic renders with h2 links to articles', async () => {
721+
test('map topic renders with links to articles', async () => {
722722
const $ = await getDOM(
723723
'/en/get-started/importing-your-projects-to-github/importing-source-code-to-github'
724724
)
725725
expect(
726726
$(
727-
'a[href="https://github.com/en/get-started/importing-your-projects-to-github/importing-source-code-to-github/about-github-importer"] h2'
727+
'li h2 a[href="https://github.com/en/get-started/importing-your-projects-to-github/importing-source-code-to-github/about-github-importer"]'
728728
).length
729729
).toBe(1)
730730
})
@@ -733,15 +733,18 @@ describe('server', () => {
733733
const $ = await getDOM(
734734
'/en/get-started/importing-your-projects-to-github/importing-source-code-to-github'
735735
)
736-
const $bumpLinks = $('[data-testid=bump-link]')
737-
expect($bumpLinks.length).toBeGreaterThan(3)
736+
const $links = $('[data-testid=expanded-item]')
737+
expect($links.length).toBeGreaterThan(3)
738738
})
739739

740740
test('map topic intros are parsed', async () => {
741741
const $ = await getDOM(
742742
'/en/get-started/importing-your-projects-to-github/importing-source-code-to-github'
743743
)
744-
const $intro = $('[data-testid=bump-link][href*="source-code-migration-tools"] > p')
744+
const $parent = $('[data-testid=expanded-item] a[href*="source-code-migration-tools"]')
745+
.parent()
746+
.parent()
747+
const $intro = $('p', $parent)
745748
expect($intro.length).toBe(1)
746749
expect($intro.html()).toContain('You can use external tools to move your projects to GitHub')
747750
})

0 commit comments

Comments
 (0)