Header.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import Link from "next/link"
  2. import { FormattedMessage, useIntl } from "react-intl"
  3. import mastodonLogo from "../public/logos/wordmark-white-text.svg"
  4. import Image from "next/legacy/image"
  5. import { useState, useEffect, useRef, useId } from "react"
  6. import classNames from "classnames"
  7. import { locales } from "../data/locales"
  8. import MenuToggle from "./MenuToggle"
  9. import DisclosureArrow from "../public/ui/disclosure-arrow.svg?inline"
  10. import { useRouter } from "next/router"
  11. type HeaderProps = {
  12. /** determines whether the header is transparent on load, before scrolling down */
  13. transparent?: boolean
  14. }
  15. /** Sitewide header and navigation */
  16. const Header = ({ transparent = true }: HeaderProps) => {
  17. const intl = useIntl()
  18. const router = useRouter()
  19. const [pageScrolled, setPageScrolled] = useState(false)
  20. // prettier-ignore
  21. const navigationItems = [
  22. {
  23. value: "/servers",
  24. label: <FormattedMessage id="nav.servers.title" defaultMessage="Servers" />,
  25. }, {
  26. value: "/apps",
  27. label: <FormattedMessage id="nav.apps.title" defaultMessage="Apps" />,
  28. },
  29. {
  30. value: "https://shop.joinmastodon.org/",
  31. label: <div className="flex gap-2 items-center"><FormattedMessage id="nav.merch.title" defaultMessage="Merch" /><span className="bg-goldenrod/[0.5] text-goldenrod c2 px-1.5 rounded border border-goldenrod"><FormattedMessage id='nav.new' defaultMessage='New' /></span></div>,
  32. },
  33. {
  34. value: "/sponsors",
  35. label: <FormattedMessage id="nav.sponsors.title" defaultMessage="Donate" />,
  36. }, {
  37. key: "resources",
  38. label: <FormattedMessage id="nav.resources.title" defaultMessage="Resources" />,
  39. childItems: [
  40. {
  41. value: "https://blog.joinmastodon.org/",
  42. label: <FormattedMessage id="nav.blog.title" defaultMessage="Blog" />,
  43. description: <FormattedMessage id="nav.blog.description" defaultMessage="Get the latest news about the platform" />,
  44. }, {
  45. value: "https://docs.joinmastodon.org",
  46. label: <FormattedMessage id="nav.docs.title" defaultMessage="Documentation" />,
  47. description: <FormattedMessage id="nav.docs.description" defaultMessage="Learn how Mastodon works in-depth" />,
  48. }, {
  49. value: "https://github.com/mastodon/mastodon/discussions",
  50. label: <FormattedMessage id="nav.support.title" defaultMessage="Support" />,
  51. description: <FormattedMessage id="nav.support.description" defaultMessage="Get help or suggest a feature on GitHub" />,
  52. }, {
  53. value: "/branding",
  54. label: <FormattedMessage id="nav.branding.title" defaultMessage="Branding" />,
  55. description: <FormattedMessage id="nav.branding.description" defaultMessage="Download our logos and learn how to use them" />,
  56. },
  57. ],
  58. footer: {
  59. value: "https://github.com/mastodon/mastodon",
  60. label: <FormattedMessage id="nav.code.action" defaultMessage="Browse code" />,
  61. title: <FormattedMessage id="nav.code.title" defaultMessage="Source code" />,
  62. description: <FormattedMessage id="nav.code.description" defaultMessage="Mastodon is free and open-source software" />,
  63. },
  64. }, {
  65. value: "/verification",
  66. label: <FormattedMessage id="nav.verification.title" defaultMessage="Verification" />,
  67. }, {
  68. key: "locale",
  69. label: <span aria-label={intl.formatMessage({
  70. id: "translate_site",
  71. defaultMessage: "文A, Translate site",
  72. })}>文A</span>,
  73. compact: true,
  74. childItems: locales.map((locale) => ({
  75. key: locale.code,
  76. locale: locale.code,
  77. scroll: false,
  78. small: true,
  79. value: "", // current page
  80. label: locale.language,
  81. active: router.locale === locale.code,
  82. })),
  83. }
  84. ]
  85. // set active status on links
  86. .map((item) => ({ ...item, active: router.asPath === item.value }))
  87. const {
  88. mobileMenuOpen,
  89. openMenuIndex,
  90. bindToggle,
  91. bindPrimaryMenu,
  92. bindPrimaryMenuItem,
  93. bindSecondaryMenuItem,
  94. } = useMenu({ navigationItems })
  95. const checkPageScroll = () => {
  96. setPageScrolled(window.scrollY > 0)
  97. }
  98. useEffect(() => {
  99. window.addEventListener("scroll", checkPageScroll)
  100. checkPageScroll()
  101. return () => {
  102. window.removeEventListener("scroll", checkPageScroll)
  103. }
  104. }, [])
  105. return (
  106. <header
  107. // background needs to be on the ::before for now to get around nested compositing bug in chrome
  108. className={classNames(
  109. 'full-width-bg sticky -top-[var(--header-offset)] z-20 -mb-[var(--header-area)] pt-[var(--header-offset)] text-white before:absolute before:inset-0 before:bg-nightshade-900/[0.9] before:backdrop-blur before:transition-opacity before:content-[""]',
  110. pageScrolled || !transparent ? "before:opacity-100" : "before:opacity-0"
  111. )}
  112. >
  113. <div className="full-width-bg__inner flex h-[var(--header-height)] items-center justify-between">
  114. <div>
  115. <Link
  116. href="/"
  117. className="relative z-10 flex max-w-[11.375rem] pt-[6%] md:max-w-[12.625rem]"
  118. >
  119. <Image src={mastodonLogo} alt="Mastodon" />
  120. </Link>
  121. </div>
  122. <nav>
  123. <MenuToggle {...bindToggle()} />
  124. <ul
  125. {...bindPrimaryMenu()}
  126. className={classNames(
  127. "md:ms-0 md:-me-1 fixed inset-0 w-screen flex-col overflow-auto bg-black px-1 pt-[calc(var(--header-area)_+_1rem)] pb-8 md:relative md:w-auto md:flex-row md:gap-1 md:overflow-visible md:rounded-md md:bg-[transparent] md:p-1",
  128. mobileMenuOpen ? "flex" : "hidden md:flex"
  129. )}
  130. >
  131. {navigationItems.map((item, itemIndex) => (
  132. <li className="relative" key={item.key || item.value}>
  133. {"childItems" in item ? (
  134. // Top-level Dropdown
  135. <>
  136. <button
  137. {...bindPrimaryMenuItem(itemIndex, { hasPopup: true })}
  138. className="flex items-center gap-[0.125rem] whitespace-nowrap rounded-md p-3 px-5 text-h5 focus:outline-2 md:text-b2 md:font-medium"
  139. >
  140. {item.label}
  141. <DisclosureArrow
  142. className={classNames({
  143. "rotate-180": openMenuIndex === itemIndex,
  144. })}
  145. />
  146. </button>
  147. <div
  148. className={classNames(
  149. "end-0 top-full rounded-md md:absolute md:max-h-[calc(100vh_-_var(--header-height))] md:bg-white md:text-black md:shadow-lg",
  150. openMenuIndex === itemIndex ? "overflow-auto" : "hidden"
  151. )}
  152. >
  153. <ul
  154. role="menu"
  155. className={classNames(
  156. item.compact
  157. ? "py-2 md:px-2"
  158. : "w-screen max-w-md py-2 md:grid md:max-w-lg md:grid-cols-2 md:gap-6 md:px-3 md:py-4"
  159. )}
  160. >
  161. {item.childItems.map((child, childIndex) => (
  162. // Child Items
  163. <li key={child.key || child.value} role="menu">
  164. <Link
  165. href={child.value}
  166. locale={child.locale || undefined}
  167. scroll={child.scroll ?? true}
  168. {...bindSecondaryMenuItem(child)}
  169. className={classNames(
  170. "block rounded-md hover:md:bg-nightshade-50",
  171. item.compact
  172. ? "py-2 px-5 md:px-4"
  173. : "py-3 px-5 md:px-4",
  174. item.compact && child.active && "font-extrabold"
  175. )}
  176. aria-current={child.active ? "page" : undefined}
  177. >
  178. <span
  179. className={classNames(
  180. "block",
  181. !item.compact && "font-extrabold"
  182. )}
  183. >
  184. {child.label}
  185. </span>
  186. <span className="mt-1 block font-extranormal text-gray-1">
  187. {child.description}
  188. </span>
  189. </Link>
  190. </li>
  191. ))}
  192. </ul>
  193. {item.footer && (
  194. <div className="md:bg-gray-4 md:p-4">
  195. <a
  196. href={item.footer.value}
  197. className="group flex items-center justify-between rounded-md px-5 py-3 md:p-2"
  198. >
  199. <span>
  200. <span className="font-extrabold">
  201. {item.footer.title}
  202. </span>
  203. <span className="mt-1 block font-extranormal text-gray-1">
  204. {item.footer.description}
  205. </span>
  206. </span>
  207. <span className="b3 hidden h-12 items-center justify-center rounded-md border-2 border-blurple-500 bg-blurple-500 p-4 !font-semibold text-white transition-colors group-hover:border-blurple-600 group-hover:bg-blurple-600 md:flex">
  208. {item.footer.label}
  209. </span>
  210. </a>
  211. </div>
  212. )}
  213. </div>
  214. </>
  215. ) : (
  216. // Top-level Link
  217. <Link
  218. href={item.value}
  219. className={classNames(
  220. "block whitespace-nowrap rounded-md p-3 px-5 text-h5 font-medium md:text-b2",
  221. item.active && "font-extrabold"
  222. )}
  223. aria-current={item.active ? "page" : undefined}
  224. {...bindPrimaryMenuItem(itemIndex)}
  225. >
  226. {item.label}
  227. </Link>
  228. )}
  229. </li>
  230. ))}
  231. </ul>
  232. </nav>
  233. </div>
  234. </header>
  235. )
  236. }
  237. /**
  238. * `useMenu` provides a React Hook for managing menu state and attributes for accessibility.
  239. */
  240. const useMenu = ({ navigationItems }) => {
  241. const menuId = useId()
  242. const rootElement = useRef<HTMLUListElement>(null)
  243. const [mobileMenuOpen, setMobileMenuOpen] = useState(false)
  244. /** `null` means the secondary menu is closed */
  245. const [openMenuIndex, setOpenMenuIndex] = useState<number | null>(null)
  246. const secondaryMenuOpen = openMenuIndex !== null
  247. // check for clicks outside of the menu
  248. useEffect(() => {
  249. const handleClickOutside = (e: MouseEvent) => {
  250. if (!rootElement.current.contains(e.target as Node)) {
  251. setOpenMenuIndex(null)
  252. }
  253. }
  254. if (rootElement.current) {
  255. document.addEventListener("click", handleClickOutside, false)
  256. }
  257. return () => {
  258. document.removeEventListener("click", handleClickOutside, false)
  259. }
  260. }, [])
  261. // Element attributes / listeners
  262. const bindToggle = () => ({
  263. open: mobileMenuOpen,
  264. attributes: {
  265. "aria-expanded": mobileMenuOpen,
  266. "aria-controls": menuId,
  267. },
  268. onClick: () => setMobileMenuOpen(!mobileMenuOpen),
  269. })
  270. const bindPrimaryMenu = () => {
  271. return {
  272. ref: rootElement,
  273. id: menuId,
  274. onBlur: (e) => {
  275. const focusLeftMenu = !rootElement.current.contains(e.relatedTarget)
  276. /*if (focusLeftMenu) {
  277. setOpenMenuIndex(null)
  278. setMobileMenuOpen(false)
  279. }*/
  280. },
  281. onKeyDown: (e) => {
  282. if (e.key === "Escape") {
  283. if (openMenuIndex) {
  284. setOpenMenuIndex(null)
  285. } else {
  286. setMobileMenuOpen(false)
  287. }
  288. }
  289. },
  290. }
  291. }
  292. const bindPrimaryMenuItem = (
  293. itemIndex: number,
  294. { hasPopup } = { hasPopup: false }
  295. ) => {
  296. const isDropdownOpen = openMenuIndex === itemIndex
  297. const isExpanded = hasPopup && isDropdownOpen
  298. return {
  299. "aria-haspopup": hasPopup,
  300. "aria-expanded": hasPopup ? isExpanded : undefined,
  301. onKeyDown: (e: React.KeyboardEvent) => {
  302. if (e.key === "Enter" || e.key === " ") {
  303. if (hasPopup) {
  304. e.preventDefault()
  305. }
  306. setOpenMenuIndex(itemIndex)
  307. }
  308. },
  309. onClick: () => {
  310. if (!hasPopup) {
  311. setMobileMenuOpen(false)
  312. }
  313. },
  314. onMouseDown: () => {
  315. if (hasPopup) {
  316. setOpenMenuIndex(isDropdownOpen ? null : itemIndex)
  317. } else {
  318. setOpenMenuIndex(null)
  319. }
  320. },
  321. }
  322. }
  323. const bindSecondaryMenuItem = (child) => {
  324. return {
  325. onKeyDown: (e) => {
  326. if (e.key === "Escape") {
  327. setOpenMenuIndex(null)
  328. }
  329. },
  330. onClick: () => {
  331. setMobileMenuOpen(false)
  332. },
  333. hrefLang: child.locale || undefined,
  334. lang: child.locale || undefined,
  335. role: "menuitem",
  336. }
  337. }
  338. return {
  339. mobileMenuOpen,
  340. openMenuIndex,
  341. bindToggle,
  342. bindPrimaryMenu,
  343. bindPrimaryMenuItem,
  344. bindSecondaryMenuItem,
  345. secondaryMenuOpen,
  346. }
  347. }
  348. export default Header