2018-11-12 14:57:21 +03:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2019-03-28 02:51:23 +03:00
|
|
|
import PropTypes from "prop-types";
|
2018-11-12 14:57:21 +03:00
|
|
|
import AutoHideScrollbar from "./AutoHideScrollbar";
|
2021-03-09 05:35:10 +03:00
|
|
|
import {replaceableComponent} from "../../utils/replaceableComponent";
|
2018-11-12 14:57:21 +03:00
|
|
|
|
2021-03-09 05:35:10 +03:00
|
|
|
@replaceableComponent("structures.IndicatorScrollbar")
|
2018-11-12 14:57:21 +03:00
|
|
|
export default class IndicatorScrollbar extends React.Component {
|
2019-09-06 16:27:33 +03:00
|
|
|
static propTypes = {
|
2019-03-28 02:51:23 +03:00
|
|
|
// If true, the scrollbar will append mx_IndicatorScrollbar_leftOverflowIndicator
|
|
|
|
// and mx_IndicatorScrollbar_rightOverflowIndicator elements to the list for positioning
|
|
|
|
// by the parent element.
|
|
|
|
trackHorizontalOverflow: PropTypes.bool,
|
2019-04-05 01:43:04 +03:00
|
|
|
|
|
|
|
// If true, when the user tries to use their mouse wheel in the component it will
|
|
|
|
// scroll horizontally rather than vertically. This should only be used on components
|
|
|
|
// with no vertical scroll opportunity.
|
|
|
|
verticalScrollsHorizontally: PropTypes.bool,
|
2019-03-28 02:51:23 +03:00
|
|
|
};
|
|
|
|
|
2018-11-12 14:57:21 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this._collectScroller = this._collectScroller.bind(this);
|
2018-12-18 16:29:42 +03:00
|
|
|
this._collectScrollerComponent = this._collectScrollerComponent.bind(this);
|
2018-11-12 14:57:21 +03:00
|
|
|
this.checkOverflow = this.checkOverflow.bind(this);
|
2018-12-18 16:29:42 +03:00
|
|
|
this._scrollElement = null;
|
|
|
|
this._autoHideScrollbar = null;
|
2019-06-26 01:08:11 +03:00
|
|
|
this._likelyTrackpadUser = null;
|
|
|
|
this._checkAgainForTrackpad = 0; // ts in milliseconds to recheck this._likelyTrackpadUser
|
2019-03-28 02:51:23 +03:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
leftIndicatorOffset: 0,
|
|
|
|
rightIndicatorOffset: 0,
|
|
|
|
};
|
2018-11-12 14:57:21 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 02:03:06 +03:00
|
|
|
moveToOrigin() {
|
|
|
|
if (!this._scrollElement) return;
|
|
|
|
|
|
|
|
this._scrollElement.scrollLeft = 0;
|
|
|
|
this._scrollElement.scrollTop = 0;
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:57:21 +03:00
|
|
|
_collectScroller(scroller) {
|
2018-12-18 16:29:42 +03:00
|
|
|
if (scroller && !this._scrollElement) {
|
|
|
|
this._scrollElement = scroller;
|
|
|
|
this._scrollElement.addEventListener("scroll", this.checkOverflow);
|
2018-11-12 14:57:21 +03:00
|
|
|
this.checkOverflow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 16:29:42 +03:00
|
|
|
_collectScrollerComponent(autoHideScrollbar) {
|
|
|
|
this._autoHideScrollbar = autoHideScrollbar;
|
|
|
|
}
|
|
|
|
|
2020-03-25 21:26:15 +03:00
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const prevLen = prevProps && prevProps.children && prevProps.children.length || 0;
|
|
|
|
const curLen = this.props.children && this.props.children.length || 0;
|
|
|
|
// check overflow only if amount of children changes.
|
|
|
|
// if we don't guard here, we end up with an infinite
|
|
|
|
// render > componentDidUpdate > checkOverflow > setState > render loop
|
|
|
|
if (prevLen !== curLen) {
|
|
|
|
this.checkOverflow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.checkOverflow();
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:57:21 +03:00
|
|
|
checkOverflow() {
|
2018-12-18 16:29:42 +03:00
|
|
|
const hasTopOverflow = this._scrollElement.scrollTop > 0;
|
|
|
|
const hasBottomOverflow = this._scrollElement.scrollHeight >
|
|
|
|
(this._scrollElement.scrollTop + this._scrollElement.clientHeight);
|
2019-03-28 02:51:23 +03:00
|
|
|
const hasLeftOverflow = this._scrollElement.scrollLeft > 0;
|
|
|
|
const hasRightOverflow = this._scrollElement.scrollWidth >
|
|
|
|
(this._scrollElement.scrollLeft + this._scrollElement.clientWidth);
|
|
|
|
|
2018-11-12 14:57:21 +03:00
|
|
|
if (hasTopOverflow) {
|
2018-12-18 16:29:42 +03:00
|
|
|
this._scrollElement.classList.add("mx_IndicatorScrollbar_topOverflow");
|
2018-11-12 14:57:21 +03:00
|
|
|
} else {
|
2018-12-18 16:29:42 +03:00
|
|
|
this._scrollElement.classList.remove("mx_IndicatorScrollbar_topOverflow");
|
2018-11-12 14:57:21 +03:00
|
|
|
}
|
|
|
|
if (hasBottomOverflow) {
|
2018-12-18 16:29:42 +03:00
|
|
|
this._scrollElement.classList.add("mx_IndicatorScrollbar_bottomOverflow");
|
2018-11-12 14:57:21 +03:00
|
|
|
} else {
|
2018-12-18 16:29:42 +03:00
|
|
|
this._scrollElement.classList.remove("mx_IndicatorScrollbar_bottomOverflow");
|
|
|
|
}
|
2019-03-28 02:51:23 +03:00
|
|
|
if (hasLeftOverflow) {
|
|
|
|
this._scrollElement.classList.add("mx_IndicatorScrollbar_leftOverflow");
|
|
|
|
} else {
|
|
|
|
this._scrollElement.classList.remove("mx_IndicatorScrollbar_leftOverflow");
|
|
|
|
}
|
|
|
|
if (hasRightOverflow) {
|
|
|
|
this._scrollElement.classList.add("mx_IndicatorScrollbar_rightOverflow");
|
|
|
|
} else {
|
|
|
|
this._scrollElement.classList.remove("mx_IndicatorScrollbar_rightOverflow");
|
|
|
|
}
|
2018-12-18 16:29:42 +03:00
|
|
|
|
2019-03-28 02:51:23 +03:00
|
|
|
if (this.props.trackHorizontalOverflow) {
|
|
|
|
this.setState({
|
|
|
|
// Offset from absolute position of the container
|
2019-03-28 19:43:01 +03:00
|
|
|
leftIndicatorOffset: hasLeftOverflow ? `${this._scrollElement.scrollLeft}px` : '0',
|
2019-03-28 02:51:23 +03:00
|
|
|
|
|
|
|
// Negative because we're coming from the right
|
2019-03-28 19:43:01 +03:00
|
|
|
rightIndicatorOffset: hasRightOverflow ? `-${this._scrollElement.scrollLeft}px` : '0',
|
2019-03-28 02:51:23 +03:00
|
|
|
});
|
|
|
|
}
|
2018-11-12 14:57:21 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 16:49:14 +03:00
|
|
|
getScrollTop() {
|
|
|
|
return this._autoHideScrollbar.getScrollTop();
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:57:21 +03:00
|
|
|
componentWillUnmount() {
|
2018-12-18 16:29:42 +03:00
|
|
|
if (this._scrollElement) {
|
|
|
|
this._scrollElement.removeEventListener("scroll", this.checkOverflow);
|
2018-11-12 14:57:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 01:43:04 +03:00
|
|
|
onMouseWheel = (e) => {
|
|
|
|
if (this.props.verticalScrollsHorizontally && this._scrollElement) {
|
2019-04-11 20:07:31 +03:00
|
|
|
// xyThreshold is the amount of horizontal motion required for the component to
|
|
|
|
// ignore the vertical delta in a scroll. Used to stop trackpads from acting in
|
|
|
|
// strange ways. Should be positive.
|
|
|
|
const xyThreshold = 0;
|
|
|
|
|
|
|
|
// yRetention is the factor multiplied by the vertical delta to try and reduce
|
|
|
|
// the harshness of the scroll behaviour. Should be a value between 0 and 1.
|
|
|
|
const yRetention = 1.0;
|
|
|
|
|
2019-06-26 23:13:17 +03:00
|
|
|
// whenever we see horizontal scrolling, assume the user is on a trackpad
|
|
|
|
// for at least the next 1 minute.
|
2019-06-26 20:37:59 +03:00
|
|
|
const now = new Date().getTime();
|
|
|
|
if (Math.abs(e.deltaX) > 0) {
|
|
|
|
this._likelyTrackpadUser = true;
|
2019-06-26 23:13:17 +03:00
|
|
|
this._checkAgainForTrackpad = now + (1 * 60 * 1000);
|
2019-06-26 23:47:55 +03:00
|
|
|
} else {
|
2019-06-26 23:13:17 +03:00
|
|
|
// if we haven't seen any horizontal scrolling for a while, assume
|
2019-06-26 20:37:59 +03:00
|
|
|
// the user might have plugged in a mousewheel
|
|
|
|
if (this._likelyTrackpadUser && now >= this._checkAgainForTrackpad) {
|
|
|
|
this._likelyTrackpadUser = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't mess with the horizontal scroll for trackpad users
|
2020-08-03 18:02:26 +03:00
|
|
|
// See https://github.com/vector-im/element-web/issues/10005
|
2019-06-26 20:37:59 +03:00
|
|
|
if (this._likelyTrackpadUser) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Math.abs(e.deltaX) <= xyThreshold) { // we are vertically scrolling.
|
2019-06-03 08:59:02 +03:00
|
|
|
// HACK: We increase the amount of scroll to counteract smooth scrolling browsers.
|
|
|
|
// Smooth scrolling browsers (Firefox) use the relative area to determine the scroll
|
|
|
|
// amount, which means the likely small area of content results in a small amount of
|
|
|
|
// movement - not what people expect. We pick arbitrary values for when to apply more
|
|
|
|
// scroll, and how much to apply. On Windows 10, Chrome scrolls 100 units whereas
|
|
|
|
// Firefox scrolls just 3 due to smooth scrolling.
|
|
|
|
|
|
|
|
const additionalScroll = e.deltaY < 0 ? -50 : 50;
|
|
|
|
|
2019-04-11 20:07:31 +03:00
|
|
|
// noinspection JSSuspiciousNameCombination
|
2019-06-26 23:13:17 +03:00
|
|
|
const val = Math.abs(e.deltaY) < 25 ? (e.deltaY + additionalScroll) : e.deltaY;
|
2019-06-03 08:59:02 +03:00
|
|
|
this._scrollElement.scrollLeft += val * yRetention;
|
2019-04-11 20:07:31 +03:00
|
|
|
}
|
2019-04-05 01:43:04 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-12 14:57:21 +03:00
|
|
|
render() {
|
2019-03-28 02:51:23 +03:00
|
|
|
const leftIndicatorStyle = {left: this.state.leftIndicatorOffset};
|
|
|
|
const rightIndicatorStyle = {right: this.state.rightIndicatorOffset};
|
|
|
|
const leftOverflowIndicator = this.props.trackHorizontalOverflow
|
|
|
|
? <div className="mx_IndicatorScrollbar_leftOverflowIndicator" style={leftIndicatorStyle} /> : null;
|
|
|
|
const rightOverflowIndicator = this.props.trackHorizontalOverflow
|
|
|
|
? <div className="mx_IndicatorScrollbar_rightOverflowIndicator" style={rightIndicatorStyle} /> : null;
|
|
|
|
|
2019-01-11 04:18:21 +03:00
|
|
|
return (<AutoHideScrollbar
|
|
|
|
ref={this._collectScrollerComponent}
|
|
|
|
wrappedRef={this._collectScroller}
|
2019-04-05 01:43:04 +03:00
|
|
|
onWheel={this.onMouseWheel}
|
2020-07-18 14:19:03 +03:00
|
|
|
{...this.props}
|
2019-01-11 04:18:21 +03:00
|
|
|
>
|
2019-03-28 02:51:23 +03:00
|
|
|
{ leftOverflowIndicator }
|
2018-11-12 14:57:21 +03:00
|
|
|
{ this.props.children }
|
2019-03-28 02:51:23 +03:00
|
|
|
{ rightOverflowIndicator }
|
2018-11-12 14:57:21 +03:00
|
|
|
</AutoHideScrollbar>);
|
|
|
|
}
|
|
|
|
}
|