mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 23:30:00 +08:00
feat(web): establish accessible design primitives
This commit is contained in:
@@ -43,6 +43,17 @@
|
||||
--input: #e6decc;
|
||||
--ring: #b0501e;
|
||||
--overlay: rgba(28, 24, 22, 0.42);
|
||||
--success: #237a4b;
|
||||
--success-foreground: #14532d;
|
||||
--success-surface: #ecf8f0;
|
||||
--warning: #a15c00;
|
||||
--warning-foreground: #713f12;
|
||||
--warning-surface: #fff7df;
|
||||
--info: #2563eb;
|
||||
--info-foreground: #1e3a8a;
|
||||
--info-surface: #eff6ff;
|
||||
--surface-raised: #ffffff;
|
||||
--shadow-raised: 0 16px 40px rgba(69, 49, 31, 0.12);
|
||||
}
|
||||
|
||||
/* Dark — warm near-black, same terracotta family as Cream. */
|
||||
@@ -68,6 +79,17 @@
|
||||
--input: #3a3634;
|
||||
--ring: #d4734b;
|
||||
--overlay: rgba(0, 0, 0, 0.6);
|
||||
--success: #5fd08a;
|
||||
--success-foreground: #bbf7d0;
|
||||
--success-surface: #173424;
|
||||
--warning: #f2b84b;
|
||||
--warning-foreground: #fde68a;
|
||||
--warning-surface: #3a2c16;
|
||||
--info: #7ba7ff;
|
||||
--info-foreground: #bfdbfe;
|
||||
--info-surface: #172641;
|
||||
--surface-raised: #2a2826;
|
||||
--shadow-raised: 0 16px 44px rgba(0, 0, 0, 0.32);
|
||||
}
|
||||
|
||||
/* Default — pure-white neutral canvas (theme id "snow" kept for stored
|
||||
@@ -96,6 +118,17 @@
|
||||
--input: #e5e5e5;
|
||||
--ring: #2563eb;
|
||||
--overlay: rgba(0, 0, 0, 0.4);
|
||||
--success: #16794b;
|
||||
--success-foreground: #14532d;
|
||||
--success-surface: #ecfdf3;
|
||||
--warning: #a15c00;
|
||||
--warning-foreground: #713f12;
|
||||
--warning-surface: #fffaeb;
|
||||
--info: #2563eb;
|
||||
--info-foreground: #1e3a8a;
|
||||
--info-surface: #eff6ff;
|
||||
--surface-raised: #ffffff;
|
||||
--shadow-raised: 0 16px 40px rgba(13, 13, 13, 0.12);
|
||||
}
|
||||
|
||||
/* Glass — translucent purple panels on near-black. Slightly brighter
|
||||
@@ -116,10 +149,23 @@
|
||||
--muted-foreground: #b5b5c7;
|
||||
--accent: rgba(255, 255, 255, 0.12);
|
||||
--accent-foreground: #ffffff;
|
||||
--destructive: #f07178;
|
||||
--destructive-foreground: #ffffff;
|
||||
--border: rgba(255, 255, 255, 0.12);
|
||||
--input: rgba(255, 255, 255, 0.06);
|
||||
--ring: #a855f7;
|
||||
--overlay: rgba(0, 0, 0, 0.55);
|
||||
--success: #68dfa0;
|
||||
--success-foreground: #bbf7d0;
|
||||
--success-surface: rgba(34, 197, 94, 0.14);
|
||||
--warning: #f7c65a;
|
||||
--warning-foreground: #fde68a;
|
||||
--warning-surface: rgba(245, 158, 11, 0.16);
|
||||
--info: #9ab7ff;
|
||||
--info-foreground: #dbeafe;
|
||||
--info-surface: rgba(59, 130, 246, 0.16);
|
||||
--surface-raised: rgba(35, 32, 51, 0.96);
|
||||
--shadow-raised: 0 18px 48px rgba(0, 0, 0, 0.42);
|
||||
}
|
||||
|
||||
.theme-glass body {
|
||||
@@ -167,6 +213,9 @@
|
||||
|
||||
html {
|
||||
-webkit-font-smoothing: antialiased;
|
||||
--motion-fast: 120ms;
|
||||
--motion-base: 180ms;
|
||||
--motion-slow: 240ms;
|
||||
}
|
||||
|
||||
html[data-scroll-behavior="smooth"] {
|
||||
@@ -189,6 +238,22 @@
|
||||
summary::-webkit-details-marker {
|
||||
display: none;
|
||||
}
|
||||
|
||||
:focus-visible {
|
||||
outline: 2px solid var(--ring);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
*,
|
||||
*::before,
|
||||
*::after {
|
||||
scroll-behavior: auto !important;
|
||||
animation-duration: 0.01ms !important;
|
||||
animation-iteration-count: 1 !important;
|
||||
transition-duration: 0.01ms !important;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@layer components {
|
||||
@@ -199,6 +264,11 @@
|
||||
.muted-chip {
|
||||
@apply rounded-full bg-[var(--muted)] px-3 py-1 text-xs text-[var(--muted-foreground)];
|
||||
}
|
||||
|
||||
.surface-raised {
|
||||
background: var(--surface-raised);
|
||||
box-shadow: var(--shadow-raised);
|
||||
}
|
||||
}
|
||||
|
||||
@layer utilities {
|
||||
|
||||
@@ -1,64 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import { Loader2 } from "lucide-react";
|
||||
|
||||
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
||||
variant?: "primary" | "secondary" | "danger" | "ghost";
|
||||
size?: "sm" | "md" | "lg";
|
||||
loading?: boolean;
|
||||
icon?: React.ReactNode;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
const variantStyles = {
|
||||
primary:
|
||||
"bg-[var(--primary)] text-[var(--primary-foreground)] hover:opacity-90 shadow-md shadow-[var(--primary)]/15",
|
||||
secondary:
|
||||
"bg-[var(--secondary)] text-[var(--secondary-foreground)] hover:bg-[var(--muted)]",
|
||||
danger:
|
||||
"bg-[var(--destructive)] text-[var(--destructive-foreground)] hover:opacity-90 shadow-md shadow-[var(--destructive)]/15",
|
||||
ghost:
|
||||
"text-[var(--muted-foreground)] hover:bg-[var(--muted)] hover:text-[var(--foreground)]",
|
||||
};
|
||||
|
||||
const sizeStyles = {
|
||||
sm: "px-3 py-1.5 text-xs",
|
||||
md: "px-4 py-2 text-sm",
|
||||
lg: "px-6 py-3 text-base",
|
||||
};
|
||||
|
||||
export default function Button({
|
||||
variant = "primary",
|
||||
size = "md",
|
||||
loading = false,
|
||||
icon,
|
||||
children,
|
||||
className = "",
|
||||
disabled,
|
||||
...props
|
||||
}: ButtonProps) {
|
||||
return (
|
||||
<button
|
||||
className={`
|
||||
inline-flex items-center justify-center gap-2 font-medium rounded-lg transition-all
|
||||
focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--ring)]
|
||||
focus-visible:ring-offset-2 focus-visible:ring-offset-[var(--background)]
|
||||
disabled:opacity-50 disabled:cursor-not-allowed
|
||||
${variantStyles[variant]}
|
||||
${sizeStyles[size]}
|
||||
${className}
|
||||
`}
|
||||
disabled={disabled || loading}
|
||||
aria-busy={loading || undefined}
|
||||
{...props}
|
||||
>
|
||||
{loading ? (
|
||||
<Loader2 aria-hidden="true" className="w-4 h-4 animate-spin" />
|
||||
) : icon ? (
|
||||
icon
|
||||
) : null}
|
||||
{children}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
export { Button as default, Button } from "@/shared/ui/Button";
|
||||
export type {
|
||||
ButtonProps,
|
||||
ButtonSize,
|
||||
ButtonVariant,
|
||||
} from "@/shared/ui/Button";
|
||||
|
||||
70
web/shared/ui/Button.tsx
Normal file
70
web/shared/ui/Button.tsx
Normal file
@@ -0,0 +1,70 @@
|
||||
"use client";
|
||||
|
||||
import { forwardRef, type ButtonHTMLAttributes, type ReactNode } from "react";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { cn } from "./styles";
|
||||
|
||||
export type ButtonVariant = "primary" | "secondary" | "danger" | "ghost";
|
||||
export type ButtonSize = "sm" | "md" | "lg";
|
||||
|
||||
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
||||
variant?: ButtonVariant;
|
||||
size?: ButtonSize;
|
||||
loading?: boolean;
|
||||
icon?: ReactNode;
|
||||
}
|
||||
|
||||
const variants: Record<ButtonVariant, string> = {
|
||||
primary:
|
||||
"bg-primary text-primary-foreground shadow-sm hover:brightness-95 active:brightness-90",
|
||||
secondary:
|
||||
"border border-border bg-secondary text-secondary-foreground hover:bg-muted active:bg-accent",
|
||||
danger:
|
||||
"bg-destructive text-destructive-foreground shadow-sm hover:brightness-95 active:brightness-90",
|
||||
ghost:
|
||||
"text-muted-foreground hover:bg-muted hover:text-foreground active:bg-accent",
|
||||
};
|
||||
|
||||
const sizes: Record<ButtonSize, string> = {
|
||||
sm: "min-h-8 rounded-lg px-3 text-xs",
|
||||
md: "min-h-10 rounded-xl px-4 text-sm",
|
||||
lg: "min-h-12 rounded-xl px-5 text-base",
|
||||
};
|
||||
|
||||
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(
|
||||
{
|
||||
variant = "primary",
|
||||
size = "md",
|
||||
loading = false,
|
||||
icon,
|
||||
children,
|
||||
className,
|
||||
disabled,
|
||||
type = "button",
|
||||
...props
|
||||
},
|
||||
ref,
|
||||
) {
|
||||
return (
|
||||
<button
|
||||
ref={ref}
|
||||
type={type}
|
||||
className={cn(
|
||||
"inline-flex items-center justify-center gap-2 font-medium transition-[background-color,color,box-shadow,filter] duration-[var(--motion-fast)]",
|
||||
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
|
||||
"disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50",
|
||||
variants[variant],
|
||||
sizes[size],
|
||||
className,
|
||||
)}
|
||||
disabled={disabled || loading}
|
||||
aria-busy={loading || undefined}
|
||||
{...props}
|
||||
>
|
||||
{loading ? <Loader2 aria-hidden className="h-4 w-4 animate-spin" /> : icon}
|
||||
{children}
|
||||
</button>
|
||||
);
|
||||
});
|
||||
|
||||
export default Button;
|
||||
176
web/shared/ui/Dialog.tsx
Normal file
176
web/shared/ui/Dialog.tsx
Normal file
@@ -0,0 +1,176 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
useEffect,
|
||||
useId,
|
||||
useRef,
|
||||
type KeyboardEvent,
|
||||
type MouseEvent,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import { X } from "lucide-react";
|
||||
import { IconButton } from "./IconButton";
|
||||
import { cn } from "./styles";
|
||||
|
||||
const FOCUSABLE =
|
||||
'a[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), [tabindex]:not([tabindex="-1"]), [contenteditable="true"]';
|
||||
|
||||
export interface DialogProps {
|
||||
open: boolean;
|
||||
title: string;
|
||||
children: ReactNode;
|
||||
onClose: () => void;
|
||||
description?: string;
|
||||
footer?: ReactNode;
|
||||
size?: "sm" | "md" | "lg" | "xl";
|
||||
closeLabel?: string;
|
||||
closeOnBackdrop?: boolean;
|
||||
closeOnEscape?: boolean;
|
||||
busy?: boolean;
|
||||
alert?: boolean;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
const widths = {
|
||||
sm: "max-w-sm",
|
||||
md: "max-w-lg",
|
||||
lg: "max-w-2xl",
|
||||
xl: "max-w-4xl",
|
||||
};
|
||||
|
||||
export function Dialog({
|
||||
open,
|
||||
title,
|
||||
children,
|
||||
onClose,
|
||||
description,
|
||||
footer,
|
||||
size = "md",
|
||||
closeLabel = "Close",
|
||||
closeOnBackdrop = true,
|
||||
closeOnEscape = true,
|
||||
busy = false,
|
||||
alert = false,
|
||||
className,
|
||||
}: DialogProps) {
|
||||
const dialogRef = useRef<HTMLDivElement>(null);
|
||||
const titleId = useId();
|
||||
const descriptionId = useId();
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
const previous = document.activeElement as HTMLElement | null;
|
||||
const previousOverflow = document.body.style.overflow;
|
||||
document.body.style.overflow = "hidden";
|
||||
const frame = requestAnimationFrame(() => {
|
||||
const root = dialogRef.current;
|
||||
const target =
|
||||
root?.querySelector<HTMLElement>("[data-autofocus]") ??
|
||||
root?.querySelector<HTMLElement>(FOCUSABLE) ??
|
||||
root;
|
||||
target?.focus();
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelAnimationFrame(frame);
|
||||
document.body.style.overflow = previousOverflow;
|
||||
if (previous && document.contains(previous)) previous.focus();
|
||||
};
|
||||
}, [open]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || !closeOnEscape || busy) return;
|
||||
const handleEscape = (event: globalThis.KeyboardEvent) => {
|
||||
if (event.key !== "Escape") return;
|
||||
event.stopPropagation();
|
||||
onClose();
|
||||
};
|
||||
window.addEventListener("keydown", handleEscape);
|
||||
return () => window.removeEventListener("keydown", handleEscape);
|
||||
}, [busy, closeOnEscape, onClose, open]);
|
||||
|
||||
if (!open) return null;
|
||||
|
||||
const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
|
||||
if (event.key !== "Tab") return;
|
||||
const focusable = Array.from(
|
||||
dialogRef.current?.querySelectorAll<HTMLElement>(FOCUSABLE) ?? [],
|
||||
);
|
||||
if (focusable.length === 0) {
|
||||
event.preventDefault();
|
||||
dialogRef.current?.focus();
|
||||
return;
|
||||
}
|
||||
const first = focusable[0];
|
||||
const last = focusable[focusable.length - 1];
|
||||
if (event.shiftKey && document.activeElement === first) {
|
||||
event.preventDefault();
|
||||
last.focus();
|
||||
} else if (!event.shiftKey && document.activeElement === last) {
|
||||
event.preventDefault();
|
||||
first.focus();
|
||||
}
|
||||
};
|
||||
|
||||
const handleBackdrop = (event: MouseEvent<HTMLDivElement>) => {
|
||||
if (
|
||||
event.target === event.currentTarget &&
|
||||
closeOnBackdrop &&
|
||||
!busy
|
||||
) {
|
||||
onClose();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
className="fixed inset-0 z-[100] flex items-center justify-center bg-[var(--overlay)] p-4 backdrop-blur-[2px]"
|
||||
onMouseDown={handleBackdrop}
|
||||
>
|
||||
<div
|
||||
ref={dialogRef}
|
||||
role={alert ? "alertdialog" : "dialog"}
|
||||
aria-modal="true"
|
||||
aria-labelledby={titleId}
|
||||
aria-describedby={description ? descriptionId : undefined}
|
||||
aria-busy={busy || undefined}
|
||||
tabIndex={-1}
|
||||
onKeyDown={handleKeyDown}
|
||||
className={cn(
|
||||
"surface-raised flex max-h-[min(90vh,52rem)] w-full flex-col overflow-hidden rounded-2xl border border-border text-foreground",
|
||||
widths[size],
|
||||
className,
|
||||
)}
|
||||
>
|
||||
<header className="flex items-start justify-between gap-4 border-b border-border px-5 py-4">
|
||||
<div className="min-w-0">
|
||||
<h2 id={titleId} className="text-base font-semibold leading-6">
|
||||
{title}
|
||||
</h2>
|
||||
{description ? (
|
||||
<p
|
||||
id={descriptionId}
|
||||
className="mt-1 text-sm leading-5 text-muted-foreground"
|
||||
>
|
||||
{description}
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
<IconButton
|
||||
label={closeLabel}
|
||||
icon={<X aria-hidden className="h-4 w-4" />}
|
||||
size="sm"
|
||||
disabled={busy}
|
||||
onClick={onClose}
|
||||
/>
|
||||
</header>
|
||||
<div className="min-h-0 flex-1 overflow-y-auto px-5 py-4">{children}</div>
|
||||
{footer ? (
|
||||
<footer className="flex shrink-0 items-center justify-end gap-2 border-t border-border px-5 py-4">
|
||||
{footer}
|
||||
</footer>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
45
web/shared/ui/EmptyState.tsx
Normal file
45
web/shared/ui/EmptyState.tsx
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { ReactNode } from "react";
|
||||
import { cn } from "./styles";
|
||||
|
||||
export interface EmptyStateProps {
|
||||
title: string;
|
||||
description?: string;
|
||||
icon?: ReactNode;
|
||||
action?: ReactNode;
|
||||
compact?: boolean;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function EmptyState({
|
||||
title,
|
||||
description,
|
||||
icon,
|
||||
action,
|
||||
compact = false,
|
||||
className,
|
||||
}: EmptyStateProps) {
|
||||
return (
|
||||
<section
|
||||
className={cn(
|
||||
"flex flex-col items-center justify-center text-center",
|
||||
compact ? "gap-2 px-4 py-6" : "gap-3 px-6 py-12",
|
||||
className,
|
||||
)}
|
||||
>
|
||||
{icon ? (
|
||||
<div className="flex h-11 w-11 items-center justify-center rounded-2xl bg-muted text-muted-foreground">
|
||||
{icon}
|
||||
</div>
|
||||
) : null}
|
||||
<div>
|
||||
<h3 className="text-sm font-semibold text-foreground">{title}</h3>
|
||||
{description ? (
|
||||
<p className="mt-1 max-w-sm text-sm leading-5 text-muted-foreground">
|
||||
{description}
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
{action ? <div className="mt-1">{action}</div> : null}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
65
web/shared/ui/Field.tsx
Normal file
65
web/shared/ui/Field.tsx
Normal file
@@ -0,0 +1,65 @@
|
||||
import { useId, type ReactElement, type ReactNode } from "react";
|
||||
import { cloneElement } from "react";
|
||||
import { cn } from "./styles";
|
||||
|
||||
export interface FieldProps {
|
||||
label: string;
|
||||
children: ReactElement<{
|
||||
id?: string;
|
||||
"aria-describedby"?: string;
|
||||
"aria-invalid"?: boolean;
|
||||
}>;
|
||||
hint?: ReactNode;
|
||||
error?: ReactNode;
|
||||
optionalLabel?: string;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function Field({
|
||||
label,
|
||||
children,
|
||||
hint,
|
||||
error,
|
||||
optionalLabel,
|
||||
className,
|
||||
}: FieldProps) {
|
||||
const generatedId = useId();
|
||||
const controlId = children.props.id ?? generatedId;
|
||||
const hintId = `${generatedId}-hint`;
|
||||
const errorId = `${generatedId}-error`;
|
||||
const describedBy = [
|
||||
children.props["aria-describedby"],
|
||||
hint ? hintId : undefined,
|
||||
error ? errorId : undefined,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" ");
|
||||
|
||||
return (
|
||||
<div className={cn("grid gap-1.5", className)}>
|
||||
<label htmlFor={controlId} className="text-sm font-medium text-foreground">
|
||||
{label}
|
||||
{optionalLabel ? (
|
||||
<span className="ml-1 font-normal text-muted-foreground">
|
||||
{optionalLabel}
|
||||
</span>
|
||||
) : null}
|
||||
</label>
|
||||
{cloneElement(children, {
|
||||
id: controlId,
|
||||
"aria-describedby": describedBy || undefined,
|
||||
"aria-invalid": error ? true : children.props["aria-invalid"],
|
||||
})}
|
||||
{hint ? (
|
||||
<p id={hintId} className="text-xs leading-5 text-muted-foreground">
|
||||
{hint}
|
||||
</p>
|
||||
) : null}
|
||||
{error ? (
|
||||
<p id={errorId} className="text-xs font-medium leading-5 text-destructive">
|
||||
{error}
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
54
web/shared/ui/IconButton.tsx
Normal file
54
web/shared/ui/IconButton.tsx
Normal file
@@ -0,0 +1,54 @@
|
||||
"use client";
|
||||
|
||||
import { forwardRef, type ButtonHTMLAttributes, type ReactNode } from "react";
|
||||
import { Tooltip } from "./Tooltip";
|
||||
import { cn } from "./styles";
|
||||
|
||||
export interface IconButtonProps
|
||||
extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "children"> {
|
||||
label: string;
|
||||
icon: ReactNode;
|
||||
size?: "sm" | "md" | "lg";
|
||||
tooltipSide?: "top" | "right" | "bottom" | "left";
|
||||
}
|
||||
|
||||
const sizes = {
|
||||
sm: "h-8 w-8 rounded-lg",
|
||||
md: "h-10 w-10 rounded-xl",
|
||||
lg: "h-12 w-12 rounded-xl",
|
||||
};
|
||||
|
||||
export const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>(
|
||||
function IconButton(
|
||||
{
|
||||
label,
|
||||
icon,
|
||||
size = "md",
|
||||
tooltipSide = "bottom",
|
||||
className,
|
||||
type = "button",
|
||||
...props
|
||||
},
|
||||
ref,
|
||||
) {
|
||||
return (
|
||||
<Tooltip label={label} side={tooltipSide} disabled={props.disabled}>
|
||||
<button
|
||||
ref={ref}
|
||||
type={type}
|
||||
aria-label={label}
|
||||
className={cn(
|
||||
"inline-flex shrink-0 items-center justify-center text-muted-foreground transition-colors duration-[var(--motion-fast)] hover:bg-muted hover:text-foreground",
|
||||
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
|
||||
"disabled:pointer-events-none disabled:opacity-45",
|
||||
sizes[size],
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{icon}
|
||||
</button>
|
||||
</Tooltip>
|
||||
);
|
||||
},
|
||||
);
|
||||
53
web/shared/ui/InlineAlert.tsx
Normal file
53
web/shared/ui/InlineAlert.tsx
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { HTMLAttributes, ReactNode } from "react";
|
||||
import { AlertCircle, CheckCircle2, Info, TriangleAlert } from "lucide-react";
|
||||
import { cn } from "./styles";
|
||||
import type { StatusTone } from "./StatusChip";
|
||||
|
||||
export interface InlineAlertProps extends HTMLAttributes<HTMLDivElement> {
|
||||
tone?: Exclude<StatusTone, "neutral">;
|
||||
title?: string;
|
||||
action?: ReactNode;
|
||||
}
|
||||
|
||||
const toneStyles = {
|
||||
info: "border-info/20 bg-info-surface text-info-foreground",
|
||||
success: "border-success/20 bg-success-surface text-success-foreground",
|
||||
warning: "border-warning/20 bg-warning-surface text-warning-foreground",
|
||||
danger: "border-destructive/20 bg-destructive/10 text-destructive",
|
||||
};
|
||||
|
||||
const icons = {
|
||||
info: Info,
|
||||
success: CheckCircle2,
|
||||
warning: TriangleAlert,
|
||||
danger: AlertCircle,
|
||||
};
|
||||
|
||||
export function InlineAlert({
|
||||
tone = "info",
|
||||
title,
|
||||
action,
|
||||
children,
|
||||
className,
|
||||
...props
|
||||
}: InlineAlertProps) {
|
||||
const Icon = icons[tone];
|
||||
return (
|
||||
<div
|
||||
role={tone === "danger" ? "alert" : "status"}
|
||||
className={cn(
|
||||
"flex items-start gap-3 rounded-xl border px-4 py-3 text-sm",
|
||||
toneStyles[tone],
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<Icon aria-hidden className="mt-0.5 h-4 w-4 shrink-0" />
|
||||
<div className="min-w-0 flex-1">
|
||||
{title ? <p className="font-semibold">{title}</p> : null}
|
||||
<div className={cn("leading-5", title && "mt-0.5 opacity-90")}>{children}</div>
|
||||
</div>
|
||||
{action ? <div className="shrink-0">{action}</div> : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
12
web/shared/ui/Skeleton.tsx
Normal file
12
web/shared/ui/Skeleton.tsx
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { HTMLAttributes } from "react";
|
||||
import { cn } from "./styles";
|
||||
|
||||
export function Skeleton({ className, ...props }: HTMLAttributes<HTMLDivElement>) {
|
||||
return (
|
||||
<div
|
||||
aria-hidden="true"
|
||||
className={cn("animate-pulse rounded-lg bg-muted", className)}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
39
web/shared/ui/StatusChip.tsx
Normal file
39
web/shared/ui/StatusChip.tsx
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { HTMLAttributes, ReactNode } from "react";
|
||||
import { cn } from "./styles";
|
||||
|
||||
export type StatusTone = "neutral" | "info" | "success" | "warning" | "danger";
|
||||
|
||||
export interface StatusChipProps extends HTMLAttributes<HTMLSpanElement> {
|
||||
tone?: StatusTone;
|
||||
icon?: ReactNode;
|
||||
}
|
||||
|
||||
const tones: Record<StatusTone, string> = {
|
||||
neutral: "bg-muted text-muted-foreground",
|
||||
info: "bg-info-surface text-info-foreground",
|
||||
success: "bg-success-surface text-success-foreground",
|
||||
warning: "bg-warning-surface text-warning-foreground",
|
||||
danger: "bg-destructive/10 text-destructive",
|
||||
};
|
||||
|
||||
export function StatusChip({
|
||||
tone = "neutral",
|
||||
icon,
|
||||
children,
|
||||
className,
|
||||
...props
|
||||
}: StatusChipProps) {
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"inline-flex min-h-6 items-center gap-1.5 rounded-full px-2.5 py-1 text-xs font-medium leading-none",
|
||||
tones[tone],
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{icon}
|
||||
{children}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
93
web/shared/ui/Tooltip.tsx
Normal file
93
web/shared/ui/Tooltip.tsx
Normal file
@@ -0,0 +1,93 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
cloneElement,
|
||||
isValidElement,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useId,
|
||||
useRef,
|
||||
useState,
|
||||
type ReactElement,
|
||||
} from "react";
|
||||
import { cn } from "./styles";
|
||||
|
||||
export interface TooltipProps {
|
||||
label: string;
|
||||
children: ReactElement<{ "aria-describedby"?: string }>;
|
||||
side?: "top" | "right" | "bottom" | "left";
|
||||
delay?: number;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
const positions = {
|
||||
top: "bottom-full left-1/2 mb-2 -translate-x-1/2",
|
||||
right: "left-full top-1/2 ml-2 -translate-y-1/2",
|
||||
bottom: "left-1/2 top-full mt-2 -translate-x-1/2",
|
||||
left: "right-full top-1/2 mr-2 -translate-y-1/2",
|
||||
};
|
||||
|
||||
export function Tooltip({
|
||||
label,
|
||||
children,
|
||||
side = "bottom",
|
||||
delay = 180,
|
||||
disabled = false,
|
||||
}: TooltipProps) {
|
||||
const id = useId();
|
||||
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const [visible, setVisible] = useState(false);
|
||||
|
||||
const clear = useCallback(() => {
|
||||
if (timer.current) clearTimeout(timer.current);
|
||||
timer.current = null;
|
||||
}, []);
|
||||
const show = useCallback(
|
||||
(immediate = false) => {
|
||||
clear();
|
||||
if (disabled) return;
|
||||
if (immediate) setVisible(true);
|
||||
else timer.current = setTimeout(() => setVisible(true), delay);
|
||||
},
|
||||
[clear, delay, disabled],
|
||||
);
|
||||
const hide = useCallback(() => {
|
||||
clear();
|
||||
setVisible(false);
|
||||
}, [clear]);
|
||||
|
||||
useEffect(() => clear, [clear]);
|
||||
|
||||
if (!isValidElement(children)) return children;
|
||||
|
||||
return (
|
||||
<span
|
||||
className="relative inline-flex"
|
||||
onMouseEnter={() => show(false)}
|
||||
onMouseLeave={hide}
|
||||
onFocusCapture={() => show(true)}
|
||||
onBlurCapture={hide}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === "Escape") hide();
|
||||
}}
|
||||
>
|
||||
{cloneElement(children, {
|
||||
"aria-describedby": visible ? id : children.props["aria-describedby"],
|
||||
})}
|
||||
{visible ? (
|
||||
<span
|
||||
id={id}
|
||||
role="tooltip"
|
||||
className={cn(
|
||||
"pointer-events-none absolute z-[90] max-w-64 whitespace-nowrap rounded-lg bg-foreground px-2.5 py-1.5 text-xs font-medium text-background shadow-lg",
|
||||
positions[side],
|
||||
)}
|
||||
>
|
||||
{label}
|
||||
</span>
|
||||
) : null}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
export default Tooltip;
|
||||
18
web/shared/ui/index.ts
Normal file
18
web/shared/ui/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export { Button } from "./Button";
|
||||
export type { ButtonProps, ButtonSize, ButtonVariant } from "./Button";
|
||||
export { Dialog } from "./Dialog";
|
||||
export type { DialogProps } from "./Dialog";
|
||||
export { EmptyState } from "./EmptyState";
|
||||
export type { EmptyStateProps } from "./EmptyState";
|
||||
export { Field } from "./Field";
|
||||
export type { FieldProps } from "./Field";
|
||||
export { IconButton } from "./IconButton";
|
||||
export type { IconButtonProps } from "./IconButton";
|
||||
export { InlineAlert } from "./InlineAlert";
|
||||
export type { InlineAlertProps } from "./InlineAlert";
|
||||
export { Skeleton } from "./Skeleton";
|
||||
export { StatusChip } from "./StatusChip";
|
||||
export type { StatusChipProps, StatusTone } from "./StatusChip";
|
||||
export { Tooltip } from "./Tooltip";
|
||||
export type { TooltipProps } from "./Tooltip";
|
||||
export { cn } from "./styles";
|
||||
6
web/shared/ui/styles.ts
Normal file
6
web/shared/ui/styles.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { clsx, type ClassValue } from "clsx";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
export function cn(...values: ClassValue[]) {
|
||||
return twMerge(clsx(values));
|
||||
}
|
||||
@@ -4,6 +4,8 @@ module.exports = {
|
||||
content: [
|
||||
"./pages/**/*.{js,ts,jsx,tsx,mdx}",
|
||||
"./components/**/*.{js,ts,jsx,tsx,mdx}",
|
||||
"./features/**/*.{js,ts,jsx,tsx,mdx}",
|
||||
"./shared/**/*.{js,ts,jsx,tsx,mdx}",
|
||||
"./app/**/*.{js,ts,jsx,tsx,mdx}",
|
||||
],
|
||||
theme: {
|
||||
@@ -69,6 +71,21 @@ module.exports = {
|
||||
DEFAULT: "var(--card)",
|
||||
foreground: "var(--card-foreground)",
|
||||
},
|
||||
success: {
|
||||
DEFAULT: "var(--success)",
|
||||
foreground: "var(--success-foreground)",
|
||||
surface: "var(--success-surface)",
|
||||
},
|
||||
warning: {
|
||||
DEFAULT: "var(--warning)",
|
||||
foreground: "var(--warning-foreground)",
|
||||
surface: "var(--warning-surface)",
|
||||
},
|
||||
info: {
|
||||
DEFAULT: "var(--info)",
|
||||
foreground: "var(--info-foreground)",
|
||||
surface: "var(--info-surface)",
|
||||
},
|
||||
},
|
||||
backgroundImage: {
|
||||
"gradient-radial": "radial-gradient(var(--tw-gradient-stops))",
|
||||
|
||||
42
web/tests/design-token-parity.test.ts
Normal file
42
web/tests/design-token-parity.test.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
const css = fs.readFileSync(path.resolve(process.cwd(), "app/globals.css"), "utf8");
|
||||
const selectors = [":root", ".dark", ".theme-snow", ".theme-glass"];
|
||||
const requiredTokens = [
|
||||
"--background",
|
||||
"--foreground",
|
||||
"--card",
|
||||
"--primary",
|
||||
"--muted",
|
||||
"--destructive",
|
||||
"--success",
|
||||
"--success-surface",
|
||||
"--warning",
|
||||
"--warning-surface",
|
||||
"--info",
|
||||
"--info-surface",
|
||||
"--surface-raised",
|
||||
"--shadow-raised",
|
||||
];
|
||||
|
||||
test("every visual theme publishes the complete semantic token contract", () => {
|
||||
for (const selector of selectors) {
|
||||
const escaped = selector.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
const block = css.match(new RegExp(`${escaped}\\s*\\{([^}]+)\\}`))?.[1] ?? "";
|
||||
assert.ok(block, `missing theme block ${selector}`);
|
||||
for (const token of requiredTokens) {
|
||||
assert.match(block, new RegExp(`${token}\\s*:`), `${selector} is missing ${token}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("reduced-motion and keyboard focus policies are global", () => {
|
||||
assert.match(css, /prefers-reduced-motion:\s*reduce/);
|
||||
assert.match(css, /:focus-visible/);
|
||||
assert.match(css, /--motion-fast:\s*120ms/);
|
||||
assert.match(css, /--motion-base:\s*180ms/);
|
||||
assert.match(css, /--motion-slow:\s*240ms/);
|
||||
});
|
||||
89
web/tests/integration/ui-primitives.spec.tsx
Normal file
89
web/tests/integration/ui-primitives.spec.tsx
Normal file
@@ -0,0 +1,89 @@
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
Button,
|
||||
Dialog,
|
||||
EmptyState,
|
||||
Field,
|
||||
IconButton,
|
||||
InlineAlert,
|
||||
StatusChip,
|
||||
} from "@/shared/ui";
|
||||
|
||||
describe("shared UI primitives", () => {
|
||||
it("keeps loading buttons named, busy, and disabled", () => {
|
||||
render(<Button loading>Save changes</Button>);
|
||||
const button = screen.getByRole("button", { name: "Save changes" });
|
||||
expect(button).toBeDisabled();
|
||||
expect(button).toHaveAttribute("aria-busy", "true");
|
||||
});
|
||||
|
||||
it("gives icon-only actions an accessible name and described tooltip", async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<IconButton label="Open details" icon={<span aria-hidden>+</span>} />);
|
||||
const button = screen.getByRole("button", { name: "Open details" });
|
||||
await user.tab();
|
||||
const tooltip = screen.getByRole("tooltip");
|
||||
expect(button).toHaveAttribute("aria-describedby", tooltip.id);
|
||||
});
|
||||
|
||||
it("traps focus, closes with Escape, and restores the trigger", async () => {
|
||||
const onClose = vi.fn();
|
||||
const user = userEvent.setup();
|
||||
const { rerender } = render(
|
||||
<>
|
||||
<button>Trigger</button>
|
||||
<Dialog open={false} title="Preferences" onClose={onClose}>
|
||||
<button>Inside</button>
|
||||
</Dialog>
|
||||
</>,
|
||||
);
|
||||
const trigger = screen.getByRole("button", { name: "Trigger" });
|
||||
trigger.focus();
|
||||
rerender(
|
||||
<>
|
||||
<button>Trigger</button>
|
||||
<Dialog open title="Preferences" onClose={onClose}>
|
||||
<button data-autofocus>Inside</button>
|
||||
</Dialog>
|
||||
</>,
|
||||
);
|
||||
expect(await screen.findByRole("dialog", { name: "Preferences" })).toBeVisible();
|
||||
await user.keyboard("{Escape}");
|
||||
expect(onClose).toHaveBeenCalledOnce();
|
||||
rerender(
|
||||
<>
|
||||
<button>Trigger</button>
|
||||
<Dialog open={false} title="Preferences" onClose={onClose}>
|
||||
<button>Inside</button>
|
||||
</Dialog>
|
||||
</>,
|
||||
);
|
||||
expect(screen.getByRole("button", { name: "Trigger" })).toHaveFocus();
|
||||
});
|
||||
|
||||
it("connects fields to hint and error text", () => {
|
||||
render(
|
||||
<Field label="Workspace name" hint="Shown to collaborators" error="Required">
|
||||
<input />
|
||||
</Field>,
|
||||
);
|
||||
const input = screen.getByRole("textbox", { name: "Workspace name" });
|
||||
expect(input).toHaveAttribute("aria-invalid", "true");
|
||||
expect(input.getAttribute("aria-describedby")?.split(" ")).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("renders semantic status, alert, and empty state copy", () => {
|
||||
render(
|
||||
<>
|
||||
<StatusChip tone="success">Connected</StatusChip>
|
||||
<InlineAlert tone="warning">Connection is slow</InlineAlert>
|
||||
<EmptyState title="No sessions yet" description="Start with a question." />
|
||||
</>,
|
||||
);
|
||||
expect(screen.getByText("Connected")).toHaveClass("bg-success-surface");
|
||||
expect(screen.getByRole("status")).toHaveTextContent("Connection is slow");
|
||||
expect(screen.getByRole("heading", { name: "No sessions yet" })).toBeVisible();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user