Question presented to candidate: "Build a reusable toggle switch, the on/off control you see in phone settings. I want to use it for a small settings panel: Notifications, Email digest and Sound. The Email digest switch should be disabled, and switched off, whenever Notifications is off. Make it accessible."
Requirements:
- A reusable switch component with a visible label, a sliding knob, and a colour change between off and on.
- Controlled: the parent passes
checkedand receives the new value through a change callback or event; the switch stores no state of its own. - A
disabledstate that cannot be toggled by click, keyboard or label. - Clicking the label toggles the switch; Tab reaches it, Space and Enter toggle it.
- The settings panel enforces the rule "digest off and disabled when notifications are off" in the parent.
What a strong answer should cover:
- The controlled component pattern and why the parent owns the value (it can veto or derive other values).
role="switch"witharia-checkedon a nativebutton, and why a button rather than adiv.- Label association (
label forplus a unique id fromuseId, VueuseId, or a counter), so the label is the accessible name and is clickable. - Disabled handling: the native attribute, plus a guard in the handler.
- CSS-only knob animation with
transform, andprefers-reduced-motion.
Clarifying questions expected:
- "Should it be controlled, uncontrolled, or support both?"
- "Does it need to work inside a form submission, like a checkbox?"
- "Do you want an on/off text inside the track?"
Code / implementation expected: Yes. A reusable switch component plus a small panel using three of them, roughly 50-80 lines.