gpt4 book ai didi

javascript - 如何在悬停在 anchorEl 和 'popover' 上时继续显示 'popover'?

转载 作者:行者123 更新时间:2023-11-29 15:59:43 26 4
gpt4 key购买 nike

这里是material-ui的例子https://material-ui.com/utils/popover/#mouse-over-interaction

按照示例 material-ui 的这些步骤 https://material-ui.com/utils/popover/#mouse-over-interaction

  1. 在上面的示例中,将鼠标放在文本上 Hover with a Popover.--- 你看到了 popover

  2. 尝试将鼠标移动到 popover 附近 --- popover 消失了吗?但即使我将鼠标悬停在 popover

  3. 上,我也想显示 popover

只有当用户没有将鼠标悬停在popoverHover with a Popover 上时,才让弹出窗口消失。(基本上是anchorEl)

我正在从他们的演示中复制代码

  import React from 'react';
import PropTypes from 'prop-types';
import Popover from '@material-ui/core/Popover';
import Typography from '@material-ui/core/Typography';
import { withStyles } from '@material-ui/core/styles';

const styles = theme => ({
popover: {
pointerEvents: 'none',
},
paper: {
padding: theme.spacing.unit,
},
});

class MouseOverPopover extends React.Component {
state = {
anchorEl: null,
};

handlePopoverOpen = event => {
this.setState({ anchorEl: event.currentTarget });
};

handlePopoverClose = () => {
this.setState({ anchorEl: null });
};

render() {
const { classes } = this.props;
const { anchorEl } = this.state;
const open = Boolean(anchorEl);

return (
<div>
<Typography
aria-owns={open ? 'mouse-over-popover' : undefined}
aria-haspopup="true"
onMouseEnter={this.handlePopoverOpen}
onMouseLeave={this.handlePopoverClose}
>
Hover with a Popover.
</Typography>
<Popover
id="mouse-over-popover"
className={classes.popover}
classes={{
paper: classes.paper,
}}
open={open}
anchorEl={anchorEl}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'left',
}}
onClose={this.handlePopoverClose}
disableRestoreFocus
>
<Typography>I use Popover.</Typography>
</Popover>
</div>
);
}
}

MouseOverPopover.propTypes = {
classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(MouseOverPopover);

我需要在此处进行哪些代码更改?你可以试验 https://codesandbox.io/s/6l3wk6kv3

最佳答案

我遇到了同样的问题,没有找到任何答案,我花了一段时间才明白如何解决它。

实际上问题来自 pointerEvents:弹出窗口上不需要任何东西来防止同时触发您的 onMouseEnter/onMouseLeave。

但是你可以为你的popover pointerEvents的内容设置:auto。

然后您可以在弹出窗口的内容上添加一个 onMouseEnter 和一个 onMouseLeave。

这是一个更明确的例子:

import React, { useState, useRef } from 'react';
import { makeStyles } from '@material-ui/core/styles';
import { Popover } from '@material-ui/core';

const useStyles = makeStyles(theme => ({
popover: {
pointerEvents: 'none',
},
popoverContent: {
pointerEvents: 'auto',
},
}));

const MyComponent = ({ loading, login, wrong, clearWrongLogin }: Props) => {
const [openedPopover, setOpenedPopover] = useState(false)
const popoverAnchor = useRef(null);

const popoverEnter = ({ currentTarget }) => {
setOpenedPopover(true)
};

const popoverLeave = ({ currentTarget }) => {
setOpenedPopover(false)
};

const classes = useStyles();

return (
<div>
<span
ref={popoverAnchor}
aria-owns="mouse-over-popover"
aria-haspopup="true"
onMouseEnter={popoverEnter}
onMouseLeave={popoverLeave}
>Hover this el !
</span>
<Popover
id="mouse-over-popover"
className={classes.popover}
classes={{
paper: classes.popoverContent,
}}
open={openedPopover}
anchorEl={popoverAnchor.current}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'right',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'left',
}}
PaperProps={{onMouseEnter: popoverEnter, onMouseLeave: popoverLeave}}
>
<div>
My popover content...
</div>
</Popover>
</div>
);
};

export default MyComponent

关于javascript - 如何在悬停在 anchorEl 和 'popover' 上时继续显示 'popover'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com