Skip to content

Commit

Permalink
change(header): dropdowns now auto open and close on focus and blur w…
Browse files Browse the repository at this point in the history
…hen using keyboard navigation
  • Loading branch information
ichim-david committed Nov 12, 2023
1 parent 947b1f9 commit d558e9b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/ui/Header/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const TopItem = ({ children, className, id }) => (
);

const onKeyDownHandler = (event) => {
console.log('on key');
if (event.key === 'Enter') {
event.preventDefault();
event.target.click();
Expand All @@ -70,6 +71,7 @@ const TopDropdownMenu = ({
const Component = ({ mobileText }) => {
const headerText = mobileText || text;
const label = ariaLabel || headerText;
const dropdownRef = React.useRef(null);

return (
<>
Expand All @@ -81,12 +83,20 @@ const TopDropdownMenu = ({
{headerText}
</div>
)}
ref={dropdownRef}
icon={icon || 'chevron down'}
aria-label={label}
closeOnChange={true}
closeOnBlur={false}
closeOnEscape={true}
openOnFocus={false}
openOnFocus={true}
onBlur={(e) => {
const dropdown = dropdownRef.current;
const ref = dropdown.ref.current;
if (e.target !== ref && !ref.contains(e.relatedTarget)) {
dropdown.close();
}
}}
onKeyDown={onKeyDownHandler}
>
<Dropdown.Menu role="option">{children}</Dropdown.Menu>
Expand Down

0 comments on commit d558e9b

Please sign in to comment.