UNPKG

@primer/react

Version:

An implementation of GitHub's Primer Design System using React

36 lines (35 loc) 1.2 kB
import BaseStyles from "../BaseStyles.js"; import { ConfirmationDialog } from "./ConfirmationDialog.js"; import React from "react"; import { createRoot } from "react-dom/client"; //#region src/ConfirmationDialog/useConfirm.ts async function confirm(options) { const { content, ...confirmationDialogProps } = options; return new Promise((resolve) => { const hostElement = document.createElement("div"); document.body.append(hostElement); const root = createRoot(hostElement); const onClose = (gesture) => { root.unmount(); hostElement.remove(); resolve(gesture === "confirm"); }; root.render(/*#__PURE__*/ React.createElement(BaseStyles, null, /*#__PURE__*/ React.createElement(ConfirmationDialog, { ...confirmationDialogProps, onClose }, content))); }); } /** * 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() { return _temp; } function _temp(options) { return confirm(options); } //#endregion export { useConfirm };