Header.tsx 13 KB

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