2019-06-30 10:58:59 +01:00
|
|
|
/*
|
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-06-30 10:58:59 +01:00
|
|
|
|
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-06-30 10:58:59 +01:00
|
|
|
|
2019-10-03 09:35:39 +01:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2019-06-30 10:58:59 +01:00
|
|
|
|
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.
|
|
|
|
|
*/
|
2019-06-30 10:58:59 +01:00
|
|
|
|
2022-05-09 18:02:03 -04:00
|
|
|
import React, { SyntheticEvent, FocusEvent } from 'react';
|
2019-06-30 10:58:59 +01:00
|
|
|
|
|
|
|
|
import AccessibleButton from "./AccessibleButton";
|
2021-06-29 13:11:58 +01:00
|
|
|
import Tooltip, { Alignment } from './Tooltip';
|
2019-06-30 10:58:59 +01:00
|
|
|
|
2022-02-14 23:54:46 +00:00
|
|
|
interface IProps extends React.ComponentProps<typeof AccessibleButton> {
|
2020-07-01 01:50:31 +01:00
|
|
|
title: string;
|
2020-07-16 15:15:00 +01:00
|
|
|
tooltip?: React.ReactNode;
|
2022-04-15 16:22:59 +02:00
|
|
|
label?: string;
|
2020-07-01 01:50:31 +01:00
|
|
|
tooltipClassName?: string;
|
2020-08-14 12:01:16 +01:00
|
|
|
forceHide?: boolean;
|
2020-10-15 11:14:48 +01:00
|
|
|
yOffset?: number;
|
2021-05-21 12:41:29 -04:00
|
|
|
alignment?: Alignment;
|
2022-05-04 16:41:56 +02:00
|
|
|
onHover?: (hovering: boolean) => void;
|
2022-02-14 23:54:46 +00:00
|
|
|
onHideTooltip?(ev: SyntheticEvent): void;
|
2020-07-01 01:50:31 +01:00
|
|
|
}
|
2019-06-30 10:58:59 +01:00
|
|
|
|
2020-07-01 01:50:31 +01:00
|
|
|
interface IState {
|
|
|
|
|
hover: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 23:54:46 +00:00
|
|
|
export default class AccessibleTooltipButton extends React.PureComponent<IProps, IState> {
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-06-30 10:58:59 +01:00
|
|
|
|
2022-02-14 23:54:46 +00:00
|
|
|
componentDidUpdate(prevProps: Readonly<IProps>) {
|
2020-08-14 12:01:16 +01:00
|
|
|
if (!prevProps.forceHide && this.props.forceHide && this.state.hover) {
|
|
|
|
|
this.setState({
|
|
|
|
|
hover: false,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 23:54:46 +00:00
|
|
|
private showTooltip = () => {
|
2022-05-04 16:41:56 +02:00
|
|
|
if (this.props.onHover) this.props.onHover(true);
|
2020-08-14 12:01:16 +01:00
|
|
|
if (this.props.forceHide) return;
|
2019-06-30 10:58:59 +01:00
|
|
|
this.setState({
|
|
|
|
|
hover: true,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-14 23:54:46 +00:00
|
|
|
private hideTooltip = (ev: SyntheticEvent) => {
|
2022-05-04 16:41:56 +02:00
|
|
|
if (this.props.onHover) this.props.onHover(false);
|
2019-06-30 10:58:59 +01:00
|
|
|
this.setState({
|
|
|
|
|
hover: false,
|
|
|
|
|
});
|
2022-02-14 23:54:46 +00:00
|
|
|
this.props.onHideTooltip?.(ev);
|
2019-06-30 10:58:59 +01:00
|
|
|
};
|
|
|
|
|
|
2022-05-09 18:02:03 -04:00
|
|
|
private onFocus = (ev: FocusEvent) => {
|
|
|
|
|
// 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();
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-30 10:58:59 +01:00
|
|
|
render() {
|
2020-10-07 10:40:20 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2022-02-14 23:54:46 +00:00
|
|
|
const { title, tooltip, children, tooltipClassName, forceHide, yOffset, alignment, onHideTooltip,
|
|
|
|
|
...props } = this.props;
|
2019-06-30 10:58:59 +01:00
|
|
|
|
2021-11-02 10:41:33 +01:00
|
|
|
const tip = this.state.hover && <Tooltip
|
|
|
|
|
tooltipClassName={tooltipClassName}
|
2020-07-16 15:15:00 +01:00
|
|
|
label={tooltip || title}
|
2020-10-15 11:14:48 +01:00
|
|
|
yOffset={yOffset}
|
2021-05-21 12:41:29 -04:00
|
|
|
alignment={alignment}
|
2021-11-02 10:41:33 +01:00
|
|
|
/>;
|
2019-06-30 10:58:59 +01:00
|
|
|
return (
|
2020-07-18 12:01:51 +01:00
|
|
|
<AccessibleButton
|
|
|
|
|
{...props}
|
2021-12-09 11:47:50 +01:00
|
|
|
onMouseOver={this.showTooltip}
|
|
|
|
|
onMouseLeave={this.hideTooltip}
|
2022-05-09 18:02:03 -04:00
|
|
|
onFocus={this.onFocus}
|
2021-12-09 11:47:50 +01:00
|
|
|
onBlur={this.hideTooltip}
|
2020-07-18 12:01:51 +01:00
|
|
|
aria-label={title}
|
|
|
|
|
>
|
2019-12-19 20:09:05 +00:00
|
|
|
{ children }
|
2021-09-07 17:10:09 +01:00
|
|
|
{ this.props.label }
|
|
|
|
|
{ (tooltip || title) && tip }
|
2019-06-30 10:58:59 +01:00
|
|
|
</AccessibleButton>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|