Design SystemComponentsSwitch

Switch

Installation

import { Switch } from "@olab/components";

Overview

開關:點一下立刻切換開 / 關,有 md(20 × 40px,預設)與 sm(16 × 28px)兩種尺寸。開啟時軌道填品牌色,白色圓點滑到右邊。

Switch 代表「立刻生效」的設定;要按送出才生效的選項請用 Checkbox。

Interactive Demo

TOGGLE — click to flip

darkMode = false · notifications = true · autoSave = true

State Matrix

STATE × SIZE — static snapshot

state
md
sm
unchecked
checked
disabled
disabled + checked

沒有 hover、pressed 與 focus 的專屬樣式(見 Spec)。

Examples

WITHOUT LABEL — control only

Spec

PartValueSource class
Track · md20 × 40px,內距 1px,膠囊形h-5 w-10 p-px rounded-full
Track · sm16 × 28px,內距 2pxh-4 w-7 p-0.5
Track · off淡底data-[state=unchecked]:bg-secondary
Track · on品牌色data-[state=checked]:bg-brand-solid
Track · disabled跟頁面底色相同data-[disabled]:bg-primary!
Thumbmd 18px · sm 12px,白色圓點h-4.5 w-4.5 / size-3 · bg-[#fff]
Thumb · disabled淡灰data-[disabled]:bg-tertiary!
Label14px,disabled 轉淡text-sm text-primary / text-tertiary!
Track ↔ label8pxgap-2
Hover / pressed / focus沒有定義

數值為 2026-06-10 在 dark 模式下的實測(getComputedStyle);source class 對照目前的元件原始碼,色票隨目前的 dark / light 模式即時解析。完整量測、狀態細節與已知不一致見 live-ds-spec/switch.md。

Usage

Do

  • 點了就立刻生效的設定:通知開關、自動儲存、介面偏好。
  • label 寫成「這個設定是什麼」(Push notifications),不要寫成「開 / 關」。
  • 設定清單用 md;空間很擠的列內用 sm。

Don't

  • 不要拿 Switch 做要按送出才生效的選項;那是 Checkbox 的工作。
  • 不要用 Switch 在兩個不同選項之間切換(例如 YES / NO);請用 Tabs 或一排 Button。
  • 不要用 className 改圓點或軌道顏色。