"use client";
import { useCallback, useRef, useState } from "react";
import { BELOW_INNER_LEFT_ANCHOR } from "../positioning/constants.js";
const noop = ()=>{
// do nothing
};
/**
 * This hook controls the visibility and positioning for a context menu.
 *
 * @example
 * Simple Example
 * ```tsx
 * import type { ReactElement } from "react";
 * import [ Menu, MenuItem, useContextMenu ] from "@react-md/core":
 *
 * function Example(): ReactElement {
 *   const { menuProps, onContextMenu } = useContextMenu();
 *
 *   return (
 *     <div onContextMenu={onContextMenu}>
 *       <textarea />
 *       <Menu {...menuProps}>
 *         <MenuItem>Cut</MenuItem>
 *         <MenuItem>Copy</MenuItem>
 *         <MenuItem>Paste</MenuItem>
 *         <MenuItem>Undo</MenuItem>
 *       </Menu>
 *     </div>
 *   );
 * }
 * ```
 *
 * @remarks
 * \@since 5.0.0
 * \@since 6.0.0 No longer supports overriding most of the `Menu` props. The
 * props must be passed to the `Menu` component manually.
 */ export function useContextMenu(options = {}) {
    const { anchor = BELOW_INNER_LEFT_ANCHOR, menuLabel = "Context Menu", onContextMenu = noop, preventScroll = true } = options;
    const [coords, setCoords] = useState({});
    const [visible, setVisible] = useState(false);
    const fixedTo = useRef(null);
    const onRequestClose = useCallback(()=>{
        setVisible(false);
    }, []);
    return {
        visible,
        setVisible,
        menuProps: {
            "aria-label": menuLabel,
            anchor,
            ...coords,
            fixedTo,
            visible,
            onRequestClose,
            preventScroll
        },
        onContextMenu (event) {
            onContextMenu(event);
            if (event.isPropagationStopped()) {
                return;
            }
            event.preventDefault();
            event.stopPropagation();
            setCoords({
                initialX: event.clientX,
                initialY: event.clientY
            });
            setVisible(true);
        }
    };
}

//# sourceMappingURL=useContextMenu.js.map