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

SubMenu: Fix expanding sub menu items on touch devices #93208

Merged
merged 9 commits into from
Oct 7, 2024

Conversation

yincongcyincong
Copy link
Contributor

Fixes #93151
actually, i add mobile click event onTouchStart, and use event delegation to avoid menu is closed
please take a look @tskarhed @torkelo
https://github.com/user-attachments/assets/4ce7541c-7c5d-4d35-93bd-18ff6e960829

@yincongcyincong yincongcyincong requested a review from a team as a code owner September 11, 2024 09:43
@yincongcyincong yincongcyincong requested review from Clarity-89 and JoaoSilvaGrafana and removed request for a team September 11, 2024 09:43
@github-actions github-actions bot added this to the 11.3.x milestone Sep 11, 2024
@grafana-pr-automation grafana-pr-automation bot added area/frontend pr/external This PR is from external contributor labels Sep 11, 2024
Copy link
Contributor

@ashharrison90 ashharrison90 left a comment

Choose a reason for hiding this comment

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

thanks for raising the issue and looking at this! 🙌

think maybe a better approach would be to change the onClick in MenuItem to be something like:

        onClick={(event) => {
          if (hasSubMenu) {
            event.preventDefault();
            event.stopPropagation();
          }
          onClick?.(event);
        }}

that should stop the click event propagating up to the overlay and closing the menu.

note: this isn't perfect, if the new submenu overflows the bounds of the mobile view it will resize the whole window. but it at least unblocks people from using these submenus. i think the changes needed to properly support mobile behaviour for the Menu and MenuItem elements are bigger than can be delivered in a community PR 😅

