|
@@ -1,12 +1,15 @@
|
|
|
'use client'
|
|
'use client'
|
|
|
|
|
|
|
|
import {
|
|
import {
|
|
|
|
|
+ type ComponentProps,
|
|
|
type ComponentType,
|
|
type ComponentType,
|
|
|
type CSSProperties,
|
|
type CSSProperties,
|
|
|
createContext,
|
|
createContext,
|
|
|
type PointerEvent as ReactPointerEvent,
|
|
type PointerEvent as ReactPointerEvent,
|
|
|
|
|
+ type ReactNode,
|
|
|
useCallback,
|
|
useCallback,
|
|
|
useContext,
|
|
useContext,
|
|
|
|
|
+ useEffect,
|
|
|
useId,
|
|
useId,
|
|
|
useRef,
|
|
useRef,
|
|
|
useState,
|
|
useState,
|
|
@@ -95,23 +98,285 @@ function KeyHint({ children }: { children: string }) {
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * The confirmation layout itself: the icon chip and title, the hairline, a
|
|
|
|
|
+ * scrolling body and the button bar; a centred card on larger screens and a
|
|
|
|
|
+ * sheet on the bottom edge of a phone. `useConfirm()` draws every plain
|
|
|
|
|
+ * question with it. A prompt that needs more than a question can ask (content
|
|
|
|
|
+ * that loads while it is open, a third button, a checkbox) renders the frame
|
|
|
|
|
+ * itself, so it still looks and behaves like every other prompt.
|
|
|
|
|
+ */
|
|
|
|
|
+export function ConfirmFrame({
|
|
|
|
|
+ open,
|
|
|
|
|
+ onDismiss,
|
|
|
|
|
+ title,
|
|
|
|
|
+ icon,
|
|
|
|
|
+ destructive = false,
|
|
|
|
|
+ onOpenAutoFocus,
|
|
|
|
|
+ footer,
|
|
|
|
|
+ children,
|
|
|
|
|
+}: {
|
|
|
|
|
+ open: boolean
|
|
|
|
|
+ /** Escape, a tap outside, or the phone sheet dragged down: the safe answer. */
|
|
|
|
|
+ onDismiss: () => void
|
|
|
|
|
+ title: string
|
|
|
|
|
+ /** The mark in the header. Defaults to a warning when destructive, a question otherwise. */
|
|
|
|
|
+ icon?: ConfirmIcon
|
|
|
|
|
+ destructive?: boolean
|
|
|
|
|
+ /** Without it focus opens on the cancel button. */
|
|
|
|
|
+ onOpenAutoFocus?: (event: Event) => void
|
|
|
|
|
+ /** The buttons: `ConfirmFrameCancel` and `ConfirmFrameAction`, safest first. */
|
|
|
|
|
+ footer: ReactNode
|
|
|
|
|
+ /** What a screen reader announces as the description, in reading order. */
|
|
|
|
|
+ children: ReactNode
|
|
|
|
|
+}) {
|
|
|
|
|
+ const [dragY, setDragY] = useState(0)
|
|
|
|
|
+ const dragStart = useRef<number | null>(null)
|
|
|
|
|
+
|
|
|
|
|
+ // A sheet dragged away must not open the next time still pushed down.
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ if (open) setDragY(0)
|
|
|
|
|
+ }, [open])
|
|
|
|
|
+
|
|
|
|
|
+ const Icon = icon ?? (destructive ? TriangleAlert : CircleHelp)
|
|
|
|
|
+
|
|
|
|
|
+ // One colour drives the chip, the glow and the hairline. `--confirm-ink` is
|
|
|
|
|
+ // that colour pulled toward the text colour, so a red icon still reads on a
|
|
|
|
|
+ // dark card.
|
|
|
|
|
+ const tone = {
|
|
|
|
|
+ '--confirm-tone': destructive ? 'var(--destructive)' : 'var(--primary)',
|
|
|
|
|
+ '--confirm-ink': 'color-mix(in oklab, var(--confirm-tone) 72%, var(--foreground))',
|
|
|
|
|
+ transform: dragY > 0 ? `translateY(${dragY}px)` : undefined,
|
|
|
|
|
+ transition: dragStart.current === null ? undefined : 'none',
|
|
|
|
|
+ } as CSSProperties
|
|
|
|
|
+
|
|
|
|
|
+ const onGrabDown = (e: ReactPointerEvent<HTMLDivElement>) => {
|
|
|
|
|
+ e.currentTarget.setPointerCapture?.(e.pointerId)
|
|
|
|
|
+ dragStart.current = e.clientY
|
|
|
|
|
+ }
|
|
|
|
|
+ const onGrabMove = (e: ReactPointerEvent<HTMLDivElement>) => {
|
|
|
|
|
+ if (dragStart.current === null) return
|
|
|
|
|
+ setDragY(Math.max(0, e.clientY - dragStart.current))
|
|
|
|
|
+ }
|
|
|
|
|
+ const onGrabUp = () => {
|
|
|
|
|
+ if (dragStart.current === null) return
|
|
|
|
|
+ dragStart.current = null
|
|
|
|
|
+ if (dragY > DRAG_TO_DISMISS_PX) onDismiss()
|
|
|
|
|
+ else setDragY(0)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return (
|
|
|
|
|
+ <AlertDialog open={open} onOpenChange={(v) => !v && onDismiss()}>
|
|
|
|
|
+ <AlertDialogPortal>
|
|
|
|
|
+ {/* Tapping outside is the safe answer, the same as Escape. */}
|
|
|
|
|
+ <AlertDialogOverlay onClick={onDismiss} />
|
|
|
|
|
+ <AlertDialogPrimitive.Content
|
|
|
|
|
+ data-slot="confirm-dialog"
|
|
|
|
|
+ data-tone={destructive ? 'destructive' : 'neutral'}
|
|
|
|
|
+ style={tone}
|
|
|
|
|
+ onOpenAutoFocus={onOpenAutoFocus}
|
|
|
|
|
+ className={cn(
|
|
|
|
|
+ 'bg-card text-card-foreground border-card-edge fixed z-50 flex flex-col overflow-hidden border outline-none duration-200',
|
|
|
|
|
+ 'shadow-[0_1px_2px_rgb(0_0_0/0.06),0_24px_48px_-20px_rgb(0_0_0/0.45)]',
|
|
|
|
|
+ 'data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
|
|
|
|
|
+ // Phone: a sheet on the bottom edge, so the buttons sit under the thumb.
|
|
|
|
|
+ 'max-sm:data-[state=open]:slide-in-from-bottom max-sm:data-[state=closed]:slide-out-to-bottom max-sm:inset-x-0 max-sm:bottom-0 max-sm:max-h-[90dvh] max-sm:rounded-t-[1.375rem] max-sm:border-b-0',
|
|
|
|
|
+ // Larger screens: the house card, centred.
|
|
|
|
|
+ 'sm:data-[state=open]:zoom-in-95 sm:data-[state=closed]:zoom-out-95 sm:top-1/2 sm:left-1/2 sm:max-h-[85vh] sm:w-full sm:max-w-md sm:-translate-x-1/2 sm:-translate-y-1/2 sm:rounded-xl'
|
|
|
|
|
+ )}
|
|
|
|
|
+ >
|
|
|
|
|
+ {/* The sheen, centred on the chip like the house card's. */}
|
|
|
|
|
+ <div
|
|
|
|
|
+ aria-hidden="true"
|
|
|
|
|
+ className="pointer-events-none absolute inset-0 bg-[radial-gradient(9rem_5.5rem_at_2.4rem_2.2rem,color-mix(in_oklab,var(--confirm-tone)_9%,transparent),transparent_70%)]"
|
|
|
|
|
+ />
|
|
|
|
|
+ <div
|
|
|
|
|
+ aria-hidden="true"
|
|
|
|
|
+ data-slot="confirm-grab"
|
|
|
|
|
+ onPointerDown={onGrabDown}
|
|
|
|
|
+ onPointerMove={onGrabMove}
|
|
|
|
|
+ onPointerUp={onGrabUp}
|
|
|
|
|
+ onPointerCancel={onGrabUp}
|
|
|
|
|
+ className="relative shrink-0 touch-none px-5 pt-2.5 pb-1 sm:hidden"
|
|
|
|
|
+ >
|
|
|
|
|
+ <div className="bg-muted-foreground/45 mx-auto h-1 w-10 rounded-full" />
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <div className="relative flex shrink-0 items-center gap-3 px-5 pt-3 pb-3.5 sm:pt-4">
|
|
|
|
|
+ <div
|
|
|
|
|
+ data-slot="confirm-icon"
|
|
|
|
|
+ className="flex size-9 shrink-0 items-center justify-center rounded-lg bg-[linear-gradient(to_bottom,color-mix(in_oklab,var(--confirm-tone)_12%,transparent),color-mix(in_oklab,var(--confirm-tone)_4%,transparent))] text-(--confirm-ink) shadow-[inset_0_0_0_1px_color-mix(in_oklab,var(--confirm-tone)_22%,transparent),inset_0_1px_0_rgb(255_255_255/0.15)]"
|
|
|
|
|
+ >
|
|
|
|
|
+ <Icon className="size-[17px]" />
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <AlertDialogPrimitive.Title className="font-display min-w-0 text-[15px] leading-snug font-semibold tracking-tight text-balance">
|
|
|
|
|
+ {title}
|
|
|
|
|
+ </AlertDialogPrimitive.Title>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ {/* The house hairline: the tone under the chip, gone by the far edge. */}
|
|
|
|
|
+ <div
|
|
|
|
|
+ aria-hidden="true"
|
|
|
|
|
+ className="relative h-px shrink-0 bg-[linear-gradient(to_right,color-mix(in_oklab,var(--confirm-tone)_55%,transparent),var(--card-edge)_45%,transparent)]"
|
|
|
|
|
+ />
|
|
|
|
|
+
|
|
|
|
|
+ {/* Everything between the hairline and the buttons is the description
|
|
|
|
|
+ a screen reader announces, in reading order. */}
|
|
|
|
|
+ <AlertDialogPrimitive.Description asChild>
|
|
|
|
|
+ <div className="relative min-h-0 flex-1 overflow-y-auto text-sm">{children}</div>
|
|
|
|
|
+ </AlertDialogPrimitive.Description>
|
|
|
|
|
+
|
|
|
|
|
+ <div className="border-card-edge/60 bg-muted/45 relative flex shrink-0 flex-col-reverse gap-2 border-t px-5 py-3 max-sm:pt-3.5 max-sm:pb-[max(1.25rem,env(safe-area-inset-bottom))] sm:flex-row sm:flex-wrap sm:justify-end">
|
|
|
|
|
+ {footer}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </AlertDialogPrimitive.Content>
|
|
|
|
|
+ </AlertDialogPortal>
|
|
|
|
|
+ </AlertDialog>
|
|
|
|
|
+ )
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** Body text of a prompt drawn with `ConfirmFrame`: the spacing the shared confirm gives its description. */
|
|
|
|
|
+export function ConfirmFrameText({ className, ...props }: ComponentProps<'div'>) {
|
|
|
|
|
+ return (
|
|
|
|
|
+ <div
|
|
|
|
|
+ className={cn('text-muted-foreground grid gap-2.5 px-5 pt-4 pb-5 leading-relaxed', className)}
|
|
|
|
|
+ {...props}
|
|
|
|
|
+ />
|
|
|
|
|
+ )
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** The safe button of a `ConfirmFrame`. Closes the prompt; Escape presses it. */
|
|
|
|
|
+export function ConfirmFrameCancel({
|
|
|
|
|
+ className,
|
|
|
|
|
+ children,
|
|
|
|
|
+ ...props
|
|
|
|
|
+}: ComponentProps<typeof AlertDialogCancel>) {
|
|
|
|
|
+ return (
|
|
|
|
|
+ <AlertDialogCancel className={cn('max-sm:h-11 max-sm:text-[15px]', className)} {...props}>
|
|
|
|
|
+ <span>{children}</span>
|
|
|
|
|
+ <KeyHint>Esc</KeyHint>
|
|
|
|
|
+ </AlertDialogCancel>
|
|
|
|
|
+ )
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** A button of a `ConfirmFrame` that does something. Closes the prompt when pressed. */
|
|
|
|
|
+export function ConfirmFrameAction({
|
|
|
|
|
+ className,
|
|
|
|
|
+ children,
|
|
|
|
|
+ destructive = false,
|
|
|
|
|
+ enterHint = false,
|
|
|
|
|
+ variant,
|
|
|
|
|
+ ...props
|
|
|
|
|
+}: ComponentProps<typeof AlertDialogAction> & {
|
|
|
|
|
+ destructive?: boolean
|
|
|
|
|
+ /** Draw the Enter key on the button. Only for the button focus opens on. */
|
|
|
|
|
+ enterHint?: boolean
|
|
|
|
|
+}) {
|
|
|
|
|
+ return (
|
|
|
|
|
+ <AlertDialogAction
|
|
|
|
|
+ // The button's own destructive variant, not a class override: two
|
|
|
|
|
+ // background utilities on one element are settled by stylesheet
|
|
|
|
|
+ // order, which is how a red button ends up amber.
|
|
|
|
|
+ variant={variant ?? (destructive ? 'destructive' : 'default')}
|
|
|
|
|
+ className={cn('max-sm:h-11 max-sm:text-[15px]', className)}
|
|
|
|
|
+ {...props}
|
|
|
|
|
+ >
|
|
|
|
|
+ <span>{children}</span>
|
|
|
|
|
+ {enterHint && <KeyHint>↵</KeyHint>}
|
|
|
|
|
+ </AlertDialogAction>
|
|
|
|
|
+ )
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** The rows naming what a prompt affects, edge to edge under its text. */
|
|
|
|
|
+export function ConfirmSubjectList({
|
|
|
|
|
+ rows,
|
|
|
|
|
+ more = 0,
|
|
|
|
|
+ divided = false,
|
|
|
|
|
+}: {
|
|
|
|
|
+ rows: ConfirmSubject[]
|
|
|
|
|
+ /** How many more things are affected than `rows` shows. */
|
|
|
|
|
+ more?: number
|
|
|
|
|
+ /** Draw a line above the first row, for rows that follow a paragraph. */
|
|
|
|
|
+ divided?: boolean
|
|
|
|
|
+}) {
|
|
|
|
|
+ const t = useTranslations('common')
|
|
|
|
|
+ if (rows.length === 0) return null
|
|
|
|
|
+ const hasIcons = rows.some((row) => row.icon)
|
|
|
|
|
+ return (
|
|
|
|
|
+ <ul
|
|
|
|
|
+ data-slot="confirm-subject"
|
|
|
|
|
+ className={cn('text-[13px]', divided && 'border-card-edge/45 border-t')}
|
|
|
|
|
+ >
|
|
|
|
|
+ {rows.map((row, index) => {
|
|
|
|
|
+ const RowIcon = row.icon
|
|
|
|
|
+ return (
|
|
|
|
|
+ <li
|
|
|
|
|
+ // Rows are fixed while the prompt is open, and two can share a name.
|
|
|
|
|
+ key={`${index}:${row.label}`}
|
|
|
|
|
+ className="border-card-edge/45 flex min-w-0 items-center gap-2.5 border-b px-5 py-2"
|
|
|
|
|
+ >
|
|
|
|
|
+ {RowIcon && <RowIcon className="text-muted-foreground size-[15px] shrink-0" />}
|
|
|
|
|
+ <span className="min-w-0 truncate font-medium">{row.label}</span>
|
|
|
|
|
+ {row.detail && (
|
|
|
|
|
+ <span className="text-muted-foreground ml-auto max-w-[55%] shrink-0 truncate tabular-nums">
|
|
|
|
|
+ {row.detail}
|
|
|
|
|
+ </span>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </li>
|
|
|
|
|
+ )
|
|
|
|
|
+ })}
|
|
|
|
|
+ {more > 0 && (
|
|
|
|
|
+ <li
|
|
|
|
|
+ className={cn(
|
|
|
|
|
+ 'text-muted-foreground border-card-edge/45 border-b py-2 pr-5',
|
|
|
|
|
+ hasIcons ? 'pl-[2.8125rem]' : 'pl-5'
|
|
|
|
|
+ )}
|
|
|
|
|
+ >
|
|
|
|
|
+ {t('confirmDialog.andMore', { count: more })}
|
|
|
|
|
+ </li>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </ul>
|
|
|
|
|
+ )
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** The short lines saying what happens next, each behind an icon in the prompt's tone. */
|
|
|
|
|
+export function ConfirmConsequenceList({ lines }: { lines: ConfirmConsequence[] }) {
|
|
|
|
|
+ if (lines.length === 0) return null
|
|
|
|
|
+ return (
|
|
|
|
|
+ <ul data-slot="confirm-consequences" className="grid gap-1.5 px-5 pt-3.5 pb-4 text-[13px]">
|
|
|
|
|
+ {lines.map((entry) => {
|
|
|
|
|
+ const line = typeof entry === 'string' ? { text: entry } : entry
|
|
|
|
|
+ const LineIcon = line.icon ?? CircleAlert
|
|
|
|
|
+ return (
|
|
|
|
|
+ <li
|
|
|
|
|
+ key={line.text}
|
|
|
|
|
+ className={cn(
|
|
|
|
|
+ 'flex gap-2.5 leading-snug',
|
|
|
|
|
+ line.emphasis ? 'font-medium' : 'text-muted-foreground'
|
|
|
|
|
+ )}
|
|
|
|
|
+ >
|
|
|
|
|
+ <LineIcon className="mt-0.5 size-[15px] shrink-0 text-(--confirm-ink)" />
|
|
|
|
|
+ <span>{line.text}</span>
|
|
|
|
|
+ </li>
|
|
|
|
|
+ )
|
|
|
|
|
+ })}
|
|
|
|
|
+ </ul>
|
|
|
|
|
+ )
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export function ConfirmProvider({ children }: { children: React.ReactNode }) {
|
|
export function ConfirmProvider({ children }: { children: React.ReactNode }) {
|
|
|
const t = useTranslations('common')
|
|
const t = useTranslations('common')
|
|
|
const [open, setOpen] = useState(false)
|
|
const [open, setOpen] = useState(false)
|
|
|
const [options, setOptions] = useState<ConfirmOptions>({})
|
|
const [options, setOptions] = useState<ConfirmOptions>({})
|
|
|
const [typed, setTyped] = useState('')
|
|
const [typed, setTyped] = useState('')
|
|
|
- const [dragY, setDragY] = useState(0)
|
|
|
|
|
const resolveRef = useRef<((value: boolean) => void) | null>(null)
|
|
const resolveRef = useRef<((value: boolean) => void) | null>(null)
|
|
|
const cancelRef = useRef<HTMLButtonElement>(null)
|
|
const cancelRef = useRef<HTMLButtonElement>(null)
|
|
|
const actionRef = useRef<HTMLButtonElement>(null)
|
|
const actionRef = useRef<HTMLButtonElement>(null)
|
|
|
const inputRef = useRef<HTMLInputElement>(null)
|
|
const inputRef = useRef<HTMLInputElement>(null)
|
|
|
- const dragStart = useRef<number | null>(null)
|
|
|
|
|
const typeLabelId = useId()
|
|
const typeLabelId = useId()
|
|
|
|
|
|
|
|
const confirm = useCallback((opts: ConfirmOptions) => {
|
|
const confirm = useCallback((opts: ConfirmOptions) => {
|
|
|
setOptions(opts)
|
|
setOptions(opts)
|
|
|
setTyped('')
|
|
setTyped('')
|
|
|
- setDragY(0)
|
|
|
|
|
setOpen(true)
|
|
setOpen(true)
|
|
|
return new Promise<boolean>((resolve) => {
|
|
return new Promise<boolean>((resolve) => {
|
|
|
resolveRef.current = resolve
|
|
resolveRef.current = resolve
|
|
@@ -133,7 +398,6 @@ export function ConfirmProvider({ children }: { children: React.ReactNode }) {
|
|
|
// once the name matches.
|
|
// once the name matches.
|
|
|
const enterConfirms = mustType ? typedMatches : focusTarget === 'confirm'
|
|
const enterConfirms = mustType ? typedMatches : focusTarget === 'confirm'
|
|
|
|
|
|
|
|
- const Icon = options.icon ?? (destructive ? TriangleAlert : CircleHelp)
|
|
|
|
|
const consequences: Exclude<ConfirmConsequence, string>[] = [
|
|
const consequences: Exclude<ConfirmConsequence, string>[] = [
|
|
|
...(options.consequences ?? []).map((c) => (typeof c === 'string' ? { text: c } : c)),
|
|
...(options.consequences ?? []).map((c) => (typeof c === 'string' ? { text: c } : c)),
|
|
|
...(options.irreversible
|
|
...(options.irreversible
|
|
@@ -141,226 +405,87 @@ export function ConfirmProvider({ children }: { children: React.ReactNode }) {
|
|
|
: []),
|
|
: []),
|
|
|
]
|
|
]
|
|
|
const subject = options.subject ?? []
|
|
const subject = options.subject ?? []
|
|
|
- const subjectHasIcons = subject.some((row) => row.icon)
|
|
|
|
|
- const more = options.subjectMore ?? 0
|
|
|
|
|
-
|
|
|
|
|
- // One colour drives the chip, the glow and the hairline. `--confirm-ink` is
|
|
|
|
|
- // that colour pulled toward the text colour, so a red icon still reads on a
|
|
|
|
|
- // dark card.
|
|
|
|
|
- const tone = {
|
|
|
|
|
- '--confirm-tone': destructive ? 'var(--destructive)' : 'var(--primary)',
|
|
|
|
|
- '--confirm-ink': 'color-mix(in oklab, var(--confirm-tone) 72%, var(--foreground))',
|
|
|
|
|
- transform: dragY > 0 ? `translateY(${dragY}px)` : undefined,
|
|
|
|
|
- transition: dragStart.current === null ? undefined : 'none',
|
|
|
|
|
- } as CSSProperties
|
|
|
|
|
-
|
|
|
|
|
- const onGrabDown = (e: ReactPointerEvent<HTMLDivElement>) => {
|
|
|
|
|
- e.currentTarget.setPointerCapture?.(e.pointerId)
|
|
|
|
|
- dragStart.current = e.clientY
|
|
|
|
|
- }
|
|
|
|
|
- const onGrabMove = (e: ReactPointerEvent<HTMLDivElement>) => {
|
|
|
|
|
- if (dragStart.current === null) return
|
|
|
|
|
- setDragY(Math.max(0, e.clientY - dragStart.current))
|
|
|
|
|
- }
|
|
|
|
|
- const onGrabUp = () => {
|
|
|
|
|
- if (dragStart.current === null) return
|
|
|
|
|
- dragStart.current = null
|
|
|
|
|
- if (dragY > DRAG_TO_DISMISS_PX) settle(false)
|
|
|
|
|
- else setDragY(0)
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
<ConfirmContext value={{ confirm }}>
|
|
<ConfirmContext value={{ confirm }}>
|
|
|
{children}
|
|
{children}
|
|
|
- <AlertDialog open={open} onOpenChange={(v) => !v && settle(false)}>
|
|
|
|
|
- <AlertDialogPortal>
|
|
|
|
|
- {/* Tapping outside is the safe answer, the same as Escape. */}
|
|
|
|
|
- <AlertDialogOverlay onClick={() => settle(false)} />
|
|
|
|
|
- <AlertDialogPrimitive.Content
|
|
|
|
|
- data-slot="confirm-dialog"
|
|
|
|
|
- data-tone={destructive ? 'destructive' : 'neutral'}
|
|
|
|
|
- style={tone}
|
|
|
|
|
- onOpenAutoFocus={(e) => {
|
|
|
|
|
- e.preventDefault()
|
|
|
|
|
- const target = mustType
|
|
|
|
|
- ? inputRef.current
|
|
|
|
|
- : focusTarget === 'confirm'
|
|
|
|
|
- ? actionRef.current
|
|
|
|
|
- : cancelRef.current
|
|
|
|
|
- target?.focus()
|
|
|
|
|
- }}
|
|
|
|
|
|
|
+ <ConfirmFrame
|
|
|
|
|
+ open={open}
|
|
|
|
|
+ onDismiss={() => settle(false)}
|
|
|
|
|
+ title={options.title || t('confirmDialog.title')}
|
|
|
|
|
+ icon={options.icon}
|
|
|
|
|
+ destructive={destructive}
|
|
|
|
|
+ onOpenAutoFocus={(e) => {
|
|
|
|
|
+ e.preventDefault()
|
|
|
|
|
+ const target = mustType
|
|
|
|
|
+ ? inputRef.current
|
|
|
|
|
+ : focusTarget === 'confirm'
|
|
|
|
|
+ ? actionRef.current
|
|
|
|
|
+ : cancelRef.current
|
|
|
|
|
+ target?.focus()
|
|
|
|
|
+ }}
|
|
|
|
|
+ footer={
|
|
|
|
|
+ <>
|
|
|
|
|
+ <ConfirmFrameCancel ref={cancelRef}>
|
|
|
|
|
+ {options.cancelLabel || t('buttons.cancel')}
|
|
|
|
|
+ </ConfirmFrameCancel>
|
|
|
|
|
+ <ConfirmFrameAction
|
|
|
|
|
+ ref={actionRef}
|
|
|
|
|
+ disabled={!typedMatches}
|
|
|
|
|
+ onClick={() => settle(true)}
|
|
|
|
|
+ destructive={destructive}
|
|
|
|
|
+ enterHint={enterConfirms}
|
|
|
|
|
+ >
|
|
|
|
|
+ {options.confirmLabel || t('buttons.confirm')}
|
|
|
|
|
+ </ConfirmFrameAction>
|
|
|
|
|
+ </>
|
|
|
|
|
+ }
|
|
|
|
|
+ >
|
|
|
|
|
+ {options.description && (
|
|
|
|
|
+ <p className="text-muted-foreground px-5 pt-4 pb-5 leading-relaxed">
|
|
|
|
|
+ {options.description}
|
|
|
|
|
+ </p>
|
|
|
|
|
+ )}
|
|
|
|
|
+ <ConfirmSubjectList
|
|
|
|
|
+ rows={subject}
|
|
|
|
|
+ more={options.subjectMore}
|
|
|
|
|
+ divided={Boolean(options.description)}
|
|
|
|
|
+ />
|
|
|
|
|
+ <ConfirmConsequenceList lines={consequences} />
|
|
|
|
|
+ {mustType && (
|
|
|
|
|
+ <div
|
|
|
className={cn(
|
|
className={cn(
|
|
|
- 'bg-card text-card-foreground border-card-edge fixed z-50 flex flex-col overflow-hidden border outline-none duration-200',
|
|
|
|
|
- 'shadow-[0_1px_2px_rgb(0_0_0/0.06),0_24px_48px_-20px_rgb(0_0_0/0.45)]',
|
|
|
|
|
- 'data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
|
|
|
|
|
- // Phone: a sheet on the bottom edge, so the buttons sit under the thumb.
|
|
|
|
|
- 'max-sm:data-[state=open]:slide-in-from-bottom max-sm:data-[state=closed]:slide-out-to-bottom max-sm:inset-x-0 max-sm:bottom-0 max-sm:max-h-[90dvh] max-sm:rounded-t-[1.375rem] max-sm:border-b-0',
|
|
|
|
|
- // Larger screens: the house card, centred.
|
|
|
|
|
- 'sm:data-[state=open]:zoom-in-95 sm:data-[state=closed]:zoom-out-95 sm:top-1/2 sm:left-1/2 sm:max-h-[85vh] sm:w-full sm:max-w-md sm:-translate-x-1/2 sm:-translate-y-1/2 sm:rounded-xl'
|
|
|
|
|
|
|
+ 'grid gap-1.5 px-5 pb-4 text-[13px]',
|
|
|
|
|
+ consequences.length > 0 ? 'pt-1' : 'pt-3.5'
|
|
|
)}
|
|
)}
|
|
|
>
|
|
>
|
|
|
- {/* The sheen, centred on the chip like the house card's. */}
|
|
|
|
|
- <div
|
|
|
|
|
- aria-hidden="true"
|
|
|
|
|
- className="pointer-events-none absolute inset-0 bg-[radial-gradient(9rem_5.5rem_at_2.4rem_2.2rem,color-mix(in_oklab,var(--confirm-tone)_9%,transparent),transparent_70%)]"
|
|
|
|
|
- />
|
|
|
|
|
- <div
|
|
|
|
|
- aria-hidden="true"
|
|
|
|
|
- data-slot="confirm-grab"
|
|
|
|
|
- onPointerDown={onGrabDown}
|
|
|
|
|
- onPointerMove={onGrabMove}
|
|
|
|
|
- onPointerUp={onGrabUp}
|
|
|
|
|
- onPointerCancel={onGrabUp}
|
|
|
|
|
- className="relative shrink-0 touch-none px-5 pt-2.5 pb-1 sm:hidden"
|
|
|
|
|
- >
|
|
|
|
|
- <div className="bg-muted-foreground/45 mx-auto h-1 w-10 rounded-full" />
|
|
|
|
|
- </div>
|
|
|
|
|
-
|
|
|
|
|
- <div className="relative flex shrink-0 items-center gap-3 px-5 pt-3 pb-3.5 sm:pt-4">
|
|
|
|
|
- <div
|
|
|
|
|
- data-slot="confirm-icon"
|
|
|
|
|
- className="flex size-9 shrink-0 items-center justify-center rounded-lg bg-[linear-gradient(to_bottom,color-mix(in_oklab,var(--confirm-tone)_12%,transparent),color-mix(in_oklab,var(--confirm-tone)_4%,transparent))] text-(--confirm-ink) shadow-[inset_0_0_0_1px_color-mix(in_oklab,var(--confirm-tone)_22%,transparent),inset_0_1px_0_rgb(255_255_255/0.15)]"
|
|
|
|
|
- >
|
|
|
|
|
- <Icon className="size-[17px]" />
|
|
|
|
|
- </div>
|
|
|
|
|
- <AlertDialogPrimitive.Title className="font-display min-w-0 text-[15px] leading-snug font-semibold tracking-tight text-balance">
|
|
|
|
|
- {options.title || t('confirmDialog.title')}
|
|
|
|
|
- </AlertDialogPrimitive.Title>
|
|
|
|
|
- </div>
|
|
|
|
|
- {/* The house hairline: the tone under the chip, gone by the far edge. */}
|
|
|
|
|
- <div
|
|
|
|
|
- aria-hidden="true"
|
|
|
|
|
- className="relative h-px shrink-0 bg-[linear-gradient(to_right,color-mix(in_oklab,var(--confirm-tone)_55%,transparent),var(--card-edge)_45%,transparent)]"
|
|
|
|
|
|
|
+ <label htmlFor={`${typeLabelId}-input`} id={typeLabelId}>
|
|
|
|
|
+ {t.rich('confirmDialog.typeToConfirm', {
|
|
|
|
|
+ text: mustType,
|
|
|
|
|
+ b: (chunks) => <b className="font-semibold">{chunks}</b>,
|
|
|
|
|
+ })}
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <Input
|
|
|
|
|
+ ref={inputRef}
|
|
|
|
|
+ id={`${typeLabelId}-input`}
|
|
|
|
|
+ value={typed}
|
|
|
|
|
+ autoComplete="off"
|
|
|
|
|
+ autoCapitalize="off"
|
|
|
|
|
+ spellCheck={false}
|
|
|
|
|
+ onChange={(e) => setTyped(e.target.value)}
|
|
|
|
|
+ onKeyDown={(e) => {
|
|
|
|
|
+ // An Enter that accepts an input method's composition
|
|
|
|
|
+ // finishes the text; it is not an answer.
|
|
|
|
|
+ if (e.nativeEvent.isComposing || e.keyCode === 229) return
|
|
|
|
|
+ if (e.key === 'Enter' && typedMatches) {
|
|
|
|
|
+ e.preventDefault()
|
|
|
|
|
+ settle(true)
|
|
|
|
|
+ }
|
|
|
|
|
+ }}
|
|
|
/>
|
|
/>
|
|
|
-
|
|
|
|
|
- {/* Everything between the hairline and the buttons is the description
|
|
|
|
|
- a screen reader announces, in reading order. */}
|
|
|
|
|
- <AlertDialogPrimitive.Description asChild>
|
|
|
|
|
- <div className="relative min-h-0 flex-1 overflow-y-auto text-sm">
|
|
|
|
|
- {options.description && (
|
|
|
|
|
- <p className="text-muted-foreground px-5 pt-4 pb-5 leading-relaxed">
|
|
|
|
|
- {options.description}
|
|
|
|
|
- </p>
|
|
|
|
|
- )}
|
|
|
|
|
- {subject.length > 0 && (
|
|
|
|
|
- <ul
|
|
|
|
|
- data-slot="confirm-subject"
|
|
|
|
|
- className={cn(
|
|
|
|
|
- 'text-[13px]',
|
|
|
|
|
- options.description && 'border-card-edge/45 border-t'
|
|
|
|
|
- )}
|
|
|
|
|
- >
|
|
|
|
|
- {subject.map((row) => {
|
|
|
|
|
- const RowIcon = row.icon
|
|
|
|
|
- return (
|
|
|
|
|
- <li
|
|
|
|
|
- key={`${row.label}:${row.detail ?? ''}`}
|
|
|
|
|
- className="border-card-edge/45 flex min-w-0 items-center gap-2.5 border-b px-5 py-2"
|
|
|
|
|
- >
|
|
|
|
|
- {RowIcon && (
|
|
|
|
|
- <RowIcon className="text-muted-foreground size-[15px] shrink-0" />
|
|
|
|
|
- )}
|
|
|
|
|
- <span className="min-w-0 truncate font-medium">{row.label}</span>
|
|
|
|
|
- {row.detail && (
|
|
|
|
|
- <span className="text-muted-foreground ml-auto max-w-[55%] shrink-0 truncate tabular-nums">
|
|
|
|
|
- {row.detail}
|
|
|
|
|
- </span>
|
|
|
|
|
- )}
|
|
|
|
|
- </li>
|
|
|
|
|
- )
|
|
|
|
|
- })}
|
|
|
|
|
- {more > 0 && (
|
|
|
|
|
- <li
|
|
|
|
|
- className={cn(
|
|
|
|
|
- 'text-muted-foreground border-card-edge/45 border-b py-2 pr-5',
|
|
|
|
|
- subjectHasIcons ? 'pl-[2.8125rem]' : 'pl-5'
|
|
|
|
|
- )}
|
|
|
|
|
- >
|
|
|
|
|
- {t('confirmDialog.andMore', { count: more })}
|
|
|
|
|
- </li>
|
|
|
|
|
- )}
|
|
|
|
|
- </ul>
|
|
|
|
|
- )}
|
|
|
|
|
- {consequences.length > 0 && (
|
|
|
|
|
- <ul
|
|
|
|
|
- data-slot="confirm-consequences"
|
|
|
|
|
- className="grid gap-1.5 px-5 pt-3.5 pb-4 text-[13px]"
|
|
|
|
|
- >
|
|
|
|
|
- {consequences.map((line) => {
|
|
|
|
|
- const LineIcon = line.icon ?? CircleAlert
|
|
|
|
|
- return (
|
|
|
|
|
- <li
|
|
|
|
|
- key={line.text}
|
|
|
|
|
- className={cn(
|
|
|
|
|
- 'flex gap-2.5 leading-snug',
|
|
|
|
|
- line.emphasis ? 'font-medium' : 'text-muted-foreground'
|
|
|
|
|
- )}
|
|
|
|
|
- >
|
|
|
|
|
- <LineIcon className="mt-0.5 size-[15px] shrink-0 text-(--confirm-ink)" />
|
|
|
|
|
- <span>{line.text}</span>
|
|
|
|
|
- </li>
|
|
|
|
|
- )
|
|
|
|
|
- })}
|
|
|
|
|
- </ul>
|
|
|
|
|
- )}
|
|
|
|
|
- {mustType && (
|
|
|
|
|
- <div
|
|
|
|
|
- className={cn(
|
|
|
|
|
- 'grid gap-1.5 px-5 pb-4 text-[13px]',
|
|
|
|
|
- consequences.length > 0 ? 'pt-1' : 'pt-3.5'
|
|
|
|
|
- )}
|
|
|
|
|
- >
|
|
|
|
|
- <label htmlFor={`${typeLabelId}-input`} id={typeLabelId}>
|
|
|
|
|
- {t.rich('confirmDialog.typeToConfirm', {
|
|
|
|
|
- text: mustType,
|
|
|
|
|
- b: (chunks) => <b className="font-semibold">{chunks}</b>,
|
|
|
|
|
- })}
|
|
|
|
|
- </label>
|
|
|
|
|
- <Input
|
|
|
|
|
- ref={inputRef}
|
|
|
|
|
- id={`${typeLabelId}-input`}
|
|
|
|
|
- value={typed}
|
|
|
|
|
- autoComplete="off"
|
|
|
|
|
- autoCapitalize="off"
|
|
|
|
|
- spellCheck={false}
|
|
|
|
|
- onChange={(e) => setTyped(e.target.value)}
|
|
|
|
|
- onKeyDown={(e) => {
|
|
|
|
|
- // An Enter that accepts an input method's composition
|
|
|
|
|
- // finishes the text; it is not an answer.
|
|
|
|
|
- if (e.nativeEvent.isComposing || e.keyCode === 229) return
|
|
|
|
|
- if (e.key === 'Enter' && typedMatches) {
|
|
|
|
|
- e.preventDefault()
|
|
|
|
|
- settle(true)
|
|
|
|
|
- }
|
|
|
|
|
- }}
|
|
|
|
|
- />
|
|
|
|
|
- </div>
|
|
|
|
|
- )}
|
|
|
|
|
- </div>
|
|
|
|
|
- </AlertDialogPrimitive.Description>
|
|
|
|
|
-
|
|
|
|
|
- <div className="border-card-edge/60 bg-muted/45 relative flex shrink-0 flex-col-reverse gap-2 border-t px-5 py-3 max-sm:pt-3.5 max-sm:pb-[max(1.25rem,env(safe-area-inset-bottom))] sm:flex-row sm:flex-wrap sm:justify-end">
|
|
|
|
|
- <AlertDialogCancel ref={cancelRef} className="max-sm:h-11 max-sm:text-[15px]">
|
|
|
|
|
- <span>{options.cancelLabel || t('buttons.cancel')}</span>
|
|
|
|
|
- <KeyHint>Esc</KeyHint>
|
|
|
|
|
- </AlertDialogCancel>
|
|
|
|
|
- <AlertDialogAction
|
|
|
|
|
- ref={actionRef}
|
|
|
|
|
- disabled={!typedMatches}
|
|
|
|
|
- onClick={() => settle(true)}
|
|
|
|
|
- // The button's own destructive variant, not a class override: two
|
|
|
|
|
- // background utilities on one element are settled by stylesheet
|
|
|
|
|
- // order, which is how a red button ends up amber.
|
|
|
|
|
- variant={destructive ? 'destructive' : 'default'}
|
|
|
|
|
- className="max-sm:h-11 max-sm:text-[15px]"
|
|
|
|
|
- >
|
|
|
|
|
- <span>{options.confirmLabel || t('buttons.confirm')}</span>
|
|
|
|
|
- {enterConfirms && <KeyHint>↵</KeyHint>}
|
|
|
|
|
- </AlertDialogAction>
|
|
|
|
|
- </div>
|
|
|
|
|
- </AlertDialogPrimitive.Content>
|
|
|
|
|
- </AlertDialogPortal>
|
|
|
|
|
- </AlertDialog>
|
|
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </ConfirmFrame>
|
|
|
</ConfirmContext>
|
|
</ConfirmContext>
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|