本文へ移動
日本語

仕組み

The thumb is two circles plus a rectangle between them: on press the second circle moves and the rectangle expands, forming a stretched capsule, with a drop-shadow on the parent for one outline. Only transform animates, never width. On release it springs over in 420ms. The off track is a 16% tint of the text colour, so it reads on white cards and grey panels alike; hover deepens it, and keyboard focus grows a soft accent halo. The stretch direction is remembered so the retract never jumps sides. On touch the thumb follows the finger 1:1 and release past halfway decides.

向いている場面

  • Instant on/off options in settings: notifications, dark mode, auto-save.
  • Preference switches that benefit from a bit of tactile feel, such as profile settings.
  • Mobile settings lists: drag works and the hit area is at least 44px.

向かない場面

  • Options that only apply after pressing Save: use a checkbox, since a switch implies instant effect.
  • Choosing one of several options: use radios or a segmented control.
  • Rows of switches in a table: many bounces at once look busy, so use size sm without icons or use checkboxes.

使い方

import { useState } from 'react'
import { ElasticSwitch } from './ElasticSwitch.jsx'

export function NotifySetting() {
  const [on, setOn] = useState(true)
  return (
    <ElasticSwitch
      label="Notifications"
      description="Instant alerts for orders and messages"
      labelPlacement="start"
      icon="check"
      checked={on}
      onCheckedChange={setOn}
    />
  )
}

プロパティ

プロパティ型既定値説明
checkedboolean—Controlled on/off state; use with onCheckedChange.
defaultCheckedbooleanfalseInitial state when uncontrolled.
onCheckedChange(checked: boolean) => void—Called on every change: click, keyboard, drag or label click.
labelReactNode—Label text; clicking it also toggles, and it is the switch's accessible name.
descriptionReactNode—Helper text under the label, linked through aria-describedby.
labelPlacement'start' | 'end''end'Label position; start fills the row and pushes the switch right, good for settings lists.
size'sm' | 'md' | 'lg''md'Size: 34×20 / 44×26 / 56×32 (thumb 16 / 20 / 26px).
icon'none' | 'check' | 'theme' | { on: ReactNode, off: ReactNode }'none'Icon inside the thumb: check (tick/cross), theme (sun/moon), or your own pair.
namestring—Form field name; submits name=value when on and nothing when off, like a native checkbox.
valuestring'on'Value submitted when on.
disabledbooleanfalseDisables the switch.
refRef<HTMLButtonElement>—Forwarded to the role=switch button, for example to focus it.
...restButtonHTMLAttributes—className and style go on the outer element; other native attributes (id, aria-*, onFocus…) go to the role=switch button.

アクセシビリティ

  • A native <button role="switch"> whose aria-checked reflects the state; Tab focuses it and Space or Enter toggles.
  • Label and description are linked with aria-labelledby and aria-describedby; clicking the label also toggles.
  • A soft accent halo shows on keyboard focus (a transparent outline keeps it visible in forced-colors mode); decorative parts such as icons and the thumb are aria-hidden.
  • A transparent enlarged hit area makes the target at least 44px tall.
  • With reduced motion there is no stretch or bounce: the position switches directly and only the color fades.

モーション

Press stretch 160ms ease-out; release movement and retract 420ms with a spring curve; track fill 240ms; hover tint 160ms; focus halo 220ms ease-out; icon rotate and scale 360ms spring plus 160ms opacity.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

ElasticSwitch.jsxelastic-switch.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フォーム入力
「空き時間グリッド」は Pro に含まれます。解除するとプレビューが表示されます。
「図形のブール演算」は Pro に含まれます。解除するとプレビューが表示されます。
「カスケード選択」は Pro に含まれます。解除するとプレビューが表示されます。