|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | +import { Dialog as BaseDialog } from '@base-ui/react/dialog' |
| 9 | +import * as m from 'motion/react-m' |
| 10 | +import { useRef, type ReactNode } from 'react' |
| 11 | + |
| 12 | +import { Close12Icon } from '@oxide/design-system/icons/react' |
| 13 | + |
| 14 | +import { Modal } from '~/ui/lib/Modal' |
| 15 | +import { ModalContext, useSideModalPopupRef } from '~/ui/lib/modal-context' |
| 16 | + |
| 17 | +type ConfirmModalProps = { |
| 18 | + isOpen: boolean |
| 19 | + onDismiss: () => void |
| 20 | + onConfirm: () => void |
| 21 | + /** Short question, sentence case. e.g. "Cancel upload?" */ |
| 22 | + title: string |
| 23 | + /** One or two short lines. State the consequence first. */ |
| 24 | + children: ReactNode |
| 25 | + /** Verb phrase matching the destructive action. e.g. "Cancel upload" */ |
| 26 | + confirmText: string |
| 27 | + /** Verb phrase meaning "stay where I am". e.g. "Keep uploading" */ |
| 28 | + dismissText: string |
| 29 | + /** @default 'danger' */ |
| 30 | + actionType?: 'primary' | 'danger' |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * A confirm dialog stacked over a SideModal (e.g. a nav guard on an edited |
| 35 | + * form, or a cancel guard on an in-flight upload). Portals into the |
| 36 | + * SideModal's popup rather than document body, so the scrim and dialog use |
| 37 | + * the SideModal as their positioning context — auto-centered, no hard-coded |
| 38 | + * widths. |
| 39 | + * |
| 40 | + * On open, focus lands on the destructive primary action so a user who got |
| 41 | + * here by triggering a dismiss can press Enter once more to confirm. Esc and |
| 42 | + * the × close only this dialog, leaving the SideModal open. |
| 43 | + * |
| 44 | + * Must be rendered inside a SideModal — relies on SideModalPopupRefContext. |
| 45 | + */ |
| 46 | +export function ConfirmModal({ |
| 47 | + isOpen, |
| 48 | + onDismiss, |
| 49 | + onConfirm, |
| 50 | + title, |
| 51 | + children, |
| 52 | + confirmText, |
| 53 | + dismissText, |
| 54 | + actionType = 'danger', |
| 55 | +}: ConfirmModalProps) { |
| 56 | + const actionRef = useRef<HTMLButtonElement>(null) |
| 57 | + const sideModalRef = useSideModalPopupRef() |
| 58 | + if (!isOpen || !sideModalRef) return null |
| 59 | + return ( |
| 60 | + <ModalContext.Provider value> |
| 61 | + <BaseDialog.Root |
| 62 | + open |
| 63 | + onOpenChange={(open, { reason }) => { |
| 64 | + // Ignore focus-out to prevent a dismiss loop when a native confirm() |
| 65 | + // dialog steals and returns focus. Same trick as Modal. |
| 66 | + if (!open && reason !== 'focus-out') onDismiss() |
| 67 | + }} |
| 68 | + > |
| 69 | + {/* Portal into the SideModal so absolute children use the SideModal |
| 70 | + as their positioning context — no hard-coded widths. */} |
| 71 | + <BaseDialog.Portal container={sideModalRef}> |
| 72 | + {/* Scrim. absolute inset-0 fills the SideModal's popup, not the |
| 73 | + viewport. forceRender so base-ui doesn't hide the nested backdrop. */} |
| 74 | + <BaseDialog.Backdrop |
| 75 | + forceRender |
| 76 | + render={ |
| 77 | + <m.div |
| 78 | + className="bg-raise/80 absolute inset-0 z-10 -mx-(--gutter)" |
| 79 | + initial={{ opacity: 0 }} |
| 80 | + animate={{ opacity: 1 }} |
| 81 | + exit={{ opacity: 0 }} |
| 82 | + transition={{ duration: 0.15, ease: 'easeOut' }} |
| 83 | + /> |
| 84 | + } |
| 85 | + /> |
| 86 | + <BaseDialog.Popup |
| 87 | + initialFocus={actionRef} |
| 88 | + render={ |
| 89 | + <m.div |
| 90 | + initial={{ x: '-50%', y: 'calc(-50% - 16px)', opacity: 0 }} |
| 91 | + animate={{ x: '-50%', y: '-50%', opacity: 1 }} |
| 92 | + transition={{ type: 'spring', duration: 0.3, bounce: 0 }} |
| 93 | + className="bg-default light:bg-default shadow-modal pointer-events-auto absolute top-1/2 left-1/2 z-20 flex max-h-[calc(100%-2rem)] w-full max-w-md -translate-y-1/2 flex-col overflow-hidden rounded-lg" |
| 94 | + /> |
| 95 | + } |
| 96 | + > |
| 97 | + <Modal.Section> |
| 98 | + <BaseDialog.Title className="text-sans-semi-lg mb-2"> |
| 99 | + {title} |
| 100 | + </BaseDialog.Title> |
| 101 | + {children} |
| 102 | + </Modal.Section> |
| 103 | + <Modal.Footer |
| 104 | + onDismiss={onDismiss} |
| 105 | + onAction={onConfirm} |
| 106 | + cancelText={dismissText} |
| 107 | + actionText={confirmText} |
| 108 | + actionType={actionType} |
| 109 | + actionRef={actionRef} |
| 110 | + /> |
| 111 | + <BaseDialog.Close |
| 112 | + className="hover:bg-hover absolute top-2 right-2 flex items-center justify-center rounded-md p-2" |
| 113 | + aria-label="Close" |
| 114 | + > |
| 115 | + <Close12Icon className="text-default" /> |
| 116 | + </BaseDialog.Close> |
| 117 | + </BaseDialog.Popup> |
| 118 | + </BaseDialog.Portal> |
| 119 | + </BaseDialog.Root> |
| 120 | + </ModalContext.Provider> |
| 121 | + ) |
| 122 | +} |
0 commit comments