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 scrolling on code block issue #10659

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import _ from 'underscore';
import withLocalize from '../../../withLocalize';
import htmlRendererPropTypes from '../htmlRendererPropTypes';
import BasePreRenderer from './BasePreRenderer';
Expand All @@ -8,6 +9,7 @@ class PreRenderer extends React.Component {
super(props);

this.scrollNode = this.scrollNode.bind(this);
this.debouncedIsScrollingVertically = _.debounce(this.isScrollingVertically.bind(this), 100, true);
}

componentDidMount() {
Expand All @@ -23,17 +25,27 @@ class PreRenderer extends React.Component {
.removeEventListener('wheel', this.scrollNode);
}

/**
* Check if user is scrolling vertically based on deltaX and deltaY. We debounce this
* method in the constructor to make sure it's called only for the first event.
* @param {WheelEvent} event Wheel event
* @returns {Boolean} true if user is scrolling vertically
*/
isScrollingVertically(event) {
// Mark as vertical scrolling only when absolute value of deltaY is more than the double of absolute
// value of deltaX, so user can use trackpad scroll on the code block horizontally at a wide angle.
return Math.abs(event.deltaY) > (Math.abs(event.deltaX) * 2);
}

/**
* Manually scrolls the code block if code block horizontal scrollable, then prevents the event from being passed up to the parent.
* @param {Object} event native event
*/
scrollNode(event) {
const node = this.ref.getScrollableNode();
const horizontalOverflow = node.scrollWidth > node.offsetWidth;

// Account for vertical scrolling variation when horizontally scrolling via touchpad by checking a large delta.
const isVerticalScrolling = Math.abs(event.deltaY) > 3; // This is for touchpads sensitive
if ((event.currentTarget === node) && horizontalOverflow && !isVerticalScrolling) {
const isScrollingVertically = this.debouncedIsScrollingVertically(event);
if ((event.currentTarget === node) && horizontalOverflow && !isScrollingVertically) {
node.scrollLeft += event.deltaX;
event.preventDefault();
event.stopPropagation();
Expand Down