コンテンツにスキップ

spst-select

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

元 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-select.json

components.json の registries に追加

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

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/ui/select.tsx

Select

Select({ ...props }: React.ComponentProps<typeof SelectPrimitive.Root>)
Prop型必須既定イベント
…React.ComponentProps<typeof SelectPrimitive.Root>———

SelectGroup

SelectGroup({ ...props }: React.ComponentProps<typeof SelectPrimitive.Group>)
Prop型必須既定イベント
…React.ComponentProps<typeof SelectPrimitive.Group>———

SelectValue

SelectValue({ ...props }: React.ComponentProps<typeof SelectPrimitive.Value>)
Prop型必須既定イベント
…React.ComponentProps<typeof SelectPrimitive.Value>———

SelectTrigger

SelectTrigger({
  className,
  children,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Trigger>)
Prop型必須既定イベント
…React.ComponentProps<typeof SelectPrimitive.Trigger>———

SelectContent

SelectContent({
  className,
  children,
  container,
  position = "item-aligned",
  align = "center",
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Content> &
  Pick<React.ComponentProps<typeof SelectPrimitive.Portal>, "container">)
Prop型必須既定イベント
…React.ComponentProps<typeof SelectPrimitive.Content>———
…Pick<React.ComponentProps<typeof SelectPrimitive.Portal>, "container">———
分割代入の既定値(継承した props も含む)
position = "item-aligned"
align = "center"

SelectLabel

SelectLabel({ className, ...props }: React.ComponentProps<typeof SelectPrimitive.Label>)
Prop型必須既定イベント
…React.ComponentProps<typeof SelectPrimitive.Label>———

SelectItem

SelectItem({
  className,
  children,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Item>)
Prop型必須既定イベント
…React.ComponentProps<typeof SelectPrimitive.Item>———

SelectSeparator

SelectSeparator({
  className,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Separator>)
Prop型必須既定イベント
…React.ComponentProps<typeof SelectPrimitive.Separator>———

SelectScrollUpButton

SelectScrollUpButton({
  className,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.ScrollUpButton>)
Prop型必須既定イベント
…React.ComponentProps<typeof SelectPrimitive.ScrollUpButton>———

SelectScrollDownButton

SelectScrollDownButton({
  className,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.ScrollDownButton>)
Prop型必須既定イベント
…React.ComponentProps<typeof SelectPrimitive.ScrollDownButton>———
型宣言とソースを確認
// 出典: shadcn/ui new-york-v4/select(commit c1ead5c92572ce6e95dc7bb3221feaf2b3aa59a7、MIT)。
"use client";

import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from "lucide-react";
import { Select as SelectPrimitive } from "radix-ui";
import type * as React from "react";
import { cn } from "../../lib/spritestack/utils";

function Select({ ...props }: React.ComponentProps<typeof SelectPrimitive.Root>) {
  return <SelectPrimitive.Root data-slot="select" {...props} />;
}

function SelectGroup({ ...props }: React.ComponentProps<typeof SelectPrimitive.Group>) {
  return <SelectPrimitive.Group data-slot="select-group" {...props} />;
}

function SelectValue({ ...props }: React.ComponentProps<typeof SelectPrimitive.Value>) {
  return <SelectPrimitive.Value data-slot="select-value" {...props} />;
}

function SelectTrigger({
  className,
  children,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Trigger>) {
  return (
    <SelectPrimitive.Trigger
      data-slot="select-trigger"
      className={cn(
        "flex w-fit items-center justify-between gap-2 rounded-input border border-input bg-card px-3 py-2 text-foreground enabled:hover:bg-accent aria-invalid:border-destructive-foreground aria-invalid:bg-destructive aria-invalid:enabled:hover:bg-destructive aria-invalid:text-destructive-foreground aria-invalid:data-[placeholder]:text-destructive-foreground data-[placeholder]:text-muted-foreground font-sans text-sm font-(weight:--spst-font-weight-control) outline-none focus-visible:outline-solid focus-visible:outline-(length:--spst-focus-width) focus-visible:outline-ring focus-visible:outline-offset-(--spst-focus-offset) disabled:cursor-not-allowed",
        className,
      )}
      {...props}
    >
      {children}
      <SelectPrimitive.Icon asChild>
        <ChevronDownIcon aria-hidden="true" className="size-4" />
      </SelectPrimitive.Icon>
    </SelectPrimitive.Trigger>
  );
}

function SelectContent({
  className,
  children,
  container,
  position = "item-aligned",
  align = "center",
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Content> &
  Pick<React.ComponentProps<typeof SelectPrimitive.Portal>, "container">) {
  return (
    <SelectPrimitive.Portal container={container}>
      <SelectPrimitive.Content
        data-slot="select-content"
        className={cn(
          "relative z-50 max-h-(--radix-select-content-available-height) overflow-x-hidden overflow-y-auto rounded-card border border-input bg-popover text-popover-foreground shadow-popover font-sans",
          position === "popper" &&
            "data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
          className,
        )}
        position={position}
        align={align}
        {...props}
      >
        <SelectScrollUpButton />
        <SelectPrimitive.Viewport
          className={cn(
            "p-1",
            position === "popper" &&
              "h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)] scroll-my-1",
          )}
        >
          {children}
        </SelectPrimitive.Viewport>
        <SelectScrollDownButton />
      </SelectPrimitive.Content>
    </SelectPrimitive.Portal>
  );
}

function SelectLabel({ className, ...props }: React.ComponentProps<typeof SelectPrimitive.Label>) {
  return (
    <SelectPrimitive.Label
      data-slot="select-label"
      className={cn("px-2 py-1 text-sm text-muted-foreground", className)}
      {...props}
    />
  );
}

function SelectItem({
  className,
  children,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Item>) {
  return (
    <SelectPrimitive.Item
      data-slot="select-item"
      className={cn(
        "relative flex w-full cursor-default items-center gap-2 rounded-input py-2 pr-6 pl-2 text-sm text-foreground select-none focus:bg-accent data-[state=checked]:bg-accent data-[state=checked]:inset-ring data-[state=checked]:inset-ring-ring data-[disabled]:cursor-not-allowed outline-none focus-visible:outline-solid focus-visible:outline-(length:--spst-focus-width) focus-visible:outline-ring focus-visible:outline-offset-(--spst-focus-offset)",
        className,
      )}
      {...props}
    >
      <span
        data-slot="select-item-indicator"
        className="absolute right-2 flex size-4 items-center justify-center"
      >
        <SelectPrimitive.ItemIndicator>
          <CheckIcon aria-hidden="true" className="size-4" />
        </SelectPrimitive.ItemIndicator>
      </span>
      <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
    </SelectPrimitive.Item>
  );
}

function SelectSeparator({
  className,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Separator>) {
  return (
    <SelectPrimitive.Separator
      data-slot="select-separator"
      className={cn("pointer-events-none -mx-1 my-1 border-t border-border", className)}
      {...props}
    />
  );
}

function SelectScrollUpButton({
  className,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.ScrollUpButton>) {
  return (
    <SelectPrimitive.ScrollUpButton
      data-slot="select-scroll-up-button"
      className={cn("flex cursor-default items-center justify-center py-1", className)}
      {...props}
    >
      <ChevronUpIcon aria-hidden="true" className="size-4" />
    </SelectPrimitive.ScrollUpButton>
  );
}

function SelectScrollDownButton({
  className,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.ScrollDownButton>) {
  return (
    <SelectPrimitive.ScrollDownButton
      data-slot="select-scroll-down-button"
      className={cn("flex cursor-default items-center justify-center py-1", className)}
      {...props}
    >
      <ChevronDownIcon aria-hidden="true" className="size-4" />
    </SelectPrimitive.ScrollDownButton>
  );
}

export {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectLabel,
  SelectScrollDownButton,
  SelectScrollUpButton,
  SelectSeparator,
  SelectTrigger,
  SelectValue,
};

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

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

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