Repository navigation
Conversation
Content opened from a menu item, such as a Modal or ConfirmationButton, is rendered through a portal and so is not a DOM descendant of the menu. usePortal's outside-click check therefore treated clicks inside it as outside, closed the menu and unmounted the modal with it. Handle outside clicks in the component instead: a capture handler on the wrapper records any mousedown that starts inside the menu's React tree (React events bubble through portals), and the document listener ignores that event. Outside clicks, closeOnOutsideClick={false} and the main-button-only rule behave as before. Fixes canonical#1049
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Done
ContextualMenuno longer closes when the user clicks inside content that the menu renders but that is portalled elsewhere in the DOM, such as aModalorConfirmationButtonopened from a menu item. Previously the click counted as "outside", the menu closed and the modal unmounted with it, so projects were disablingcloseOnOutsideClickaltogether.usePortal's DOMcontainscheck into the component. A capture handler on the wrapper records mousedowns that start anywhere in the menu's React tree (React events bubble through portals, so this includes the dropdown and anything portalled out of it), and a document listener ignores that event. Nothing is coupled toModal. Existing behaviour is unchanged: outside clicks close the menu,closeOnOutsideClick={false}disables that, and only the main button counts.main.Before / after
https://www.loom.com/share/f92a9293633b4b60b76ba7dc60f465aa
Before: clicking inside the confirmation modal closes the menu and the modal with it. After: both stay open until the user cancels or clicks outside.
QA
Pinging @canonical/react-library-maintainers for a review.
QA steps
yarn docs, open ContextualMenu -> Opens a modal.Testing
yarn lintandtsc --noEmit: clean.jest src/components/ContextualMenu: 57 tests pass. With the component change stashed, the two new portal tests fail as expected.Percy steps
One new snapshot for the ContextualMenu "Opens a modal" story (menu closed). No changes to existing stories expected.
Fixes
Fixes: #1049