UNPKG

@atlaskit/popup

Version:

A popup displays brief content in an overlay.

240 lines (231 loc) • 10.5 kB
/* popup-top-layer.tsx generated by @compiled/babel-plugin v3.0.2 */ import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import * as React from 'react'; import { ax, ix } from "@compiled/react/runtime"; 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 { Fragment, memo, useCallback, useEffect, useMemo, useRef } from 'react'; import noop from '@atlaskit/ds-lib/noop'; import { getAriaForTrigger } from '@atlaskit/top-layer/get-aria-for-trigger'; 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 { usePopoverId } from '@atlaskit/top-layer/use-popover-id'; import { getPopupAxisSizes } from './internal/get-popup-axis-sizes'; import { useRoleProps } from './internal/top-layer-bridge'; // Top-layer positioning is handled by CSS Anchor Positioning, not inline styles. var EMPTY_STYLE = {}; /** * Top-layer implementation of Popup. * * Replaces the legacy @atlaskit/popup rendering pipeline * (Popper.js + Portal + focus-trap + @atlaskit/layering) * with the native Popover API + CSS Anchor Positioning via @atlaskit/top-layer. * * Gated behind the `platform-dst-top-layer` feature flag. */ export var PopupTopLayer = /*#__PURE__*/memo(function PopupTopLayer(_ref) { var _roleProps$role; var xcss = _ref.xcss, isOpen = _ref.isOpen, providedId = _ref.id, offsetProp = _ref.offset, testId = _ref.testId, trigger = _ref.trigger, content = _ref.content, _ref$onClose = _ref.onClose, onClose = _ref$onClose === void 0 ? noop : _ref$onClose, _ref$placement = _ref.placement, placement = _ref$placement === void 0 ? 'auto' : _ref$placement, _fallbackPlacements = _ref.fallbackPlacements, _ref$shouldFlip = _ref.shouldFlip, _shouldFlip = _ref$shouldFlip === void 0 ? true : _ref$shouldFlip, PopupContainer = _ref.popupComponent, _ref$autoFocus = _ref.autoFocus, autoFocus = _ref$autoFocus === void 0 ? true : _ref$autoFocus, _ref$shouldFitContain = _ref.shouldFitContainer, shouldFitContainer = _ref$shouldFitContain === void 0 ? false : _ref$shouldFitContain, _ref$shouldReturnFocu = _ref.shouldReturnFocus, _shouldReturnFocus = _ref$shouldReturnFocu === void 0 ? true : _ref$shouldReturnFocu, role = _ref.role, label = _ref.label, titleId = _ref.titleId, _zIndex = _ref.zIndex, _shouldRenderToParent = _ref.shouldRenderToParent, _strategy = _ref.strategy, _modifiers = _ref.modifiers, _boundary = _ref.boundary, _rootBoundary = _ref.rootBoundary, _shouldUseCaptureOnOutsideClick = _ref.shouldUseCaptureOnOutsideClick, _shouldDisableFocusLock = _ref.shouldDisableFocusLock, _ref$shouldFitViewpor = _ref.shouldFitViewport, shouldFitViewport = _ref$shouldFitViewpor === void 0 ? false : _ref$shouldFitViewpor, _appearance = _ref.appearance; var triggerRef = useRef(null); var popoverRef = useRef(null); /** * Escape hatch for consumer-chosen initial focus target. * * The legacy `Popup` surfaced `setInitialFocusRef` on the content * 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 rather than component state. Flipping state on every open * caused a needless re-render and, more importantly, raced the * initial-focus effect below. The ref is read in the open-side * effect to override the role-based default focus chosen by * `useInitialFocus` inside the underlying `Popover`. */ var consumerInitialFocusRef = useRef(null); var setInitialFocusRef = useCallback(function (value) { consumerInitialFocusRef.current = typeof value === 'function' ? value(consumerInitialFocusRef.current) : value; }, []); /** * Override the role-based initial focus chosen by the underlying * `Popover` (its internal `useInitialFocus`). * * React flushes child effects before parent effects, so by the time * this effect runs the role-based default focus has already moved. * We then apply the legacy `Popup` contract: * * 1. If the consumer wired `setInitialFocusRef`, focus that element * (escape hatch wins). * 2. Otherwise, if `autoFocus={false}`, return focus to the trigger. * The `autoFocus` prop on `Popup` opts out of any initial-focus * movement, even for roles like `dialog`. * * When neither applies the role-based default from the underlying * `Popover` stays in place. */ useEffect(function () { if (!isOpen) { /** * Clear the consumer-supplied focus target on close so a * subsequent open does not silently inherit a stale ref from * the previous open cycle. The next open will re-collect a * target only if the content render prop re-wires * `setInitialFocusRef`. */ consumerInitialFocusRef.current = null; return; } var consumerTarget = consumerInitialFocusRef.current; if (consumerTarget && consumerTarget.isConnected) { consumerTarget.focus(); return; } if (!autoFocus) { var _trigger = triggerRef.current; if (_trigger && _trigger.isConnected) { _trigger.focus(); } } }, [isOpen, autoFocus]); var popoverId = usePopoverId(); // Placement conversion. // Legacy `offset` is the popper `[along, away]` tuple; `fromLegacyPlacement` // folds it into the new `placement.offset` shape so it travels with placement. var topLayerPlacement = useMemo(function () { return fromLegacyPlacement({ legacy: placement, offset: offsetProp }); }, [placement, offsetProp]); useAnchoredPopover(_objectSpread({ anchorRef: triggerRef, popoverRef: popoverRef, placement: topLayerPlacement, isOpen: isOpen }, getPopupAxisSizes({ shouldFitContainer: shouldFitContainer, shouldFitViewport: shouldFitViewport }))); // onClose bridge. // Translates top-layer's { reason: TPopoverCloseReason } into legacy // onClose(event, currentLevel?) by synthesizing DOM events. // // Focus restoration is handled natively by the Popover API: // - Escape: browser restores focus to the trigger automatically // - Click-outside: browser does NOT restore (correct behavior) // No manual triggerRef.current?.focus() is needed. var handleOnClose = useCallback(function (_ref2) { var reason = _ref2.reason; onClose(createPopoverCloseEvent({ reason: reason })); }, [onClose]); // Content render-prop bridge. // Legacy content() receives { isOpen, update, setInitialFocusRef, onClose }. // update is a no-op because CSS Anchor Positioning self-updates. var contentProps = useMemo(function () { return { isOpen: isOpen, update: noop, onClose: onClose, setInitialFocusRef: setInitialFocusRef }; }, [isOpen, onClose, setInitialFocusRef]); // Role mapping. // Build the role/label props for Popover. // Roles requiring accessible names must have label or labelledBy. var roleProps = useRoleProps({ role: role, label: label, titleId: titleId }); var resolvedPopoverId = providedId !== null && providedId !== void 0 ? providedId : popoverId; var ariaAttributes = getAriaForTrigger({ role: (_roleProps$role = roleProps.role) !== null && _roleProps$role !== void 0 ? _roleProps$role : 'dialog', isOpen: isOpen, popoverId: resolvedPopoverId }); // Narrow to ForwardRefExoticComponent so JSX accepts the ref prop. // All popupComponent implementations use forwardRef per the PopupComponentProps contract. var Container = PopupContainer; // Map to legacy TriggerProps. var triggerProps = { ref: function ref(node) { triggerRef.current = node; }, 'aria-controls': ariaAttributes['aria-controls'], // `aria-expanded` reflects current open state. 'aria-expanded': ariaAttributes['aria-expanded'], 'aria-haspopup': ariaAttributes['aria-haspopup'] }; return /*#__PURE__*/React.createElement(Fragment, null, trigger(triggerProps), /*#__PURE__*/React.createElement(Popover, _extends({ ref: popoverRef, id: resolvedPopoverId }, roleProps, { isOpen: isOpen, onClose: handleOnClose, shouldAnimate: true, placement: topLayerPlacement, testId: testId && "".concat(testId, "--container") }), Container ? /*#__PURE__*/React.createElement(Container, { style: EMPTY_STYLE, id: providedId, "data-placement": placement, "data-testid": testId, tabIndex: autoFocus ? -1 : undefined, xcss: xcss // Forwarded because several in-tree containers key their own // `overflow: auto` branch off it. , shouldFitViewport: shouldFitViewport }, content(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 `compositional/popup-content-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 }, content(contentProps))))); });