Build a button-triggered dropdown menu with keyboard support.
Skip to solutionKEEP THE
easyMachine Coding
Build a Dropdown Menu
299 views
01
Understand the problem
ReactMenuAccessibility
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Solve in
Build a Dropdown Menu in React
Mental Model: The core idea is to manage the dropdown's visibility with a state variable. We'll use a button to toggle this state and an onClick handler on the document to close it when clicking outside. Keyboard navigation will involve onKeyDown handlers to manage focus and v
Solution ready — 2 min read
Classified // press E to declassify
04
React solution
Complete solutionRun Playground
import React, { useState } from 'react';
import DropdownMenu from './src/DropdownMenu';
export default function App() {
const [selectedOption, setSelectedOption] = useState('None');
return (
<div style={{ fontFamily: 'sans-serif', padding: '20px' }}>
<h1>Dropdown Menu Demo</h1>
<p>Click the button or use keyboard (Space/Enter to open, Arrows to navigate, Escape to close).</p>
<DropdownMenu onSelect={setSelectedOption} />
<p style={{ marginTop: '20px' }}>Current selected option: <strong>{selectedOption}</strong></p>
</div>
);
}05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 16 of 87 decoded in the Frontend Machine Coding track. One more won't hurt.