import React, { useState } from 'react'; import { AlertDialog, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, Button, type ButtonProps, LoadingIndicator, StickyFooter, } from '@tryghost/shade/components'; import { cn } from '@tryghost/shade/utils'; export const STICKY_FOOTER_TESTID = 'modal-sticky-footer'; export interface ConfirmationModalProps { title?: React.ReactNode; prompt?: React.ReactNode; cancelLabel?: string; okLabel?: string; okRunningLabel?: string; okVariant?: ButtonProps['variant']; onCancel?: () => void; onOk?: (modal?: { remove: () => void }) => void | Promise; customFooter?: React.ReactNode; formSheet?: boolean; stickyFooter?: boolean; /** Widens the dialog past its default `max-w-lg` for content that needs it. */ contentClassName?: string; testId?: string; } export type ConfirmationHostProps = { visible?: boolean; onRemove: () => void; }; export const ConfirmationModalContent: React.FC = ({ visible = true, onRemove, title = 'Are you sure?', prompt, cancelLabel = 'Cancel', okLabel = 'OK', okRunningLabel, okVariant = 'default', onCancel, onOk, customFooter, formSheet = true, stickyFooter = false, contentClassName, testId = 'confirmation-modal', }) => { const [taskState, setTaskState] = useState<'running' | ''>(''); const isRunning = taskState === 'running'; const runningLabel = okRunningLabel || okLabel; const handleCancel = () => { if (isRunning) { return; } if (onCancel) { onCancel(); } else { onRemove(); } }; const handleConfirm = async () => { setTaskState('running'); try { await onOk?.({ remove: onRemove }); } catch (error) { // eslint-disable-next-line no-console console.error( 'Unhandled Promise Rejection. Make sure you catch errors in your onOk handler.', error, ); } setTaskState(''); }; const defaultFooter = ( {cancelLabel && ( )} {okLabel && ( )} ); const footer = customFooter === undefined ? defaultFooter : customFooter; return ( !open && handleCancel()}> event.stopPropagation()} > {title}
{prompt}
{footer && (stickyFooter ? ( // StickyFooter sizes its box in raw pixels but spaces its parts // with `h-6`/`-mb-6`, which render at 38.4px on Shade's `0.4rem` // scale — hence the explicit sizing overrides. {footer} ) : ( footer ))}
); };