import type {HTMLNextUIProps, PropGetter} from "@nextui-org/system";
import type {PopoverProps} from "@nextui-org/popover";
import type {MenuTriggerType} from "@react-types/menu";
import type {Ref} from "react";
import {useMenuTriggerState} from "@react-stately/menu";
import {useMenuTrigger} from "@react-aria/menu";
import {dropdown} from "@nextui-org/theme";
import {clsx} from "@nextui-org/shared-utils";
import {ReactRef, mergeRefs} from "@nextui-org/react-utils";
import {useMemo, useRef} from "react";
import {mergeProps} from "@react-aria/utils";
import {MenuProps} from "@nextui-org/menu";
interface Props extends HTMLNextUIProps<"div"> {
/**
* Type of overlay that is opened by the trigger.
*/
type?: "menu" | "listbox";
/**
* Ref to the DOM node.
*/
ref?: ReactRef;
/**
* How the menu is triggered.
* @default 'press'
*/
trigger?: MenuTriggerType;
/**
* Whether menu trigger is disabled.
* @default false
*/
isDisabled?: boolean;
/**
* Whether the Menu closes when a selection is made.
* @default true
*/
closeOnSelect?: boolean;
}
export type UseDropdownProps = Props & Omit;
export function useDropdown(props: UseDropdownProps) {
const {
as,
triggerRef: triggerRefProp,
isOpen,
defaultOpen,
onOpenChange,
type = "menu",
trigger = "press",
placement = "bottom",
isDisabled = false,
closeOnSelect = true,
shouldBlockScroll = true,
classNames: classNamesProp,
disableAnimation = false,
onClose,
className,
...otherProps
} = props;
const Component = as || "div";
const triggerRef = useRef(null);
const menuTriggerRef = triggerRefProp || triggerRef;
const menuRef = useRef(null);
const popoverRef = useRef(null);
const state = useMenuTriggerState({
trigger,
isOpen,
defaultOpen,
onOpenChange: (isOpen) => {
onOpenChange?.(isOpen);
if (!isOpen) {
onClose?.();
}
},
});
const {menuTriggerProps, menuProps} = useMenuTrigger