Comment on lines 76 to 83
const onOverlayClicked = (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
if (event.target instanceof HTMLDivElement || event.target instanceof HTMLSpanElement) {
const innerText = event.target.innerText
if (innerText.includes("More...")) {
return
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

this is definitely too brittle i'm afraid 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for your review, bro. i will modify it.

@yincongcyincong yincongcyincong requested a review from a team as a code owner September 14, 2024 02:00
@yincongcyincong yincongcyincong requested review from axelavargas and mdvictor and removed request for a team September 14, 2024 02:00
@yincongcyincong
Copy link
Contributor Author

@ashharrison90 could you please look this pr again?

Copy link
Contributor

@tskarhed tskarhed left a comment

Choose a reason for hiding this comment

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

This doesn't seem to quite work. We want all the use cases for the menu to support touch for submenu, so it is better if the stopPropagation is used in MenuItem. Try the approach that @ashharrison90 suggested!

@yincongcyincong
Copy link
Contributor Author

yincongcyincong commented Sep 24, 2024

This doesn't seem to quite work. We want all the use cases for the menu to support touch for submenu, so it is better if the stopPropagation is used in MenuItem. Try the approach that @ashharrison90 suggested!

thanks for review. i have add stopPropagation at MenuItem. please take a look at lastest pr.
https://github.com/grafana/grafana/pull/93208/files#diff-6274d9463d0536b679c48ba4551dbc84354b1a742e5d385f01d61fa494f27d75R78

@tskarhed
Copy link
Contributor

thanks for review. i have add stopPropagation at MenuItem. please take a look at lastest pr. https://github.com/grafana/grafana/pull/93208/files#diff-6274d9463d0536b679c48ba4551dbc84354b1a742e5d385f01d61fa494f27d75R78

The issue is still there:
https://github.com/user-attachments/assets/158db928-5672-42db-b590-d61e2bdda399

You have added stopPropagation only for onMore, which doesn't account for inspect or any other use case.

Try adding the ash's requested changes here:

@yincongcyincong
Copy link
Contributor Author

thanks for review. i have add stopPropagation at MenuItem. please take a look at lastest pr. https://github.com/grafana/grafana/pull/93208/files#diff-6274d9463d0536b679c48ba4551dbc84354b1a742e5d385f01d61fa494f27d75R78

The issue is still there: https://github.com/user-attachments/assets/158db928-5672-42db-b590-d61e2bdda399

You have added stopPropagation only for onMore, which doesn't account for inspect or any other use case.

Try adding the ash's requested changes here:

yep. actually this pr don't work on inspect. because inspect is very special. itself has a click event. if i modify the click event. it will result some other bug. like this.
image

@tskarhed
Copy link
Contributor

yep. actually this pr don't work on inspect. because inspect is very special. itself has a click event. if i modify the click event. it will result some other bug. like this. image

You're right. This functionality doesn't make sense. Not calling onClick when there is a submenu is more consistent behavior. Can you add those changes too?

@yincongcyincong
Copy link
Contributor Author

yep. actually this pr don't work on inspect. because inspect is very special. itself has a click event. if i modify the click event. it will result some other bug. like this. image

You're right. This functionality doesn't make sense. Not calling onClick when there is a submenu is more consistent behavior. Can you add those changes too?

i'm pleasure contribute to grafana. i will give a new pr!

@tskarhed
Copy link
Contributor

i'm pleasure contribute to grafana. i will give a new pr!

It is okay if you do it in this PR, as it will make it easier to fix the other issue :)

@yincongcyincong
Copy link
Contributor Author

i'm pleasure contribute to grafana. i will give a new pr!

It is okay if you do it in this PR, as it will make it easier to fix the other issue :)

ok, i will change this pr. thanks for your review.

@yincongcyincong
Copy link
Contributor Author

@tskarhed bro, i delete inspect click function and add stopPropagation at menu that have submenu. please take a look again. thanks

video-1727321713273.mp4

@@ -108,7 +108,8 @@ export const MenuItem = React.memo(
[styles.disabled]: disabled,
[styles.destructive]: destructive && !disabled,
},
className
className,
hasSubMenu ? 'hasSubmenu' : 'noSubmenu'
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of using a class as a selector, you could add aria-haspopup="menu" to the item that has a submenu and use that when selecting closest.

Copy link
Contributor

@ashharrison90 ashharrison90 left a comment

Choose a reason for hiding this comment

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

think the changes in Menu/MenuItem can simplify down into just modifying the onClick - see suggestions. i think this handles all cases 🙏 thanks again for tackling this 🙇

onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
onKeyDown={handleKeys}
onTouchStart={onMouseEnter}
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
onTouchStart={onMouseEnter}

@@ -108,7 +108,8 @@ export const MenuItem = React.memo(
[styles.disabled]: disabled,
[styles.destructive]: destructive && !disabled,
},
className
className,
hasSubMenu ? 'hasSubmenu' : 'noSubmenu'
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
hasSubMenu ? 'hasSubmenu' : 'noSubmenu'

Comment on lines 156 to 162
onClick={(event) => {
if (hasSubMenu && !(event.target as HTMLElement).closest('.noSubmenu')) {
event.preventDefault();
event.stopPropagation();
}
onClick?.(event);
}}
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
onClick={(event) => {
if (hasSubMenu && !(event.target as HTMLElement).closest('.noSubmenu')) {
event.preventDefault();
event.stopPropagation();
}
onClick?.(event);
}}
onClick={(event) => {
if (hasSubMenu && !isSubMenuOpen) {
event.preventDefault();
event.stopPropagation();
}
onClick?.(event);
}}

@yincongcyincong
Copy link
Contributor Author

@tskarhed @ashharrison90 bros, thanks for your review. i gived a new pr.

@ashharrison90 ashharrison90 changed the title fix: clicking more in mobile phone don't work SubMenu: Fix expanding sub menu items on touch devices Oct 7, 2024
Copy link
Contributor

@ashharrison90 ashharrison90 left a comment

Choose a reason for hiding this comment

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

think this looks great 👍 thanks for the contribution! 🙌

@ashharrison90 ashharrison90 merged commit 63bdbb6 into grafana:main Oct 7, 2024
16 of 17 checks passed
@joshhunt joshhunt modified the milestones: 11.3.x, 11.3.0 Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add to changelog area/frontend no-backport Skip backport of PR pr/external This PR is from external contributor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PanelChrome: Sub menu items not expanding on mobile devices
4 participants