Skip to content

Conversation

@ze-flo
Copy link
Contributor

@ze-flo ze-flo commented Jul 30, 2025

Description

Fixes an issue where clicking outside a menu dropdown does not close it if a nested item is auto-focused.

Details

  • Previously, the menu relied on an event to dispatch a state change and reset state.focusOnOpen.
  • Now, state.focusOnOpen is explicitly reset after the component updates, rather than waiting for an event.
  • This change ensures more predictable behavior, especially when menu item object references are unstable.

Checklist

  • 🌐 demo is up-to-date (npm start)
  • 💂‍♂️ includes new unit tests
  • ♿ tested for WCAG 2.1 AA compliance
  • 📝 tested in Chrome, Firefox, Safari, and Edge

@coveralls
Copy link

Coverage Status

coverage: 94.792% (-0.02%) from 94.811%
when pulling 84fccfd on ze-flo/nested-menu-blur-fix
into cbb5a34 on main.

}

return () => {
clearTimeout(timeoutId);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory leaks are unlikely here since setTimeout(..., 0) runs almost immediately. However, it's still good to follow best practices.

Comment on lines -101 to -106
// Reset `focusOnOpen` if it was previously set to true; this prevents
// forced focus on the initial item in future state updates.
if (state.focusOnOpen) {
changes = { ...state, focusOnOpen: false };
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The useEffect hook resets focusOnOpen on update, eliminating the need for an explicit dispatch call. This method has proven to be more reliable, especially with nested submenus.

@ze-flo ze-flo marked this pull request as ready for review July 31, 2025 00:05
@ze-flo ze-flo requested a review from a team as a code owner July 31, 2025 00:05

export const StateChangeTypes: Record<string, string> = {
FnSetStateRefs: 'fn:setStateRefs',
FnInternalUpdate: 'fn:internalUpdate',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the new naming might be preferable, are we concerned this introduces a corner case breaking change? Would it make sense to leave 'fn:setStateRefs' as the change type name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only stateChangeType that wasn’t previously exposed to consumers through the onChange callback, so it should be safe to rename without causing any issues.

@ze-flo ze-flo merged commit 2253f70 into main Jul 31, 2025
4 checks passed
@ze-flo ze-flo deleted the ze-flo/nested-menu-blur-fix branch July 31, 2025 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants