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
|
|
|
*/
|
|
|
|
|
|
2022-08-10 13:14:54 +01:00
|
|
|
import classNames from "classnames";
|
2023-03-08 13:28:07 +00:00
|
|
|
import React, { HTMLAttributes, ReactHTML, ReactNode, WheelEvent } from "react";
|
2018-11-01 16:32:17 +01:00
|
|
|
|
2022-08-10 13:14:54 +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">>;
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export type IProps<T extends keyof JSX.IntrinsicElements> = Omit<DynamicHtmlElementProps<T>, "onScroll"> & {
|
2023-04-13 08:52:57 +01:00
|
|
|
element: T;
|
2021-06-01 14:13:46 +01:00
|
|
|
className?: string;
|
2021-06-22 20:41:26 +01:00
|
|
|
onScroll?: (event: Event) => void;
|
|
|
|
|
onWheel?: (event: WheelEvent) => void;
|
2021-07-01 23:23:03 +01:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
|
tabIndex?: number;
|
2023-03-10 14:55:06 +00:00
|
|
|
wrappedRef?: (ref: HTMLDivElement | null) => void;
|
2023-03-08 13:28:07 +00:00
|
|
|
children: ReactNode;
|
2022-08-10 13:14:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default class AutoHideScrollbar<T extends keyof JSX.IntrinsicElements> extends React.Component<IProps<T>> {
|
2022-12-16 12:29:59 +00:00
|
|
|
public static defaultProps = {
|
2022-08-10 13:14:54 +01:00
|
|
|
element: "div" as keyof ReactHTML,
|
|
|
|
|
};
|
2021-06-01 14:13:46 +01:00
|
|
|
|
2021-12-15 09:55:53 +00:00
|
|
|
public readonly containerRef: React.RefObject<HTMLDivElement> = React.createRef();
|
2018-12-18 14:29:42 +01:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 13:14:54 +01:00
|
|
|
this.props.wrappedRef?.(this.containerRef.current);
|
2021-05-28 14:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00: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);
|
2018-11-12 12:57:21 +01:00
|
|
|
}
|
2023-05-09 08:50:57 +01:00
|
|
|
|
|
|
|
|
this.props.wrappedRef?.(null);
|
2018-11-01 16:32:17 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2021-06-03 08:31:06 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2022-08-10 13:14:54 +01:00
|
|
|
const { element, className, onScroll, tabIndex, wrappedRef, children, ...otherProps } = this.props;
|
2021-06-03 08:31:06 +01:00
|
|
|
|
2022-08-10 13:14:54 +01:00
|
|
|
return React.createElement(
|
|
|
|
|
element,
|
|
|
|
|
{
|
|
|
|
|
...otherProps,
|
|
|
|
|
ref: this.containerRef,
|
|
|
|
|
className: classNames("mx_AutoHideScrollbar", className),
|
2021-08-06 12:28:06 +01:00
|
|
|
// Firefox sometimes makes this element focusable due to
|
|
|
|
|
// overflow:scroll;, so force it out of tab order by default.
|
2022-08-10 13:14:54 +01:00
|
|
|
tabIndex: tabIndex ?? -1,
|
|
|
|
|
},
|
|
|
|
|
children,
|
|
|
|
|
);
|
2018-11-01 16:32:17 +01:00
|
|
|
}
|
|
|
|
|
}
|