Grid Lights
UI Coding
Medium
30 min
state
timers
effects
accessibility
Build a grid of lights driven by a configuration matrix. A 1 is a light and a 0 is an empty gap. The default layout is a 3 × 3 square without its centre (8 lights).
const CONFIG = [
[1, 1, 1],
[1, 0, 1],
[1, 1, 1],
];
API
export function GridLights({ config = CONFIG, delay = 300 })
Requirements
- All lights start off. Clicking an off light turns it on. Clicking a light that is already on does nothing.
- The component remembers the order in which lights were switched on.
- When the last light turns on, the grid starts deactivating: every
delayms, the most recently activated light turns off, in reverse activation order, until all are off. - While deactivating, clicks are ignored. Once everything is off, the grid is interactive again.
- The layout must follow
configfor any shape (gaps take up space but render nothing interactive). - Accessibility:
- Each light is a toggle
<button>witharia-pressed, named by position:Light at row 1, column 2. - During deactivation, lights are marked unavailable (
aria-disabled) but stay focusable. - Announce
All lights on. Switching off…when deactivation starts.
- Each light is a toggle
- Clean up any pending timer if the component unmounts mid-sequence.
Follow-up
- Let users switch a light off manually, removing it from the order.
- What breaks if you implement step 3 with a single
setIntervalthat capturesorder? - Make the deactivation pausable.
Spotted a mistake or a rights concern? Report an issue