import { Menu, Transition } from '@headlessui/react'; import classNames from 'classnames'; import type { ElementType, ReactNode } from 'react'; import { Children as ReactChildren } from 'react'; import { forwardRef, useMemo } from 'react'; import { DropdownButton } from '@/components/elements/dropdown/DropdownButton'; import { DropdownItem } from '@/components/elements/dropdown/DropdownItem'; import styles from './style.module.css'; interface Props { as?: ElementType; children: ReactNode; } const DropdownGap = ({ invisible }: { invisible?: boolean }) => (
); type TypedChild = ReactNode & { type?: JSX.Element; }; const Dropdown = forwardRef(({ as, children }, ref) => { const [Button, items] = useMemo(() => { const list = ReactChildren.toArray(children) as unknown as TypedChild[]; return [ list.filter(child => child.type === DropdownButton), list.filter(child => child.type !== DropdownButton), ]; }, [children]); if (!Button) { throw new Error('Cannot mount component without a child .'); } return ( {Button}
{items}
); }); const _Dropdown = Object.assign(Dropdown, { Button: DropdownButton, Item: DropdownItem, Gap: DropdownGap, }); export { _Dropdown as default };