Components · Form Controls
Switch
Turns a single setting on or off right away.
Component Workbench
Switch between the live interactive preview, syntax-highlighted implementation code, copyable AI coding agent prompt, and designer anatomy blueprint.
Switch · Canonical Spec
<label class="as-switch-field">
<span class="as-switch">
<input type="checkbox" role="switch" class="as-switch-input" checked>
<span class="as-switch-track"><span class="as-switch-thumb"></span></span>
</span>
<span class="as-switch-field__content">
<span class="as-switch-field__label">Automatic Node Eviction</span>
</span>
</label> # Aster Component Implementation Prompt — Switch
## 1. Component Purpose & Canonical Role
- **Component:** Switch (`switch`)
- **Category:** Form Controls
- **Native Element:** `<input type="checkbox" role="switch">`
- **Summary:** Turns a single setting on or off right away.
- **When to Use:** Instantaneous preference toggles (e.g. dark mode, auto-eviction, notifications).
- **When Not to Use:** Options inside a form requiring a Submit button (use Checkbox).
## 2. Authoritative Repository Files
- **Component CSS:** `components/switch/switch.css`
- **Component Spec:** `docs/components/switch.md`
- **Agent Contract:** `skills/using-aster-components/SKILL.md`
- **Compiled Bundle:** `dist/css/aster.css` and `dist/js/aster.js`
## 3. Canonical HTML Structure & Classes
```html
<label class="as-switch-field">
<span class="as-switch">
<input type="checkbox" role="switch" class="as-switch-input" checked>
<span class="as-switch-track"><span class="as-switch-thumb"></span></span>
</span>
<span class="as-switch-field__content">
<span class="as-switch-field__label">Automatic Node Eviction</span>
</span>
</label>
```
## 4. Measured Geometry
Measured from the rendered component when this page loads.
## 5. Non-Negotiable Aster Constraints
1. **Zero Primitive Color Leaks:** Bind colors strictly to `--as-semantic-color-*` custom properties. Never use `--as-color-primitive-*` or hardcoded hex values in component usage.
2. **Minimum 4px Radius Policy:** Never use `2px` border-radius. Use `--as-radius-sm` (`4px`), `--as-radius-md` (`8px`), `--as-radius-lg` (`12px`), or `--as-radius-full` (`9999px`).
3. **Single Standard Density Mode:** Compact mode (`data-density="compact"` / `.as-*--compact`) is permanently retired. Do not emit compact classes or attributes.
4. **WCAG 2.2 AA Accessibility:** Maintain >= 4.5:1 text contrast, >= 3:1 UI/border contrast, >= 24x24px interactive hit targets, and full keyboard operability (`Tab`, `Enter`, `Space`, `Escape`, Arrow keys).
## 6. Verification Commands
```bash
npm run test:components
npm run test:contrast
npm run test:browser
``` When to use it
Do
- Use it for settings that apply immediately.
- Name the setting, not the action.
Don't
- Use it in a form that needs saving. Use Checkbox instead.
- Change the label when the state changes.
Variants & States
Every option, rendered live with real styles.
Accessibility & Keyboard Interaction
Requirements for using this component accessibly, following the W3C ARIA Authoring Practices Guide.
| Interaction / Key | Behavior & Compliance Requirement |
|---|---|
| Role Switch | Declares role="switch" so assistive tech announces on/off state. |
Space | Toggles switch state. |
Tokens
Values update with the current theme.
| Role | Token | Current value |
|---|---|---|
| Active Track | --as-semantic-color-interactive-primary | |
| Active Thumb | --as-semantic-color-interactive-primary-text |