1
0
Fork 0
freesewing/sites/shared/components/navigation/primary.mjs

331 lines
10 KiB
JavaScript
Raw Normal View History

import { NavigationContext } from 'shared/context/navigation-context.mjs'
import { useContext } from 'react'
2021-12-12 18:58:24 +01:00
import Link from 'next/link'
2021-12-16 19:01:37 +01:00
import orderBy from 'lodash.orderby'
2023-03-26 16:50:46 +02:00
import {
2023-04-09 15:57:25 +02:00
CommunityIcon,
DesignIcon,
DocsIcon,
2023-03-26 16:50:46 +02:00
RssIcon,
ShowcaseIcon,
UserIcon,
2023-04-30 21:24:35 +02:00
MeasureIcon,
PageIcon,
CodeIcon,
I18nIcon,
WrenchIcon,
FreeSewingIcon,
HeartIcon,
2023-05-22 19:53:24 +02:00
BulletIcon,
2023-03-26 16:50:46 +02:00
} from 'shared/components/icons.mjs'
import { Breadcrumbs } from 'shared/components/breadcrumbs.mjs'
2021-12-12 18:58:24 +01:00
2023-03-28 16:47:07 +02:00
export const ns = ['sections']
// List of icons matched to top-level slug
2023-04-09 15:57:25 +02:00
export const icons = {
2023-03-26 16:50:46 +02:00
// FreeSewing.dev
api: (className = '') => <DocsIcon className={className} />,
design: (className = '') => <DesignIcon className={className} />,
contribute: (className = '') => <CodeIcon className={className} />,
i18n: (className = '') => <I18nIcon className={className} />,
infra: (className = '') => <WrenchIcon className={className} stroke={1.5} />,
about: (className = '') => <FreeSewingIcon className={className} stroke={1.5} />,
support: (className = '') => <HeartIcon className={className} stroke={1.5} fill />,
2023-03-26 16:50:46 +02:00
// FreeSewing.org
2023-04-09 15:57:25 +02:00
account: (className = '') => <UserIcon className={className} />,
2023-03-26 16:50:46 +02:00
blog: (className = '') => <RssIcon className={className} stroke={3} />,
2023-04-09 15:57:25 +02:00
designs: (className = '') => <DesignIcon className={className} stroke={3} />,
2023-03-26 16:50:46 +02:00
docs: (className = '') => <DocsIcon className={className} />,
2023-04-09 15:57:25 +02:00
showcase: (className = '') => <ShowcaseIcon className={className} />,
community: (className = '') => <CommunityIcon className={className} />,
2023-04-30 21:24:35 +02:00
sets: (className = '') => <MeasureIcon className={className} />,
patterns: (className = '') => <PageIcon className={className} />,
}
2021-12-25 13:43:41 +01:00
/* helper method to order nav entries */
const order = (obj) => orderBy(obj, ['o', 't'], ['asc', 'asc'])
2021-12-25 13:43:41 +01:00
2021-12-17 17:51:20 +01:00
// Component for the collapse toggle
// Exported for re-use
export const Chevron = ({ w = 8, m = 2 }) => (
<svg
className={`
fill-current opacity-75 w-${w} h-${w} mr-${m}
details-toggle hover:text-secondary sm:hover:text-secondary
`}
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
>
<path d="M12.95 10.707l.707-.707L8 4.343 6.586 5.757 10.828 10l-4.242 4.243L8 15.657l4.95-4.95z" />
</svg>
)
2021-12-17 17:51:20 +01:00
// Helper method to filter out the real children
const currentChildren = (current) =>
Object.values(order(current)).filter((entry) => typeof entry === 'object')
2021-12-16 19:01:37 +01:00
2021-12-21 20:47:13 +01:00
// Shared classes for links
// Exported for re-use
export const linkClasses = `
py-1
text-base text-base-content sm:text-base-content
hover:text-secondary
sm:hover:text-secondary
`
// Figure out whether a page is on the path to the active page
2023-04-09 15:57:25 +02:00
export const isActive = (slug, active) => {
2023-04-07 17:10:54 +02:00
if (!slug) return false
if (slug === active) return true
let result = true
const slugParts = slug.split('/')
const activeParts = active.split('/')
for (const i in slugParts) {
if (slugParts[i] !== activeParts[i]) result = false
}
return result
}
2023-05-22 15:43:41 +02:00
const hasChildren = (page) => {
const keys = new Set([...Object.keys(page)])
for (const key of ['t', 's']) keys.delete(key)
return keys.size
}
2021-12-17 17:51:20 +01:00
// Component that renders a sublevel of navigation
const SubLevel = ({ nodes = {}, active = '' }) => (
2021-12-21 20:47:13 +01:00
<ul className="pl-5 list-inside">
{currentChildren(nodes).map((child) =>
2023-05-22 15:43:41 +02:00
hasChildren(child) ? (
<li key={child.s} className="flex flex-row">
<details className="grow" open={isActive(child.s, active)}>
<summary
className={`
2021-12-21 20:47:13 +01:00
flex flex-row
px-2
text-base-content
sm:text-base-content
2021-12-17 17:51:20 +01:00
hover:cursor-row-resize
2021-12-21 20:47:13 +01:00
items-center
`}
>
<Link
2023-03-26 16:50:46 +02:00
href={`/${child.s}`}
title={child.t}
className={`
grow pl-2 border-l-2
${linkClasses}
hover:cursor-pointer
hover:border-secondary
sm:hover:border-secondary
${
child.s === active
? 'text-secondary border-secondary sm:text-secondary sm:border-secondary'
: 'text-base-content sm:text-base-content'
}
`}
>
<span className={`${linkClasses} grow hover:cursor-pointer`}>
<span
className={`
text-3xl mr-2 inline-block p-0 leading-3
${
child.s === active
? 'text-secondary sm:text-secondary translate-y-1'
: 'translate-y-3'
}
`}
>
{child.s === active ? <>&bull;</> : <>&deg;</>}
</span>
<span className={child.s === active ? 'font-bold' : ''}>{child.t}</span>
</span>
2021-12-21 20:47:13 +01:00
</Link>
<Chevron w={6} m={3} />
2021-12-17 17:51:20 +01:00
</summary>
<SubLevel nodes={child} active={active} />
2021-12-17 17:51:20 +01:00
</details>
</li>
) : (
<li className="pl-2 flex flex-row items-center" key={child.s}>
<Link
2023-03-26 16:50:46 +02:00
href={`/${child.s}`}
title={child.t}
className={`
pl-2 border-l-2
grow
${linkClasses}
hover:cursor-pointer
hover:border-secondary
sm:hover:border-secondary
${
child.s === active
? 'text-secondary border-secondary sm:text-secondary sm:border-secondary'
: 'text-base-content sm:text-base-content'
}`}
>
<span className={`${linkClasses} hover:cursor-pointer`}>
<span
className={`
text-3xl mr-2 inline-block p-0 leading-3
${
child.s === active
? 'text-secondary sm:text-secondary translate-y-1'
: 'translate-y-3'
}
`}
>
{child.s === active ? <>&bull;</> : <>&deg;</>}
</span>
<span className={child.s === active ? 'font-bold' : ''}>{child.t}</span>
</span>
2021-12-17 17:51:20 +01:00
</Link>
</li>
)
)}
</ul>
)
2021-12-16 19:01:37 +01:00
2022-05-12 10:27:42 +02:00
export const Icons = ({
ulClasses = '',
linkClasses = `grow text-lg lg:text-xl py-1 text-base-content sm:text-base-content
2022-05-12 10:27:42 +02:00
hover:text-secondary sm:hover:text-secondary hover:cursor-pointer
2022-06-06 14:06:49 -05:00
flex flex-col items-center`,
linkStyle = {},
2022-05-12 10:27:42 +02:00
}) => {
const { nav } = useContext(NavigationContext)
if (!nav) return null
const output = []
for (const page of order(nav)) {
output.push(
<li key={page.s}>
2023-03-26 16:50:46 +02:00
<Link href={`/${page.s}`} className={linkClasses} title={page.t} style={linkStyle}>
2023-05-22 19:53:24 +02:00
{icons[page.s] ? icons[page.s]('w-14 h-14') : <BulletIcon />}
<span className="font-bold">{page.t}</span>
</Link>
</li>
)
}
2022-05-12 10:27:42 +02:00
return <ul className={ulClasses}>{output}</ul>
2021-12-12 18:58:24 +01:00
}
2023-05-22 19:53:24 +02:00
export const Spacer = () => (
<hr
className={`pb-2 mt-2 border-2 border-solid border-neutral
border-b-0 border-r-0 border-l-0`}
/>
)
export const MainSections = () => {
const { sections = false, slug } = useContext(NavigationContext)
if (!sections) return null
2023-03-28 16:47:07 +02:00
// Ensure each page as an `o` key so we can put them in order
const sortableSections = sections.map((s) => ({ ...s, o: s.o ? s.o : s.t }))
const output = []
2023-03-28 16:47:07 +02:00
for (const page of orderBy(sortableSections, ['o', 't'])) {
const act = isActive(page.s, slug)
const txt = (
<>
{icons[page.s] ? (
2023-05-22 19:53:24 +02:00
icons[page.s](`w-6 h-6 ${act ? 'text-base-100 opacity-70' : ''}`)
) : (
2023-05-22 19:53:24 +02:00
<BulletIcon fill={act} className={`w-6 h-6 ${act ? 'text-base-100 opacity-70' : ''}`} />
)}
<span className={`font-bold ${act ? 'text-secondary-content' : ''}`}>{page.t}</span>
</>
)
2023-05-22 19:53:24 +02:00
const item =
page.t === 'spacer' ? (
<li key={page.s} className="opacity-10">
<Spacer />
</li>
) : (
<li key={page.s}>
{act ? (
<span
className={`
flex flex-row gap-4 items-center
text-secondary-content
hover:text-base-content
bg-secondary
p-2 px-4 rounded
bg-base-200
rounded-none
`}
title={page.t}
>
{txt}
</span>
) : (
<Link
href={`/${page.s}`}
className={`
flex flex-row gap-4 items-center
2023-05-22 19:53:24 +02:00
hover:bg-secondary hover:bg-opacity-25 hover:cursor-pointer
p-2 px-4 rounded
rounded-none
`}
2023-05-22 19:53:24 +02:00
title={page.t}
>
{txt}
</Link>
)}
</li>
)
output.push(item)
}
return <ul>{output}</ul>
}
const getCrumb = (index, crumbs) => crumbs[index].s.split('/').pop()
export const ActiveSection = () => {
// Get navigation context
2023-05-19 16:31:28 +02:00
const { crumbs = [], nav = {}, slug } = useContext(NavigationContext)
2023-04-16 16:19:49 +02:00
// Don't bother if we don't know where we are
if (!crumbs || !Array.isArray(crumbs) || crumbs.length < 1) return null
2023-04-16 16:19:49 +02:00
let slice = 1
let nodes = nav
2023-04-16 16:19:49 +02:00
// Some sections are further trimmed
2023-05-19 16:31:28 +02:00
if (crumbs[0].s === 'docs') {
if (crumbs.length > 1 && crumbs[1].s === 'docs/faq') {
slice = 2
nodes = nav[getCrumb(1, crumbs)]
} else if (crumbs.length === 2) {
2023-04-16 16:19:49 +02:00
slice = 2
nodes = nav[getCrumb(1, crumbs)]
2023-04-16 16:19:49 +02:00
} else if (
crumbs.length === 4 &&
crumbs[1].s === 'docs/patterns' &&
crumbs[3].s.split('/').pop() === 'options'
2023-04-16 16:19:49 +02:00
) {
slice = 4
nodes = nav[getCrumb(1, crumbs)][getCrumb(2, crumbs)][getCrumb(3, crumbs)]
} else if (crumbs.length > 2 && crumbs[1].s === 'docs/patterns') {
2023-04-16 16:19:49 +02:00
slice = 3
nodes = nav[getCrumb(1, crumbs)][getCrumb(2, crumbs)]
2023-04-16 16:19:49 +02:00
}
}
return (
<div>
2023-05-19 16:31:28 +02:00
<div className="pl-4 my-2">
<Breadcrumbs crumbs={crumbs.slice(0, slice)} />
</div>
2023-04-16 16:19:49 +02:00
<div className="pr-2">
<SubLevel hasChildren={1} nodes={nodes} active={slug} />
</div>
</div>
2023-04-16 16:19:49 +02:00
)
}