Feedback
Badge, StatusWidget, Toast, Spinner, Loader, Progress, Skeleton, Empty, Kbd, ErrorBoundary.
Badge
Ocho variantes alineadas con los intents de los botones. Polimórfico vía render.
Uso:
import { Badge } from "@flow-industries/ui/components/badge";
<Badge>Default</Badge>
<Badge variant="brand">Nuevo</Badge>
<Badge variant="success">En vivo</Badge>
<Badge variant="destructive">Fallando</Badge>
<Badge variant="outline">Beta</Badge>Código:
import { mergeProps } from "@base-ui/react/merge-props";
import { useRender } from "@base-ui/react/use-render";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "../../utils/cn";
const badgeVariants = cva(
"group/badge inline-flex h-5 w-fit shrink-0 items-center justify-center gap-1 overflow-hidden rounded-4xl border-[length:var(--border-width)] border-transparent px-2 py-0.5 text-xs font-medium whitespace-nowrap transition-[color,background-color,border-color] focus-visible:border-[length:var(--border-width-focus)] focus-visible:border-focus has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 aria-invalid:border-destructive [&>svg]:pointer-events-none [&>svg]:size-3!",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground [a]:hover:bg-primary/80",
secondary:
"bg-secondary text-secondary-foreground [a]:hover:bg-secondary/80",
destructive:
"bg-destructive/10 text-destructive dark:bg-destructive/20 [a]:hover:bg-destructive/20",
success:
"bg-success-background text-success-foreground [a]:hover:bg-success/20 [&>svg]:text-success",
brand: "bg-brand text-brand-foreground [a]:hover:bg-brand/90",
outline:
"border-secondary text-foreground [a]:hover:bg-muted [a]:hover:text-muted-foreground",
ghost:
"hover:bg-muted hover:text-muted-foreground dark:hover:bg-muted/50",
link: "text-primary underline-offset-4 hover:underline",
},
},
defaultVariants: {
variant: "default",
},
},
);
function Badge({
className,
variant = "default",
render,
...props
}: useRender.ComponentProps<"span"> & VariantProps<typeof badgeVariants>) {
return useRender({
defaultTagName: "span",
props: mergeProps<"span">(
{
className: cn(badgeVariants({ variant }), className),
},
props,
),
render,
state: {
slot: "badge",
variant,
},
});
}
export { Badge, badgeVariants };StatusWidget
Píldora de estado de la flota en vivo: un punto de color más una etiqueta («All systems operational» / «Partial outage» / …) que enlaza a la página de estado. Lee la API pública de estados de Gatus y deriva un estado general de «peor de todos»; ante cualquier error se queda en «unknown» en lugar de romper el footer que lo contiene. El punto pulsa en su color de estado.
Uso:
import { StatusWidget } from "@flow-industries/ui/components/status-widget";
<StatusWidget />
<StatusWidget statusUrl="https://status.flow.industries" />
// En la propia página de estado, usa su proxy del mismo origen:
<StatusWidget apiUrl="" />Código:
import type * as React from "react";
import { useEffect, useState } from "react";
import { cn } from "../../utils/cn";
type Overall = "operational" | "degraded" | "down" | "unknown";
interface ProbeResult {
success: boolean;
}
interface Endpoint {
results?: ProbeResult[];
}
const META = {
operational: { label: "All systems operational", dot: "bg-success" },
degraded: { label: "Degraded performance", dot: "bg-orange" },
down: { label: "Partial outage", dot: "bg-destructive" },
unknown: { label: "Status unavailable", dot: "bg-muted-foreground" },
} satisfies Record<Overall, { label: string; dot: string }>;
const RECENT = 10;
// Mirrors the status page's worst-of derivation, kept self-contained so the widget has no app deps.
function deriveOverall(endpoints: Endpoint[]): Overall {
const results = endpoints
.map((e) => e.results)
.filter((r) => r !== undefined)
.filter((r) => r.length > 0);
if (results.length === 0) return "unknown";
if (results.some((r) => r.at(-1)?.success === false)) return "down";
if (results.some((r) => r.slice(-RECENT).some((x) => !x.success))) {
return "degraded";
}
return "operational";
}
/**
* Live fleet-status pill: a colored dot + label ("All systems operational" / "Partial outage" / …)
* that links to the status page. Reads the public Gatus statuses API; on any error it stays "unknown"
* rather than breaking the footer it sits in.
*
* @param statusUrl Where the pill links to (the public status page).
* @param apiUrl Origin to fetch `/api/v1/endpoints/statuses` from; defaults to `statusUrl`. Pass
* "" on the status page itself to use its same-origin proxy.
*/
export function StatusWidget({
statusUrl = "https://status.flow.industries",
apiUrl,
className,
...props
}: Omit<React.ComponentProps<"a">, "href"> & {
statusUrl?: string;
apiUrl?: string;
}) {
const [overall, setOverall] = useState<Overall>("unknown");
const base = apiUrl ?? statusUrl;
useEffect(() => {
const ctrl = new AbortController();
fetch(`${base}/api/v1/endpoints/statuses?page=1&pageSize=20`, {
signal: ctrl.signal,
headers: { Accept: "application/json" },
})
.then((res) => (res.ok ? res.json() : Promise.reject(res.status)))
.then((data: Endpoint[]) => {
if (Array.isArray(data)) setOverall(deriveOverall(data));
})
.catch(() => {});
return () => ctrl.abort();
}, [base]);
const meta = META[overall];
return (
<a
data-slot="status-widget"
href={statusUrl}
className={cn(
"-m-0.5 inline-flex items-center gap-2 rounded-sm border-[length:var(--border-width)] border-transparent text-muted-foreground text-xs outline-none [transition:color_80ms_ease] hover:text-foreground focus-visible:border-focus pointer-coarse:min-h-11",
className,
)}
{...props}
>
<span
className="relative flex size-2 shrink-0 items-center justify-center"
aria-hidden="true"
>
<span
className={cn(
"absolute inline-flex size-full animate-ping rounded-full opacity-75 motion-reduce:animate-none",
meta.dot,
)}
/>
<span
className={cn("relative inline-flex size-2 rounded-full", meta.dot)}
/>
</span>
{meta.label}
</a>
);
}Toast
Notificaciones apilables. Usa el Toast de Base UI — no sonner. Exporta: ToastProvider, toast (función con helpers .success, .error, .warning, .info).
Uso:
import { ToastProvider, toast } from "@flow-industries/ui/components/toast";
// Una vez, cerca del root:
<ToastProvider>{children}</ToastProvider>
// En cualquier parte:
toast.success("Cambios guardados");
toast.error("Algo salió mal");
toast.info("Link copiado");
toast.warning("Conexión inestable");
toast("Título", { description: "Mensaje normal" });Código:
"use client";
import { Toast } from "@base-ui/react/toast";
import {
CircleCheckIcon,
InfoIcon,
OctagonXIcon,
TriangleAlertIcon,
XIcon,
} from "lucide-react";
import type * as React from "react";
const toastManager = Toast.createToastManager();
type ToastData = {
title?: string;
description?: string;
variant?: "default" | "success" | "error" | "warning" | "info";
};
function ToastProvider({ children }: { children: React.ReactNode }) {
return (
<Toast.Provider toastManager={toastManager} timeout={5000} limit={5}>
{children}
<Toast.Portal>
<Toast.Viewport className="fixed bottom-8 right-8 z-[100] w-[360px]">
<ToastList />
</Toast.Viewport>
</Toast.Portal>
</Toast.Provider>
);
}
function ToastList() {
const { toasts } = Toast.useToastManager<ToastData>();
return toasts.map((t) => {
const variant = t.data?.variant ?? "default";
const icon = {
success: <CircleCheckIcon className="size-4 text-success" />,
error: <OctagonXIcon className="size-4 text-destructive" />,
warning: <TriangleAlertIcon className="size-4 text-orange" />,
info: <InfoIcon className="size-4 text-blue" />,
default: null,
}[variant];
const accent = {
success: "var(--success)",
error: "var(--destructive)",
warning: "var(--color-orange)",
info: "var(--color-blue)",
default: null,
}[variant];
return (
<Toast.Root
key={t.id}
toast={t}
className="group pointer-events-auto absolute bottom-0 right-0 w-full select-none rounded-lg bg-background p-4 shadow-lg ring-[length:var(--border-width)] ring-primary/10 [--gap:0.75rem] [--height:var(--toast-frontmost-height,var(--toast-height))] [--offset-y:calc(var(--toast-offset-y)*-1+(var(--toast-index)*var(--gap)*-1)+var(--toast-swipe-movement-y,0px))] [--peek:0.75rem] [--scale:calc(max(0,1-(var(--toast-index)*0.1)))] [--shrink:calc(1-var(--scale))] z-[calc(1000-var(--toast-index))] h-[var(--height)] origin-bottom transition-[transform,scale,translate,opacity,height] duration-500 ease-(--ease-out-expo) [transform:translateX(var(--toast-swipe-movement-x,0px))_translateY(calc(var(--toast-swipe-movement-y,0px)-(var(--toast-index)*var(--peek))-(var(--shrink)*var(--height))))_scale(var(--scale))] data-expanded:h-[var(--toast-height)] data-expanded:[transform:translateX(var(--toast-swipe-movement-x,0px))_translateY(var(--offset-y))] data-starting-style:[transform:translateY(150%)] data-ending-style:opacity-0 data-ending-style:scale-95 data-ending-style:translate-y-4 before:pointer-events-none data-expanded:before:pointer-events-auto before:absolute before:inset-x-0 before:-top-[var(--gap)] before:h-[var(--gap)] before:content-['']"
style={
accent
? {
backgroundColor:
variant === "success"
? "var(--success-background)"
: undefined,
backgroundImage:
variant === "success"
? undefined
: `radial-gradient(circle at top left, color-mix(in oklch, ${accent} 10%, transparent) 0%, transparent 50%)`,
boxShadow: `0 0 0 var(--border-width) color-mix(in oklch, ${accent} 25%, transparent), 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1)`,
}
: undefined
}
>
<Toast.Content className="relative flex items-center gap-3 overflow-hidden transition-opacity duration-200 ease-(--ease-out) data-behind:pointer-events-none data-behind:opacity-0 data-expanded:pointer-events-auto data-expanded:opacity-100">
{icon && <div className="shrink-0">{icon}</div>}
<div className="flex-1 space-y-1">
{t.data?.title && (
<Toast.Title className="text-sm font-medium tracking-tighter text-muted-foreground">
{t.data.title}
</Toast.Title>
)}
{t.data?.description && (
<Toast.Description className="text-sm text-muted-foreground">
{t.data.description}
</Toast.Description>
)}
</div>
<Toast.Close className="shrink-0 rounded-md p-1 text-muted-foreground opacity-0 transition-[opacity,scale,color] duration-150 ease-(--ease-out) hover:text-foreground group-hover:opacity-100 active:scale-95">
<XIcon className="size-3.5" />
</Toast.Close>
</Toast.Content>
</Toast.Root>
);
});
}
function toast(titleOrDescription: string, opts?: { description?: string }) {
if (opts?.description) {
toastManager.add({
data: {
title: titleOrDescription,
description: opts.description,
variant: "default",
},
});
} else {
toastManager.add({
data: { description: titleOrDescription, variant: "default" },
});
}
}
toast.success = (description: string) => {
toastManager.add({ data: { description, variant: "success" } });
};
toast.error = (description: string) => {
toastManager.add({ data: { description, variant: "error" } });
};
toast.warning = (description: string) => {
toastManager.add({ data: { description, variant: "warning" } });
};
toast.info = (description: string) => {
toastManager.add({ data: { description, variant: "info" } });
};
export { ToastProvider, toast };Spinner
Envuelve el Loader2Icon de lucide con animate-spin. Usa currentColor.
Uso:
import { Spinner } from "@flow-industries/ui/components/spinner";
<Spinner />
<Spinner className="size-6 text-muted-foreground" />Código:
import { Loader2Icon } from "lucide-react";
import { cn } from "../../utils/cn";
function Spinner({ className, ...props }: React.ComponentProps<"svg">) {
return (
<Loader2Icon
role="status"
aria-label="Loading"
className={cn(
"size-4 animate-spin motion-reduce:animate-none",
className,
)}
{...props}
/>
);
}
export { Spinner };Loader
Una animación de carga a nivel de página: una figura Hairline que se dibuja a sí misma como un trazo isométrico y se repite por su cuenta. Úsala en lugar de un Spinner en línea o de un Skeleton de página completa cuando se carga una vista entera. Impórtala desde @flow-industries/ui/loaders con la figura que quieras y una label para lectores de pantalla; mantiene una pose de reposo compuesta con movimiento reducido y se renderiza en el servidor como una caja 5:4 vacía, de modo que nada se desplaza cuando se dibuja.
El paquete incluye una figura por cada producto de Flow, cada una repitiéndose desde el primer fotograma y respondiendo al puntero:
harbour— barcos amarrados en un muelle suben y bajan con el oleaje a lo largo de la línea; el puntero mueve la cresta, y el barco debajo de ella es el que reporta.idcard— una tarjeta de identidad siendo leída: los corchetes se cierran sobre el retrato, luego cada línea se ilumina por turno; apunta a una parte para leerla.lighthouse— un faro sobre una roca: su haz gira por su cuenta y se orienta para encontrar el puntero.metronome— un metrónomo marca el tiempo: el puntero desliza el peso por la varilla, y cuanto más alto queda, más lento es el vaivén.paperplane— un avión de papel vuela un ocho entre un emisor y un receptor; el puntero gira la ruta hacia sí mismo.
Uso:
import { Loader, paperplane } from "@flow-industries/ui/loaders";
<Loader figure={paperplane} label="Loading your messages" />La fuente de cada figura está en src/loaders/figures/, escrita con la skill hairline-create y mantenida en su formato.
Código:
import { type ComponentProps, useEffect, useRef } from "react";
import { cn } from "../utils/cn";
import { HL } from "./kernel";
/** What a figure's `mount` receives: the stage it answers the pointer on, the svg it draws into, and its read-out. */
export interface LoaderHost {
stage: HTMLElement;
svg: SVGSVGElement;
read: { textContent: string | null };
}
/**
* A Hairline figure, as the `hairline-create` skill writes it: an isometric line drawing that loops and answers the pointer.
* Loaders play the figure's own ambient loop, not its `tour`: a tour stop holds the figure still, which reads as frozen.
*/
export interface LoaderFigure {
name: string;
means: string;
/** The figure's own number at intensity 0, 0.5 and 1; the loader plays it at the middle one. */
range: readonly [low: number, middle: number, high: number];
tour: readonly (readonly [number, number] | null)[] | null;
mount: (
host: LoaderHost,
value: number,
) => { set: (value: number) => void; destroy: () => void };
}
export interface LoaderProps extends Omit<ComponentProps<"div">, "children"> {
figure: LoaderFigure;
/** Announced to screen readers while the loader is shown, e.g. "Loading the fleet". */
label: string;
}
/** The figure's palette, taken from the page's own tokens so it sits on the background it is drawn over. */
const palette = [
"[--hairline-plate:var(--background)]",
"[--hairline-hi:var(--foreground)]",
"[--hairline-edge:color-mix(in_oklch,var(--foreground)_42%,var(--background))]",
"[--hairline-mid:color-mix(in_oklch,var(--foreground)_22%,var(--background))]",
"[--hairline-lo:color-mix(in_oklch,var(--foreground)_10%,var(--background))]",
];
/**
* How long an unmounted loader's figure keeps its place, so a loader for the same figure that mounts
* in the next moment (a page swapping one loading state for the next) carries on with the same loop.
*/
const HANDOFF_MS = 1500;
interface Running {
stage: HTMLElement;
destroy: () => void;
timer?: ReturnType<typeof setTimeout>;
}
const parked = new Map<LoaderFigure, Running>();
function start(figure: LoaderFigure, doc: Document): Running {
HL.inject(doc);
const stage = doc.createElement("div");
stage.dataset.hairline = figure.name;
stage.className = "block aspect-[5/4] w-full";
const svg: SVGSVGElement = HL.mk(
"svg",
{ viewBox: "0 0 400 320", "aria-hidden": "true" },
stage,
);
const handle = figure.mount(
{ stage, svg, read: { textContent: null } },
figure.range[1],
);
return {
stage,
destroy: () => {
handle.destroy();
stage.remove();
},
};
}
/**
* A page-level loading animation: a Hairline figure playing its own loop. It holds its rest pose under
* reduced motion, and server-renders as an empty 5:4 box so nothing shifts when it draws.
*/
export function Loader({ figure, label, className, ...props }: LoaderProps) {
const hostRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const host = hostRef.current;
if (!host) return;
const waiting = parked.get(figure);
parked.delete(figure);
clearTimeout(waiting?.timer);
const running = waiting ?? start(figure, host.ownerDocument);
host.append(running.stage);
return () => {
running.stage.remove();
const previous = parked.get(figure);
if (previous) {
clearTimeout(previous.timer);
previous.destroy();
}
running.timer = setTimeout(() => {
if (parked.get(figure) === running) parked.delete(figure);
running.destroy();
}, HANDOFF_MS);
parked.set(figure, running);
};
}, [figure]);
return (
<div
role="status"
className={cn("relative w-56 max-w-full", palette, className)}
{...props}
>
<span className="sr-only">{label}</span>
<div ref={hostRef} className="aspect-[5/4] w-full" />
</div>
);
}Progress
Barra de progreso determinada. Exporta: Progress, ProgressTrack, ProgressIndicator, ProgressLabel, ProgressValue.
Uso:
import { Progress, ProgressLabel, ProgressValue } from "@flow-industries/ui/components/progress";
<Progress value={42}>
<ProgressLabel>Subiendo</ProgressLabel>
<ProgressValue />
</Progress>Código:
"use client";
import { Progress as ProgressPrimitive } from "@base-ui/react/progress";
import { cn } from "../../utils/cn";
function Progress({
className,
children,
value,
...props
}: ProgressPrimitive.Root.Props) {
return (
<ProgressPrimitive.Root
value={value}
data-slot="progress"
className={cn("flex flex-wrap gap-3", className)}
{...props}
>
{children}
<ProgressTrack>
<ProgressIndicator />
</ProgressTrack>
</ProgressPrimitive.Root>
);
}
function ProgressTrack({ className, ...props }: ProgressPrimitive.Track.Props) {
return (
<ProgressPrimitive.Track
className={cn(
"relative flex h-1 w-full items-center overflow-x-hidden rounded-full bg-muted",
className,
)}
data-slot="progress-track"
{...props}
/>
);
}
function ProgressIndicator({
className,
...props
}: ProgressPrimitive.Indicator.Props) {
return (
<ProgressPrimitive.Indicator
data-slot="progress-indicator"
className={cn(
"h-full bg-primary transition-[width] duration-500 ease-(--ease-out-expo)",
className,
)}
{...props}
/>
);
}
function ProgressLabel({ className, ...props }: ProgressPrimitive.Label.Props) {
return (
<ProgressPrimitive.Label
className={cn("text-sm font-medium", className)}
data-slot="progress-label"
{...props}
/>
);
}
function ProgressValue({ className, ...props }: ProgressPrimitive.Value.Props) {
return (
<ProgressPrimitive.Value
className={cn(
"ml-auto text-sm text-muted-foreground tabular-nums",
className,
)}
data-slot="progress-value"
{...props}
/>
);
}
export {
Progress,
ProgressIndicator,
ProgressLabel,
ProgressTrack,
ProgressValue,
};Skeleton
Placeholder con pulso.
Uso:
import { Skeleton } from "@flow-industries/ui/components/skeleton";
<Skeleton className="h-4 w-48" />
<Skeleton className="h-10 w-full rounded-full" />Código:
import { cn } from "../../utils/cn";
function Skeleton({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="skeleton"
className={cn(
"animate-pulse rounded-md bg-muted motion-reduce:animate-none",
className,
)}
{...props}
/>
);
}
export { Skeleton };Empty
Primitiva de estado vacío. Exporta: Empty, EmptyHeader, EmptyTitle, EmptyDescription, EmptyContent, EmptyMedia.
Uso:
import {
Empty, EmptyHeader, EmptyMedia, EmptyTitle, EmptyDescription, EmptyContent,
} from "@flow-industries/ui/components/empty";
import { SearchIcon } from "lucide-react";
<Empty>
<EmptyHeader>
<EmptyMedia variant="icon"><SearchIcon /></EmptyMedia>
<EmptyTitle>Sin resultados</EmptyTitle>
<EmptyDescription>Prueba con una búsqueda más amplia.</EmptyDescription>
</EmptyHeader>
<EmptyContent>
<Button>Limpiar filtros</Button>
</EmptyContent>
</Empty>Código:
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "../../utils/cn";
function Empty({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="empty"
className={cn(
"flex w-full min-w-0 flex-1 flex-col items-center justify-center gap-4 rounded-xl border-[length:var(--border-width)] border-dashed p-6 text-center text-balance",
className,
)}
{...props}
/>
);
}
function EmptyHeader({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="empty-header"
className={cn("flex max-w-sm flex-col items-center gap-2", className)}
{...props}
/>
);
}
const emptyMediaVariants = cva(
"mb-2 flex shrink-0 items-center justify-center [&_svg]:pointer-events-none [&_svg]:shrink-0",
{
variants: {
variant: {
default: "bg-transparent",
icon: "flex size-8 shrink-0 items-center justify-center rounded-lg bg-muted text-foreground [&_svg:not([class*='size-'])]:size-4",
},
},
defaultVariants: {
variant: "default",
},
},
);
function EmptyMedia({
className,
variant = "default",
...props
}: React.ComponentProps<"div"> & VariantProps<typeof emptyMediaVariants>) {
return (
<div
data-slot="empty-icon"
data-variant={variant}
className={cn(emptyMediaVariants({ variant, className }))}
{...props}
/>
);
}
function EmptyTitle({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="empty-title"
className={cn("text-sm font-medium tracking-tight", className)}
{...props}
/>
);
}
function EmptyDescription({ className, ...props }: React.ComponentProps<"p">) {
return (
<div
data-slot="empty-description"
className={cn(
"text-sm/relaxed text-muted-foreground [&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary",
className,
)}
{...props}
/>
);
}
function EmptyContent({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="empty-content"
className={cn(
"flex w-full max-w-sm min-w-0 flex-col items-center gap-2.5 text-sm text-balance",
className,
)}
{...props}
/>
);
}
export {
Empty,
EmptyContent,
EmptyDescription,
EmptyHeader,
EmptyMedia,
EmptyTitle,
};Kbd
Muestra de tecla de teclado. Exporta: Kbd, KbdGroup.
Uso:
import { Kbd, KbdGroup } from "@flow-industries/ui/components/kbd";
<p>Pulsa <KbdGroup><Kbd>⌘</Kbd><Kbd>K</Kbd></KbdGroup> para abrir.</p>Código:
import { cn } from "../../utils/cn";
function Kbd({ className, ...props }: React.ComponentProps<"kbd">) {
return (
<kbd
data-slot="kbd"
className={cn(
"pointer-events-none inline-flex h-5 w-fit min-w-5 items-center justify-center gap-1 rounded-sm bg-muted px-1 font-sans text-xs font-medium text-muted-foreground select-none in-data-[slot=tooltip-content]:bg-background/20 in-data-[slot=tooltip-content]:text-background dark:in-data-[slot=tooltip-content]:bg-background/10 [&_svg:not([class*='size-'])]:size-3",
className,
)}
{...props}
/>
);
}
function KbdGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<kbd
data-slot="kbd-group"
className={cn("inline-flex items-center gap-1", className)}
{...props}
/>
);
}
export { Kbd, KbdGroup };ErrorBoundary
Un límite de error de clase más el ErrorFallback que renderiza por defecto: un Logo centrado, título, subtítulo, un botón de reintento y un enlace a inicio, con una traza de pila colapsable opcional (showDetails). Pasa un nodo o render-prop fallback para personalizarlo, onError para registrar, y resetKeys para recuperarse automáticamente cuando cambia un valor (p. ej. la ruta actual). ErrorFallback enfoca su encabezado al montarse para que los lectores de pantalla anuncien el error. Exporta: ErrorBoundary, ErrorFallback.
Uso:
import { ErrorBoundary, ErrorFallback } from "@flow-industries/ui/components/error-boundary";
<ErrorBoundary
onError={(error, info) => report(error, info)}
fallback={({ error, reset }) => (
<ErrorFallback error={error} reset={reset} showDetails />
)}
>
<App />
</ErrorBoundary>Código:
import {
Component,
type ErrorInfo,
type ReactNode,
useEffect,
useRef,
} from "react";
import { cn } from "../../utils/cn";
import { Logo } from "../logo";
import { Button } from "./button";
import { Subtitle, Title } from "./typography";
type ErrorFallbackProps = {
error?: unknown;
reset?: () => void;
title?: string;
description?: string;
homeHref?: string;
homeLabel?: string;
retryLabel?: string;
showDetails?: boolean;
autoFocus?: boolean;
className?: string;
};
function ErrorFallback({
error,
reset,
title = "Something went wrong",
description = "This page hit an error. You can try again or head back home.",
homeHref = "/",
homeLabel = "Back to home",
retryLabel = "Try again",
showDetails = false,
autoFocus = true,
className,
}: ErrorFallbackProps) {
const headingRef = useRef<HTMLHeadingElement>(null);
useEffect(() => {
if (autoFocus) headingRef.current?.focus({ preventScroll: true });
}, [autoFocus]);
const details =
showDetails && error !== undefined
? error instanceof Error
? (error.stack ?? error.message)
: String(error)
: null;
return (
<section
role="alert"
data-slot="error-fallback"
className={cn(
"flex w-full flex-col items-center justify-center gap-6 p-6 text-center text-balance",
className,
)}
>
<Logo size={40} />
<div className="flex max-w-md flex-col items-center gap-2">
<Title
ref={headingRef}
tabIndex={-1}
size="md"
className="outline-none"
>
{title}
</Title>
<Subtitle size="md">{description}</Subtitle>
</div>
<div className="flex flex-wrap items-center justify-center gap-3">
<Button
className="pointer-coarse:min-h-11"
onClick={() => (reset ? reset() : window.location.reload())}
>
{retryLabel}
</Button>
<Button
variant="secondary"
className="pointer-coarse:min-h-11"
nativeButton={false}
render={<a href={homeHref} />}
>
{homeLabel}
</Button>
</div>
{details && (
<details className="w-full max-w-lg text-left">
<summary className="cursor-pointer rounded-sm border-[length:var(--border-width)] border-transparent text-sm text-muted-foreground outline-none focus-visible:border-focus">
Error details
</summary>
<pre className="mt-2 max-h-64 overflow-auto rounded-lg bg-muted p-3 font-mono text-xs leading-relaxed text-foreground/80 whitespace-pre-wrap break-words">
{details}
</pre>
</details>
)}
</section>
);
}
type ErrorBoundaryFallback =
| ReactNode
| ((props: { error: unknown; reset: () => void }) => ReactNode);
type ErrorBoundaryProps = {
children: ReactNode;
fallback?: ErrorBoundaryFallback;
// biome-ignore lint/plugin: React can catch any thrown JavaScript value.
onError?: (error: unknown, info: ErrorInfo) => void;
onReset?: () => void;
resetKeys?: unknown[];
};
type ErrorBoundaryState = { hasError: boolean; error: unknown };
function resetKeysChanged(previous?: unknown[], next?: unknown[]) {
if (previous === next) return false;
if (!previous || !next || previous.length !== next.length) return true;
return previous.some((key, index) => !Object.is(key, next[index]));
}
class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
state: ErrorBoundaryState = { hasError: false, error: null };
// biome-ignore lint/plugin: React can catch any thrown JavaScript value.
static getDerivedStateFromError(error: unknown): ErrorBoundaryState {
return { hasError: true, error };
}
// biome-ignore lint/plugin: React can catch any thrown JavaScript value.
componentDidCatch(error: unknown, info: ErrorInfo) {
this.props.onError?.(error, info);
}
componentDidUpdate(previousProps: ErrorBoundaryProps) {
if (
this.state.hasError &&
resetKeysChanged(previousProps.resetKeys, this.props.resetKeys)
) {
this.reset();
}
}
reset = () => {
this.props.onReset?.();
this.setState({ hasError: false, error: null });
};
render() {
const { hasError, error } = this.state;
if (!hasError) return this.props.children;
const { fallback } = this.props;
if (fallback instanceof Function) {
return fallback({ error, reset: this.reset });
}
if (fallback !== undefined) return fallback;
return <ErrorFallback error={error} reset={this.reset} />;
}
}
export {
ErrorBoundary,
type ErrorBoundaryProps,
ErrorFallback,
type ErrorFallbackProps,
};