1
0
Fork 0
freesewing/packages/freesewing.shared/components/layouts/default.js

150 lines
4.3 KiB
JavaScript
Raw Normal View History

2021-12-11 14:04:05 +01:00
import { useState } from 'react'
2021-12-21 20:47:13 +01:00
import { useRouter } from 'next/router'
import Link from 'next/link'
2021-12-11 14:04:05 +01:00
// Shared components
2021-12-21 20:47:13 +01:00
import Logo from 'shared/components/logos/freesewing.js'
import PrimaryNavigation from 'shared/components/navigation/primary'
import get from 'lodash.get'
2021-12-25 14:14:18 +01:00
// Site components
import Header from 'site/components/header'
import Footer from 'site/components/footer'
import Search from 'site/components/search'
2021-12-11 14:04:05 +01:00
const iconSize= 48
2021-12-21 20:47:13 +01:00
const PageTitle = ({ app, slug, title }) => {
if (title) return <h1>{title}</h1>
if (slug) return <h1>{get(app.navigation, slug.split('/')).__title}</h1>
2021-12-11 14:04:05 +01:00
2021-12-21 20:47:13 +01:00
return <h1>FIXME: This page has no title</h1>
}
const Breadcrumbs = ({ app, slug=false, title }) => {
if (!slug) return null
const crumbs = []
const chunks = slug.split('/')
for (const i in chunks) {
const j = parseInt(i)+parseInt(1)
const page = get(app.navigation, chunks.slice(0,j))
2021-12-26 12:52:00 +01:00
if (page) crumbs.push([page.__linktitle, '/'+chunks.slice(0,j).join('/'), (j < chunks.length)])
2021-12-21 20:47:13 +01:00
}
return (
2021-12-27 10:05:40 +01:00
<ul className="flex flex-row flex-wrap gap-2 font-bold">
2021-12-21 20:47:13 +01:00
<li>
<Link href="/">
2021-12-26 16:43:35 +01:00
<a title="To the homepage" className="text-base-content">
<Logo size={24} fill="currentColor" stroke={false}/>
2021-12-21 20:47:13 +01:00
</a>
</Link>
</li>
{crumbs.map(crumb => (
<>
2021-12-26 16:43:35 +01:00
<li className="text-base-content">&raquo;</li>
2021-12-21 20:47:13 +01:00
<li>
{crumb[2]
? (
<Link href={crumb[1]}>
<a title={crumb[0]} className="text-secondary hover:text-secondary-focus">
{crumb[0]}
</a>
</Link>
)
2021-12-26 16:43:35 +01:00
: <span className="text-base-content">{crumb[0]}</span>
2021-12-21 20:47:13 +01:00
}
</li>
</>
))}
</ul>
)
}
const DefaultLayout = ({ app, title=false, children=[], search, setSearch}) => {
2021-12-21 20:47:13 +01:00
const startNavigation = () => {
app.startLoading()
// Force close of menu on mobile if it is open
if (app.primaryNavigation) app.setPrimaryNavigation(false)
// Force close of search modal if it is open
if (search) setSearch(false)
}
2021-12-21 20:47:13 +01:00
const router = useRouter()
router?.events?.on('routeChangeStart', startNavigation)
router?.events?.on('routeChangeComplete', () => app.stopLoading())
2021-12-21 20:47:13 +01:00
const slug = router.asPath.slice(1)
2021-12-11 14:04:05 +01:00
const [leftNav, setLeftNav] = useState(false)
const toggleLeftNav = () => setLeftNav(!leftNav)
return (
<div className={`
flex flex-col justify-between
min-h-screen
bg-base-100
`}>
2021-12-31 10:15:51 +01:00
<Header app={app} setSearch={setSearch} />
2021-12-11 14:04:05 +01:00
<main className={`
grow flex flex-row
2021-12-18 16:26:55 +01:00
gap-2
lg:gap-8
xl:gap-16
2021-12-11 14:04:05 +01:00
`}>
<aside className={`
fixed top-0 right-0
2021-12-21 20:47:13 +01:00
${app.primaryMenu ? '' : 'translate-x-[-100%]'} transition-transform
2021-12-30 18:17:10 +01:00
pt-28
sm:pt-8 sm:mt-16
2021-12-30 18:17:10 +01:00
pb-4 px-2
sm:px-1 md:px-4 lg:px-8 xl:px-16 2xl:px-32
2021-12-11 14:04:05 +01:00
sm:relative sm:transform-none
2021-12-12 18:58:24 +01:00
h-screen w-screen
2021-12-25 14:14:18 +01:00
bg-base-100
2021-12-21 20:47:13 +01:00
sm:bg-base-50
2021-12-11 14:04:05 +01:00
sm:max-w-[38.2%]
sm:flex sm:flex-row-reverse
2021-12-21 20:47:13 +01:00
sm:sticky
overflow-y-scroll
2021-12-26 12:52:00 +01:00
z-20
bg-base-100 text-base-content
sm:bg-neutral sm:bg-opacity-95 sm:text-neutral-content
2021-12-11 14:04:05 +01:00
`}>
2021-12-21 20:47:13 +01:00
<PrimaryNavigation app={app} active={slug}/>
2021-12-11 14:04:05 +01:00
</aside>
2021-12-30 18:17:10 +01:00
<section className={`
max-w-61.8% p-4 pt-24 sm:pt-28 w-full
sm:px-1 md:px-4 lg:px-8 xl:px-16 2xl:px-32
`}>
<div className="max-w-5xl">
{title && (
<>
<Breadcrumbs app={app} slug={slug} title={title} />
<PageTitle app={app} slug={slug} title={title} />
</>
)}
{children}
</div>
2021-12-11 14:04:05 +01:00
</section>
</main>
{search && (
2021-12-31 10:15:51 +01:00
<>
<div className={`
fixed w-full max-h-screen bg-base-100 top-0 z-30 pt-0 pb-16 px-8
sm:rounded-lg sm:top-24
sm:max-w-xl sm:m-auto sm:inset-x-12
md:max-w-2xl
lg:max-w-4xl
`}>
<Search app={app} search={search} setSearch={setSearch}/>
</div>
2021-12-31 10:15:51 +01:00
<div className="fixed top-0 left-0 w-full min-h-screen bg-neutral z-20 bg-opacity-70"></div>
</>
)}
<Footer app={app} />
2021-12-11 14:04:05 +01:00
</div>
)
}
export default DefaultLayout