Repository navigation
[Web]: (feature/docs) Disable the context menu from browser. #78671
Description
Since I found not no docs and no help on forums, etc. We should clarify that here:
How to disable the context menu of the browser when right click or long press (browser, on touch press release)?
In JavaScript it's:
document.body.addEventListener('contextmenu', (evemt) => {
event.preventDefault();
});But how to do this in Flutter (2.x, Web)?
If this feature is missing: Here's the feature request: Implement the possibility to disable the context menu. No workaround like blocking right click, please. Because it also appears on mobile long press.
Ok, I could disable the context menu when deploy the flutter app. Because it's just an index.html with the flutter JS / Canvas etc. But how to access the html during debugging / coding?
E.g. is there a way to inject JavaScript? But note: It should also work (ignored) as native mobile app.
Btw. Flutter should disable the browser context menu by default. Because there is nothing to select. It's jsut the canvas.
Activity
darshankawar commentedon Mar 22, 2021
@y0nd0
Can you give an example or reproducible use case that shows the context menu on web and unable to disable it ?
Thanks.
y0nd0 commentedon Mar 22, 2021
@darshankawar
Everywhere, the whole page. FAB or background. You can test it. Just download the latest Flutter (2.x) and run it on Web (default). You can either right click or long press in mobile mode.
This behavior does not exist on docs examples like FAB. I think it's because the editor already has the context menu disabled by JavaScript? So you have to test it locally in debug mode.
It's annoying and disallows long press. So we need to prevent the default behavior.
Note that Flutter has not DOM elements. It's just a canvas. So, there is no sense for that context menu. We should disable it by default.
darshankawar commentedon Mar 23, 2021
flutter doctor -v
[-](feature/docs) Disable the context menu from browser.[/-][+][Web]: (feature/docs) Disable the context menu from browser.[/+]yjbanov commentedon Mar 25, 2021
You can import
dart:htmland use the same API as in JavaScript. If you are cross-compiling for non-web platforms you may need to use conditional imports, becausedart:htmlis not available outside web.4 remaining items
darshankawar commentedon Mar 29, 2021
Reopening based on author's request above.
Cc: @yjbanov for further insights.
yjbanov commentedon Apr 1, 2021
Oh, I see. You want to be able to prevent the menu for Flutter's own pointer events. That makes sense. I'm going to keep it open as a feature request.
y0nd0 commentedon Apr 3, 2021
Correct, it's only about Flutters internal behavior. No API etc. Just disable the context menu of the browser (like described above).
Flutter is rendered in a canvas. This means, with the conterxt menu, you can just e.g. save the canvas as image etc. It's not a good UX to let this context menu enabled. ...
Even if Flutter can select, copy and paste text, etc. (e.g. with
SelectableTextwidget). In this case it's not related to thecanvas. It's a separate DOM element (textarea). Means: Only disable the context menu for the canvas element which renders the app. Not on the body etc. Otherwise you cannot copy / paste text anymore. ... I think this issue is much easier than maybe expected. Just add thecontextmenuevent to the canvas topreventDefault. No options in my opinion. ...It would be nice, if someone could post the code section of that canvas creation for Flutter web.
Btw. we could also thread this as bug (unexpected behavior). Because it's not really a feature. More an improvement / bugfix. The context menu is useless and annoying. Bad UX and DX (developer experience). ... So why not higher prio and bug. It's done in 5 minutes. :-) Seriously...
infacto commentedon Apr 9, 2021
Yes, this is really annoying when e.g. using gestures. I would prefer to handle this issue as bug and higher priority. Especially since this can be fixed very easily and quickly. I would do it. But I don't know the source code. The line of Flutter web which creates the canvas element...
infacto commentedon Apr 9, 2021
A good reason for the Flutter team: You provide tooltips on e.g. FAB buttons which are displayed on long press. But in this case you also trigger the context menu of the browser. (Web Mobile)
So this is a bug. We really should disable the context menu of the Flutter renderer canvas DOM element. :)
justinmc commentedon Apr 7, 2022
If this issue was fixed then I could allow users to show custom context menus on the web.
I've recently done some work on making context menus more usable in Flutter (design doc). If Web could allow us to stop showing the browser context menu for a certain widget subtree, then this would all work on Web similar to the other platforms.
github-actions commentedon Mar 3, 2023
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.