Files
ThreadNet-Web/src/components/structures/IndicatorScrollbar.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

200 lines
8.4 KiB
TypeScript
Raw Normal View History

/*
2024-09-09 14:57:16 +01:00
Copyright 2018-2024 New Vector Ltd.
2024-09-09 14:57:16 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import React, { createRef } from "react";
2021-12-09 09:10:23 +00:00
import AutoHideScrollbar, { IProps as AutoHideScrollbarProps } from "./AutoHideScrollbar";
import UIStore, { UI_EVENTS } from "../../stores/UIStore";
export type IProps<T extends keyof JSX.IntrinsicElements> = Omit<AutoHideScrollbarProps<T>, "onWheel" | "element"> & {
element?: T;
2021-09-12 09:24:46 +02: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?: boolean;
// 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?: boolean;
2021-09-14 17:19:57 +02:00
children: React.ReactNode;
};
2021-09-12 09:24:46 +02:00
interface IState {
leftIndicatorOffset: string;
rightIndicatorOffset: string;
2021-09-12 09:24:46 +02:00
}
export default class IndicatorScrollbar<T extends keyof JSX.IntrinsicElements> extends React.Component<
IProps<T>,
IState
> {
private autoHideScrollbar = createRef<AutoHideScrollbar<any>>();
private scrollElement?: HTMLDivElement;
private likelyTrackpadUser: boolean | null = null;
2021-09-12 09:24:46 +02:00
private checkAgainForTrackpad = 0; // ts in milliseconds to recheck this._likelyTrackpadUser
public constructor(props: IProps<T>) {
super(props);
2019-03-27 17:51:23 -06:00
this.state = {
leftIndicatorOffset: "0",
rightIndicatorOffset: "0",
2019-03-27 17:51:23 -06:00
};
}
2021-09-12 09:24:46 +02:00
private collectScroller = (scroller: HTMLDivElement): void => {
this.props.wrappedRef?.(scroller);
2021-09-12 09:24:46 +02:00
if (scroller && !this.scrollElement) {
this.scrollElement = scroller;
2021-05-28 14:59:14 +01:00
// Using the passive option to not block the main thread
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#improving_scrolling_performance_with_passive_listeners
2021-09-12 09:24:46 +02:00
this.scrollElement.addEventListener("scroll", this.checkOverflow, { passive: true });
this.checkOverflow();
}
2021-09-12 09:24:46 +02:00
};
public componentDidUpdate(prevProps: IProps<T>): void {
2021-09-14 17:19:57 +02:00
const prevLen = React.Children.count(prevProps.children);
const curLen = React.Children.count(this.props.children);
// 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();
}
}
2021-09-12 09:24:46 +02:00
public componentDidMount(): void {
this.checkOverflow();
UIStore.instance.on(UI_EVENTS.Resize, this.checkOverflow);
}
2021-09-12 09:24:46 +02:00
private checkOverflow = (): void => {
if (!this.scrollElement) return;
2021-09-12 09:24:46 +02:00
const hasTopOverflow = this.scrollElement.scrollTop > 0;
const hasBottomOverflow =
this.scrollElement.scrollHeight > this.scrollElement.scrollTop + this.scrollElement.clientHeight;
const hasLeftOverflow = this.scrollElement.scrollLeft > 0;
const hasRightOverflow =
this.scrollElement.scrollWidth > this.scrollElement.scrollLeft + this.scrollElement.clientWidth;
2019-03-27 17:51:23 -06:00
if (hasTopOverflow) {
2021-09-12 09:24:46 +02:00
this.scrollElement.classList.add("mx_IndicatorScrollbar_topOverflow");
} else {
2021-09-12 09:24:46 +02:00
this.scrollElement.classList.remove("mx_IndicatorScrollbar_topOverflow");
}
if (hasBottomOverflow) {
2021-09-12 09:24:46 +02:00
this.scrollElement.classList.add("mx_IndicatorScrollbar_bottomOverflow");
} else {
2021-09-12 09:24:46 +02:00
this.scrollElement.classList.remove("mx_IndicatorScrollbar_bottomOverflow");
}
2019-03-27 17:51:23 -06:00
if (hasLeftOverflow) {
2021-09-12 09:24:46 +02:00
this.scrollElement.classList.add("mx_IndicatorScrollbar_leftOverflow");
2019-03-27 17:51:23 -06:00
} else {
2021-09-12 09:24:46 +02:00
this.scrollElement.classList.remove("mx_IndicatorScrollbar_leftOverflow");
2019-03-27 17:51:23 -06:00
}
if (hasRightOverflow) {
2021-09-12 09:24:46 +02:00
this.scrollElement.classList.add("mx_IndicatorScrollbar_rightOverflow");
2019-03-27 17:51:23 -06:00
} else {
2021-09-12 09:24:46 +02:00
this.scrollElement.classList.remove("mx_IndicatorScrollbar_rightOverflow");
2019-03-27 17:51:23 -06:00
}
2019-03-27 17:51:23 -06:00
if (this.props.trackHorizontalOverflow) {
this.setState({
// Offset from absolute position of the container
2021-09-12 09:24:46 +02:00
leftIndicatorOffset: hasLeftOverflow ? `${this.scrollElement.scrollLeft}px` : "0",
2019-03-27 17:51:23 -06:00
// Negative because we're coming from the right
2021-09-12 09:24:46 +02:00
rightIndicatorOffset: hasRightOverflow ? `-${this.scrollElement.scrollLeft}px` : "0",
2019-03-27 17:51:23 -06:00
});
}
2021-09-12 09:24:46 +02:00
};
2021-09-12 09:24:46 +02:00
public componentWillUnmount(): void {
this.scrollElement?.removeEventListener("scroll", this.checkOverflow);
UIStore.instance.off(UI_EVENTS.Resize, this.checkOverflow);
}
2021-09-12 09:24:46 +02:00
private onMouseWheel = (e: React.WheelEvent): void => {
if (this.props.verticalScrollsHorizontally && this.scrollElement) {
// 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;
2019-04-08 10:53:09 -06:00
// 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-04-08 10:53:09 -06:00
2019-06-26 21:13:17 +01:00
// whenever we see horizontal scrolling, assume the user is on a trackpad
// for at least the next 1 minute.
const now = new Date().getTime();
if (Math.abs(e.deltaX) > 0) {
2021-09-12 09:24:46 +02:00
this.likelyTrackpadUser = true;
this.checkAgainForTrackpad = now + 1 * 60 * 1000;
2019-06-26 21:47:55 +01:00
} else {
2019-06-26 21:13:17 +01:00
// if we haven't seen any horizontal scrolling for a while, assume
// the user might have plugged in a mousewheel
2021-09-12 09:24:46 +02:00
if (this.likelyTrackpadUser && now >= this.checkAgainForTrackpad) {
this.likelyTrackpadUser = false;
}
}
// don't mess with the horizontal scroll for trackpad users
// See https://github.com/vector-im/element-web/issues/10005
2021-09-12 09:24:46 +02:00
if (this.likelyTrackpadUser) {
return;
}
if (Math.abs(e.deltaX) <= xyThreshold) {
// we are vertically scrolling.
2019-06-02 23:59:02 -06: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;
// noinspection JSSuspiciousNameCombination
2019-06-26 21:13:17 +01:00
const val = Math.abs(e.deltaY) < 25 ? e.deltaY + additionalScroll : e.deltaY;
2021-09-12 09:24:46 +02:00
this.scrollElement.scrollLeft += val * yRetention;
}
}
};
public render(): React.ReactNode {
2021-09-12 09:24:46 +02:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { children, trackHorizontalOverflow, verticalScrollsHorizontally, ...otherProps } = this.props;
2021-06-29 13:11:58 +01:00
const leftIndicatorStyle = { left: this.state.leftIndicatorOffset };
const rightIndicatorStyle = { right: this.state.rightIndicatorOffset };
const leftOverflowIndicator = trackHorizontalOverflow ? (
2019-03-27 17:51:23 -06:00
<div className="mx_IndicatorScrollbar_leftOverflowIndicator" style={leftIndicatorStyle} />
) : null;
const rightOverflowIndicator = trackHorizontalOverflow ? (
2019-03-27 17:51:23 -06:00
<div className="mx_IndicatorScrollbar_rightOverflowIndicator" style={rightIndicatorStyle} />
) : null;
2019-01-10 19:18:21 -06:00
return (
<AutoHideScrollbar
{...otherProps}
2021-09-12 09:24:46 +02:00
ref={this.autoHideScrollbar}
wrappedRef={this.collectScroller}
onWheel={this.onMouseWheel}
2019-01-10 19:18:21 -06:00
>
2019-03-27 17:51:23 -06:00
{leftOverflowIndicator}
{children}
2019-03-27 17:51:23 -06:00
{rightOverflowIndicator}
</AutoHideScrollbar>
);
}
}