Timace

Toast

Project archived
You can restore it for 30 days.
Install
npm install react-aria-components
components/ui/toast.tsx
"use client";

import type { ReactNode } from "react";
import { Button } from "react-aria-components";

const marks = {
  neutral: "bg-(--tui-toast-neutral)",
  success: "bg-(--tui-toast-success)",
  danger: "bg-(--tui-toast-danger)",
};

export interface ToastProps {
  variant?: keyof typeof marks;
  title: ReactNode;
  children?: ReactNode;
  /** A control on the right: an undo button, a link. */
  action?: ReactNode;
  /** Shows a close button and is called when it is pressed. */
  onClose?: () => void;
  className?: string;
}

/**
 * A short message about something that just happened. This is the message
 * itself: where it appears and when it leaves are the app's to decide.
 */
export function Toast({ variant = "neutral", title, children, action, onClose, className }: ToastProps) {
  return (
    <div
      data-slot="toast"
      role={variant === "danger" ? "alert" : "status"}
      className={[
        "flex w-(--tui-toast-width) max-w-full items-start gap-(--tui-toast-gap) rounded-(--tui-toast-radius) border-solid border-(length:--tui-toast-border-width) border-(--tui-toast-border) bg-(--tui-toast-bg) p-(--tui-toast-padding) font-(family-name:--tui-font) text-(length:--tui-toast-font-size) leading-normal text-(--tui-toast-fg) shadow-(--tui-toast-shadow)",
        className,
      ]
        .filter(Boolean)
        .join(" ")}
    >
      <span aria-hidden className={`mt-[0.5em] size-2 shrink-0 rounded-full ${marks[variant]}`} />
      <div className="min-w-0 flex-1">
        <div className="font-(--tui-toast-title-weight)">{title}</div>
        {children && <div className="text-(--tui-toast-muted)">{children}</div>}
      </div>
      {action}
      {onClose && (
        <Button aria-label="Dismiss" onPress={onClose} className="flex size-6 shrink-0 cursor-default items-center justify-center rounded-full text-(--tui-toast-muted) outline-none data-focus-visible:[outline:2px_solid_var(--tui-ring)] data-hovered:text-(--tui-toast-fg)">
          <svg aria-hidden viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" className="size-3.5">
            <path d="M4 4l8 8M12 4l-8 8" />
          </svg>
        </Button>
      )}
    </div>
  );
}
styles/timace-ui.css
:root {
  color-scheme: light;
  --tui-bg: #ffffff;
  --tui-fg: #0a0a0a;
  --tui-muted: #f5f5f5;
  --tui-muted-fg: #737373;
  --tui-border: #e5e5e5;
  --tui-primary: #171717;
  --tui-primary-fg: #fafafa;
  --tui-ring: #a3a3a3;
  --tui-success: #16a34a;
  --tui-warning: #d97706;
  --tui-danger: #dc2626;
  --tui-radius: 8px;
  --tui-border-width: 1px;
  --tui-space: 4px;
  --tui-shadow: none;
  --tui-font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --tui-text: 14px;
  --tui-duration: 150ms;

  /* Toast */
  --tui-toast-width: 340px;
  --tui-toast-padding: calc(var(--tui-space) * 3.5);
  --tui-toast-gap: calc(var(--tui-space) * 2.5);
  --tui-toast-radius: calc(var(--tui-radius) * 1.25);
  --tui-toast-border-width: var(--tui-border-width);
  --tui-toast-shadow: 0 12px 32px rgb(0 0 0 / 0.14);
  --tui-toast-font-size: var(--tui-text);
  --tui-toast-title-weight: 600;
  --tui-toast-bg: var(--tui-bg);
  --tui-toast-fg: var(--tui-fg);
  --tui-toast-muted: var(--tui-muted-fg);
  --tui-toast-border: var(--tui-border);
  --tui-toast-neutral: var(--tui-muted-fg);
  --tui-toast-success: var(--tui-success);
  --tui-toast-danger: var(--tui-danger);
}

.dark,
[data-theme="dark"] {
  color-scheme: dark;
  --tui-bg: #0a0a0a;
  --tui-fg: #fafafa;
  --tui-muted: #1c1c1c;
  --tui-muted-fg: #a3a3a3;
  --tui-border: #2a2a2a;
  --tui-primary: #fafafa;
  --tui-primary-fg: #171717;
  --tui-ring: #737373;
  --tui-success: #22c55e;
  --tui-warning: #f59e0b;
  --tui-danger: #ef4444;
}

Needs Tailwind CSS v4. Import the stylesheet once, after Tailwind.