generated from acdh-oeaw/template-app-next
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
550caff
commit a038a52
Showing
5 changed files
with
160 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
"use client"; | ||
import { useTranslations } from "next-intl"; | ||
import { type ReactNode, useId, useState } from "react"; | ||
|
||
import { AppNavLink } from "@/components/app-nav-link"; | ||
import type { LinkProps } from "@/components/link"; | ||
import { createHref } from "@/lib/create-href"; | ||
import { otherCategories, proseCategories } from "@/lib/model"; | ||
import { usePathname } from "@/lib/navigation"; | ||
|
||
import { DisclosureButton } from "./disclosure-button"; | ||
|
||
export function AppHeaderNavMenu(): ReactNode { | ||
const t = useTranslations("AppHeader"); | ||
const catt = useTranslations("BernhardCategories"); | ||
|
||
const links = { | ||
home: { href: createHref({ pathname: "/" }), label: t("links.home") }, | ||
languages: { | ||
href: createHref({ pathname: "/languages" }), | ||
label: t("links.languages"), | ||
}, | ||
translators: { | ||
href: createHref({ pathname: "/translators" }), | ||
label: t("links.translators"), | ||
}, | ||
search: { | ||
href: createHref({ pathname: "/search" }), | ||
label: t("links.search"), | ||
}, | ||
} satisfies Record<string, { href: LinkProps["href"]; label: string }>; | ||
|
||
const worksMenu = useId(); | ||
const proseMenu = useId(); | ||
|
||
// disclosures might start off open if we first land on a work page | ||
const pathname = usePathname(); | ||
const [proseMenuOpen, setProseMenuOpen] = useState( | ||
proseCategories.some((c) => { | ||
return pathname.startsWith(`/works/${c}`); | ||
}), | ||
); | ||
const [worksMenuOpen, setWorksMenuOpen] = useState( | ||
proseMenuOpen || | ||
otherCategories.some((c) => { | ||
return pathname.startsWith(`/works/${c}`); | ||
}), | ||
); | ||
|
||
return ( | ||
<nav aria-label={t("navigation-primary")}> | ||
<ul className="flex h-10 items-center gap-4 text-sm" role="list"> | ||
{Object.entries(links).map(([id, link]) => { | ||
return ( | ||
<li key={id}> | ||
<AppNavLink | ||
href={link.href} | ||
onClick={() => { | ||
setWorksMenuOpen(false); | ||
}} | ||
> | ||
{link.label} | ||
</AppNavLink> | ||
</li> | ||
); | ||
})} | ||
<li> | ||
<DisclosureButton | ||
controls={worksMenu} | ||
label={t("links.works")} | ||
setState={setWorksMenuOpen} | ||
state={worksMenuOpen} | ||
/> | ||
</li> | ||
</ul> | ||
{worksMenuOpen ? ( | ||
<ul className="flex h-10 items-center gap-4 text-sm" id={worksMenu} role="list"> | ||
<li> | ||
<DisclosureButton | ||
controls={proseMenu} | ||
label={catt("prose")} | ||
setState={setProseMenuOpen} | ||
state={proseMenuOpen} | ||
/> | ||
</li> | ||
{otherCategories.map((c) => { | ||
return ( | ||
<AppNavLink | ||
key={c} | ||
href={`/works/${c}`} | ||
onClick={() => { | ||
setProseMenuOpen(false); | ||
}} | ||
> | ||
{catt(c)} | ||
</AppNavLink> | ||
); | ||
})} | ||
</ul> | ||
) : null} | ||
{worksMenuOpen && proseMenuOpen ? ( | ||
<ul className="flex h-10 items-center gap-4 text-sm" id={proseMenu} role="list"> | ||
{proseCategories.map((c) => { | ||
return ( | ||
<AppNavLink key={c} href={`/works/${c}`}> | ||
{catt(c)} | ||
</AppNavLink> | ||
); | ||
})} | ||
</ul> | ||
) : null} | ||
</nav> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { ReactNode } from "react"; | ||
|
||
interface DisclosureButtonProps { | ||
controls: string; | ||
label: string; | ||
state: boolean; | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
setState: Function; | ||
} | ||
|
||
export function DisclosureButton(props: DisclosureButtonProps): ReactNode { | ||
const toggleState = () => { | ||
props.setState(!props.state); | ||
}; | ||
return ( | ||
<button | ||
aria-controls={props.controls} | ||
aria-expanded={props.state} | ||
onClick={toggleState} | ||
type="button" | ||
> | ||
{props.label} | ||
</button> | ||
); | ||
} |