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] Fix Context Menu selection lost on Safari #44903

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/mui-material/src/Menu/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import composeClasses from '@mui/utils/composeClasses';
import HTMLElementType from '@mui/utils/HTMLElementType';
import { useRtl } from '@mui/system/RtlProvider';
import useSlotProps from '@mui/utils/useSlotProps';
import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
import MenuList from '../MenuList';
import Popover, { PopoverPaper } from '../Popover';
import rootShouldForwardProp from '../styles/rootShouldForwardProp';
Expand Down Expand Up @@ -106,6 +107,21 @@ const Menu = React.forwardRef(function Menu(inProps, ref) {

const menuListActionsRef = React.useRef(null);

useEnhancedEffect(() => {
if (open) {
const selection = document.getSelection();

if (selection && selection.rangeCount > 0) {
const range = selection.getRangeAt(0);

setTimeout(() => {
// Restore the selection before focusing, to not affect the text selection on Safari and Firefox.
selection.addRange(range);
});
}
}
}, [open]);

const handleEntering = (element, isAppearing) => {
if (menuListActionsRef.current) {
menuListActionsRef.current.adjustStyleForScrollbar(element, {
Expand Down
10 changes: 10 additions & 0 deletions packages/mui-material/src/MenuList/MenuList.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ function moveFocus(
// Move to the next element.
nextFocus = traversalFunction(list, nextFocus, disableListWrap);
} else {
const selection = document.getSelection();
if (selection && selection.rangeCount > 0) {
const range = selection.getRangeAt(0);

setTimeout(() => {
// Restore the selection before focusing, to not affect the text selection on Safari and Firefox.
selection.addRange(range);
});
}

nextFocus.focus();
return true;
}
Expand Down
Loading