コンテンツにスキップ

spst-category-strip

実サービスへ通信しない見本です。モデルの描画は静止した代替表示です。

元 commit: d204666f04a0ee7b6f18543f8aec414b4128874e · shadcn build 4.23.1

Noto Sans JP · @fontsource/noto-sans-jp 5.3.1 · OFL-1.1

操作を試す

全画面で開く

取り込む(公開予定)

以下の URL は公開予定です。公開前は取り込めません。既存ファイルとの差分を確認してから実行し、--overwrite を既定にしないでください。

npx shadcn@latest add https://spritestack.dev/r/spst-category-strip.json

components.json の registries に追加

{"registries":{"@spritestack":"https://spritestack.dev/r/{name}.json"}}
npx shadcn@latest add @spritestack/spst-category-strip

React 19・Tailwind v4・shadcn の src/ を持つ構成が対象です。src/ の無い構成は未検証です。取り込みはソースのコピーで、自動更新されません。

import "./styles/spritestack/theme.css";

<div className="spst-theme" data-theme="light">
  {/* Render components here. */}
</div>

Portal の container / portalContainer は .spst-theme 内の要素を渡してください。Noto Sans JP の 400・600・700 は利用先が自己ホストしてください。未導入時は system-ui です。

依存

Props / API

以下は実際のソースを構文解析して生成しています。… の行は継承する React / Radix の公開型です。既定欄の — は、この関数の引数で既定を指定していないことを表します。union の必須条件は型宣言も確認してください。

src/components/spritestack/category-strip.tsx

CategoryStrip

CategoryStrip(props: CategoryStripProps)
Prop型必須既定イベント
messagesPartial<CategoryStripMessages>いいえ——
valuestringはい——
layout"scroll" | "wrap"いいえ——
labelstringいいえ——
…( | { mode: "links"; categories: readonly { value: string; label: string; href: string }[]; } | { mode?: "toggle"; categories: readonly { value: string; label: string; disabled?: boolean }[]; onValueChange: (value: string) => void; disabled?: boolean; } )———
型宣言とソースを確認
// 出典: SpriteStack デザインシステム設計 §4.2・§4.4(Issue 313)。基本部品は同梱の shadcn/ui 派生を合成。
"use client";
import { cn } from "../../lib/spritestack/utils";
import { ToggleGroup, ToggleGroupItem } from "../ui/toggle-group";
export type CategoryStripMessages = {
  label: string;
};
const defaultMessages: CategoryStripMessages = {
  label: "カテゴリ",
};
type CategoryStripBaseProps = {
  messages?: Partial<CategoryStripMessages>;
  value: string;
  layout?: "scroll" | "wrap";
  label?: string;
};
export type CategoryStripProps = CategoryStripBaseProps &
  (
    | {
        mode: "links";
        categories: readonly { value: string; label: string; href: string }[];
      }
    | {
        mode?: "toggle";
        categories: readonly { value: string; label: string; disabled?: boolean }[];
        onValueChange: (value: string) => void;
        disabled?: boolean;
      }
  );
export function CategoryStrip(props: CategoryStripProps) {
  const { messages, value, layout = "scroll", label } = props;
  const m = { ...defaultMessages, ...messages };
  if (props.mode === "links")
    return (
      <nav
        data-component="category-strip"
        aria-label={label ?? m.label}
        className="min-w-0 max-w-full overflow-x-auto p-1"
      >
        <div className={cn("flex w-max gap-2 p-1", layout === "wrap" && "w-full flex-wrap")}>
          {props.categories.map((category) => (
            <a
              key={category.value}
              href={category.href}
              aria-current={value === category.value ? "page" : undefined}
              onFocus={(event) =>
                event.currentTarget.scrollIntoView({ block: "nearest", inline: "nearest" })
              }
              className={cn(
                "shrink-0 whitespace-nowrap rounded-badge border border-input px-3 py-2 text-sm font-(weight:--spst-font-weight-control) focus-visible:outline-ring",
                value === category.value
                  ? "bg-primary text-primary-foreground"
                  : "bg-card text-foreground hover:bg-accent",
              )}
            >
              {category.label}
            </a>
          ))}
        </div>
      </nav>
    );
  const { onValueChange, disabled, categories: options } = props;
  return (
    <div data-component="category-strip" className="min-w-0 max-w-full overflow-x-auto p-1">
      <ToggleGroup
        aria-label={label ?? m.label}
        value={value}
        onValueChange={onValueChange}
        disabled={disabled}
        className={cn(
          "gap-2 border-0 bg-transparent",
          layout === "wrap" && "w-full flex-wrap justify-start",
        )}
      >
        {options.map((category) => (
          <ToggleGroupItem
            key={category.value}
            value={category.value}
            disabled={category.disabled}
            className="shrink-0 rounded-badge border border-input"
          >
            {category.label}
          </ToggleGroupItem>
        ))}
      </ToggleGroup>
    </div>
  );
}

アクセシビリティの利用条件

  • 入力には Label または読み上げ名を与え、エラーを aria-describedby で関連づけます。色だけで状態を伝えません。
  • 画像にはモデル名と代替表示を与えます。単独アイコンのボタンにも名前が必要です。
  • Dialog / Sheet に題名と説明を与え、Portal をテーマ内に置きます。Tab・矢印・Escape と閉じた後のフォーカスを利用先でも確認します。
  • Slider は名前と有限の min < max、ToggleGroup は空でない single の値が必要です。pending は親が制御します。

対応画像: 01・02 · 暗色とエラーは設計による補完です。