Rebuild Tansy UI on Base UI with one API and dark mode (v0.3.0)
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.
This commit is contained in:
1 parent
dc5e6ff800
commit
f01de83733
185 files changed
+22542
-5182
No files matched your search
+122
-128
@@ -1,156 +1,150 @@
|
||||
import { useEffect, useRef, useState, type ComponentProps, type ReactNode } from 'react'
|
||||
import { CircleAlert, CircleCheck, CircleX, Info, TriangleAlert, X } from 'lucide-react'
|
||||
import { cn } from '../lib/cn'
|
||||
import { useControllableState } from '../lib/useControllableState'
|
||||
import { IconButton } from './Button'
|
||||
import {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
forwardRef,
|
||||
type HTMLAttributes,
|
||||
type ReactNode,
|
||||
} from 'react'
|
||||
import * as Icons from 'lucide-react'
|
||||
import { cn } from '@/lib/cn'
|
||||
import {
|
||||
ALERT_COLORS,
|
||||
ALERT_GRADIENT,
|
||||
ALERT_CLOSE_STYLES,
|
||||
ALERT_ICON_STYLES,
|
||||
ALERT_STYLES,
|
||||
ALERT_URGENT_COLORS,
|
||||
type AlertColor,
|
||||
type AlertVariant,
|
||||
} from './alert.tokens'
|
||||
|
||||
/**
|
||||
* Dismissible inline alert.
|
||||
*
|
||||
* Ported from the original `Alert` component.
|
||||
*
|
||||
* `iconName` takes a Lucide icon name as a string (looked up on the module), so
|
||||
* the icon is controllable from Storybook without importing every icon.
|
||||
*/
|
||||
|
||||
export type AlertProps = HTMLAttributes<HTMLDivElement> & {
|
||||
title?: string
|
||||
message?: ReactNode
|
||||
/** Name of a Lucide icon, e.g. `"Info"`, `"CheckCircle2"`. Falls back to `Info`. */
|
||||
iconName?: string
|
||||
onClose?: () => void
|
||||
variant?: AlertVariant
|
||||
export type AlertProps = Omit<ComponentProps<'div'>, 'color' | 'title'> & {
|
||||
/** Semantic colour. `danger` and `warning` use `role="alert"`; the rest are polite `role="status"`. */
|
||||
color?: AlertColor
|
||||
showCloseButton?: boolean
|
||||
width?: string
|
||||
height?: string
|
||||
closeOnOutsideClick?: boolean
|
||||
/** Auto-close delay in ms. `0` disables. */
|
||||
/** Visual weight. */
|
||||
variant?: AlertVariant
|
||||
/** Short bold heading. */
|
||||
title?: ReactNode
|
||||
/** The message. */
|
||||
children?: ReactNode
|
||||
/** Replaces the colour's default icon. */
|
||||
icon?: ReactNode
|
||||
/** Hides the icon. */
|
||||
hideIcon?: boolean
|
||||
/** Buttons or links shown under the message. */
|
||||
actions?: ReactNode
|
||||
/** Shows a close button labelled "Dismiss". */
|
||||
dismissible?: boolean
|
||||
/** Controlled visibility. */
|
||||
open?: boolean
|
||||
/** Initial visibility when uncontrolled. */
|
||||
defaultOpen?: boolean
|
||||
/** Called when the alert is dismissed or auto-closes. */
|
||||
onOpenChange?: (open: boolean) => void
|
||||
/** Closes the alert after this many milliseconds. Pauses while hovered or focused. `0` disables. */
|
||||
autoClose?: number
|
||||
showMessage?: boolean
|
||||
customClose?: ReactNode
|
||||
}
|
||||
|
||||
export const Alert = forwardRef<HTMLDivElement, AlertProps>(function Alert(
|
||||
{
|
||||
title,
|
||||
message,
|
||||
iconName = 'Info',
|
||||
onClose,
|
||||
variant = 'default',
|
||||
color = 'success',
|
||||
className = '',
|
||||
showCloseButton = true,
|
||||
width = 'w-full',
|
||||
height = 'h-auto',
|
||||
closeOnOutsideClick = false,
|
||||
autoClose = 0,
|
||||
showMessage = true,
|
||||
customClose,
|
||||
...props
|
||||
},
|
||||
forwardedRef,
|
||||
) {
|
||||
const [visible, setVisible] = useState(true)
|
||||
const internalRef = useRef<HTMLDivElement>(null)
|
||||
const DEFAULT_ICONS: Record<AlertColor, ReactNode> = {
|
||||
info: <Info />,
|
||||
success: <CircleCheck />,
|
||||
warning: <TriangleAlert />,
|
||||
danger: <CircleX />,
|
||||
neutral: <Info />,
|
||||
primary: <CircleAlert />,
|
||||
}
|
||||
|
||||
const handleClose = useCallback(() => {
|
||||
setVisible(false)
|
||||
onClose?.()
|
||||
}, [onClose])
|
||||
|
||||
// Mirror the internal node onto the forwarded ref.
|
||||
/** Counts down `delay` ms, pausing while `paused` is true, then calls `onDone`. */
|
||||
function useAutoClose(delay: number, paused: boolean, active: boolean, onDone: () => void) {
|
||||
const remaining = useRef(delay)
|
||||
const onDoneRef = useRef(onDone)
|
||||
useEffect(() => {
|
||||
const node = internalRef.current
|
||||
if (typeof forwardedRef === 'function') forwardedRef(node)
|
||||
else if (forwardedRef) forwardedRef.current = node
|
||||
}, [forwardedRef])
|
||||
|
||||
onDoneRef.current = onDone
|
||||
})
|
||||
useEffect(() => {
|
||||
if (autoClose > 0) {
|
||||
const timer = setTimeout(handleClose, autoClose)
|
||||
return () => clearTimeout(timer)
|
||||
remaining.current = delay
|
||||
}, [delay])
|
||||
useEffect(() => {
|
||||
if (!active || delay <= 0 || paused) return
|
||||
const started = Date.now()
|
||||
const timer = setTimeout(() => onDoneRef.current(), remaining.current)
|
||||
return () => {
|
||||
clearTimeout(timer)
|
||||
remaining.current = Math.max(0, remaining.current - (Date.now() - started))
|
||||
}
|
||||
}, [autoClose, handleClose])
|
||||
}, [active, delay, paused])
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!closeOnOutsideClick) return
|
||||
/** An inline status message, optionally dismissible or auto-closing. */
|
||||
export function Alert({
|
||||
color = 'info',
|
||||
variant = 'soft',
|
||||
title,
|
||||
children,
|
||||
icon,
|
||||
hideIcon = false,
|
||||
actions,
|
||||
dismissible = false,
|
||||
open: openProp,
|
||||
defaultOpen = true,
|
||||
onOpenChange,
|
||||
autoClose = 0,
|
||||
className,
|
||||
onMouseEnter,
|
||||
onMouseLeave,
|
||||
onFocus,
|
||||
onBlur,
|
||||
...props
|
||||
}: AlertProps) {
|
||||
const [open, setOpen] = useControllableState(openProp, defaultOpen, onOpenChange)
|
||||
const [hovered, setHovered] = useState(false)
|
||||
const [focused, setFocused] = useState(false)
|
||||
|
||||
const handleClickOutside = (event: MouseEvent) => {
|
||||
if (internalRef.current && !internalRef.current.contains(event.target as Node)) {
|
||||
handleClose()
|
||||
}
|
||||
}
|
||||
useAutoClose(autoClose, hovered || focused, open, () => setOpen(false))
|
||||
|
||||
document.addEventListener('mousedown', handleClickOutside)
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside)
|
||||
}, [closeOnOutsideClick, handleClose])
|
||||
if (!open) return null
|
||||
|
||||
if (!visible) return null
|
||||
|
||||
const iconMap = Icons as unknown as Record<string, Icons.LucideIcon>
|
||||
const LucideIcon = (iconName && iconMap[iconName]) || Icons.Info
|
||||
const theme = ALERT_COLORS[color] ?? ALERT_COLORS.success
|
||||
|
||||
let variantClasses: string
|
||||
if (variant === 'filled') {
|
||||
variantClasses = cn(theme.bg, 'text-white')
|
||||
} else if (variant === 'outlined') {
|
||||
variantClasses = cn('bg-transparent', theme.text, theme.border, 'border')
|
||||
} else if (variant === 'gradient') {
|
||||
variantClasses = ALERT_GRADIENT
|
||||
} else {
|
||||
variantClasses = cn(theme.lightBg, theme.text)
|
||||
}
|
||||
const urgent = ALERT_URGENT_COLORS.includes(color)
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={internalRef}
|
||||
role="alert"
|
||||
role={urgent ? 'alert' : 'status'}
|
||||
className={cn(
|
||||
'rounded-md p-3 transition-all duration-300',
|
||||
width,
|
||||
height,
|
||||
variantClasses,
|
||||
'flex w-full items-start gap-3 rounded-xl border p-4 text-sm leading-5',
|
||||
ALERT_STYLES[variant][color],
|
||||
className,
|
||||
)}
|
||||
onMouseEnter={(e) => {
|
||||
setHovered(true)
|
||||
onMouseEnter?.(e)
|
||||
}}
|
||||
onMouseLeave={(e) => {
|
||||
setHovered(false)
|
||||
onMouseLeave?.(e)
|
||||
}}
|
||||
onFocus={(e) => {
|
||||
setFocused(true)
|
||||
onFocus?.(e)
|
||||
}}
|
||||
onBlur={(e) => {
|
||||
if (!e.currentTarget.contains(e.relatedTarget as Node | null)) setFocused(false)
|
||||
onBlur?.(e)
|
||||
}}
|
||||
{...props}
|
||||
>
|
||||
<div className="flex justify-between gap-3">
|
||||
<div className="flex items-start gap-3 w-full">
|
||||
{LucideIcon && <LucideIcon className="w-5 h-5 mt-0.5 shrink-0" aria-hidden="true" />}
|
||||
|
||||
<div className="flex flex-col gap-1 w-full">
|
||||
{title && <h5 className="font-semibold text-sm leading-tight">{title}</h5>}
|
||||
{showMessage && message && <p className="text-sm opacity-90 leading-normal">{message}</p>}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{showCloseButton && (
|
||||
<button
|
||||
onClick={handleClose}
|
||||
className="shrink-0 p-1 rounded-md hover:bg-black/10 transition-colors cursor-pointer"
|
||||
type="button"
|
||||
aria-label="Close alert"
|
||||
>
|
||||
{customClose ?? <Icons.X className="w-4 h-4" />}
|
||||
</button>
|
||||
)}
|
||||
{!hideIcon && (
|
||||
<span aria-hidden="true" className={cn('flex shrink-0 [&_svg]:size-5', ALERT_ICON_STYLES[variant][color])}>
|
||||
{icon ?? DEFAULT_ICONS[color]}
|
||||
</span>
|
||||
)}
|
||||
<div className="min-w-0 flex-1">
|
||||
{title != null && <p className="font-semibold">{title}</p>}
|
||||
{children != null && <div className={cn(title != null && 'mt-1')}>{children}</div>}
|
||||
{actions != null && <div className="mt-3 flex flex-wrap items-center gap-2">{actions}</div>}
|
||||
</div>
|
||||
{dismissible && (
|
||||
<IconButton
|
||||
size="sm"
|
||||
aria-label="Dismiss"
|
||||
icon={<X />}
|
||||
onClick={() => setOpen(false)}
|
||||
className={cn('-my-1.5 -mr-1.5 size-8', ALERT_CLOSE_STYLES[variant])}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})
|
||||
|
||||
export default Alert
|
||||
}
|
||||
Reference in new issue
Block a user