UNPKG

@primer/components

Version:
186 lines (159 loc) • 9.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _styledComponents = _interopRequireDefault(require("styled-components")); var _react = _interopRequireWildcard(require("react")); var _constants = require("./constants"); var _hooks = require("./hooks"); var _Portal = _interopRequireDefault(require("./Portal")); var _sx = _interopRequireDefault(require("./sx")); var _useCombinedRefs = require("./hooks/useCombinedRefs"); var _ThemeProvider = require("./ThemeProvider"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } const heightMap = { xsmall: '192px', small: '256px', medium: '320px', large: '432px', xlarge: '600px', auto: 'auto', initial: 'auto' // Passing 'initial' initially applies 'auto' }; const widthMap = { small: '256px', medium: '320px', large: '480px', xlarge: '640px', xxlarge: '960px', auto: 'auto' }; const animationDuration = 200; function getSlideAnimationStartingVector(anchorSide) { if (anchorSide !== null && anchorSide !== void 0 && anchorSide.endsWith('bottom')) { return { x: 0, y: -1 }; } else if (anchorSide !== null && anchorSide !== void 0 && anchorSide.endsWith('top')) { return { x: 0, y: 1 }; } else if (anchorSide !== null && anchorSide !== void 0 && anchorSide.endsWith('right')) { return { x: -1, y: 0 }; } else if (anchorSide !== null && anchorSide !== void 0 && anchorSide.endsWith('left')) { return { x: 1, y: 0 }; } return { x: 0, y: 0 }; } const StyledOverlay = _styledComponents.default.div.withConfig({ displayName: "Overlay__StyledOverlay", componentId: "jhwkzw-0" })(["background-color:", ";box-shadow:", ";position:absolute;min-width:192px;max-width:640px;height:", ";max-height:", ";width:", ";border-radius:12px;overflow:hidden;animation:overlay-appear ", "ms ", ";@keyframes overlay-appear{0%{opacity:0;}100%{opacity:1;}}visibility:var(--styled-overlay-visibility);:focus{outline:none;}", ";", ";"], (0, _constants.get)('colors.canvas.overlay'), (0, _constants.get)('shadows.overlay.shadow'), props => heightMap[props.height || 'auto'], props => props.maxHeight && heightMap[props.maxHeight], props => widthMap[props.width || 'auto'], animationDuration, (0, _constants.get)('animation.easeOutCubic'), _constants.COMMON, _sx.default); /** * An `Overlay` is a flexible floating surface, used to display transient content such as menus, * selection options, dialogs, and more. Overlays use shadows to express elevation. The `Overlay` * component handles all behaviors needed by overlay UIs as well as the common styles that all overlays * should have. * @param ignoreClickRefs Optional. An array of ref objects to ignore clicks on in the `onOutsideClick` behavior. This is often used to ignore clicking on the element that toggles the open/closed state for the `Overlay` to prevent the `Overlay` from being toggled twice. * @param initialFocusRef Optional. Ref for the element to focus when the `Overlay` is opened. If nothing is provided, the first focusable element in the `Overlay` body is focused. * @param returnFocusRef Required. Ref for the element to focus when the `Overlay` is closed. * @param onClickOutside Required. Function to call when clicking outside of the `Overlay`. Typically this function removes the Overlay. * @param onEscape Required. Function to call when user presses `Escape`. Typically this function removes the Overlay. * @param width Sets the width of the `Overlay`, pick from our set list of widths, or pass `auto` to automatically set the width based on the content of the `Overlay`. `small` corresponds to `256px`, `medium` corresponds to `320px`, `large` corresponds to `480px`, `xlarge` corresponds to `640px`, `xxlarge` corresponds to `960px`. * @param height Sets the height of the `Overlay`, pick from our set list of heights, or pass `auto` to automatically set the height based on the content of the `Overlay`, or pass `initial` to set the height based on the initial content of the `Overlay` (i.e. ignoring content changes). `xsmall` corresponds to `192px`, `small` corresponds to `256px`, `medium` corresponds to `320px`, `large` corresponds to `432px`, `xlarge` corresponds to `600px`. * @param maxHeight Sets the maximum height of the `Overlay`, pick from our set list of heights. `xsmall` corresponds to `192px`, `small` corresponds to `256px`, `medium` corresponds to `320px`, `large` corresponds to `432px`, `xlarge` corresponds to `600px`. * @param anchorSide If provided, the Overlay will slide into position from the side of the anchor with a brief animation * @param top Optional. Vertical position of the overlay, relative to its closest positioned ancestor (often its `Portal`). * @param left Optional. Horizontal position of the overlay, relative to its closest positioned ancestor (often its `Portal`). * @param portalContainerName Optional. The name of the portal container to render the Overlay into. */ const Overlay = /*#__PURE__*/_react.default.forwardRef(({ onClickOutside, role = 'none', initialFocusRef, returnFocusRef, ignoreClickRefs, onEscape, visibility = 'visible', height, top, left, anchorSide, portalContainerName, preventFocusOnOpen, ...rest }, forwardedRef) => { const overlayRef = (0, _react.useRef)(null); const combinedRef = (0, _useCombinedRefs.useCombinedRefs)(overlayRef, forwardedRef); const { theme } = (0, _ThemeProvider.useTheme)(); const slideAnimationDistance = parseInt((0, _constants.get)('space.2')(theme).replace('px', '')); const slideAnimationEasing = (0, _constants.get)('animation.easeOutCubic')(theme); (0, _hooks.useOverlay)({ overlayRef, returnFocusRef, onEscape, ignoreClickRefs, onClickOutside, initialFocusRef, preventFocusOnOpen }); (0, _react.useEffect)(() => { var _combinedRef$current; if (height === 'initial' && (_combinedRef$current = combinedRef.current) !== null && _combinedRef$current !== void 0 && _combinedRef$current.clientHeight) { combinedRef.current.style.height = `${combinedRef.current.clientHeight}px`; } }, [height, combinedRef]); (0, _react.useLayoutEffect)(() => { var _overlayRef$current; const { x, y } = getSlideAnimationStartingVector(anchorSide); if (!x && !y || !((_overlayRef$current = overlayRef.current) !== null && _overlayRef$current !== void 0 && _overlayRef$current.animate) || visibility === 'hidden') { return; } // JS animation is required because Safari does not allow css animations to start paused and then run overlayRef.current.animate({ transform: [`translate(${slideAnimationDistance * x}px, ${slideAnimationDistance * y}px)`, `translate(0, 0)`] }, { duration: animationDuration, easing: slideAnimationEasing }); }, [anchorSide, slideAnimationDistance, slideAnimationEasing, visibility]); const styledOverlay = /*#__PURE__*/_react.default.createElement(StyledOverlay, _extends({ height: height, role: role }, rest, { ref: combinedRef, style: { top: `${top || 0}px`, left: `${left || 0}px`, ...rest.style, '--styled-overlay-visibility': visibility } })); return /*#__PURE__*/_react.default.createElement(_Portal.default, { containerName: portalContainerName }, styledOverlay); }); Overlay.defaultProps = { height: 'auto', width: 'auto' }; var _default = Overlay; exports.default = _default;