Coding task · JavaScript and React course · Module 15: Patterns and Architecture

Practice: Headless useDropdown and useToggle

A headless hook holds the state and the behaviour and leaves the look to components. A prop getter such as getToggleProps() returns a ready object with onClick and ARIA attributes that you spread on an element with the spread operator: {...dropdown.getToggleProps()}. That way a screen reader knows whether the list is expanded (aria-expanded) and which option is selected (aria-selected), and the hidden attribute hides a collapsed panel from assistive technologies too. A comparison (===) already returns true or false.

Task

Finish two headless hooks and the two looks built on them. In useDropdown ___BLANK1___: after an option is selected the list closes (use the ready close action). ___BLANK2___: getToggleProps sets aria-expanded according to whether the list is open. ___BLANK3___: getOptionProps(index) sets aria-selected to true only for the selected option (selectedIndex) and to false for the others. In useToggle ___BLANK4___: getPanelProps hides the panel with the hidden attribute when the toggle is closed. ___BLANK5___: SimpleDropdown spreads on its button the props returned by getToggleProps of the useDropdown hook (the result of the call, not the function itself).