var $048d76b84370f141$exports = require("./utils.cjs");
var $89c727392dd0ab84$exports = require("./OverlayArrow.cjs");
var $88595bf043e542ec$exports = require("./Dialog.cjs");
var $kmEDY$reactariausePopover = require("react-aria/usePopover");
var $kmEDY$reactariaOverlay = require("react-aria/Overlay");
var $kmEDY$reactariaprivateutilsfilterDOMProps = require("react-aria/private/utils/filterDOMProps");
var $kmEDY$reactariaprivateinteractionsfocusSafely = require("react-aria/private/interactions/focusSafely");
var $kmEDY$reactariaprivateinteractionsuseFocusVisible = require("react-aria/private/interactions/useFocusVisible");
var $kmEDY$reactariaprivateutilsshadowdomDOMFunctions = require("react-aria/private/utils/shadowdom/DOMFunctions");
var $kmEDY$reactariamergeProps = require("react-aria/mergeProps");
var $kmEDY$reactstatelyuseOverlayTriggerState = require("react-stately/useOverlayTriggerState");
var $kmEDY$react = require("react");
var $kmEDY$reactariaprivateutilsanimation = require("react-aria/private/utils/animation");
var $kmEDY$reactariaprivatecollectionsHidden = require("react-aria/private/collections/Hidden");
var $kmEDY$reactariaprivateutilsuseLayoutEffect = require("react-aria/private/utils/useLayoutEffect");
var $kmEDY$reactariaI18nProvider = require("react-aria/I18nProvider");


function $parcel$interopDefault(a) {
  return a && a.__esModule ? a.default : a;
}

function $parcel$export(e, n, v, s) {
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
}

$parcel$export(module.exports, "PopoverContext", function () { return $74e35a768d38d46b$export$9b9a0cd73afb7ca4; });
$parcel$export(module.exports, "Popover", function () { return $74e35a768d38d46b$export$5b6b19405a83ff9d; });
/*
 * Copyright 2022 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */ 















