@atlaskit/popup
Version:
A popup displays brief content in an overlay.
188 lines (180 loc) • 7.54 kB
JavaScript
/* popup-content-top-layer.tsx generated by @compiled/babel-plugin v3.0.2 */
import _extends from "@babel/runtime/helpers/extends";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { useCallback, useContext, useMemo, useRef } from 'react';
import noop from '@atlaskit/ds-lib/noop';
import { fromLegacyPlacement } from '@atlaskit/top-layer/placement-map/index';
import { PopoverSurface } from '@atlaskit/top-layer/popover-surface';
import { createPopoverCloseEvent } from '@atlaskit/top-layer/popover/create-close-event';
import { Popover } from '@atlaskit/top-layer/popover/popover';
import { useAnchoredPopover } from '@atlaskit/top-layer/use-anchored-popover';
import { getPopupAxisSizes } from '../internal/get-popup-axis-sizes';
import { useRoleProps } from '../internal/top-layer-bridge';
import { TriggerRefObjectContext } from './trigger-ref-object-context';
// Top-layer positioning is handled by CSS Anchor Positioning, not inline styles.
const EMPTY_STYLE = {};
/**
* Top-layer implementation of the compositional PopupContent.
*
* Reads isOpen, id, triggerRef from the legacy compositional Popup context,
* then renders via @atlaskit/top-layer's Popup compound.
*
* Gated behind the `platform-dst-top-layer` feature flag.
*/
export function PopupContentTopLayer({
xcss,
children,
offset: offsetProp,
onClose,
testId,
placement = 'auto',
fallbackPlacements: _fallbackPlacements,
popupComponent: PopupContainer,
// top-layer: initial focus follows `role`. No-op.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
autoFocus: _autoFocus = true,
shouldFitContainer = false,
// Keep the explicit `false`. See the note in `popup-top-layer.tsx`.
shouldFitViewport = false,
role,
label,
titleId,
isOpen,
id: providedId,
// ── No-op props ──
// top-layer: stacking managed by browser top layer. zIndex is a no-op.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
zIndex: _zIndex,
// top-layer: always renders in top layer. shouldRenderToParent is a no-op.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
shouldRenderToParent: _shouldRenderToParent,
// top-layer: CSS Anchor Positioning replaces Popper strategy. No-op.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
strategy: _strategy,
// top-layer: viewport is the natural boundary. No-op.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
boundary: _boundary,
// top-layer: viewport is the natural boundary. No-op.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
rootBoundary: _rootBoundary,
// top-layer: native light dismiss replaces capture-phase click handler.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
shouldUseCaptureOnOutsideClick: _shouldUseCaptureOnOutsideClick,
// top-layer: focus trapping is role-based. shouldDisableFocusLock is a no-op.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
shouldDisableFocusLock: _shouldDisableFocusLock,
// top-layer: shouldFlip handled by CSS position-try fallbacks.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
shouldFlip: _shouldFlip,
// top-layer: appearance is accepted but UNSAFE_modal-below-sm is not yet implemented.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
appearance: _appearance,
// top-layer: GPU acceleration is not applicable.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
shouldDisableGpuAcceleration: _shouldDisableGpuAcceleration
}) {
/**
* Consumer-chosen initial focus target.
*
* The legacy compositional `PopupContent` surfaced
* `setInitialFocusRef` on the content children render prop with a
* `Dispatch<SetStateAction<HTMLElement | null>>` signature so it
* could be passed as a React `ref` callback. We keep the same shape
* for API compatibility, but store the value in a ref. `Popover`
* reads it as `initialFocusRef` when it opens. For a role that moves
* focus, a target inside the popup takes focus instead of the role
* default, and focus moves once. Other roles ignore it.
* `autoFocus` has no effect here: role and focus go together. See
* `@atlaskit/top-layer` `notes/decisions/role-and-focus.md`.
*/
const consumerInitialFocusRef = useRef(null);
const setInitialFocusRef = useCallback(value => {
consumerInitialFocusRef.current = typeof value === 'function' ? value(consumerInitialFocusRef.current) : value;
}, []);
// Placement conversion
// Legacy `offset` is the popper `[along, away]` tuple; `fromLegacyPlacement`
// folds it into the new `placement.offset` shape.
const topLayerPlacement = useMemo(() => fromLegacyPlacement({
legacy: placement,
offset: offsetProp
}), [placement, offsetProp]);
// ── onClose bridge ──
const handleOnClose = useCallback(({
reason
}) => {
if (!onClose) {
return;
}
onClose(createPopoverCloseEvent({
reason
}));
}, [onClose]);
// ── Content render-prop bridge ──
// The args always hold the setter; the `TPopupContent` types decide who can see it.
const contentProps = useMemo(() => ({
isOpen,
update: noop,
onClose,
setInitialFocusRef
}), [isOpen, onClose, setInitialFocusRef]);
// ── Role mapping ──
const roleProps = useRoleProps({
role,
label,
titleId,
initialFocusRef: consumerInitialFocusRef
});
const popoverRef = useRef(null);
const anchorRef = useContext(TriggerRefObjectContext);
// `isOpen` is included so the anchor positioning effect re-runs when
// the Popover host element is unmounted/remounted across open cycles.
useAnchoredPopover({
anchorRef,
popoverRef,
placement: topLayerPlacement,
isOpen,
...getPopupAxisSizes({
shouldFitContainer,
shouldFitViewport
})
});
// Narrow to ForwardRefExoticComponent so JSX accepts the ref prop.
// All popupComponent implementations use forwardRef per the PopupComponentProps contract.
const Container = PopupContainer;
return /*#__PURE__*/React.createElement(Popover, _extends({
ref: popoverRef
}, roleProps, {
isOpen: isOpen,
shouldAnimate: true,
placement: topLayerPlacement,
onClose: handleOnClose,
testId: testId && `${testId}--container`
}), Container ? /*#__PURE__*/React.createElement(Container, {
style: EMPTY_STYLE,
id: providedId,
"data-placement": placement,
"data-testid": testId
// Focusable by script and by a click on empty space inside, so focus
// stays in the popup; -1 adds no tab stop.
,
tabIndex: -1,
xcss: xcss
// Forwarded because several in-tree containers key their own
// `overflow: auto` branch off it.
,
shouldFitViewport: shouldFitViewport
}, children(contentProps)) :
/*#__PURE__*/
// `PopoverSurface` owns `overflow: auto` and the `box-shadow` on the
// same element, so it is the scroll container. The wrapper exists only
// to carry the consumer's `xcss`: `PopoverSurface` exposes no
// `className` or `xcss` by design, so `xcss` lands on a CHILD of the
// surface. `width` works there; surface-level declarations apply one
// level in. Same shape as `../popup-top-layer.tsx`.
React.createElement(PopoverSurface, null, /*#__PURE__*/React.createElement("div", {
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop
className: xcss,
"data-testid": testId
}, children(contentProps))));
}