UNPKG

@primer/components

Version:
191 lines (160 loc) • 7.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useConfirm = useConfirm; exports.ConfirmationDialog = void 0; var _react = _interopRequireWildcard(require("react")); var _reactDom = _interopRequireDefault(require("react-dom")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _Box = _interopRequireDefault(require("../Box")); var _ThemeProvider = require("../ThemeProvider"); var _focusZone = require("../behaviors/focusZone"); var _constants = require("../constants"); var _Dialog = require("../Dialog/Dialog"); var _useFocusZone = require("../hooks/useFocusZone"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _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 StyledConfirmationHeader = _styledComponents.default.header.withConfig({ displayName: "ConfirmationDialog__StyledConfirmationHeader", componentId: "sc-1ub32x2-0" })(["padding:", ";display:flex;flex-direction:row;"], (0, _constants.get)('space.2')); const StyledTitle = (0, _styledComponents.default)(_Box.default).withConfig({ displayName: "ConfirmationDialog__StyledTitle", componentId: "sc-1ub32x2-1" })(["font-size:", ";font-weight:", ";padding:6px ", ";flex-grow:1;"], (0, _constants.get)('fontSizes.3'), (0, _constants.get)('fontWeights.bold'), (0, _constants.get)('space.2')); const ConfirmationHeader = ({ title, onClose, dialogLabelId }) => { const onCloseClick = (0, _react.useCallback)(() => { onClose('close-button'); }, [onClose]); return /*#__PURE__*/_react.default.createElement(StyledConfirmationHeader, null, /*#__PURE__*/_react.default.createElement(StyledTitle, { id: dialogLabelId }, title), /*#__PURE__*/_react.default.createElement(_Dialog.Dialog.CloseButton, { onClose: onCloseClick })); }; ConfirmationHeader.displayName = "ConfirmationHeader"; const StyledConfirmationBody = (0, _styledComponents.default)(_Box.default).withConfig({ displayName: "ConfirmationDialog__StyledConfirmationBody", componentId: "sc-1ub32x2-2" })(["font-size:", ";padding:0 ", " ", " ", ";color:", ";flex-grow:1;"], (0, _constants.get)('fontSizes.1'), (0, _constants.get)('space.3'), (0, _constants.get)('space.3'), (0, _constants.get)('space.3'), (0, _constants.get)('colors.fg.muted')); const ConfirmationBody = ({ children }) => { return /*#__PURE__*/_react.default.createElement(StyledConfirmationBody, null, children); }; ConfirmationBody.displayName = "ConfirmationBody"; const StyledConfirmationFooter = (0, _styledComponents.default)(_Box.default).withConfig({ displayName: "ConfirmationDialog__StyledConfirmationFooter", componentId: "sc-1ub32x2-3" })(["display:grid;grid-auto-flow:column;grid-auto-columns:max-content;grid-gap:", ";align-items:end;justify-content:end;padding:", " ", " ", ";"], (0, _constants.get)('space.2'), (0, _constants.get)('space.1'), (0, _constants.get)('space.3'), (0, _constants.get)('space.3')); const ConfirmationFooter = ({ footerButtons }) => { const { containerRef: footerRef } = (0, _useFocusZone.useFocusZone)({ bindKeys: _focusZone.FocusKeys.ArrowHorizontal | _focusZone.FocusKeys.Tab, focusInStrategy: 'closest' }); // Must have exactly 2 buttons! return /*#__PURE__*/_react.default.createElement(StyledConfirmationFooter, { ref: footerRef }, /*#__PURE__*/_react.default.createElement(_Dialog.Dialog.Buttons, { buttons: footerButtons !== null && footerButtons !== void 0 ? footerButtons : [] })); }; ConfirmationFooter.displayName = "ConfirmationFooter"; /** * A ConfirmationDialog is a special kind of dialog with more rigid behavior. It * is used to confirm a user action. ConfirmationDialogs always have exactly * two buttons: one to cancel the action and one to confirm it. No custom * rendering capabilities are provided for ConfirmationDialog. */ const ConfirmationDialog = props => { const { onClose, title, cancelButtonContent = 'Cancel', confirmButtonContent = 'OK', confirmButtonType = 'normal', children } = props; const onCancelButtonClick = (0, _react.useCallback)(() => { onClose('cancel'); }, [onClose]); const onConfirmButtonClick = (0, _react.useCallback)(() => { onClose('confirm'); }, [onClose]); const cancelButton = { content: cancelButtonContent, onClick: onCancelButtonClick }; const confirmButton = { content: confirmButtonContent, buttonType: confirmButtonType, onClick: onConfirmButtonClick, autoFocus: true }; const footerButtons = [cancelButton, confirmButton]; return /*#__PURE__*/_react.default.createElement(_Dialog.Dialog, { onClose: onClose, title: title, footerButtons: footerButtons, role: "alertdialog", width: "medium", renderHeader: ConfirmationHeader, renderBody: ConfirmationBody, renderFooter: ConfirmationFooter }, children); }; exports.ConfirmationDialog = ConfirmationDialog; ConfirmationDialog.displayName = "ConfirmationDialog"; async function confirm(themeProps, options) { const { content, ...confirmationDialogProps } = options; return new Promise(resolve => { const hostElement = document.createElement('div'); const onClose = gesture => { _reactDom.default.unmountComponentAtNode(hostElement); if (gesture === 'confirm') { resolve(true); } else { resolve(false); } }; _reactDom.default.render( /*#__PURE__*/_react.default.createElement(_ThemeProvider.ThemeProvider, themeProps, /*#__PURE__*/_react.default.createElement(ConfirmationDialog, _extends({}, confirmationDialogProps, { onClose: onClose }), content)), hostElement); }); } /** * This hook takes no parameters and returns an `async` function, `confirm`. When `confirm` * is called, it shows the confirmation dialog. When the dialog is dismissed, a promise is * resolved with `true` or `false` depending on whether or not the confirm button was used. */ function useConfirm() { const { theme, colorMode, dayScheme, nightScheme } = (0, _ThemeProvider.useTheme)(); const result = (0, _react.useCallback)(options => { const themeProps = { theme, colorMode, dayScheme, nightScheme }; return confirm(themeProps, options); }, [theme, colorMode, dayScheme, nightScheme]); return result; }