const $74e35a768d38d46b$export$9b9a0cd73afb7ca4 = /*#__PURE__*/ (0, $kmEDY$react.createContext)(null);
// Stores a ref for the portal container for a group of popovers (e.g. submenus).
const $74e35a768d38d46b$var$PopoverGroupContext = /*#__PURE__*/ (0, $kmEDY$react.createContext)(null);
const $74e35a768d38d46b$export$5b6b19405a83ff9d = /*#__PURE__*/ (0, $kmEDY$react.forwardRef)(function Popover(props, ref) {
    [props, ref] = (0, $048d76b84370f141$exports.useContextProps)(props, ref, $74e35a768d38d46b$export$9b9a0cd73afb7ca4);
    let contextState = (0, $kmEDY$react.useContext)((0, $88595bf043e542ec$exports.OverlayTriggerStateContext));
    let localState = (0, $kmEDY$reactstatelyuseOverlayTriggerState.useOverlayTriggerState)(props);
    let state = props.isOpen != null || props.defaultOpen != null || !contextState ? localState : contextState;
    let isExiting = (0, $kmEDY$reactariaprivateutilsanimation.useExitAnimation)(ref, state.isOpen) || props.isExiting || false;
    let isHidden = (0, $kmEDY$reactariaprivatecollectionsHidden.useIsHidden)();
    let { direction: direction } = (0, $kmEDY$reactariaI18nProvider.useLocale)();
    // If we are in a hidden tree, we still need to preserve our children.
    if (isHidden) {
        let children = props.children;
        if (typeof children === 'function') children = children({
            trigger: props.trigger || null,
            placement: 'bottom',
            isEntering: false,
            isExiting: false,
            defaultChildren: null
        });
        return /*#__PURE__*/ (0, ($parcel$interopDefault($kmEDY$react))).createElement((0, ($parcel$interopDefault($kmEDY$react))).Fragment, null, children);
    }
    if (state && !state.isOpen && !isExiting) return null;
    return /*#__PURE__*/ (0, ($parcel$interopDefault($kmEDY$react))).createElement($74e35a768d38d46b$var$PopoverInner, {
        ...props,
        triggerRef: props.triggerRef,
        state: state,
        popoverRef: ref,
        isExiting: isExiting,
        dir: direction
    });
});
function $74e35a768d38d46b$var$PopoverInner({ state: state, isExiting: isExiting, UNSTABLE_portalContainer: UNSTABLE_portalContainer, clearContexts: clearContexts, ...props }) {
    // Calculate the arrow size internally (and remove props.arrowSize from PopoverProps)
    // Referenced from: packages/@react-spectrum/tooltip/src/TooltipTrigger.tsx
    let arrowRef = (0, $kmEDY$react.useRef)(null);
    let containerRef = (0, $kmEDY$react.useRef)(null);
    let groupCtx = (0, $kmEDY$react.useContext)($74e35a768d38d46b$var$PopoverGroupContext);
    let isSubPopover = groupCtx && props.trigger === 'SubmenuTrigger';
    let { popoverProps: popoverProps, underlayProps: underlayProps, arrowProps: arrowProps, placement: placement, triggerAnchorPoint: triggerAnchorPoint } = (0, $kmEDY$reactariausePopover.usePopover)({
        ...props,
        offset: props.offset ?? 8,
        arrowRef: arrowRef,
        // If this is a submenu/subdialog, use the root popover's container
        // to detect outside interaction and add aria-hidden.
        groupRef: isSubPopover ? groupCtx : containerRef
    }, state);
    let ref = props.popoverRef;
    let isEntering = (0, $kmEDY$reactariaprivateutilsanimation.useEnterAnimation)(ref, !!placement) || props.isEntering || false;
    let renderProps = (0, $048d76b84370f141$exports.useRenderProps)({
        ...props,
        defaultClassName: 'react-aria-Popover',
        values: {
            trigger: props.trigger || null,
            placement: placement,
            isEntering: isEntering,
            isExiting: isExiting
        }
    });
    // Automatically render Popover with role=dialog except when isNonModal is true,
    // or a dialog is already nested inside the popover.
    let shouldBeDialog = !props.isNonModal || props.trigger === 'SubmenuTrigger';
    let [isDialog, setDialog] = (0, $kmEDY$react.useState)(false);
    (0, $kmEDY$reactariaprivateutilsuseLayoutEffect.useLayoutEffect)(()=>{
        if (ref.current) setDialog(shouldBeDialog && !ref.current.querySelector('[role=dialog]'));
    }, [
        ref,
        shouldBeDialog
    ]);
    // Focus the popover itself on mount, unless a child element is already focused.
    // Skip this for submenus since hovering a submenutrigger should keep focus on the trigger
    (0, $kmEDY$react.useEffect)(()=>{
        if (isDialog && (props.trigger !== 'SubmenuTrigger' || (0, $kmEDY$reactariaprivateinteractionsuseFocusVisible.getInteractionModality)() !== 'pointer') && ref.current && !(0, $kmEDY$reactariaprivateutilsshadowdomDOMFunctions.isFocusWithin)(ref.current)) (0, $kmEDY$reactariaprivateinteractionsfocusSafely.focusSafely)(ref.current);
    }, [
        isDialog,
        ref,
        props.trigger
    ]);
    let children = (0, $kmEDY$react.useMemo)(()=>{
        let children = renderProps.children;
        if (clearContexts) for (let Context of clearContexts)children = /*#__PURE__*/ (0, ($parcel$interopDefault($kmEDY$react))).createElement(Context.Provider, {
            value: null
        }, children);
        return children;
    }, [
        renderProps.children,
        clearContexts
    ]);
    let style = {
        ...popoverProps.style,
        '--trigger-anchor-point': triggerAnchorPoint ? `${triggerAnchorPoint.x}px ${triggerAnchorPoint.y}px` : undefined,
        ...renderProps.style
    };
    let overlay = /*#__PURE__*/ (0, ($parcel$interopDefault($kmEDY$react))).createElement((0, $048d76b84370f141$exports.dom).div, {
        ...(0, $kmEDY$reactariamergeProps.mergeProps)((0, $kmEDY$reactariaprivateutilsfilterDOMProps.filterDOMProps)(props, {
            global: true
        }), popoverProps),
        ...renderProps,
        role: isDialog ? 'dialog' : undefined,
        tabIndex: isDialog ? -1 : undefined,
        "aria-label": props['aria-label'],
        "aria-labelledby": props['aria-labelledby'],
        ref: ref,
        slot: props.slot || undefined,
        style: style,
        dir: props.dir,
        "data-trigger": props.trigger,
        "data-placement": placement,
        "data-entering": isEntering || undefined,
        "data-exiting": isExiting || undefined
    }, !props.isNonModal && /*#__PURE__*/ (0, ($parcel$interopDefault($kmEDY$react))).createElement((0, $kmEDY$reactariaOverlay.DismissButton), {
        onDismiss: state.close
    }), /*#__PURE__*/ (0, ($parcel$interopDefault($kmEDY$react))).createElement((0, $89c727392dd0ab84$exports.OverlayArrowContext).Provider, {
        value: {
            ...arrowProps,
            placement: placement,
            ref: arrowRef
        }
    }, children), /*#__PURE__*/ (0, ($parcel$interopDefault($kmEDY$react))).createElement((0, $kmEDY$reactariaOverlay.DismissButton), {
        onDismiss: state.close
    }));
    // If this is a root popover, render an extra div to act as the portal container for submenus/subdialogs.
    if (!isSubPopover) return /*#__PURE__*/ (0, ($parcel$interopDefault($kmEDY$react))).createElement((0, $kmEDY$reactariaOverlay.Overlay), {
        ...props,
        shouldContainFocus: isDialog,
        isExiting: isExiting,
        portalContainer: UNSTABLE_portalContainer
    }, !props.isNonModal && state.isOpen && /*#__PURE__*/ (0, ($parcel$interopDefault($kmEDY$react))).createElement("div", {
        "data-testid": "underlay",
        ...underlayProps,
        style: {
            position: 'fixed',
            inset: 0
        }
    }), /*#__PURE__*/ (0, ($parcel$interopDefault($kmEDY$react))).createElement("div", {
        ref: containerRef,
        style: {
            display: 'contents'
        }
    }, /*#__PURE__*/ (0, ($parcel$interopDefault($kmEDY$react))).createElement($74e35a768d38d46b$var$PopoverGroupContext.Provider, {
        value: containerRef
    }, overlay)));
    // Submenus/subdialogs are mounted into the root popover's container.
    return /*#__PURE__*/ (0, ($parcel$interopDefault($kmEDY$react))).createElement((0, $kmEDY$reactariaOverlay.Overlay), {
        ...props,
        shouldContainFocus: isDialog,
        isExiting: isExiting,
        portalContainer: UNSTABLE_portalContainer ?? groupCtx?.current ?? undefined
    }, overlay);
}


//# sourceMappingURL=Popover.cjs.map
