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(menu): Safari issue on closing menu clicking on trigger #2338

Merged
merged 7 commits into from
Nov 24, 2022
Merged

Conversation

massao
Copy link
Contributor

@massao massao commented Nov 23, 2022

Purpose of PR

Fix issue with Safari when trying to close menu by clicking on the trigger

Obs:

  • Due to safari not correctly passing focus to the button, sometimes it causes the blue border to always show on the menu when opened.
  • Another proposed solution around this issue is to add a div with tabindex=0 inside the button and use it to trigger the events, which in our case would be tricky since the trigger could be a button or not.
  • Downshift has another approach but I couldn't make it work in the menu, the blur event was always being triggered after the MouseDown, so trying to set the focus on the onClick wasn't working. Which would means that we would have to manually focus on the events.

@changeset-bot
Copy link

changeset-bot bot commented Nov 23, 2022

🦋 Changeset detected

Latest commit: ff399fb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 33 packages
Name Type
@contentful/f36-menu Patch
@contentful/f36-popover Patch
@contentful/f36-accordion Patch
@contentful/f36-asset Patch
@contentful/f36-autocomplete Patch
@contentful/f36-badge Patch
@contentful/f36-button Patch
@contentful/f36-card Patch
@contentful/f36-collapse Patch
@contentful/f36-copybutton Patch
@contentful/f36-datepicker Patch
@contentful/f36-datetime Patch
@contentful/f36-drag-handle Patch
@contentful/f36-entity-list Patch
@contentful/f36-forms Patch
@contentful/f36-icon Patch
@contentful/f36-icons Patch
@contentful/f36-list Patch
@contentful/f36-modal Patch
@contentful/f36-note Patch
@contentful/f36-notification Patch
@contentful/f36-pagination Patch
@contentful/f36-pill Patch
@contentful/f36-skeleton Patch
@contentful/f36-spinner Patch
@contentful/f36-table Patch
@contentful/f36-tabs Patch
@contentful/f36-text-link Patch
@contentful/f36-tooltip Patch
@contentful/f36-typography Patch
@contentful/f36-utils Patch
@contentful/f36-core Patch
@contentful/f36-components Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Nov 23, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
forma-36 ✅ Ready (Inspect) Visit Preview Nov 24, 2022 at 2:45PM (UTC)

@github-actions
Copy link

github-actions bot commented Nov 23, 2022

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
CommonJS 121.52 KB (+0.08% 🔺) 2.5 s (+0.08% 🔺) 92 ms (-8.45% 🔽) 2.6 s
Module 119.26 KB (+0.05% 🔺) 2.4 s (+0.05% 🔺) 75 ms (+1.33% 🔺) 2.5 s

@denkristoffer
Copy link
Collaborator

Can you explain your considerations around the Downshift approach a bit more? Looking at the code I think I understand it better to be honest.

bgutsol
bgutsol previously approved these changes Nov 23, 2022
Copy link
Contributor

@bgutsol bgutsol left a comment

Choose a reason for hiding this comment

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

Nice, I like the approach you chose 👍

I would just add more explanation around the issue in the code.

@@ -150,12 +150,26 @@ export function Menu(props: MenuProps) {
[closeAndFocusTrigger, handleArrowsKeyDown],
);

const isMouseDown = useRef<Boolean>(false);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const isMouseDown = useRef<Boolean>(false);
// the comment that explains the issue in safari that we are trying to fix here. Because the one we have in the `onMouseDown` event is not clear enough for me.
const isTriggerMouseDown = useRef<Boolean>(false);

WDYT?

Comment on lines 162 to 169
// handle odd case for Safari which
// prevent triggering blur when MouseDown
isMouseDown.current = true;
_props.onMouseDown?.(event);
},
onMouseUp: (event) => {
// handle odd case for Safari
// set isMouseDown as false to enable blur
Copy link
Contributor

Choose a reason for hiding this comment

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

and then this comments we can delete I guess

@massao
Copy link
Contributor Author

massao commented Nov 23, 2022

Can you explain your considerations around the Downshift approach a bit more? Looking at the code I think I understand it better to be honest.

Sorry, had a typo there, I fixed it.
But what I meant is that I couldn't make that approach work, because the blur is triggered in between the onMouseDown and onMouseUp and setting the focus on the onClick that is triggered after onMouseUp wasn't working. As the blur was already fired at that point, and is what is causing the issue here.
onBlur triggers -> because Safari doesn't handle the relatedTarget well, it closes the modal -> onClick triggers and opens the modal again

Copy link
Contributor

@bgutsol bgutsol left a comment

Choose a reason for hiding this comment

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

and I just remembered that we need to do the same for the Popover

@bgutsol bgutsol self-requested a review November 23, 2022 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants