自定义材料-ui popover

时间:2018-04-27 07:18:49

标签: javascript reactjs material-ui css-shapes react-component

我想给图片中显示的以下形状的材料-ui popover。

enter image description here

我使用react创建了popover工作Demo并共享链接以进行编辑。有帮助吗? => Working Demo

我也在这里分享代码,但如果stackblitz工作演示用于编辑目的会很好:

import React, { Component } from 'react';
import Popover, {PopoverAnimationVertical} from 'material-ui/Popover';
import Menu from 'material-ui/Menu';
import MenuItem from 'material-ui/MenuItem';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';

const PopoverStyle = {
    top: '50px'
};

class App extends Component {

  constructor(props) {
        super(props);
        this.state = { pop_open: false };
    }

  handleProfileDropDown(e) {
        e.preventDefault();
        this.setState({
            pop_open: !this.state.pop_open,
            anchorEl: e.currentTarget,
        });
    }

    handleRequestClose() {
        this.setState({
            pop_open: false,
        });
    };

  render() {
    return (
      <div>
      <MuiThemeProvider>
        <button type="submit" name={this.state.name} onClick={this.handleProfileDropDown.bind(this)} >My Customized PopOver</button>
        <Popover
                      open={this.state.pop_open}
                      anchorEl={this.state.anchorEl}
                      className="popover_class"
                      style={PopoverStyle}
                      anchorOrigin={{horizontal: 'left', vertical: 'bottom'}}
                      targetOrigin={{horizontal: 'left', vertical: 'top'}}
                      onRequestClose={this.handleRequestClose.bind(this)}
                      animation={PopoverAnimationVertical}
                    >
                      <Menu>
                        <MenuItem primaryText="Content" />
                        <MenuItem primaryText="My Profile" />
                        <MenuItem primaryText="Settings" />
                        <MenuItem primaryText="Logout" />
                      </Menu>
                    </Popover>
            </MuiThemeProvider>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

2 个答案:

答案 0 :(得分:4)

尝试将此添加到您的css文件

.popover_class{
  margin-top: 10px;
  border: 1px solid black;
}
.popover_class::before{
  content: '';
  position: absolute;
  top: -20px;
  right: 5px;
  border-bottom: 10px solid black;
  border-right: 10px solid transparent;
  border-left: 10px solid transparent;
  border-top: 10px solid transparent;
  z-index: 10;
}

答案 1 :(得分:2)

在样式css中添加tyhis样式。你只需要调整一些边距和颜色。

.popover_class:after {
content:"";
position: absolute;
right: 4px;
top: -5px;
width: 0;`
height: 0;
border-style: solid;
border-width: 0 8px 8px 8px;
border-color: transparent transparent green transparent;
z-index:9998;
}