这似乎在 Chrome 和 Edge 中完美运行。我有一个工具提示,需要在按钮的onClick 上弹出,然后在onBlur 上消失。出于某种原因,onBlur 事件在 Safari 或 Firefox 上根本不会触发。
我已经尝试在 handleBlur 中登录到控制台,但它并没有那么远,就好像 onBlur 在此上下文中不存在一样。
class Tooltip extends Component {
componentWillUnmount() {
this.closeActiveTooltip();
}
handleKeyPress = (event) => {
if (event.keyCode === 27) { // Esc key
this.closeActiveTooltip();
}
}
handleBlur = () => {
if (this.props.activeTooltip === this.props.name) {
// setTimeout is for link click
setTimeout(this.closeActiveTooltip, 100);
}
}
closeActiveTooltip = () => {
const { activeTooltip, closeTooltip } = this.props;
if (activeTooltip) {
closeTooltip(activeTooltip);
}
}
render() {
const {
isOpen,
text,
link,
position,
} = this.props;
const popupClassName = getClassNameFor(s, 'popup', `${isOpen && 'open'} ${position}`);
const linkElement = (
<div className={s.link}>
<a
href={link}
target="_blank"
rel="noopener noreferrer"
>
More info here
</a>
</div>
);
return (
<button
type="button"
className={s.root}
onClick={this.props.toggleTooltip}
onBlur={this.handleBlur}
onKeyDown={this.handleKeyPress}
>
<svg className={s.icon}>
<use xlinkHref="#more-info" />
</svg>
<div className={popupClassName}>
{text}
{link && linkElement}
</div>
</button>
);
}
}