@atlaskit/popup
Version:
A popup displays brief content in an overlay.
267 lines (266 loc) • 13.7 kB
JavaScript
/* popper-wrapper.tsx generated by @compiled/babel-plugin v3.0.2 */
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["shouldRenderToParent", "shouldFitContainer", "children", "appearance", "className", "isReferenceHidden", "shouldFitViewport"];
import "./popper-wrapper.compiled.css";
import * as React from 'react';
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { forwardRef, Fragment, useMemo, useState } from 'react';
import { ax, ix } from '@compiled/react/runtime';
import { useLayering } from '@atlaskit/layering/use-layering';
import Motion from '@atlaskit/motion/entering/motion';
import { fg } from '@atlaskit/platform-feature-flags/fg';
import { Popper } from '@atlaskit/popper/main';
import { RepositionOnUpdate } from './reposition-on-update';
import { useCloseManager } from './use-close-manager';
import { useFocusManager } from './use-focus-manager';
var LOCAL_CURRENT_SURFACE_CSS_VAR = '--ds-elevation-surface-current';
var fullWidthStyles = null;
var wrapperStyles = {
root: "_2rko1mok _1e0c1ule _vchhusvi _1pby1nn1 _bfhk1bhr _16qs130s _syazi7uo _1q1l1bhr _nt751r31 _49pcglyw _1hvw1o36",
fullWidth: "_1bsb1osq",
motion: "_c71l1kw7 _1e0c1txw"
};
var scrollableStyles = null;
var blanketStyles = null;
var modalStyles = null;
var focusRingStyles = {
root: "_mizu1v1w _1ah3dkaa _ra3xnqa1 _128mdkaa _1cvmnqa1 _4davt94y"
};
var placementMap = {
top: {
enter: "var(--ds-popup-enter-top, 150ms cubic-bezier(0.4, 1, 0.6, 1) SlideInTop8px, 150ms cubic-bezier(0.4, 1, 0.6, 1) FadeIn0to100)",
exit: "var(--ds-popup-exit-top, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) SlideOutTop4px, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) FadeOut100to0)"
},
'top-start': {
enter: "var(--ds-popup-enter-top, 150ms cubic-bezier(0.4, 1, 0.6, 1) SlideInTop8px, 150ms cubic-bezier(0.4, 1, 0.6, 1) FadeIn0to100)",
exit: "var(--ds-popup-exit-top, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) SlideOutTop4px, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) FadeOut100to0)"
},
'top-end': {
enter: "var(--ds-popup-enter-top, 150ms cubic-bezier(0.4, 1, 0.6, 1) SlideInTop8px, 150ms cubic-bezier(0.4, 1, 0.6, 1) FadeIn0to100)",
exit: "var(--ds-popup-exit-top, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) SlideOutTop4px, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) FadeOut100to0)"
},
bottom: {
enter: "var(--ds-popup-enter-bottom, 150ms cubic-bezier(0.4, 1, 0.6, 1) SlideInBottom8px, 150ms cubic-bezier(0.4, 1, 0.6, 1) FadeIn0to100)",
exit: "var(--ds-popup-exit-bottom, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) SlideOutBottom4px, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) FadeOut100to0)"
},
'bottom-start': {
enter: "var(--ds-popup-enter-bottom, 150ms cubic-bezier(0.4, 1, 0.6, 1) SlideInBottom8px, 150ms cubic-bezier(0.4, 1, 0.6, 1) FadeIn0to100)",
exit: "var(--ds-popup-exit-bottom, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) SlideOutBottom4px, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) FadeOut100to0)"
},
'bottom-end': {
enter: "var(--ds-popup-enter-bottom, 150ms cubic-bezier(0.4, 1, 0.6, 1) SlideInBottom8px, 150ms cubic-bezier(0.4, 1, 0.6, 1) FadeIn0to100)",
exit: "var(--ds-popup-exit-bottom, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) SlideOutBottom4px, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) FadeOut100to0)"
},
left: {
enter: "var(--ds-popup-enter-left, 150ms cubic-bezier(0.4, 1, 0.6, 1) SlideInLeft8px, 150ms cubic-bezier(0.4, 1, 0.6, 1) FadeIn0to100)",
exit: "var(--ds-popup-exit-left, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) SlideOutLeft4px, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) FadeOut100to0)"
},
'left-start': {
enter: "var(--ds-popup-enter-left, 150ms cubic-bezier(0.4, 1, 0.6, 1) SlideInLeft8px, 150ms cubic-bezier(0.4, 1, 0.6, 1) FadeIn0to100)",
exit: "var(--ds-popup-exit-left, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) SlideOutLeft4px, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) FadeOut100to0)"
},
'left-end': {
enter: "var(--ds-popup-enter-left, 150ms cubic-bezier(0.4, 1, 0.6, 1) SlideInLeft8px, 150ms cubic-bezier(0.4, 1, 0.6, 1) FadeIn0to100)",
exit: "var(--ds-popup-exit-left, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) SlideOutLeft4px, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) FadeOut100to0)"
},
right: {
enter: "var(--ds-popup-enter-right, 150ms cubic-bezier(0.4, 1, 0.6, 1) SlideInRight8px, 150ms cubic-bezier(0.4, 1, 0.6, 1) FadeIn0to100)",
exit: "var(--ds-popup-exit-right, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) SlideOutRight4px, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) FadeOut100to0)"
},
'right-start': {
enter: "var(--ds-popup-enter-right, 150ms cubic-bezier(0.4, 1, 0.6, 1) SlideInRight8px, 150ms cubic-bezier(0.4, 1, 0.6, 1) FadeIn0to100)",
exit: "var(--ds-popup-exit-right, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) SlideOutRight4px, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) FadeOut100to0)"
},
'right-end': {
enter: "var(--ds-popup-enter-right, 150ms cubic-bezier(0.4, 1, 0.6, 1) SlideInRight8px, 150ms cubic-bezier(0.4, 1, 0.6, 1) FadeIn0to100)",
exit: "var(--ds-popup-exit-right, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) SlideOutRight4px, 100ms cubic-bezier(0.6, 0, 0.8, 0.6) FadeOut100to0)"
}
};
var DefaultPopupComponent = /*#__PURE__*/forwardRef(function (props, ref) {
var shouldRenderToParent = props.shouldRenderToParent,
shouldFitContainer = props.shouldFitContainer,
children = props.children,
appearance = props.appearance,
className = props.className,
_isReferenceHidden = props.isReferenceHidden,
shouldFitViewport = props.shouldFitViewport,
htmlAttributes = _objectWithoutProperties(props, _excluded);
return /*#__PURE__*/React.createElement("div", _extends({
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop
className: ax([wrapperStyles.root, appearance === 'UNSAFE_modal-below-sm' && "_dk5d1b66 _c71l1ei0 _kqsw1n9t _152t1b66", (!shouldRenderToParent || shouldFitViewport) && "_1reo1wug _18m91wug", shouldFitContainer && "_1bsb1osq", className])
}, htmlAttributes, {
ref: !fg('platform-dst-motion-uplift-popup') ? ref : undefined
}), children);
});
function PopperWrapper(_ref) {
var xcss = _ref.xcss,
isOpen = _ref.isOpen,
id = _ref.id,
offset = _ref.offset,
testId = _ref.testId,
content = _ref.content,
fallbackPlacements = _ref.fallbackPlacements,
onClose = _ref.onClose,
boundary = _ref.boundary,
rootBoundary = _ref.rootBoundary,
shouldFlip = _ref.shouldFlip,
_ref$placement = _ref.placement,
placement = _ref$placement === void 0 ? 'auto' : _ref$placement,
_ref$popupComponent = _ref.popupComponent,
PopupContainer = _ref$popupComponent === void 0 ? DefaultPopupComponent : _ref$popupComponent,
_ref$autoFocus = _ref.autoFocus,
autoFocus = _ref$autoFocus === void 0 ? true : _ref$autoFocus,
triggerRef = _ref.triggerRef,
shouldUseCaptureOnOutsideClick = _ref.shouldUseCaptureOnOutsideClick,
shouldIgnoreCloseEvent = _ref.shouldIgnoreCloseEvent,
shouldRenderToParent = _ref.shouldRenderToParent,
shouldFitContainer = _ref.shouldFitContainer,
shouldDisableFocusLock = _ref.shouldDisableFocusLock,
_ref$shouldReturnFocu = _ref.shouldReturnFocus,
shouldReturnFocus = _ref$shouldReturnFocu === void 0 ? true : _ref$shouldReturnFocu,
strategy = _ref.strategy,
role = _ref.role,
label = _ref.label,
titleId = _ref.titleId,
modifiers = _ref.modifiers,
shouldFitViewport = _ref.shouldFitViewport,
_ref$appearance = _ref.appearance,
appearance = _ref$appearance === void 0 ? 'default' : _ref$appearance,
zIndex = _ref.zIndex;
var _useState = useState(null),
_useState2 = _slicedToArray(_useState, 2),
popupRef = _useState2[0],
setPopupRef = _useState2[1];
var _useState3 = useState(null),
_useState4 = _slicedToArray(_useState3, 2),
initialFocusRef = _useState4[0],
setInitialFocusRef = _useState4[1];
// We have cases where we need to close the Popup on Tab press, e.g. DropdownMenu
var shouldCloseOnTab = shouldRenderToParent && shouldDisableFocusLock;
var shouldDisableFocusTrap = role !== 'dialog';
useFocusManager({
initialFocusRef: initialFocusRef,
popupRef: popupRef,
shouldCloseOnTab: shouldCloseOnTab,
triggerRef: triggerRef,
autoFocus: autoFocus,
shouldDisableFocusTrap: shouldDisableFocusTrap,
shouldReturnFocus: shouldReturnFocus
});
useCloseManager({
isOpen: isOpen,
onClose: onClose,
popupRef: popupRef,
triggerRef: triggerRef,
shouldUseCaptureOnOutsideClick: shouldUseCaptureOnOutsideClick,
shouldIgnoreCloseEvent: shouldIgnoreCloseEvent,
shouldCloseOnTab: shouldCloseOnTab,
autoFocus: autoFocus,
shouldDisableFocusTrap: shouldDisableFocusTrap,
shouldRenderToParent: shouldRenderToParent
});
var _useLayering = useLayering(),
currentLevel = _useLayering.currentLevel;
var mergedModifiers = useMemo(function () {
return [{
name: 'flip',
enabled: shouldFlip,
options: {
rootBoundary: rootBoundary,
boundary: boundary,
fallbackPlacements: fallbackPlacements
}
}].concat(_toConsumableArray(modifiers || []));
}, [shouldFlip, rootBoundary, boundary, fallbackPlacements, modifiers]);
return /*#__PURE__*/React.createElement(Popper, {
placement: placement,
offset: offset,
modifiers: mergedModifiers,
strategy: strategy,
shouldFitViewport: shouldFitViewport
}, function (_ref2) {
var _ref3 = _ref2.ref,
style = _ref2.style,
placement = _ref2.placement,
update = _ref2.update,
isReferenceHidden = _ref2.isReferenceHidden;
var popupContainer = /*#__PURE__*/React.createElement(PopupContainer
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop
, {
className: ax([xcss,
// @ts-expect-error: `ax` is not typed correctly
!initialFocusRef && fg('platform-design-system-apply-popup-wrapper-focus') && focusRingStyles.root]),
appearance: appearance,
id: id,
"data-ds--level": currentLevel,
"data-placement": placement,
"data-testid": testId,
role: role,
"aria-label": label,
"aria-labelledby": titleId,
ref: !fg('platform-dst-motion-uplift-popup') ? function (node) {
if (node) {
if (typeof _ref3 === 'function') {
_ref3(node);
} else {
_ref3.current = node;
}
setPopupRef(node);
}
} : undefined
// eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
,
style: fg('platform-dst-motion-uplift-popup') || appearance === 'UNSAFE_modal-below-sm' ? {} : style
// using tabIndex={-1} would cause a bug where Safari focuses
// first on the browser address bar when using keyboard
,
tabIndex: autoFocus ? 0 : undefined,
shouldRenderToParent: shouldRenderToParent,
shouldFitContainer: shouldFitContainer,
shouldFitViewport: shouldFitViewport,
isReferenceHidden: isReferenceHidden
}, /*#__PURE__*/React.createElement(RepositionOnUpdate, {
update: update
}, content({
update: update,
isOpen: isOpen,
onClose: onClose,
setInitialFocusRef: setInitialFocusRef
})));
var isAutoPlacement = placement === 'auto' || placement === 'auto-start' || placement === 'auto-end';
var container = /*#__PURE__*/React.createElement("div", {
// eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
style: _objectSpread(_objectSpread({}, style), {}, {
zIndex: zIndex
}),
// using tabIndex={-1} would cause a bug where Safari focuses
// first on the browser address bar when using keyboard
ref: function ref(node) {
if (node) {
if (typeof _ref3 === 'function') {
_ref3(node);
} else {
_ref3.current = node;
}
setPopupRef(node);
}
},
"data-testid": "".concat(testId, "--container"),
className: ax([shouldFitContainer && wrapperStyles.fullWidth])
}, /*#__PURE__*/React.createElement(Motion, {
enteringAnimation: !isAutoPlacement ? placementMap[placement].enter : undefined,
exitingAnimation: !isAutoPlacement ? placementMap[placement].exit : undefined,
xcss: wrapperStyles.motion
}, popupContainer));
return /*#__PURE__*/React.createElement(Fragment, null, fg('platform-dst-motion-uplift-popup') ? container : popupContainer, appearance === 'UNSAFE_modal-below-sm' && /*#__PURE__*/React.createElement("div", {
className: ax(["_1r04idpf _kqsw1n9t _bfhk1i5c"])
}));
});
}
// eslint-disable-next-line @repo/internal/react/require-jsdoc
export default PopperWrapper;