/** @jsxImportSource @emotion/react */ 'use client' import { createContext, useContext, useCallback, useEffect, useState, useRef, type ReactNode, type RefObject, } from 'react' import { useRouter, usePathname } from 'next/navigation' import Link from 'next/link' import debounce from 'lodash.debounce' import { BREAK_POINTS } from '../lib/media.ts' import { useMediaQuery } from '../lib/media.ts' import { SearchField } from './primitives/SearchField.tsx' import { Highlight } from './primitives/Highlight.tsx' import { Wrapper } from './primitives/Wrapper.tsx' import { Hamburger } from './icons/Hamburger.tsx' import { Button } from './primitives/Button.tsx' import { NavItem } from './docs/Navigation.tsx' import { ThemeToggle } from './ThemeToggle.tsx' import { Keystone } from './icons/Keystone.tsx' import { MobileMenu } from './MobileMenu.tsx' import { GitHub } from './icons/GitHub.tsx' import { ArrowR } from './icons/ArrowR.tsx' // TODO: Add in search for mobile via this button // import { Search } from './icons/Search'; type HeaderContextType = { mobileNavIsOpen: boolean; desktopOpenState: number } const HeaderContext = createContext({ mobileNavIsOpen: false, desktopOpenState: -1, }) export const useHeaderContext = () => useContext(HeaderContext) function Logo() { const mq = useMediaQuery() return (
Keystone 6
) } function useCurrentSection() { const pathname = usePathname() const check = (candidate: string) => pathname?.startsWith(candidate) if (['/updates', '/releases'].some(check)) return '/updates' if (['/why-keystone', '/for-'].some(check)) return '/why-keystone' if (['/docs'].some(check)) return '/docs' if (['/blog'].some(check)) return '/blog' } function LinkItem({ children, href, target, rel, }: { children: ReactNode href: string target?: string rel?: string }) { const mq = useMediaQuery() const currentSection = useCurrentSection() const isActive = href === currentSection return ( {children} ) } function useClickOutside(ref: RefObject, cb: () => void) { useEffect(() => { /** * Alert if clicked on outside of element */ function handleClickOutside(event: MouseEvent) { if (ref.current && !ref.current.contains(event.target as Node)) { cb() } } document.addEventListener('mousedown', handleClickOutside) return () => { document.removeEventListener('mousedown', handleClickOutside) } }, [ref, cb]) } function FlatMenu({ label, items = [], }: { label: string items: Array<{ label: string; href: string }> }) { const mq = useMediaQuery() const menuRef = useRef(null) const [showContent, setShowContent] = useState(false) const onClickHandler = useCallback(() => { setShowContent(b => !b) }, [setShowContent]) const closeMenu = useCallback(() => { if (showContent === true) { setShowContent(false) } }, [showContent, setShowContent]) useClickOutside(menuRef, closeMenu) return (
{items.map(({ href, label }) => { return ( {label} ) })}
) } export function Header() { const mq = useMediaQuery() const router = useRouter() const pathname = usePathname() const menuRef = useRef(null) const headerRef = useRef(null) const [mobileNavIsOpen, setMobileNavIsOpen] = useState(false) const [desktopOpenState, setDesktopOpenState] = useState(-1) useEffect(() => { const listener = () => { setMobileNavIsOpen(false) setDesktopOpenState(-1) const width = Math.max( document.body.scrollWidth, document.documentElement.scrollWidth, document.body.offsetWidth, document.documentElement.offsetWidth, document.documentElement.clientWidth ) if (width > BREAK_POINTS.sm) { setDesktopOpenState(-1) } else { setDesktopOpenState(-1) } } window.addEventListener('resize', debounce(listener, 130)) return () => { window.removeEventListener('resize', debounce(listener, 130)) } }, [setDesktopOpenState]) useEffect(() => { document.body.style.overflow = 'auto' // search - init field const searchAttempt = 0 // @ts-expect-error document.getElementById('search-field').disabled = true const loadSearch = (searchAttempt: number) => { // @ts-expect-error if (window.docsearch && searchAttempt < 10) { // @ts-expect-error document.getElementById('search-field').disabled = false // @ts-expect-error window.docsearch({ appId: 'N3ZF861Q5G', apiKey: 'f52fa93b9068fe8824beab5727ae84a1', indexName: 'keystonejs', inputSelector: '#search-field', algoliaOptions: { facetFilters: ['tags:stable'], }, transformData: (results: any) => { if (window.location.hostname == 'keystonejs.com') return results return results.map((result: object) => { // @ts-expect-error result.url = result.url.replace('https://keystonejs.com', window.location.origin) return result }) }, }) } else if (searchAttempt >= 10) { // @ts-expect-error document.getElementById('search-field-container').style.visibility = 'hidden' } else { setTimeout(() => loadSearch(searchAttempt++), 500) } } // yoo hooo loadSearch(searchAttempt) // search - keyboard shortcut const keysPressed: { [key: KeyboardEvent['key']]: boolean } = {} document.body.addEventListener('keydown', event => { // If we're typing in an input, don't ever focus the search input if ( document.activeElement && ['TEXTAREA', 'INPUT'].includes(document.activeElement.nodeName) ) { return } keysPressed[event.key] = true if (keysPressed['Meta'] && event.key == 'k') { event.preventDefault() document.getElementById('search-field')?.focus() } }) document.body.addEventListener('keyup', event => { delete keysPressed[event.key] }) }, []) const handleOpen = useCallback(() => { setMobileNavIsOpen(true) document.body.style.overflow = 'hidden' document.getElementById('mobile-menu-close-btn')?.focus() }, []) const handleClose = useCallback(() => { setMobileNavIsOpen(false) document.body.style.overflow = 'auto' document.getElementById('skip-link-navigation-btn')?.focus() }, []) useEffect(() => { handleClose() }, [pathname]) return (
{/* TODO: Add in search for mobile via this button */} {/* */} {/* TODO: This will be docs link once we add demo */} {/* Docs */} Blog Enterprise ↗
) }