UNPKG

@primer/components

Version:
161 lines (150 loc) • 7.34 kB
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); } import styled from 'styled-components'; import React, { useEffect, useLayoutEffect, useRef } from 'react'; import { get, COMMON } from './constants'; import { useOverlay } from './hooks'; import Portal from './Portal'; import sx from './sx'; import { useCombinedRefs } from './hooks/useCombinedRefs'; import { useTheme } from './ThemeProvider'; 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 = styled.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;}", ";", ";"], get('colors.canvas.overlay'), get('shadows.overlay.shadow'), props => heightMap[props.height || 'auto'], props => props.maxHeight && heightMap[props.maxHeight], props => widthMap[props.width || 'auto'], animationDuration, get('animation.easeOutCubic'), COMMON, sx); /** * 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.forwardRef(({ onClickOutside, role = 'none', initialFocusRef, returnFocusRef, ignoreClickRefs, onEscape, visibility = 'visible', height, top, left, anchorSide, portalContainerName, preventFocusOnOpen, ...rest }, forwardedRef) => { const overlayRef = useRef(null); const combinedRef = useCombinedRefs(overlayRef, forwardedRef); const { theme } = useTheme(); const slideAnimationDistance = parseInt(get('space.2')(theme).replace('px', '')); const slideAnimationEasing = get('animation.easeOutCubic')(theme); useOverlay({ overlayRef, returnFocusRef, onEscape, ignoreClickRefs, onClickOutside, initialFocusRef, preventFocusOnOpen }); 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]); 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.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.createElement(Portal, { containerName: portalContainerName }, styledOverlay); }); Overlay.defaultProps = { height: 'auto', width: 'auto' }; export default Overlay;