SunUI
創始會員NT$2,490 終身更新,1月11日截止取得席位
免費產品介面整段區塊

空狀態

虛線框的空狀態卡片,含 1.5 線寬圖示、標題、說明,以及「載入範例」與 outline 按鈕組合(也可換成自訂動作),可置中或靠左。

適合使用的時機

  • 還沒有資料、需要明確下一步的清單或表格
  • 首次使用畫面,載入範例比空白表單更快讓人理解產品

注意事項

  • 傳入 action 會取代內建的「載入範例」與次要按鈕
  • 說明保持一到兩句短句,卡片才容易掃讀

交給你的 agent 安裝

一次複製就包含安裝指令、使用時機、props 與設計規則。貼進 Claude Code、Cursor 或 Codex 即可。

用 CLI 安裝

使用 shadcn CLI。原始碼會複製到你的專案裡,每一行都由你掌握。

$ npx shadcn@latest add https://sunui.synvize.com/r/empty-state.json

或手動安裝

  1. 1. 安裝相依套件。
    $ npm install clsx tailwind-merge
  2. 2. 如果還沒有,在 lib/utils.ts 加上 cn 輔助函式。沒有用 shadcn/ui?再加上 sunui-theme.css 提供顏色變數(做法)。然後複製元件程式碼。
    empty-state.tsx
    "use client";
    
    import type { ComponentProps, ReactNode } from "react";
    
    import { cn } from "@/lib/utils";
    
    /* ---------- Buttons (same as shadcn Button default and outline) ---------- */
    
    const buttonBase =
      "inline-flex shrink-0 items-center justify-center rounded-lg border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:translate-y-px disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4";
    const buttonVariant = {
      default: "bg-primary text-primary-foreground hover:bg-primary/80",
      outline: "border-border bg-background hover:bg-muted hover:text-foreground",
    };
    const buttonSize = {
      default: "h-8 gap-1.5 px-2.5",
      sm: "h-7 gap-1 rounded-md px-2.5 text-[0.8rem] [&_svg:not([class*='size-'])]:size-3.5",
    };
    
    export interface StateButtonProps extends Omit<ComponentProps<"button">, "type"> {
      variant?: keyof typeof buttonVariant;
      size?: keyof typeof buttonSize;
      /** When given, renders as a link */
      href?: string;
    }
    
    export function StateButton({ variant = "default", size = "default", href, className, children, ...props }: StateButtonProps) {
      const cls = cn(buttonBase, buttonVariant[variant], buttonSize[size], className);
      if (href) {
        return (
          <a href={href} className={cls} onClick={props.onClick as ComponentProps<"a">["onClick"]}>
            {children}
          </a>
        );
      }
      return (
        <button type="button" className={cls} {...props}>
          {children}
        </button>
      );
    }
    
    /* ---------- Empty ---------- */
    
    export interface StateLink {
      label: ReactNode;
      href?: string;
      onClick?: () => void;
    }
    
    export interface EmptyStateProps {
      title: ReactNode;
      description?: ReactNode;
      /** Icon drawn with a 1.5 stroke width; defaults to an inbox */
      icon?: ReactNode;
      /** Custom actions; when given, the built-in two buttons are not rendered */
      action?: ReactNode;
      /** Primary button such as "Load sample data", so users first see a filled-in view */
      onLoadExample?: () => void;
      /** Outline button beside it, for example "Add members" */
      secondary?: StateLink;
      /** start: left-aligned variant (title, description and button row aligned left) */
      align?: "center" | "start";
      /** UI language: en, zh-TW, zh-CN, ja. Tags such as zh-Hant-HK or ja-JP are also accepted */
      locale?: string;
      /** Override built-in strings; takes precedence over locale */
      labels?: Partial<EmptyStateLabels>;
      className?: string;
    }
    
    export interface EmptyStateLabels {
      loadExample: ReactNode;
    }
    
    type SunLocale = "en" | "zh-TW" | "zh-CN" | "ja";
    
    /** Map any language tag to one of the four built-in languages; everything else falls back to English */
    function resolveLocale(tag?: string): SunLocale {
      const t = (tag ?? "en").toLowerCase();
      if (t.startsWith("zh")) return /hant|tw|hk|mo/.test(t) ? "zh-TW" : "zh-CN";
      if (t.startsWith("ja")) return "ja";
      return "en";
    }
    
    const MESSAGES: Record<SunLocale, EmptyStateLabels> = {
      en: { loadExample: "Load example" },
      "zh-TW": { loadExample: "載入範例" },
      "zh-CN": { loadExample: "加载示例" },
      ja: { loadExample: "サンプルを読み込む" },
    };
    
    export function EmptyState({ title, description, icon, action, onLoadExample, secondary, align = "center", locale = "en", labels, className }: EmptyStateProps) {
      const t = { ...MESSAGES[resolveLocale(locale)], ...labels };
      const start = align === "start";
      const builtIn =
        onLoadExample || secondary ? (
          <div className={cn("flex flex-wrap gap-2", start ? "justify-start" : "justify-center")}>
            {onLoadExample && (
              <StateButton onClick={onLoadExample}>
                <SparklesIcon />
                {t.loadExample}
              </StateButton>
            )}
            {secondary && (
              <StateButton variant="outline" href={secondary.href} onClick={secondary.onClick}>
                {secondary.label}
              </StateButton>
            )}
          </div>
        ) : null;
    
      return (
        <div className={cn("flex flex-col overflow-hidden rounded-xl border border-dashed border-border bg-transparent text-sm text-card-foreground", className)}>
          <div className={cn("flex flex-col gap-4 px-5 py-16", start ? "items-start text-left" : "items-center text-center")}>
            <span className="text-muted-foreground [&_svg]:size-6">{icon ?? <InboxIcon />}</span>
            <div>
              <p className="text-sm font-medium text-foreground">{title}</p>
              {description ? <p className={cn("mt-1 max-w-sm text-sm text-muted-foreground", !start && "mx-auto")}>{description}</p> : null}
            </div>
            {action ?? builtIn}
          </div>
        </div>
      );
    }
    
    /* ---------- Icons (lucide shapes, 1.5 stroke width) ---------- */
    
    const iconProps = {
      viewBox: "0 0 24 24",
      fill: "none",
      stroke: "currentColor",
      strokeWidth: 1.5,
      strokeLinecap: "round",
      strokeLinejoin: "round",
      "aria-hidden": true,
    } as const;
    
    export function InboxIcon() {
      return (
        <svg {...iconProps}>
          <polyline points="22 12 16 12 14 15 10 15 8 12 2 12" />
          <path d="M5.45 5.11 2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z" />
        </svg>
      );
    }
    
    export function UsersIcon() {
      return (
        <svg {...iconProps}>
          <path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2" />
          <circle cx="9" cy="7" r="4" />
          <path d="M22 21v-2a4 4 0 0 0-3-3.87" />
          <path d="M16 3.13a4 4 0 0 1 0 7.75" />
        </svg>
      );
    }
    
    // Icons inside buttons keep lucide's default stroke width of 2
    function SparklesIcon() {
      return (
        <svg {...iconProps} strokeWidth={2}>
          <path d="M9.937 15.5A2 2 0 0 0 8.5 14.063l-6.135-1.582a.5.5 0 0 1 0-.962L8.5 9.936A2 2 0 0 0 9.937 8.5l1.582-6.135a.5.5 0 0 1 .963 0L14.063 8.5A2 2 0 0 0 15.5 9.937l6.135 1.581a.5.5 0 0 1 0 .964L15.5 14.063a2 2 0 0 0-1.437 1.437l-1.582 6.135a.5.5 0 0 1-.963 0z" />
          <path d="M20 3v4" />
          <path d="M22 5h-4" />
          <path d="M4 17v2" />
          <path d="M5 18H3" />
        </svg>
      );
    }