Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: contextual menu container overflow #1000

Conversation

petermakowski
Copy link
Contributor

@petermakowski petermakowski commented Nov 22, 2023

Done

This pull request fixes the menu position when scrolling in overflowing containers.

  • fix contextual menu dropdown not being scrolled correctly in overflowing containers
  • replace deprecated ClientRect with DOMRect
  • fix dropdown props being passed incorrectly to the wrapper
  • add options to useOnEscapePressed and useClickOutside
  • rename useClickOutside to useOnClickOutside for consistency
  • remove the portal ref hack, update react-useportal to 1.0.19 Support opening the portal programmatically. alex-cory/react-useportal#83 (comment)
  • add options to useListener hook
  • rename wrapperClassName to contextualMenuClassName for clarity

Screenshots

Before

Google Chrome screenshot 001100

After

Google Chrome screenshot 001099

QA

QA steps

  • Go to the overflow example

  • Ensure that once you open the dropdown and scroll, menu position is in sync with the toggle

  • In the props control, set autoAdjust to false and position to right

  • Open the dropdown menu

  • Menu position should reflect the value of the prop (right)

  • Close the dropdown

  • Set autoAdjust to true

  • Open the dropdown

  • Menu position should be adjusted on open (and set to the left)

  • Link react-components locally with your project and ensure that contextual menus work as expected

Fixes

Fixes: https://warthogs.atlassian.net/browse/MAASENG-2407
#687
#769

@petermakowski petermakowski force-pushed the contextual-menu-container-element-scroll-MAASENG-2407 branch from 2517424 to 6bb3295 Compare November 22, 2023 13:10
@petermakowski petermakowski marked this pull request as ready for review November 22, 2023 13:10
@petermakowski petermakowski force-pushed the contextual-menu-container-element-scroll-MAASENG-2407 branch from 6bb3295 to 8f23d98 Compare November 22, 2023 13:50
- fix dropdown props being passed incorrectly to the wrapper
- add isEnabled option to useOnEscapePressed and useClickOutside
@petermakowski petermakowski force-pushed the contextual-menu-container-element-scroll-MAASENG-2407 branch from 8f23d98 to 809fc3f Compare November 22, 2023 14:13
Copy link

@ndv99 ndv99 left a comment

Choose a reason for hiding this comment

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

This looks good to me 👍

Copy link
Contributor

@edlerd edlerd left a comment

Choose a reason for hiding this comment

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

Looks good functionality wise. Two nitpicks about code style / simplifications. Feel free to address or keep as is.

Edit: I don't have a way to test this in other projects without a full release. But I won't expect this to break anything.

aria-label={Label.Dropdown}
ref={dropdown}
style={{
...(positionStyle as React.CSSProperties),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there is no need to cast here.

Suggested change
...(positionStyle as React.CSSProperties),
...positionStyle,

src/components/ContextualMenu/ContextualMenu.tsx Outdated Show resolved Hide resolved
@huwshimi
Copy link
Collaborator

Hi @petermakowski, this was using a portal so that it could escape its container to prevent issues such as overflow: hidden on a parent (same as we do for tooltips, portals etc.) With this change you can see that the menus get cut off in the storybook examples:

Screenshot 2023-11-23 at 9 45 58 am Screenshot 2023-11-23 at 9 51 48 am

Any ideas for mitigating that issue?

@petermakowski
Copy link
Contributor Author

Hi @petermakowski, this was using a portal so that it could escape its container to prevent issues such as overflow: hidden on a parent (same as we do for tooltips, portals etc.)
Any ideas for mitigating that issue?

@huwshimi Thanks for chipping in. Very good point, clearly I was missing some context here. I intended for this to be fixed by removing overflow: hidden from a container, but this might not work in all cases.

In the examples that you pasted specifically this can be worked around by using automatic positioning and displaying the menu at the top of a trigger when not enough space is available at the bottom (the approach that we're exploring in MAAS currently). Having said that, it's a big change in approach and something that not all teams may want to adopt. I'll give this some more thought. Marking this as do not merge for now.

@huwshimi
Copy link
Collaborator

Hi @petermakowski, this was using a portal so that it could escape its container to prevent issues such as overflow: hidden on a parent (same as we do for tooltips, portals etc.)
Any ideas for mitigating that issue?

@huwshimi Thanks for chipping in. Very good point, clearly I was missing some context here. I intended for this to be fixed by removing overflow: hidden from a container, but this might not work in all cases.

In the examples that you pasted specifically this can be worked around by using automatic positioning and displaying the menu at the top of a trigger when not enough space is available at the bottom (the approach that we're exploring in MAAS currently). Having said that, it's a big change in approach and something that not all teams may want to adopt. I'll give this some more thought. Marking this as do not merge for now.

Thanks Peter, we've definitely had cases in the past where we couldn't control the overflow which is why we ended up using portals, but maybe there's another approach we haven't considered.

Copy link
Collaborator

@huwshimi huwshimi left a comment

Choose a reason for hiding this comment

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

Just leaving a "Request changes" here so that we remember this needs to address the overflow: hidden hidden issue as discussed.

- update react-useportal to v1.0.19
- add options to useListener
@petermakowski
Copy link
Contributor Author

@huwshimi This is ready for another review. I reverted changes related to relative positioning, keeping node position in sync with the container is now handled similarly to window resize.

Copy link
Collaborator

@huwshimi huwshimi left a comment

Choose a reason for hiding this comment

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

Works great now, thanks for the improvement @petermakowski!

@petermakowski petermakowski merged commit f523d99 into canonical:main Nov 29, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants