Cell Switch
FreeAn on/off switch for a table cell. It turns one value of a row on or off without leaving the table.
npx wunderui-cli add cell-switchPlayground
| Member | Email alerts |
|---|---|
| Nora Lindgren | |
| Daniel Okafor | |
| Priya Raman |
| Member | Email alerts |
|---|---|
| Nora Lindgren | |
| Daniel Okafor | |
| Priya Raman |
| Member | Email alerts |
|---|---|
| Nora Lindgren | |
| Daniel Okafor | |
| Priya Raman |
Installation
npx wunderui-cli add cell-switchWrites the source into your project with its dependencies and, on first use, the styles - see Installation.
Then import what you need:
import { CellSwitch } from "@/components/wunderui/table-cells"Then use it inside a table cell:
import { useState } from "react"
import { CellSwitch } from "@/components/ui/cell-switch"
import { TableCell } from "@/components/ui/table"
export function EmailAlertsCell({ member }: { member: string }) {
const [on, setOn] = useState(true)
return (
<TableCell>
<CellSwitch aria-label={`Email alerts for ${member}`} checked={on} onCheckedChange={setOn} />
</TableCell>
)
}Expected result: You see a small brand-coloured switch in the cell, turned on. Click it or press Space to turn it off, and the track turns grey as the knob moves left.
When to use
Use Cell Switch for
- An on/off column in a table, such as enabled, notify or public
- A matrix of switches, where each row and column pair is one setting
- Settings that apply at once, with no Save button
Use something else for
- Switch for a setting outside a table, with a visible label
- Checkbox for a row you select for a bulk action, or a value that is sent with a form
- Selector Item for a setting with a title and a description in a card
Alignment
The switch is an inline control, so cell and header set where it sits. Align both the same way: centred suits a narrow column, left suits one beside text.
| Member | Email alerts |
|---|---|
| Nora Lindgren | |
| Daniel Okafor | |
| Priya Raman |
| Member | Email alerts |
|---|---|
| Nora Lindgren | |
| Daniel Okafor | |
| Priya Raman |
| Member | Email alerts |
|---|---|
| Nora Lindgren | |
| Daniel Okafor | |
| Priya Raman |
Keyboard interactions
Each switch is one tab stop in its cell. It is a Switch, so the browser and Base UI handle focus and activation.
- TaborShift+Tab
- Moves focus to the next or previous control in the table, row by row. The table cells around the switch take no focus.
- SpaceorEnter
- Toggles the focused switch and calls with the new value.
Arrow keys do not move between table cells. Every switch in a column is its own Tab stop, in row order.
Behavior and motion
What the cell switch does itself, and what it leaves to the table around it.
- Controlled
- CellSwitch keeps no state. You supply the checked value, every toggle calls the change handler with the new boolean, and nothing changes until you set it.
- Accessible name
- A cell has no visible label, so the switch is named by its aria-label. The default Toggle is the same in every row, so set one per row, such as Email alerts for Nora Lindgren.
- Table context
- With the first cell marked as a row header, a screen reader in table mode reads row, column and state together. In forms mode only the control is read, so its label must carry row and column.
- Size and hit area
- It is the default Switch, 32 by 18 px. The click area reaches 12 px to the sides and 8 px above and below, so a near miss still toggles it and two rows in a 36 px row never overlap.
- Alignment
- The switch is an inline control. Centre or right alignment on the cell moves it, and the column header should use the same alignment.
- Hover and pressed
- An off track gets a rim on hover and a darker grey while pressed. A brand track darkens on hover and darkens again while pressed.
- Figma states
- The Figma Editable Cell set has View, Edit, Changed, Saving, Error and Disabled, but CellSwitch is always editable. For a pending save or a disabled row, put a Switch with those states in the cell.
- Motion
- The track changes colour quickly, and the knob slides, swings slightly past the end and settles. A second click reverses it from wherever it is.
- Reduced motion
- With reduced motion the knob jumps to the other end instead of sliding. The colour change stays.
Anatomy
The figure marks one switch in its table: the column header and row header that name it, then the track and the knob. CellSwitch is the switch alone, and the table comes from the Table parts.
| Member | Email alerts |
|---|---|
| Nora |
- data-slot="table"
The table the cell sits in. The first column holds the row names, the header row names the columns, and the switch lives in a table cell.
- 1
The header of the switch column. It says what the switch changes, such as Email alerts, and aligns the same way as the cell.
- 2
The first cell of the row, marked as the row header. It says whose or which setting this is, so a screen reader reads it with the switch.
A Switch with a default aria-label of Toggle, made for a table cell with no visible label. You hold the checked state and write it back on change.
- 3
The 32 by 18 px pill. It is brand-coloured when on and grey when off, and it carries the switch role and checked state.
- 4data-slot="switch-thumb"
The 16 px circle that slides from one end of the track to the other. It swings slightly past the end before it settles.
Best practices
- Do
Name every switch by its row and column, such as Email alerts for Nora Lindgren. The default Toggle is the same in every row and tells a screen-reader user nothing.
- Do
Mark the first cell of each row as a row header and give the switch column a header that says what it changes. The cell is then read as row, column and state.
- Do
Keep the checked state in your own state and save in the same handler that writes it back. The cell keeps no state.
- Do
Align the switch like its column header: centred in a narrow column, left beside text. Use the same alignment class on the header and the cell.
- Don’t
Don't use CellSwitch outside a table. Without a visible label the switch has no context, so use Switch with a Label, or a Selector Item.
- Don’t
Don't use a switch for an action that needs a confirmation, such as deleting data. A switch applies at once.
- Don’t
Don't put two switches in one cell. One cell holds one value, so give each value its own column.
- Don’t
Don't expect a saving, error or disabled look from CellSwitch. For rows that need them, use a Switch with those states inside the cell.
API reference
CellSwitch is a plain function component with three props. It renders a Switch and sets the size and look itself, passing no other props through.
A Switch with a default aria-label of Toggle, made for a table cell with no visible label. You hold the checked state and write it back on change.
| Prop | Type | Default | Description |
|---|---|---|---|
| checked required | boolean | — | Whether the switch is on. It is a controlled value: write the new one back in . |
| onCheckedChange required | (checked) => void | — | Called with the new value, or , when the person toggles the switch with a click, Space or Enter. |
| aria-label | string | — | The name for screen readers. A cell has no visible label, so give each switch its row and column, such as . The default is . |
Notes for AI
Give your coding assistant the Markdown reference instead of screenshots.
get_component("cell-switch")npx wunderui-cli add cell-switchThe full library index for assistants is at /llms.txt.
Related components
Recent invoicesInvoices issued in the last 30 days Invoice Status Customer Amount INV-2041 Paid WunderUI $24,800.00 INV-2040 Open Halcyon Labs $3,200.00 INV-2039 Overdue Pergola Foods $980.00 INV-2038 Paid Marlow & Finch $12,400.00 Total Sum$41,380.00 Core Elements5 stylesTableRows and columns with 23 ready-made cell types; for sorting and search use Data Table.