import { useState, createContext, useContext, Fragment, PropsWithChildren, Dispatch, SetStateAction, } from 'react' import { Link, InertiaLinkProps } from '@inertiajs/react' import { Transition } from '@headlessui/react' const DropDownContext = createContext<{ open: boolean setOpen: Dispatch> toggleOpen: () => void }>({ open: false, setOpen: () => {}, toggleOpen: () => {}, }) const Dropdown = ({ children }: PropsWithChildren) => { const [open, setOpen] = useState(false) const toggleOpen = () => { setOpen((previousState) => !previousState) } return (
{children}
) } const Trigger = ({ children }: PropsWithChildren) => { const { open, setOpen, toggleOpen } = useContext(DropDownContext) return ( <>
{children}
{open && (
setOpen(false)} >
)} ) } const Content = ({ align = 'right', width = '48', contentClasses = 'py-1 bg-white dark:bg-gray-700', children, }: PropsWithChildren<{ align?: 'left' | 'right' width?: '48' contentClasses?: string }>) => { const { open, setOpen } = useContext(DropDownContext) let alignmentClasses = 'origin-top' if (align === 'left') { alignmentClasses = 'ltr:origin-top-left rtl:origin-top-right start-0' } else if (align === 'right') { alignmentClasses = 'ltr:origin-top-right rtl:origin-top-left end-0' } let widthClasses = '' if (width === '48') { widthClasses = 'w-48' } return ( <>
setOpen(false)} >
{children}
) } const DropdownLink = ({ className = '', children, ...props }: InertiaLinkProps) => { return ( {children} ) } Dropdown.Trigger = Trigger Dropdown.Content = Content Dropdown.Link = DropdownLink export default Dropdown