Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override Material-UI Popover styles?

How can I override the default value of the max-height property for the Popover component?

I tried to add style={{'maxHeight': '365px'}}, but nothing is changed:

<Popover
  style={{'maxHeight': '365px'}}
  className='notif-popover'
  open={this.state.notifOpen}
  anchorEl={this.state.anchorEl}
  anchorOrigin={{horizontal: 'left', vertical: 'bottom'}}
  targetOrigin={{horizontal: 'left', vertical: 'top'}}
  onRequestClose={this.handleRequestClose}
>
like image 491
Valip Avatar asked Nov 08 '17 09:11

Valip


People also ask

How do you override styles in material UI?

To override the styles of a specific part of the component, use the global classes provided by Material UI, as described in the previous section "Overriding nested component styles" under the sx prop section.

How do you set the popover width in material UI?

You can either change the className to style, and it should work or you're going to have to add a const variable and hook it into the withStyle HOC. Then add the height and width to that const, and then call the class you made in that to the className. All these examples are on the material-ui site.

What is popover MUI?

A Popover can be used to display some content on top of another. Things to know when using the Popover component: The component is built on top of the Modal component. The scroll and click away are blocked unlike with the Popper component.

How do you make a popover component in react?

We can use the following approach in ReactJS to use the react-bootstrap Popover Component. Popover Props: arrowProps: It is used to position the popover arrow. content: It is used to create a Popover with a Popover.


1 Answers

The only props that apply style are: className string of classes and style object with styles. Remember that these are applied to the root element (the Modal component).

Docs SourceCode (if you're using v1-beta). You can see in the sources that the remaining props are passed to the Modal component

const {
  anchorEl,
  anchorReference,
  anchorPosition,
  anchorOrigin,
  children,
  classes,
  elevation,
  getContentAnchorEl,
  marginThreshold,
  onEnter,
  onEntering,
  onEntered,
  onExit,
  onExiting,
  onExited,
  open,
  PaperProps,
  role,
  transformOrigin,
  transitionClasses,
  transitionDuration,
  ...other
} = this.props;

<Modal show={open} BackdropInvisible {...other}>

You can see in the sources that MaterialUI uses the withStyles HoC from react-jss and has a styles object for the Paper component

export const styles = {
  paper: {
    position: 'absolute',
    overflowY: 'auto',
    overflowX: 'hidden',
    // So we see the popover when it's empty.
    // It's most likely on issue on userland.
    minWidth: 16,
    minHeight: 16,
    maxWidth: 'calc(100vw - 32px)',
    maxHeight: 'calc(100vh - 32px)'

maxHeight: 'calc(100vh - 32px)'

This is bound to a class paper and then passed to the classes prop and applied to the Paper component.

Solution:

Use the className prop on the root element with nested selector that targets the Paper component (inspect and see on which element it applies the class).

Example of possible selector (should definitely use a better one, inspect element)

.rootElement > * { max-height: '375px' } 

and then you'd do <Popover className='rootElement' />

like image 166
Daniel Andrei Avatar answered Nov 23 '22 08:11

Daniel Andrei