react-aria-components
Version:
A library of styleable components built using React Aria
189 lines (177 loc) • 9.48 kB
JavaScript
import {useContextProps as $64fa3d84918910a7$export$29f1550f4b0d4415, useRenderProps as $64fa3d84918910a7$export$4d86445c2cf5e3} from "./utils.module.js";
import {OverlayArrowContext as $44f671af83e7d9e0$export$2de4954e8ae13b9f} from "./OverlayArrow.module.js";
import {OverlayTriggerStateContext as $de32f1b87079253c$export$d2f961adcb0afbe} from "./Dialog.module.js";
import {useLocale as $ehFet$useLocale, usePopover as $ehFet$usePopover, DismissButton as $ehFet$DismissButton, Overlay as $ehFet$Overlay} from "react-aria";
import {useExitAnimation as $ehFet$useExitAnimation, useLayoutEffect as $ehFet$useLayoutEffect, useEnterAnimation as $ehFet$useEnterAnimation, mergeProps as $ehFet$mergeProps, filterDOMProps as $ehFet$filterDOMProps} from "@react-aria/utils";
import {focusSafely as $ehFet$focusSafely} from "@react-aria/interactions";
import {useOverlayTriggerState as $ehFet$useOverlayTriggerState} from "react-stately";
import $ehFet$react, {createContext as $ehFet$createContext, forwardRef as $ehFet$forwardRef, useContext as $ehFet$useContext, useRef as $ehFet$useRef, useState as $ehFet$useState, useEffect as $ehFet$useEffect, useMemo as $ehFet$useMemo} from "react";
import {useIsHidden as $ehFet$useIsHidden} from "@react-aria/collections";
/*
* 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 $07b14b47974efb58$export$9b9a0cd73afb7ca4 = /*#__PURE__*/ (0, $ehFet$createContext)(null);
// Stores a ref for the portal container for a group of popovers (e.g. submenus).
const $07b14b47974efb58$var$PopoverGroupContext = /*#__PURE__*/ (0, $ehFet$createContext)(null);
const $07b14b47974efb58$export$5b6b19405a83ff9d = /*#__PURE__*/ (0, $ehFet$forwardRef)(function Popover(props, ref) {
[props, ref] = (0, $64fa3d84918910a7$export$29f1550f4b0d4415)(props, ref, $07b14b47974efb58$export$9b9a0cd73afb7ca4);
let contextState = (0, $ehFet$useContext)((0, $de32f1b87079253c$export$d2f961adcb0afbe));
let localState = (0, $ehFet$useOverlayTriggerState)(props);
let state = props.isOpen != null || props.defaultOpen != null || !contextState ? localState : contextState;
let isExiting = (0, $ehFet$useExitAnimation)(ref, state.isOpen) || props.isExiting || false;
let isHidden = (0, $ehFet$useIsHidden)();
let { direction: direction } = (0, $ehFet$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, $ehFet$react).createElement((0, $ehFet$react).Fragment, null, children);
}
if (state && !state.isOpen && !isExiting) return null;
return /*#__PURE__*/ (0, $ehFet$react).createElement($07b14b47974efb58$var$PopoverInner, {
...props,
triggerRef: props.triggerRef,
state: state,
popoverRef: ref,
isExiting: isExiting,
dir: direction
});
});
function $07b14b47974efb58$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, $ehFet$useRef)(null);
let [arrowWidth, setArrowWidth] = (0, $ehFet$useState)(0);
let containerRef = (0, $ehFet$useRef)(null);
let groupCtx = (0, $ehFet$useContext)($07b14b47974efb58$var$PopoverGroupContext);
let isSubPopover = groupCtx && props.trigger === 'SubmenuTrigger';
(0, $ehFet$useLayoutEffect)(()=>{
if (arrowRef.current && state.isOpen) setArrowWidth(arrowRef.current.getBoundingClientRect().width);
}, [
state.isOpen,
arrowRef
]);
var _props_offset;
let { popoverProps: popoverProps, underlayProps: underlayProps, arrowProps: arrowProps, placement: placement } = (0, $ehFet$usePopover)({
...props,
offset: (_props_offset = props.offset) !== null && _props_offset !== void 0 ? _props_offset : 8,
arrowSize: arrowWidth,
// 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, $ehFet$useEnterAnimation)(ref, !!placement) || props.isEntering || false;
let renderProps = (0, $64fa3d84918910a7$export$4d86445c2cf5e3)({
...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, $ehFet$useState)(false);
(0, $ehFet$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.
(0, $ehFet$useEffect)(()=>{
if (isDialog && ref.current && !ref.current.contains(document.activeElement)) (0, $ehFet$focusSafely)(ref.current);
}, [
isDialog,
ref
]);
let children = (0, $ehFet$useMemo)(()=>{
let children = renderProps.children;
if (clearContexts) for (let Context of clearContexts)children = /*#__PURE__*/ (0, $ehFet$react).createElement(Context.Provider, {
value: null
}, children);
return children;
}, [
renderProps.children,
clearContexts
]);
let style = {
...popoverProps.style,
...renderProps.style
};
let overlay = /*#__PURE__*/ (0, $ehFet$react).createElement("div", {
...(0, $ehFet$mergeProps)((0, $ehFet$filterDOMProps)(props), 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, $ehFet$react).createElement((0, $ehFet$DismissButton), {
onDismiss: state.close
}), /*#__PURE__*/ (0, $ehFet$react).createElement((0, $44f671af83e7d9e0$export$2de4954e8ae13b9f).Provider, {
value: {
...arrowProps,
placement: placement,
ref: arrowRef
}
}, children), /*#__PURE__*/ (0, $ehFet$react).createElement((0, $ehFet$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, $ehFet$react).createElement((0, $ehFet$Overlay), {
...props,
shouldContainFocus: isDialog,
isExiting: isExiting,
portalContainer: UNSTABLE_portalContainer
}, !props.isNonModal && state.isOpen && /*#__PURE__*/ (0, $ehFet$react).createElement("div", {
"data-testid": "underlay",
...underlayProps,
style: {
position: 'fixed',
inset: 0
}
}), /*#__PURE__*/ (0, $ehFet$react).createElement("div", {
ref: containerRef,
style: {
display: 'contents'
}
}, /*#__PURE__*/ (0, $ehFet$react).createElement($07b14b47974efb58$var$PopoverGroupContext.Provider, {
value: containerRef
}, overlay)));
var _ref;
// Submenus/subdialogs are mounted into the root popover's container.
return /*#__PURE__*/ (0, $ehFet$react).createElement((0, $ehFet$Overlay), {
...props,
shouldContainFocus: isDialog,
isExiting: isExiting,
portalContainer: (_ref = UNSTABLE_portalContainer !== null && UNSTABLE_portalContainer !== void 0 ? UNSTABLE_portalContainer : groupCtx === null || groupCtx === void 0 ? void 0 : groupCtx.current) !== null && _ref !== void 0 ? _ref : undefined
}, overlay);
}
export {$07b14b47974efb58$export$9b9a0cd73afb7ca4 as PopoverContext, $07b14b47974efb58$export$5b6b19405a83ff9d as Popover};
//# sourceMappingURL=Popover.module.js.map