|
| 1 | +import type { ComponentProps} from "react"; |
| 2 | +import { useState } from "react" |
| 3 | +import { useTranslation } from "react-i18next"; |
| 4 | +import { cn } from "~/utils/tailwind-utils"; |
| 5 | +import { InlineLink } from "./inline-link"; |
| 6 | + |
| 7 | +type MenuItemProps = ComponentProps<typeof InlineLink> |
| 8 | + |
| 9 | +export function MenuItem({children, ...props}: MenuItemProps) { |
| 10 | + return ( |
| 11 | + <InlineLink |
| 12 | + role="menuitem" |
| 13 | + id="menu-item" |
| 14 | + className="hover:text-blue-950 active:text-white focus:text-blue-400 text-md text-white block px-4 py-2 text-md hover:bg-slate-300 focus:bg-slate-600 active:bg-slate-800 text-md" |
| 15 | + {...props} |
| 16 | + > |
| 17 | + {children} |
| 18 | + </InlineLink> |
| 19 | + ) |
| 20 | +} |
| 21 | + |
| 22 | +interface MenuProps { |
| 23 | + className?: string; |
| 24 | + children: React.ReactNode; |
| 25 | +} |
| 26 | + |
| 27 | +export function Menu({ className, children }: MenuProps) { |
| 28 | + const { t } = useTranslation(['gcweb']); |
| 29 | + const [open, setOpen] = useState(false); |
| 30 | + const baseClassName = cn(`${open ? "bg-slate-900 text-white hover:bg-slate-800" : "bg-slate-700 text-white hover:bg-slate-600"} hover:underline text-lg inline-flex justify-center space-x-2 rounded-b-md border-b border-l border-r border-slate-700 px-4 py-2 ring-black ring-opacity-5`); |
| 31 | + |
| 32 | + const onClick = () => { |
| 33 | + setOpen((value) => !value) |
| 34 | + } |
| 35 | + |
| 36 | + return ( |
| 37 | + <div className="relative inline-block text-left"> |
| 38 | + <button |
| 39 | + onClick={onClick} |
| 40 | + className={cn(baseClassName, className)} |
| 41 | + aria-haspopup={true} |
| 42 | + aria-expanded={open} |
| 43 | + > |
| 44 | + <span>{t('gcweb:app.menu')}</span> |
| 45 | + {open ? ( |
| 46 | + <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6"> |
| 47 | + <path strokeLinecap="round" strokeLinejoin="round" d="m4.5 15.75 7.5-7.5 7.5 7.5" /> |
| 48 | + </svg> |
| 49 | + ) : ( |
| 50 | + <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6 my-auto"> |
| 51 | + <path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /> |
| 52 | + </svg> |
| 53 | + )} |
| 54 | + </button> |
| 55 | + {open && ( |
| 56 | + <div className="origin-top-left absolute left-0 mt-2 w-64 rounded-md shadow-lg bg-slate-700 ring-1 ring-black ring-opacity-5" |
| 57 | + role="menu" |
| 58 | + aria-orientation="vertical" |
| 59 | + aria-labelledby="menu-button" |
| 60 | + tabIndex={-1} |
| 61 | + > |
| 62 | + <div className="py-1" role="none"> |
| 63 | + {children} |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + )} |
| 67 | + </div> |
| 68 | + ) |
| 69 | +} |
0 commit comments