Skip to content

Commit

Permalink
Component page breadcrumbs (primer#569)
Browse files Browse the repository at this point in the history
* fix for breadcrumbs withPrefix

* bumped version

* yarn.lock

* editing component layout to include breadcrumbs

* fixes component pg breadcrumb nav

---------

Co-authored-by: Mike Perrotti <[email protected]>
  • Loading branch information
emilybrick and mperrotti authored Sep 1, 2023
1 parent c5a62a8 commit ef0b103
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/@primer/gatsby-theme-doctocat/nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
- title: Saving
url: /ui-patterns/saving
- title: Components
url: /components
children:
- title: Action bar
url: /components/action-bar
Expand Down
45 changes: 44 additions & 1 deletion src/layouts/component-layout.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
import {HEADER_HEIGHT} from '@primer/gatsby-theme-doctocat/src/components/header'
import PageFooter from '@primer/gatsby-theme-doctocat/src/components/page-footer'
import TableOfContents from '@primer/gatsby-theme-doctocat/src/components/table-of-contents'
import {Box, Heading, Text} from '@primer/react'
import {Box, Heading, Text, Breadcrumbs} from '@primer/react'
import React from 'react'
import {withPrefix} from 'gatsby'
import {BaseLayout} from '../components/base-layout'
import {ComponentPageNav} from '../components/component-page-nav'
import navItems from '@primer/gatsby-theme-doctocat/src/nav.yml'

type NavItemData = {
title: string,
url?: string,
children?: NavItemData[]
}

export default function ComponentLayout({pageContext, children, path}) {
const {title, description, reactId, railsIds, figmaId, cssId} = pageContext.frontmatter

const getPageAncestry = (url: string, object: NavItemData[]) => {
const result: NavItemData[] = []
const buildArray = (node: NavItemData, path: string) => {
if (node.url === path) {
result.push({title: node.title, url: node.url})
} else if (node.children) {
for (const child of node.children) {
buildArray(child, path)
if (result.length > 0) {
result.unshift({title: node.title, url: node.url})
break
}
}
}
}
for (const node of object) {
buildArray(node, url)
if (result.length > 0) {
break
}
}
return result
}

const breadcrumbData = getPageAncestry(path, navItems).filter(item => item.url)

return (
<BaseLayout title={title} description={description}>
<Box sx={{maxWidth: 1200, width: '100%', p: [4, 5, 6, 7], mx: 'auto'}}>
{breadcrumbData.length > 1 ? (
<Breadcrumbs sx={{mb: 4}}>
{breadcrumbData.map(item => item.url ? (
<Breadcrumbs.Item key={item.url} href={withPrefix(item.url)} selected={path === item.url}>
{item.title}
</Breadcrumbs.Item>
): null).filter(item => item)}
</Breadcrumbs>
) : null}
<Heading as="h1" sx={{fontSize: 7}}>{title}</Heading>
{description ? (
<Text as="p" sx={{fontSize: 3, m: 0, mb: 3, maxWidth: '60ch'}}>
Expand Down

0 comments on commit ef0b103

Please sign in to comment.