2019-06-30 12:58:59 +03:00
|
|
|
/*
|
2019-10-03 11:35:39 +03:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-06-30 12:58:59 +03:00
|
|
|
|
2019-10-03 11:35:39 +03: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 12:58:59 +03:00
|
|
|
|
2019-10-03 11:35:39 +03:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2019-06-30 12:58:59 +03:00
|
|
|
|
2019-10-03 11:35:39 +03: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 12:58:59 +03:00
|
|
|
|
|
|
|
import React from 'react';
|
2020-07-15 06:19:51 +03:00
|
|
|
import classNames from 'classnames';
|
2019-06-30 12:58:59 +03:00
|
|
|
|
|
|
|
import AccessibleButton from "./AccessibleButton";
|
2020-07-01 03:50:31 +03:00
|
|
|
import Tooltip from './Tooltip';
|
2019-06-30 12:58:59 +03:00
|
|
|
|
2020-07-17 18:22:56 +03:00
|
|
|
interface ITooltipProps extends React.ComponentProps<typeof AccessibleButton> {
|
2020-07-01 03:50:31 +03:00
|
|
|
title: string;
|
2020-07-16 17:15:00 +03:00
|
|
|
tooltip?: React.ReactNode;
|
2020-07-01 03:50:31 +03:00
|
|
|
tooltipClassName?: string;
|
2020-08-14 14:01:16 +03:00
|
|
|
forceHide?: boolean;
|
2020-10-15 13:14:48 +03:00
|
|
|
yOffset?: number;
|
2020-07-01 03:50:31 +03:00
|
|
|
}
|
2019-06-30 12:58:59 +03:00
|
|
|
|
2020-07-01 03:50:31 +03:00
|
|
|
interface IState {
|
|
|
|
hover: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class AccessibleTooltipButton extends React.PureComponent<ITooltipProps, IState> {
|
|
|
|
constructor(props: ITooltipProps) {
|
2020-07-01 14:28:00 +03:00
|
|
|
super(props);
|
2020-07-01 03:50:31 +03:00
|
|
|
this.state = {
|
|
|
|
hover: false,
|
|
|
|
};
|
|
|
|
}
|
2019-06-30 12:58:59 +03:00
|
|
|
|
2020-08-14 14:01:16 +03:00
|
|
|
componentDidUpdate(prevProps: Readonly<ITooltipProps>) {
|
|
|
|
if (!prevProps.forceHide && this.props.forceHide && this.state.hover) {
|
|
|
|
this.setState({
|
|
|
|
hover: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 12:58:59 +03:00
|
|
|
onMouseOver = () => {
|
2020-08-14 14:01:16 +03:00
|
|
|
if (this.props.forceHide) return;
|
2019-06-30 12:58:59 +03:00
|
|
|
this.setState({
|
|
|
|
hover: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-07-18 14:01:51 +03:00
|
|
|
onMouseLeave = () => {
|
2019-06-30 12:58:59 +03:00
|
|
|
this.setState({
|
|
|
|
hover: false,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2020-10-07 12:40:20 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2020-10-15 13:14:48 +03:00
|
|
|
const {title, tooltip, children, tooltipClassName, forceHide, yOffset, ...props} = this.props;
|
2019-06-30 12:58:59 +03:00
|
|
|
|
|
|
|
const tip = this.state.hover ? <Tooltip
|
|
|
|
className="mx_AccessibleTooltipButton_container"
|
2020-07-15 06:19:51 +03:00
|
|
|
tooltipClassName={classNames("mx_AccessibleTooltipButton_tooltip", tooltipClassName)}
|
2020-07-16 17:15:00 +03:00
|
|
|
label={tooltip || title}
|
2020-10-15 13:14:48 +03:00
|
|
|
yOffset={yOffset}
|
2019-06-30 12:58:59 +03:00
|
|
|
/> : <div />;
|
|
|
|
return (
|
2020-07-18 14:01:51 +03:00
|
|
|
<AccessibleButton
|
|
|
|
{...props}
|
|
|
|
onMouseOver={this.onMouseOver}
|
|
|
|
onMouseLeave={this.onMouseLeave}
|
|
|
|
aria-label={title}
|
|
|
|
>
|
2019-12-19 23:09:05 +03:00
|
|
|
{ children }
|
2019-06-30 12:58:59 +03:00
|
|
|
{ tip }
|
|
|
|
</AccessibleButton>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|