@atlaskit/popup
Version:
A popup displays brief content in an overlay.
253 lines (252 loc) • 10.6 kB
JavaScript
/* popper-wrapper.tsx generated by @compiled/babel-plugin v3.0.2 */
import _extends from "@babel/runtime/helpers/extends";
import "./popper-wrapper.compiled.css";
import * as React from 'react';
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';
const LOCAL_CURRENT_SURFACE_CSS_VAR = '--ds-elevation-surface-current';
const fullWidthStyles = null;
const wrapperStyles = {
root: "_2rko1mok _1e0c1ule _vchhusvi _1pby1nn1 _bfhk1bhr _16qs130s _syazi7uo _1q1l1bhr _nt751r31 _49pcglyw _1hvw1o36",
fullWidth: "_1bsb1osq",
motion: "_c71l1kw7 _1e0c1txw"
};
const scrollableStyles = null;
const blanketStyles = null;
const modalStyles = null;
const focusRingStyles = {
root: "_mizu1v1w _1ah3dkaa _ra3xnqa1 _128mdkaa _1cvmnqa1 _4davt94y"
};
const 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)"
}
};
const DefaultPopupComponent = /*#__PURE__*/forwardRef((props, ref) => {
const {
shouldRenderToParent,
shouldFitContainer,
children,
appearance,
className,
isReferenceHidden: _isReferenceHidden,
shouldFitViewport,
...htmlAttributes
} = props;
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({
xcss,
isOpen,
id,
offset,
testId,
content,
fallbackPlacements,
onClose,
boundary,
rootBoundary,
shouldFlip,
placement = 'auto',
popupComponent: PopupContainer = DefaultPopupComponent,
autoFocus = true,
triggerRef,
shouldUseCaptureOnOutsideClick,
shouldIgnoreCloseEvent,
shouldRenderToParent,
shouldFitContainer,
shouldDisableFocusLock,
shouldReturnFocus = true,
strategy,
role,
label,
titleId,
modifiers,
shouldFitViewport,
appearance = 'default',
zIndex
}) {
const [popupRef, setPopupRef] = useState(null);
const [initialFocusRef, setInitialFocusRef] = useState(null);
// We have cases where we need to close the Popup on Tab press, e.g. DropdownMenu
const shouldCloseOnTab = shouldRenderToParent && shouldDisableFocusLock;
const shouldDisableFocusTrap = role !== 'dialog';
useFocusManager({
initialFocusRef,
popupRef,
shouldCloseOnTab,
triggerRef,
autoFocus,
shouldDisableFocusTrap,
shouldReturnFocus
});
useCloseManager({
isOpen,
onClose,
popupRef,
triggerRef,
shouldUseCaptureOnOutsideClick,
shouldIgnoreCloseEvent,
shouldCloseOnTab,
autoFocus,
shouldDisableFocusTrap,
shouldRenderToParent
});
const {
currentLevel
} = useLayering();
const mergedModifiers = useMemo(() => [{
name: 'flip',
enabled: shouldFlip,
options: {
rootBoundary,
boundary,
fallbackPlacements
}
}, ...(modifiers || [])], [shouldFlip, rootBoundary, boundary, fallbackPlacements, modifiers]);
return /*#__PURE__*/React.createElement(Popper, {
placement: placement,
offset: offset,
modifiers: mergedModifiers,
strategy: strategy,
shouldFitViewport: shouldFitViewport
}, ({
ref,
style,
placement,
update,
isReferenceHidden
}) => {
const 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') ? node => {
if (node) {
if (typeof ref === 'function') {
ref(node);
} else {
ref.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,
isOpen,
onClose,
setInitialFocusRef
})));
const isAutoPlacement = placement === 'auto' || placement === 'auto-start' || placement === 'auto-end';
const container = /*#__PURE__*/React.createElement("div", {
// eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
style: {
...style,
zIndex
},
// using tabIndex={-1} would cause a bug where Safari focuses
// first on the browser address bar when using keyboard
ref: node => {
if (node) {
if (typeof ref === 'function') {
ref(node);
} else {
ref.current = node;
}
setPopupRef(node);
}
},
"data-testid": `${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;