Files
ThreadNet-Web/src/components/views/elements/AccessibleTooltipButton.tsx
T

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

100 lines
3.2 KiB
TypeScript
Raw Normal View History

/*
2019-10-03 09:35:39 +01:00
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2019 The Matrix.org Foundation C.I.C.
2019-10-03 09:35:39 +01:00
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
2019-10-03 09:35:39 +01:00
http://www.apache.org/licenses/LICENSE-2.0
2019-10-03 09:35:39 +01:00
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, { SyntheticEvent, FocusEvent } from "react";
import AccessibleButton from "./AccessibleButton";
2021-06-29 13:11:58 +01:00
import Tooltip, { Alignment } from "./Tooltip";
interface IProps extends React.ComponentProps<typeof AccessibleButton> {
2022-10-07 20:10:17 +02:00
title?: string;
2020-07-16 15:15:00 +01:00
tooltip?: React.ReactNode;
label?: string;
2020-07-01 01:50:31 +01:00
tooltipClassName?: string;
forceHide?: boolean;
2021-05-21 12:41:29 -04:00
alignment?: Alignment;
onHover?: (hovering: boolean) => void;
onHideTooltip?(ev: SyntheticEvent): void;
2020-07-01 01:50:31 +01:00
}
2020-07-01 01:50:31 +01:00
interface IState {
hover: boolean;
}
export default class AccessibleTooltipButton extends React.PureComponent<IProps, IState> {
public constructor(props: IProps) {
2020-07-01 12:28:00 +01:00
super(props);
2020-07-01 01:50:31 +01:00
this.state = {
hover: false,
};
}
public componentDidUpdate(prevProps: Readonly<IProps>): void {
if (!prevProps.forceHide && this.props.forceHide && this.state.hover) {
this.setState({
hover: false,
});
}
}
private showTooltip = (): void => {
if (this.props.onHover) this.props.onHover(true);
if (this.props.forceHide) return;
this.setState({
hover: true,
});
};
private hideTooltip = (ev: SyntheticEvent): void => {
if (this.props.onHover) this.props.onHover(false);
this.setState({
hover: false,
});
this.props.onHideTooltip?.(ev);
};
private onFocus = (ev: FocusEvent): void => {
// We only show the tooltip if focus arrived here from some other
// element, to avoid leaving tooltips hanging around when a modal closes
if (ev.relatedTarget) this.showTooltip();
};
public render(): JSX.Element {
2020-10-07 10:40:20 +01:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2022-05-09 15:58:16 +02:00
const { title, tooltip, children, tooltipClassName, forceHide, alignment, onHideTooltip, ...props } =
this.props;
2022-10-07 20:10:17 +02:00
const tip = this.state.hover && (title || tooltip) && (
2021-11-02 10:41:33 +01:00
<Tooltip tooltipClassName={tooltipClassName} label={tooltip || title} alignment={alignment} />
);
return (
<AccessibleButton
{...props}
2022-10-07 20:10:17 +02:00
onMouseOver={this.showTooltip || props.onMouseOver}
onMouseLeave={this.hideTooltip || props.onMouseLeave}
onFocus={this.onFocus || props.onFocus}
onBlur={this.hideTooltip || props.onBlur}
aria-label={title || props["aria-label"]}
>
2019-12-19 20:09:05 +00:00
{children}
{this.props.label}
{(tooltip || title) && tip}
</AccessibleButton>
);
}
}