@atlaskit/popup
Version:
A popup displays brief content in an overlay.
169 lines (168 loc) • 5.41 kB
JavaScript
/* popper-wrapper.tsx generated by @compiled/babel-plugin v0.36.1 */
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';
import { fg } from '@atlaskit/platform-feature-flags';
import { Popper } from '@atlaskit/popper';
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: "_2rkoglpi _1e0c1ule _vchhusvi _1pby1nn1 _bfhk1bhr _16qs1cd0 _syaz1fxt _1q1l1bhr _nt751r31 _49pcglyw _1hvw1o36"
};
const scrollableStyles = null;
const blanketStyles = null;
const modalStyles = null;
const focusRingStyles = {
root: "_mizu194a _1ah31bk5 _ra3xnqa1 _128m1bk5 _1cvmnqa1 _4davt94y"
};
const DefaultPopupComponent = /*#__PURE__*/forwardRef((props, ref) => {
const {
shouldRenderToParent,
shouldFitContainer,
children,
appearance,
className,
...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 _c71ldtre _kqsw1n9t _152t1b66", !shouldRenderToParent && "_1reo1wug _18m91wug", shouldFitContainer && "_1bsb1osq", className])
}, htmlAttributes, {
ref: ref
}), children);
});
function PopperWrapper({
xcss,
isOpen,
id,
offset,
testId,
content,
fallbackPlacements,
onClose,
boundary,
rootBoundary,
shouldFlip,
placement = 'auto',
// @ts-ignore: [PIT-1685] Fails in post-office due to backwards incompatibility issue with React 18
popupComponent: PopupContainer = DefaultPopupComponent,
autoFocus = true,
triggerRef,
shouldUseCaptureOnOutsideClick,
shouldRenderToParent,
shouldFitContainer,
shouldDisableFocusLock,
shouldReturnFocus = true,
strategy,
role,
label,
titleId,
modifiers,
shouldFitViewport,
appearance = 'default'
}) {
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,
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
}) => {
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 &&
// eslint-disable-next-line @atlaskit/platform/ensure-feature-flag-prefix
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: node => {
if (node) {
if (typeof ref === 'function') {
ref(node);
} else {
ref.current = node;
}
setPopupRef(node);
}
}
// eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
,
style: 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
}, /*#__PURE__*/React.createElement(RepositionOnUpdate, {
update: update
}, content({
update,
isOpen,
onClose,
setInitialFocusRef
})));
return /*#__PURE__*/React.createElement(Fragment, null, popupContainer, appearance === 'UNSAFE_modal-below-sm' && /*#__PURE__*/React.createElement("div", {
className: ax(["_1r04idpf _kqsw1n9t _bfhk10xm"])
}));
});
}
// eslint-disable-next-line @repo/internal/react/require-jsdoc
export default PopperWrapper;