|
| 1 | +'use client'; |
| 2 | +import { Bars } from '@primeicons/react/bars'; |
| 3 | +import { Times } from '@primeicons/react/times'; |
| 4 | +import { useMotion } from '@primereact/core/motion'; |
| 5 | +import { useDrawer } from '@primereact/headless/drawer'; |
| 6 | +import { usePortal } from '@primereact/headless/portal'; |
| 7 | +import * as React from 'react'; |
| 8 | +import { createPortal } from 'react-dom'; |
| 9 | + |
| 10 | +const navItems = [ |
| 11 | + { icon: 'pi pi-objects-column', label: 'Dashboard' }, |
| 12 | + { icon: 'pi pi-inbox', label: 'Inbox' }, |
| 13 | + { icon: 'pi pi-calendar', label: 'Calendar' }, |
| 14 | + { icon: 'pi pi-chart-bar', label: 'Analytics' } |
| 15 | +]; |
| 16 | + |
| 17 | +export default function BasicDemo() { |
| 18 | + const { triggerProps, backdropProps, portalProps, closeProps, headerProps, rootProps, portalMotionProps, backdropMotionProps, setMaskRef } = |
| 19 | + useDrawer(); |
| 20 | + const portal = usePortal(); |
| 21 | + |
| 22 | + const portalRef = React.useRef<HTMLDivElement>(null); |
| 23 | + const portalMotion = useMotion({ |
| 24 | + ...portalMotionProps.motionProps, |
| 25 | + elementRef: portalRef, |
| 26 | + visible: portalMotionProps.visible, |
| 27 | + enterFromClassName: '-translate-x-full', |
| 28 | + enterActiveClassName: 'transition-transform duration-200', |
| 29 | + enterToClassName: 'translate-x-0', |
| 30 | + leaveFromClassName: 'translate-x-0', |
| 31 | + leaveActiveClassName: 'transition-transform duration-200', |
| 32 | + leaveToClassName: '-translate-x-full' |
| 33 | + }); |
| 34 | + |
| 35 | + const backdropMotion = useMotion({ |
| 36 | + ...backdropMotionProps.motionProps, |
| 37 | + elementRef: setMaskRef, |
| 38 | + visible: backdropMotionProps.visible, |
| 39 | + enterFromClassName: 'opacity-0', |
| 40 | + enterActiveClassName: 'transition-opacity duration-200', |
| 41 | + enterToClassName: 'opacity-100', |
| 42 | + leaveFromClassName: 'opacity-100', |
| 43 | + leaveActiveClassName: 'transition-opacity duration-200', |
| 44 | + leaveToClassName: 'opacity-0' |
| 45 | + }); |
| 46 | + |
| 47 | + return ( |
| 48 | + <div {...rootProps} className="flex justify-center"> |
| 49 | + <button |
| 50 | + {...triggerProps} |
| 51 | + className="inline-flex items-center justify-center w-10 h-10 rounded-lg border border-surface bg-surface-0 dark:bg-surface-950 hover:bg-surface-100 dark:hover:bg-surface-900 cursor-pointer focus-visible:outline focus-visible:outline-1 focus-visible:outline-offset-2 focus-visible:outline-primary transition" |
| 52 | + > |
| 53 | + <Bars /> |
| 54 | + </button> |
| 55 | + |
| 56 | + {portal.state.mounted && |
| 57 | + portalMotion.state.rendered && |
| 58 | + createPortal( |
| 59 | + <div className="fixed inset-0 z-1100"> |
| 60 | + {backdropMotion.state.rendered && <div ref={setMaskRef} {...backdropProps} className="fixed inset-0 bg-black/50" />} |
| 61 | + {portalMotion.state.rendered && ( |
| 62 | + <div |
| 63 | + {...portalProps} |
| 64 | + ref={portalRef} |
| 65 | + className="fixed top-0 left-0 h-full w-80 bg-surface-0 dark:bg-surface-900 shadow-2xl flex flex-col z-50" |
| 66 | + > |
| 67 | + <div {...headerProps} className="flex items-center justify-between p-4 border-b border-surface"> |
| 68 | + <span className="text-lg font-semibold">Navigation</span> |
| 69 | + <button |
| 70 | + {...closeProps} |
| 71 | + className="inline-flex items-center justify-center w-8 h-8 rounded-full cursor-pointer bg-surface-50 dark:bg-surface-900 hover:bg-surface-100 dark:hover:bg-surface-800 focus-visible:outline focus-visible:outline-1 focus-visible:outline-offset-2 focus-visible:outline-primary transition" |
| 72 | + > |
| 73 | + <Times /> |
| 74 | + </button> |
| 75 | + </div> |
| 76 | + <nav className="flex-1 p-3"> |
| 77 | + <ul className="list-none m-0 p-0"> |
| 78 | + {navItems.map((item) => ( |
| 79 | + <li key={item.label}> |
| 80 | + <a className="flex items-center gap-3 px-3 py-2.5 rounded-lg cursor-pointer hover:bg-surface-50 dark:hover:bg-surface-800 transition text-sm font-medium"> |
| 81 | + <i className={`${item.icon} text-base`} /> |
| 82 | + {item.label} |
| 83 | + </a> |
| 84 | + </li> |
| 85 | + ))} |
| 86 | + </ul> |
| 87 | + </nav> |
| 88 | + </div> |
| 89 | + )} |
| 90 | + </div>, |
| 91 | + document.body |
| 92 | + )} |
| 93 | + </div> |
| 94 | + ); |
| 95 | +} |
0 commit comments