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(event/select): prevent scrolling when context menu opened #490

Merged
merged 3 commits into from
Aug 2, 2022
Merged
Changes from 2 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
21 changes: 19 additions & 2 deletions src/events/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function selectHandler(scrollbar: I.Scrollbar) {
const { containerEl, contentEl } = scrollbar;

let isSelected = false;
let isContextMenuOpened = false; // flag to prevent selection when context menu is opened
let animationID: number;

function scroll({ x, y }) {
Expand Down Expand Up @@ -38,8 +39,23 @@ export function selectHandler(scrollbar: I.Scrollbar) {
scroll(dir);
});

addEvent(contentEl, 'selectstart', (evt: Event) => {
evt.stopPropagation();
// prevent scrolling when context menu is opened
addEvent(contentEl, 'contextmenu', () => {
isSelected = false;
isContextMenuOpened = true;
});

// reset context menu flag on mouse down
// to ensure the scrolling is allowed in the next selection
addEvent(contentEl, 'mousedown', () => {
isContextMenuOpened = false;
});

addEvent(contentEl, 'selectstart', () => {
if (isContextMenuOpened) {
return;
}

cancelAnimationFrame(animationID);

isSelected = true;
Expand All @@ -49,6 +65,7 @@ export function selectHandler(scrollbar: I.Scrollbar) {
cancelAnimationFrame(animationID);

isSelected = false;
isContextMenuOpened = false;
});

// patch for touch devices
Expand Down