|
| 1 | +import { useCallback, useEffect, useMemo } from 'react' |
| 2 | +import { TemplatedNavLink } from '@/features/routing/components/templated-nav-link/templated-nav-link' |
| 3 | +import { Urls } from '@/routes/urls' |
| 4 | +import { Telescope, FlaskConical, Coins, X, Settings } from 'lucide-react' |
| 5 | +import { Search } from '@/features/search/components/search' |
| 6 | +import SvgWizard from '@/features/common/components/icons/wizard' |
| 7 | +import { cn } from '@/features/common/utils' |
| 8 | +import { useSelectedNetwork } from '@/features/network/data' |
| 9 | +import { useLayout } from '@/features/settings/data' |
| 10 | +import SvgLoraDark from '@/features/common/components/svg/lora-dark' |
| 11 | +import SvgLoraLight from '@/features/common/components/svg/lora-light' |
| 12 | +import { NetworkSelect } from '@/features/network/components/network-select' |
| 13 | +import { ThemeToggle } from '@/features/settings/components/theme-toggle' |
| 14 | + |
| 15 | +const itemBase = |
| 16 | + 'flex items-center gap-3 rounded-md border border-transparent px-3 py-2 hover:bg-accent hover:text-primary transition-colors' |
| 17 | +const itemActive = '[&.active]:bg-accent [&.active]:text-primary [&.active]:border-border' |
| 18 | +const iconBox = 'border rounded-md p-2' |
| 19 | + |
| 20 | +export default function DrawerMenu() { |
| 21 | + const [selectedNetwork] = useSelectedNetwork() |
| 22 | + const [layout, setLayout] = useLayout() |
| 23 | + const isOpen = !!layout.isDrawerMenuExpanded |
| 24 | + |
| 25 | + const menuItems = useMemo( |
| 26 | + () => [ |
| 27 | + { urlTemplate: Urls.Network.Explore, icon: <Telescope />, text: 'Explore' }, |
| 28 | + { urlTemplate: Urls.Network.AppLab, icon: <FlaskConical />, text: 'App Lab' }, |
| 29 | + { urlTemplate: Urls.Network.TransactionWizard, icon: <SvgWizard width={24} height={24} />, text: 'Txn Wizard' }, |
| 30 | + { urlTemplate: Urls.Network.Fund, icon: <Coins />, text: 'Fund' }, |
| 31 | + ], |
| 32 | + [] |
| 33 | + ) |
| 34 | + |
| 35 | + const navTextClassName = cn('visible transition-[visibility] duration-0 delay-100') |
| 36 | + |
| 37 | + const navIconClassName = cn('border rounded-md p-2') |
| 38 | + |
| 39 | + const handleClose = useCallback(() => setLayout((prev: any) => ({ ...prev, isDrawerMenuExpanded: false })), [setLayout]) |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + const onKey = (e: KeyboardEvent) => { |
| 43 | + if (e.key === 'Escape') handleClose() |
| 44 | + } |
| 45 | + if (isOpen) { |
| 46 | + document.addEventListener('keydown', onKey) |
| 47 | + const prev = document.body.style.overflow |
| 48 | + document.body.style.overflow = 'hidden' |
| 49 | + return () => { |
| 50 | + document.removeEventListener('keydown', onKey) |
| 51 | + document.body.style.overflow = prev |
| 52 | + } |
| 53 | + } |
| 54 | + }, [isOpen, handleClose]) |
| 55 | + |
| 56 | + return ( |
| 57 | + <div |
| 58 | + className={cn( |
| 59 | + 'fixed inset-0 z-50 transition-opacity duration-300', |
| 60 | + // fade backdrop in/out; disable pointer events when closed |
| 61 | + isOpen ? 'opacity-100 pointer-events-auto' : 'opacity-0 pointer-events-none', |
| 62 | + // respect reduced motion |
| 63 | + 'motion-reduce:transition-none' |
| 64 | + )} |
| 65 | + aria-hidden={!isOpen} |
| 66 | + > |
| 67 | + {/* Backdrop */} |
| 68 | + <button aria-label="Close menu" className="absolute inset-0 bg-black/40" onClick={handleClose} /> |
| 69 | + |
| 70 | + <aside |
| 71 | + className={cn( |
| 72 | + 'absolute right-0 top-0 h-full w-full md:w-4/7 bg-card border-r shadow-xl flex flex-col', |
| 73 | + 'transition-transform duration-300 will-change-transform', |
| 74 | + isOpen ? 'translate-x-0' : 'translate-x-full', |
| 75 | + 'motion-reduce:transition-none' |
| 76 | + )} |
| 77 | + role="dialog" |
| 78 | + aria-modal="true" |
| 79 | + > |
| 80 | + {/* Header */} |
| 81 | + <div className="flex flex-col items-start p-3 gap-4"> |
| 82 | + <div className="flex items-baseline justify-between w-full"> |
| 83 | + <TemplatedNavLink urlTemplate={Urls.Network.Explore} className="self-center"> |
| 84 | + <SvgLoraLight onClick={handleClose} className="block dark:hidden" /> |
| 85 | + <SvgLoraDark onClick={handleClose} className="hidden dark:block" /> |
| 86 | + </TemplatedNavLink> |
| 87 | + <button className="text-muted-foreground hover:text-foreground p-2" onClick={handleClose} aria-label="Close"> |
| 88 | + <X /> |
| 89 | + </button> |
| 90 | + </div> |
| 91 | + |
| 92 | + <Search /> |
| 93 | + </div> |
| 94 | + |
| 95 | + {/* Items */} |
| 96 | + <nav className="p-3 space-y-1"> |
| 97 | + {menuItems.map((item, idx) => ( |
| 98 | + <div onClick={handleClose}> |
| 99 | + <TemplatedNavLink |
| 100 | + key={idx} |
| 101 | + urlTemplate={item.urlTemplate} |
| 102 | + urlParams={{ networkId: selectedNetwork }} |
| 103 | + className={cn(itemBase, itemActive)} |
| 104 | + end={item.text === 'Explore'} |
| 105 | + > |
| 106 | + <div className={iconBox}>{item.icon}</div> |
| 107 | + <span className="whitespace-nowrap">{item.text}</span> |
| 108 | + </TemplatedNavLink> |
| 109 | + </div> |
| 110 | + ))} |
| 111 | + </nav> |
| 112 | + |
| 113 | + {/* Footer */} |
| 114 | + <div className="mt-auto mb-4 p-3 flex justify-between items-center text-xs text-muted-foreground"> |
| 115 | + <NetworkSelect /> |
| 116 | + <div className="mt-6 flex"> |
| 117 | + <ThemeToggle navTextClassName={navTextClassName} /> |
| 118 | + <TemplatedNavLink urlTemplate={Urls.Settings} className={'flex flex-col items-center justify-center'}> |
| 119 | + <div onClick={handleClose} className={navIconClassName}> |
| 120 | + <Settings /> |
| 121 | + </div> |
| 122 | + </TemplatedNavLink> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + </aside> |
| 126 | + </div> |
| 127 | + ) |
| 128 | +} |
0 commit comments