MCPcopy Create free account
hub / github.com/github/docs / SidebarProduct

Function SidebarProduct

components/sidebar/SidebarProduct.tsx:11–179  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9import { ProductCollapsibleSection } from './ProductCollapsibleSection'
10
11export const SidebarProduct = () => {
12 const router = useRouter()
13 const sidebarRef = useRef<HTMLUListElement>(null)
14 const { currentProduct, currentProductTree } = useMainContext()
15 const { t } = useTranslation(['products'])
16 const isRestPage = currentProduct && currentProduct.id === 'rest'
17
18 useEffect(() => {
19 const activeArticle = document.querySelector('[data-is-current-page=true]')
20 // Setting to the top doesn't give enough context of surrounding categories
21 activeArticle?.scrollIntoView({ block: 'center' })
22 // scrollIntoView affects some articles that are very low in the sidebar
23 // The content scrolls down a bit. This sets the article content back up
24 // top unless the route contains a link heading.
25 if (!router.asPath.includes('#')) window?.scrollTo(0, 0)
26 }, [])
27
28 if (!currentProductTree) {
29 return null
30 }
31
32 // remove query string and hash
33 const routePath = `/${router.locale}${router.asPath.split('?')[0].split('#')[0]}`
34
35 const hasExactCategory = !!currentProductTree?.childPages.find(({ href }) =>
36 routePath.includes(href)
37 )
38
39 const productSection = () => (
40 <li className="mt-2 mb-3" data-testid="product-sidebar-items">
41 <ul className="list-style-none">
42 {currentProductTree &&
43 currentProductTree.childPages.map((childPage, i) => {
44 const isStandaloneCategory = childPage.documentType === 'article'
45
46 const childTitle = childPage.shortTitle || childPage.title
47 const isActive =
48 routePath.includes(childPage.href + '/') || routePath === childPage.href
49 const defaultOpen = hasExactCategory ? isActive : false
50 return (
51 <li
52 key={childPage.href + i}
53 data-is-active-category={isActive}
54 data-is-current-page={isActive && isStandaloneCategory}
55 className={cx('py-1', isActive && 'color-bg-inset')}
56 >
57 {isStandaloneCategory ? (
58 <Link
59 href={childPage.href}
60 className="pl-4 pr-2 py-2 d-block flex-auto mr-3 color-fg-default no-underline text-bold"
61 >
62 {childTitle}
63 </Link>
64 ) : (
65 <ProductCollapsibleSection
66 defaultOpen={defaultOpen}
67 routePath={routePath}
68 title={childTitle}

Callers

nothing calls this directly

Calls 4

useMainContextFunction · 0.90
useTranslationFunction · 0.90
restSectionFunction · 0.85
productSectionFunction · 0.85

Tested by

no test coverage detected