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

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

71 lines
2.7 KiB
TypeScript
Raw Normal View History

2018-11-01 16:32:17 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2020-03-26 11:05:27 +01:00
Copyright 2020 The Matrix.org Foundation C.I.C.
2024-09-09 14:57:16 +01:00
Copyright 2018 New Vector Ltd
2018-11-01 16:32:17 +01:00
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.
2018-11-01 16:32:17 +01:00
*/
import classNames from "classnames";
import React, { HTMLAttributes, ReactHTML, ReactNode, WheelEvent } from "react";
2018-11-01 16:32:17 +01:00
type DynamicHtmlElementProps<T extends keyof JSX.IntrinsicElements> =
JSX.IntrinsicElements[T] extends HTMLAttributes<{}> ? DynamicElementProps<T> : DynamicElementProps<"div">;
type DynamicElementProps<T extends keyof JSX.IntrinsicElements> = Partial<Omit<JSX.IntrinsicElements[T], "ref">>;
export type IProps<T extends keyof JSX.IntrinsicElements> = Omit<DynamicHtmlElementProps<T>, "onScroll"> & {
element: T;
2021-06-01 14:13:46 +01:00
className?: string;
onScroll?: (event: Event) => void;
onWheel?: (event: WheelEvent) => void;
2021-07-01 23:23:03 +01:00
style?: React.CSSProperties;
tabIndex?: number;
wrappedRef?: (ref: HTMLDivElement | null) => void;
children: ReactNode;
};
export default class AutoHideScrollbar<T extends keyof JSX.IntrinsicElements> extends React.Component<IProps<T>> {
public static defaultProps = {
element: "div" as keyof ReactHTML,
};
2021-06-01 14:13:46 +01:00
public readonly containerRef: React.RefObject<HTMLDivElement> = React.createRef();
public componentDidMount(): void {
2021-06-01 14:13:46 +01:00
if (this.containerRef.current && this.props.onScroll) {
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-06-01 14:13:46 +01:00
this.containerRef.current.addEventListener("scroll", this.props.onScroll, { passive: true });
}
this.props.wrappedRef?.(this.containerRef.current);
2021-05-28 14:59:14 +01:00
}
public componentWillUnmount(): void {
2021-06-01 14:13:46 +01:00
if (this.containerRef.current && this.props.onScroll) {
this.containerRef.current.removeEventListener("scroll", this.props.onScroll);
}
2023-05-09 08:50:57 +01:00
this.props.wrappedRef?.(null);
2018-11-01 16:32:17 +01:00
}
public render(): React.ReactNode {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { element, className, onScroll, tabIndex, wrappedRef, children, ...otherProps } = this.props;
return React.createElement(
element,
{
...otherProps,
ref: this.containerRef,
className: classNames("mx_AutoHideScrollbar", className),
// Firefox sometimes makes this element focusable due to
// overflow:scroll;, so force it out of tab order by default.
tabIndex: tabIndex ?? -1,
},
children,
);
2018-11-01 16:32:17 +01:00
}
}