UNPKG

@primer/react

Version:

An implementation of GitHub's Primer Design System using React

131 lines (127 loc) 5.07 kB
import React, { forwardRef, useRef } from 'react'; import styled from 'styled-components'; import { IconButton } from '../Button/IconButton.js'; import '../Button/ButtonBase.js'; import '../utils/defaultSxProp.js'; import '../Button/Button.js'; import { get } from '../constants.js'; import Box from '../Box/Box.js'; import useDialog from '../hooks/useDialog.js'; import sx from '../sx.js'; import Text from '../Text/Text.js'; import { useRefObjectAsForwardedRef } from '../hooks/useRefObjectAsForwardedRef.js'; import { XIcon } from '@primer/octicons-react'; import { toggleStyledComponent } from '../internal/utils/toggleStyledComponent.js'; import '../FeatureFlags/FeatureFlags.js'; import { useFeatureFlag } from '../FeatureFlags/useFeatureFlag.js'; import '../FeatureFlags/DefaultFeatureFlags.js'; import { clsx } from 'clsx'; import classes from './Dialog.module.css.js'; function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } const CSS_MODULES_FEATURE_FLAG = 'primer_react_css_modules_ga'; // Dialog v1 const noop = () => null; const DialogBase = toggleStyledComponent(CSS_MODULES_FEATURE_FLAG, 'div', styled.div.withConfig({ displayName: "Dialog__DialogBase", componentId: "sc-1y662gy-0" })(["box-shadow:", ";border-radius:", ";position:fixed;top:0;left:50%;transform:translateX(-50%);max-height:80vh;z-index:999;margin:10vh auto;background-color:", ";width:", ";outline:none;@media screen and (max-width:750px){width:100dvw;margin:0;border-radius:0;height:100dvh;}", ";"], get('shadows.shadow.large'), get('radii.2'), get('colors.canvas.default'), props => props.narrow ? '320px' : props.wide ? '640px' : '440px', sx)); const DialogHeaderBase = toggleStyledComponent(CSS_MODULES_FEATURE_FLAG, 'div', styled(Box).withConfig({ displayName: "Dialog__DialogHeaderBase", componentId: "sc-1y662gy-1" })(["border-radius:", " ", " 0px 0px;border-bottom:1px solid ", ";display:flex;@media screen and (max-width:750px){border-radius:0px;}", ";"], get('radii.2'), get('radii.2'), get('colors.border.default'), sx)); function DialogHeader({ theme, children, backgroundColor = 'canvas.subtle', ...rest }) { if (React.Children.toArray(children).every(ch => typeof ch === 'string')) { children = /*#__PURE__*/React.createElement(Text, { fontSize: 1, fontWeight: "bold" }, children); } const enabled = useFeatureFlag(CSS_MODULES_FEATURE_FLAG); return /*#__PURE__*/React.createElement(DialogHeaderBase, _extends({ theme: theme, p: 3, backgroundColor: backgroundColor }, rest, { className: enabled ? classes.Header : undefined }), children); } DialogHeader.displayName = "DialogHeader"; const Overlay = toggleStyledComponent(CSS_MODULES_FEATURE_FLAG, 'span', styled.span.withConfig({ displayName: "Dialog__Overlay", componentId: "sc-1y662gy-2" })(["&:before{position:fixed;top:0;right:0;bottom:0;left:0;display:block;cursor:default;content:' ';background:transparent;z-index:99;background:", ";}"], get('colors.primer.canvas.backdrop'))); const Dialog = /*#__PURE__*/forwardRef(({ children, onDismiss = noop, isOpen, initialFocusRef, returnFocusRef, className, ...props }, forwardedRef) => { const overlayRef = useRef(null); const modalRef = useRef(null); useRefObjectAsForwardedRef(forwardedRef, modalRef); const closeButtonRef = useRef(null); const onCloseClick = () => { onDismiss(); if (returnFocusRef && returnFocusRef.current) { returnFocusRef.current.focus(); } }; const { getDialogProps } = useDialog({ modalRef, onDismiss: onCloseClick, isOpen, initialFocusRef, closeButtonRef, returnFocusRef, overlayRef }); const enabled = useFeatureFlag(CSS_MODULES_FEATURE_FLAG); const iconStyles = enabled ? { className: classes.CloseIcon } : { sx: { position: 'absolute', top: '8px', right: '16px' } }; return isOpen ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Overlay, { className: enabled ? classes.Overlay : undefined, ref: overlayRef }), /*#__PURE__*/React.createElement(DialogBase, _extends({ tabIndex: -1, ref: modalRef, role: "dialog", "aria-modal": "true" }, props, getDialogProps(), { className: clsx({ [classes.Dialog]: enabled }, className), "data-width": props.wide ? 'wide' : props.narrow ? 'narrow' : 'default' }), /*#__PURE__*/React.createElement(IconButton, _extends({ icon: XIcon, ref: closeButtonRef, onClick: onCloseClick, "aria-label": "Close", variant: "invisible" }, iconStyles)), children)) : null; }); DialogHeader.propTypes = { ...Box.propTypes }; DialogHeader.displayName = 'Dialog.Header'; Dialog.displayName = 'Dialog'; var Dialog$1 = Object.assign(Dialog, { Header: DialogHeader }); export { Dialog$1 as default };