|
| 1 | +import { faChevronDown, faChevronUp } from '@fortawesome/free-solid-svg-icons'; |
| 2 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
| 3 | +import { clsx } from 'clsx'; |
| 4 | +import type { FC, HTMLProps } from 'react'; |
| 5 | +import { useEffect } from 'react'; |
| 6 | +import type { LinkProps } from 'react-router'; |
| 7 | +import { Link,useLocation } from 'react-router'; |
| 8 | +import { useToggle } from '../../hooks'; |
| 9 | +import { Button } from '../form'; |
| 10 | +import type { RequiredReactNode } from '../types'; |
| 11 | +import type { DropdownProps } from './Dropdown'; |
| 12 | +import { Dropdown as BaseDropdown } from './Dropdown'; |
| 13 | + |
| 14 | +type ItemProps = { |
| 15 | + active?: boolean; |
| 16 | +}; |
| 17 | + |
| 18 | +const MenuItem: FC<LinkProps & ItemProps> = ({ className, active, ...props }) => ( |
| 19 | + <li role="menuitem" className="tw:w-full tw:flex"> |
| 20 | + <Link |
| 21 | + className={clsx( |
| 22 | + 'tw:px-2 tw:py-3', |
| 23 | + 'tw:max-md:w-full tw:max-md:px-3 tw:max-md:py-2', |
| 24 | + 'tw:text-white tw:no-underline tw:highlight:opacity-100 tw:transition-opacity', |
| 25 | + { |
| 26 | + 'tw:opacity-60': !active, |
| 27 | + 'tw:opacity-100': active, |
| 28 | + }, |
| 29 | + className, |
| 30 | + )} |
| 31 | + {...props} |
| 32 | + /> |
| 33 | + </li> |
| 34 | +); |
| 35 | + |
| 36 | +const Dropdown: FC<Omit<DropdownProps, 'menuAlignment' | 'buttonVariant' | 'menuOffset'> & ItemProps> = ( |
| 37 | + { containerClassName, buttonClassName, menuClassName, active, ...props }, |
| 38 | +) => { |
| 39 | + return ( |
| 40 | + <li role="menuitem" className="tw:w-full tw:flex"> |
| 41 | + <BaseDropdown |
| 42 | + containerClassName={clsx('tw:max-md:w-full', containerClassName)} |
| 43 | + buttonVariant="text" |
| 44 | + buttonClassName={clsx( |
| 45 | + 'tw:px-2 tw:py-3', |
| 46 | + 'tw:max-md:w-full tw:max-md:px-3 tw:max-md:py-2', |
| 47 | + 'tw:text-white tw:highlight:opacity-100 tw:transition-opacity', |
| 48 | + { |
| 49 | + 'tw:opacity-60': !active, |
| 50 | + 'tw:opacity-100': active, |
| 51 | + }, |
| 52 | + buttonClassName, |
| 53 | + )} |
| 54 | + menuAlignment="right" |
| 55 | + menuOffset={-3} |
| 56 | + menuClassName={clsx('tw:mx-2', menuClassName)} |
| 57 | + {...props} |
| 58 | + /> |
| 59 | + </li> |
| 60 | + ); |
| 61 | +}; |
| 62 | + |
| 63 | +export type NavBarProps = HTMLProps<HTMLElement> & { |
| 64 | + brand: RequiredReactNode; |
| 65 | +}; |
| 66 | + |
| 67 | +export const BaseNavBar: FC<NavBarProps> = ({ className, brand, children }) => { |
| 68 | + const { flag: menuOpen, toggle: toggleMenu, setToFalse: closeMenu } = useToggle(false, true); |
| 69 | + const { pathname } = useLocation(); |
| 70 | + |
| 71 | + // In mobile devices, collapse the navbar when the pathname changes |
| 72 | + useEffect(() => closeMenu(), [pathname, closeMenu]); |
| 73 | + |
| 74 | + return ( |
| 75 | + <nav |
| 76 | + className={clsx( |
| 77 | + 'tw:w-full tw:relative', |
| 78 | + 'tw:bg-lm-main tw:dark:bg-dm-main', |
| 79 | + 'tw:flex tw:max-md:flex-col tw:items-center tw:justify-between', |
| 80 | + className, |
| 81 | + )} |
| 82 | + > |
| 83 | + <div className="tw:w-full tw:relative"> |
| 84 | + <h4 |
| 85 | + className={clsx( |
| 86 | + 'tw:text-white tw:px-4 tw:py-3', |
| 87 | + 'tw:max-md:w-full tw:max-md:flex tw:max-md:flex-col tw:items-center', |
| 88 | + )} |
| 89 | + > |
| 90 | + {brand} |
| 91 | + </h4> |
| 92 | + <Button |
| 93 | + variant="secondary" |
| 94 | + className={clsx( |
| 95 | + 'tw:absolute tw:right-0 tw:top-[50%] tw:translate-y-[-50%]', |
| 96 | + 'tw:md:hidden tw:mx-2 tw:[&]:px-2', |
| 97 | + 'tw:opacity-60 tw:highlight:opacity-100 tw:transition-opacity', |
| 98 | + 'tw:[&]:text-inherit tw:[&]:border-white tw:[&]:highlight:bg-transparent', |
| 99 | + )} |
| 100 | + onClick={toggleMenu} |
| 101 | + aria-label={`${menuOpen ? 'Hide' : 'Show'} menu`} |
| 102 | + > |
| 103 | + <FontAwesomeIcon icon={menuOpen ? faChevronUp : faChevronDown} /> |
| 104 | + </Button> |
| 105 | + </div> |
| 106 | + <ul |
| 107 | + role="menu" |
| 108 | + className={clsx( |
| 109 | + 'tw:m-0 tw:p-0', |
| 110 | + 'tw:max-md:w-full tw:md:mr-2 tw:max-md:absolute tw:max-md:top-full tw:z-2000', |
| 111 | + 'tw:flex tw:max-md:flex-col tw:items-center', |
| 112 | + 'tw:bg-lm-main tw:dark:bg-dm-main', |
| 113 | + { 'tw:max-md:hidden': !menuOpen }, |
| 114 | + )} |
| 115 | + > |
| 116 | + {children} |
| 117 | + </ul> |
| 118 | + </nav> |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
| 122 | +export const NavBar = Object.assign(BaseNavBar, { MenuItem, Dropdown }); |
0 commit comments