Every component is rebuilt on @base-ui/react with a single prop API, token-only colours and dark mode, and 28 new components are added. Components - New: Field, FeaturedTextArea, NumberInput, Combobox, CheckboxGroup, Slider, ToggleGroup/Toggle, IconButton, Dialog/Modal, AlertDialog, Drawer/Sheet, Popover, InfoTip, DropdownMenu, Tabs, Breadcrumb, Pagination, Toast (Toaster, useToast), Progress, Meter, Skeleton, CountBadge, Avatar/AvatarGroup, Kbd, Separator, Table - Rewritten: Button, Input, FeaturedInput, TextArea, Select, FeaturedSelect (real ARIA combobox), NativeSelect, Checkbox, Radio, Switch, Tooltip, Accordion, Alert, Spinner, Badge, Card - Removed TextField (use Input) and sonner (use Toast) Foundations - Semantic tokens (surfaces, text, borders, status ramps) in src/styles/tokens.css, with class-based dark mode - Visible focus rings, linked labels/descriptions/errors, alert roles by severity, keyboard support from Base UI - Fixed the Classic Blue ramp; Emerald, Amber and Teal 500 steps now carry white text at AA - Library build (per-component ES modules, types, tokens.css, exports) Quality - Vitest, Testing Library and axe tests for every component (289 tests) - scripts/check-stories.mjs renders every story in Chrome and fails on errors or axe violations, including contrast; all 423 stories pass in light and dark mode and in all seven palettes - Storybook: controls for every prop, open states for every popup, light/dark toolbar, Introduction guides, docs/CONVENTIONS.md BREAKING CHANGE: component props changed across the kit; see CHANGELOG.md.
150 lines
5.2 KiB
TypeScript
150 lines
5.2 KiB
TypeScript
import { AlertDialog as BaseAlertDialog } from '@base-ui/react/alert-dialog'
|
|
import { useState, type ReactElement, type ReactNode } from 'react'
|
|
import { cn } from '../lib/cn'
|
|
import { useControllableState } from '../lib/useControllableState'
|
|
import { Button } from './Button'
|
|
import {
|
|
ALERT_ICON_STYLES,
|
|
backdropClass,
|
|
dialogDescriptionClass,
|
|
dialogPanelClass,
|
|
dialogTitleClass,
|
|
type AlertDialogColor,
|
|
} from './dialog.tokens'
|
|
|
|
export type AlertDialogProps = {
|
|
/** Controlled open state. Pair with `onOpenChange`. */
|
|
open?: boolean
|
|
/** Initial open state when uncontrolled. */
|
|
defaultOpen?: boolean
|
|
/** Fires with the next open state. */
|
|
onOpenChange?: (open: boolean) => void
|
|
/** Element that opens the dialog, usually a `<Button>`. */
|
|
trigger?: ReactElement
|
|
/** The question, e.g. "Delete this project?". Also the accessible name. */
|
|
title: ReactNode
|
|
/** What happens if the user confirms. */
|
|
description?: ReactNode
|
|
/** Label of the confirm button. */
|
|
confirmLabel?: ReactNode
|
|
/** Label of the cancel button. */
|
|
cancelLabel?: ReactNode
|
|
/** Label of the confirm button while `onConfirm` is pending. Defaults to `confirmLabel`. */
|
|
loadingLabel?: ReactNode
|
|
/** Colour of the confirm button and icon. `danger` for destructive actions. */
|
|
color?: AlertDialogColor
|
|
/**
|
|
* Runs when the user confirms. Return a Promise to keep the dialog open with
|
|
* the confirm button loading until it resolves; then the dialog closes. If it
|
|
* rejects, the dialog stays open so you can show the error.
|
|
*/
|
|
onConfirm?: () => void | Promise<unknown>
|
|
/** Fires when the user cancels (button or Escape). */
|
|
onCancel?: () => void
|
|
/** Icon shown in a tinted circle beside the text. */
|
|
icon?: ReactNode
|
|
/** Extra content under the description, such as a list of affected items. */
|
|
children?: ReactNode
|
|
/** Classes for the panel. */
|
|
className?: string
|
|
}
|
|
|
|
function isPromise(value: unknown): value is Promise<unknown> {
|
|
return typeof (value as Promise<unknown> | undefined)?.then === 'function'
|
|
}
|
|
|
|
/**
|
|
* Asks the user to confirm an action (Base UI AlertDialog). It doesn't close
|
|
* on outside clicks, focus starts on Cancel, and an async `onConfirm` shows a
|
|
* loading confirm button until it settles.
|
|
*/
|
|
export function AlertDialog({
|
|
open: openProp,
|
|
defaultOpen = false,
|
|
onOpenChange,
|
|
trigger,
|
|
title,
|
|
description,
|
|
confirmLabel = 'Confirm',
|
|
cancelLabel = 'Cancel',
|
|
loadingLabel,
|
|
color = 'primary',
|
|
onConfirm,
|
|
onCancel,
|
|
icon,
|
|
children,
|
|
className,
|
|
}: AlertDialogProps) {
|
|
const [open, setOpen] = useControllableState(openProp, defaultOpen, onOpenChange)
|
|
const [pending, setPending] = useState(false)
|
|
|
|
function handleOpenChange(next: boolean, details: BaseAlertDialog.Root.ChangeEventDetails) {
|
|
if (!next && pending) {
|
|
details.cancel()
|
|
return
|
|
}
|
|
if (!next) onCancel?.()
|
|
setOpen(next)
|
|
}
|
|
|
|
async function handleConfirm() {
|
|
const result = onConfirm?.()
|
|
if (!isPromise(result)) {
|
|
setOpen(false)
|
|
return
|
|
}
|
|
setPending(true)
|
|
try {
|
|
await result
|
|
setOpen(false)
|
|
} catch {
|
|
// Stay open: the caller shows the error and the user can retry or cancel.
|
|
} finally {
|
|
setPending(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<BaseAlertDialog.Root open={open} onOpenChange={handleOpenChange}>
|
|
{trigger && <BaseAlertDialog.Trigger render={trigger} />}
|
|
<BaseAlertDialog.Portal>
|
|
<BaseAlertDialog.Backdrop className={cn(backdropClass, 'z-50')} />
|
|
<BaseAlertDialog.Viewport className="fixed inset-0 z-50 flex items-center justify-center p-4 sm:p-6">
|
|
<BaseAlertDialog.Popup className={cn(dialogPanelClass, 'max-w-md', className)}>
|
|
<div className="flex flex-col gap-4 p-5 sm:flex-row sm:items-start sm:p-6">
|
|
{icon != null && (
|
|
<span
|
|
aria-hidden="true"
|
|
className={cn(
|
|
'flex size-10 shrink-0 items-center justify-center rounded-full [&_svg]:size-5',
|
|
ALERT_ICON_STYLES[color],
|
|
)}
|
|
>
|
|
{icon}
|
|
</span>
|
|
)}
|
|
<div className="flex min-w-0 flex-col gap-1.5">
|
|
<BaseAlertDialog.Title className={dialogTitleClass}>{title}</BaseAlertDialog.Title>
|
|
{description != null && (
|
|
<BaseAlertDialog.Description className={dialogDescriptionClass}>
|
|
{description}
|
|
</BaseAlertDialog.Description>
|
|
)}
|
|
{children != null && <div className="mt-2 text-sm leading-6 text-fg">{children}</div>}
|
|
</div>
|
|
</div>
|
|
<div className="flex flex-col-reverse gap-2 border-t border-border bg-surface-muted px-5 py-4 sm:flex-row sm:justify-end sm:gap-3 sm:px-6">
|
|
<BaseAlertDialog.Close disabled={pending} render={<Button variant="outline" color="neutral" />}>
|
|
{cancelLabel}
|
|
</BaseAlertDialog.Close>
|
|
<Button color={color} loading={pending} loadingText={loadingLabel} onClick={handleConfirm}>
|
|
{confirmLabel}
|
|
</Button>
|
|
</div>
|
|
</BaseAlertDialog.Popup>
|
|
</BaseAlertDialog.Viewport>
|
|
</BaseAlertDialog.Portal>
|
|
</BaseAlertDialog.Root>
|
|
)
|
|
}
|