5.1.0 • Published 10 days ago

material-ui-popup-state v5.1.0

Weekly downloads
63,883
License
MIT
Repository
github
Last release
10 days ago

material-ui-popup-state

CircleCI Coverage Status semantic-release Commitizen friendly npm version

Takes care of the boilerplate for common Menu, Popover and Popper use cases.

Provides a Custom React Hook that keeps track of the local state for a single popup, and functions to connect trigger, toggle, and popover/menu/popper components to the state.

Also provides a Render Props Component that keeps track of the local state for a single popup, and passes the state and mutation functions to a child render function.

Requirements

Requires MUI >= 5.0.0 and React >= 16.8.0. For MUI v4 you'll need material-ui-popup-state@^1.9.3.

Table of Contents

Installation

npm install --save material-ui-popup-state

Examples with React Hooks

Menu

import * as React from 'react'
import Button from '@mui/material/Button'
import Menu from '@mui/material/Menu'
import MenuItem from '@mui/material/MenuItem'
import {
  usePopupState,
  bindTrigger,
  bindMenu,
} from 'material-ui-popup-state/hooks'

const MenuPopupState = () => {
  const popupState = usePopupState({ variant: 'popover', popupId: 'demoMenu' })
  return (
    <div>
      <Button variant="contained" {...bindTrigger(popupState)}>
        Open Menu
      </Button>
      <Menu {...bindMenu(popupState)}>
        <MenuItem onClick={popupState.close}>Cake</MenuItem>
        <MenuItem onClick={popupState.close}>Death</MenuItem>
      </Menu>
    </div>
  )
}

export default MenuPopupState

Popover

import React from 'react'
import PropTypes from 'prop-types'
import { withStyles } from '@mui/material/styles'
import Typography from '@mui/material/Typography'
import Button from '@mui/material/Button'
import Popover from '@mui/material/Popover'
import {
  usePopupState,
  bindTrigger,
  bindPopover,
} from 'material-ui-popup-state/hooks'

const styles = (theme) => ({
  typography: {
    margin: theme.spacing.unit * 2,
  },
})

const PopoverPopupState = ({ classes }) => {
  const popupState = usePopupState({
    variant: 'popover',
    popupId: 'demoPopover',
  })
  return (
    <div>
      <Button variant="contained" {...bindTrigger(popupState)}>
        Open Popover
      </Button>
      <Popover
        {...bindPopover(popupState)}
        anchorOrigin={{
          vertical: 'bottom',
          horizontal: 'center',
        }}
        transformOrigin={{
          vertical: 'top',
          horizontal: 'center',
        }}
      >
        <Typography className={classes.typography}>
          The content of the Popover.
        </Typography>
      </Popover>
    </div>
  )
}

PopoverPopupState.propTypes = {
  classes: PropTypes.object.isRequired,
}

export default withStyles(styles)(PopoverPopupState)

Popper

import React from 'react'
import PropTypes from 'prop-types'
import { withStyles } from '@mui/material/styles'
import Typography from '@mui/material/Typography'
import Button from '@mui/material/Button'
import Popper from '@mui/material/Popper'
import {
  usePopupState,
  bindToggle,
  bindPopper,
} from 'material-ui-popup-state/hooks'
import Fade from '@mui/material/Fade'
import Paper from '@mui/material/Paper'

const styles = (theme) => ({
  typography: {
    padding: theme.spacing.unit * 2,
  },
})

const PopperPopupState = ({ classes }) => {
  const popupState = usePopupState({ variant: 'popper', popupId: 'demoPopper' })
  return (
    <div>
      <Button variant="contained" {...bindToggle(popupState)}>
        Toggle Popper
      </Button>
      <Popper {...bindPopper(popupState)} transition>
        {({ TransitionProps }) => (
          <Fade {...TransitionProps} timeout={350}>
            <Paper>
              <Typography className={classes.typography}>
                The content of the Popper.
              </Typography>
            </Paper>
          </Fade>
        )}
      </Popper>
    </div>
  )
}

PopperPopupState.propTypes = {
  classes: PropTypes.object.isRequired,
}

export default withStyles(styles)(PopperPopupState)

React Hooks API

Bind Functions

material-ui-popup-state/hooks exports several helper functions you can use to connect components easily:

  • anchorRef: creates a ref function to pass to the anchorEl (by default, the currentTarget of the mouse event that triggered the popup is used; only use anchorRef if you want a different element to be the anchor).
  • bindMenu: creates props to control a Menu component.
  • bindPopover: creates props to control a Popover component.
  • bindPopper: creates props to control a Popper component.
  • bindDialog: creates props to control a Dialog component.
  • bindTrigger: creates props for a component that opens the popup when clicked.
  • bindContextMenu: creates props for a component that opens the popup on when right clicked (contextmenu event). NOTE: bindPopover/bindMenu will position the Popover/Menu to the contextmenu event location. To position using the contextmenu target element instead, pass anchorReference="anchorEl" after {...bindPopover(popupState)}/{...bindMenu(popupState)}.
  • bindToggle: creates props for a component that toggles the popup when clicked.
  • bindHover: creates props for a component that opens the popup while hovered. NOTE: See this guidance if you are using bindHover with Popover or Menu.
  • bindFocus: creates props for a component that opens the popup while focus.
  • bindDoubleClick: creates props for a component that opens the popup while double click.

To use one of these functions, you should call it with the object returned by usePopupState and spread the return value into the desired element:

import * as React from 'react'
import Button from '@mui/material/Button'
import Menu from '@mui/material/Menu'
import MenuItem from '@mui/material/MenuItem'
import {
  usePopupState,
  bindTrigger,
  bindMenu,
} from 'material-ui-popup-state/hooks'

const MenuPopupState = () => {
  const popupState = usePopupState({ variant: 'popover', popupId: 'demoMenu' })
  return (
    <div>
      <Button variant="contained" {...bindTrigger(popupState)}>
        Open Menu
      </Button>
      <Menu {...bindMenu(popupState)}>
        <MenuItem onClick={popupState.close}>Cake</MenuItem>
        <MenuItem onClick={popupState.close}>Death</MenuItem>
      </Menu>
    </div>
  )
}

export default MenuPopupState

usePopupState

This is a Custom Hook that uses useState internally, therefore the Rules of Hooks apply to usePopupState.

usePopupState Props

variant ('popover', 'popper', or 'dialog', required)

Use 'popover' if your popup is a Popover or Menu; use 'popper' if your popup is a Popper.

Right now this only affects whether bindTrigger/bindToggle/bindHover return an aria-controls prop or an aria-describedby prop.

popupId (string, optional but strongly encouraged)

The id for the popup component. It will be passed to the child props so that the trigger component may declare the same id in an ARIA prop.

disableAutoFocus (boolean, optional)

If true, will not steal focus when the popup is opened. (And bindPopover/bindMenu) will inject disableAutoFocus, disableEnforceFocus, and disableRestoreFocus).

Defaults to true when the popup is opened by the bindHover or bindFocus element.

usePopupState return value

An object with the following properties:

  • open([eventOrAnchorEl]): opens the popup. You must pass in an anchor element or an event with a currentTarget, otherwise the popup will not position properly and you will get a warning; MUI needs an anchor element to position the popup.
  • close(): closes the popup
  • toggle([eventOrAnchorEl]): opens the popup if it is closed, or closes the popup if it is open. If the popup is currently closed, you must pass an anchor element or an event with a currentTarget, otherwise the popup will not position properly and you will get a warning; MUI needs an anchor element to position the popup.
  • setOpen(open, [eventOrAnchorEl]): sets whether the popup is open. If open is truthy, you must pass in an anchor element or an event with a currentTarget, otherwise the popup will not position properly and you will get a warning; MUI needs an anchor element to position the popup.
  • isOpen: true/false if the popup is open/closed
  • anchorEl: the current anchor element
  • anchorPosition: the current anchor position
  • setAnchorEl: sets the anchor element (the currentTarget of the triggering mouse event is used by default unless you have called setAnchorEl)
  • popupId: the popupId prop you passed to PopupState
  • variant: the variant prop you passed to PopupState

Examples with Render Props

Menu

import * as React from 'react'
import Button from '@mui/material/Button'
import Menu from '@mui/material/Menu'
import MenuItem from '@mui/material/MenuItem'
import PopupState, { bindTrigger, bindMenu } from 'material-ui-popup-state'

const MenuPopupState = () => (
  <PopupState variant="popover" popupId="demoMenu">
    {(popupState) => (
      <React.Fragment>
        <Button variant="contained" {...bindTrigger(popupState)}>
          Open Menu
        </Button>
        <Menu {...bindMenu(popupState)}>
          <MenuItem onClick={popupState.close}>Cake</MenuItem>
          <MenuItem onClick={popupState.close}>Death</MenuItem>
        </Menu>
      </React.Fragment>
    )}
  </PopupState>
)

export default MenuPopupState

Popover

import React from 'react'
import PropTypes from 'prop-types'
import { withStyles } from '@mui/material/styles'
import Typography from '@mui/material/Typography'
import Button from '@mui/material/Button'
import Popover from '@mui/material/Popover'
import PopupState, { bindTrigger, bindPopover } from 'material-ui-popup-state'

const styles = (theme) => ({
  typography: {
    margin: theme.spacing.unit * 2,
  },
})

const PopoverPopupState = ({ classes }) => (
  <PopupState variant="popover" popupId="demoPopover">
    {(popupState) => (
      <div>
        <Button variant="contained" {...bindTrigger(popupState)}>
          Open Popover
        </Button>
        <Popover
          {...bindPopover(popupState)}
          anchorOrigin={{
            vertical: 'bottom',
            horizontal: 'center',
          }}
          transformOrigin={{
            vertical: 'top',
            horizontal: 'center',
          }}
        >
          <Typography className={classes.typography}>
            The content of the Popover.
          </Typography>
        </Popover>
      </div>
    )}
  </PopupState>
)

PopoverPopupState.propTypes = {
  classes: PropTypes.object.isRequired,
}

export default withStyles(styles)(PopoverPopupState)

Mouse Over Interaction

import React from 'react'
import PropTypes from 'prop-types'
import { withStyles } from '@mui/material/styles'
import Typography from '@mui/material/Typography'
import HoverPopover from 'material-ui-popup-state/HoverPopover'
import PopupState, { bindHover, bindPopover } from 'material-ui-popup-state'

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

const HoverPopoverPopupState = ({ classes }) => (
  <PopupState variant="popover" popupId="demoPopover">
    {(popupState) => (
      <div>
        <Typography {...bindHover(popupState)}>
          Hover with a Popover.
        </Typography>
        <HoverPopover
          {...bindPopover(popupState)}
          className={classes.popover}
          classes={{
            paper: classes.paper,
          }}
          anchorOrigin={{
            vertical: 'bottom',
            horizontal: 'center',
          }}
          transformOrigin={{
            vertical: 'top',
            horizontal: 'center',
          }}
        >
          <Typography>The content of the Popover.</Typography>
        </HoverPopover>
      </div>
    )}
  </PopupState>
)

HoverPopoverPopupState.propTypes = {
  classes: PropTypes.object.isRequired,
}

export default withStyles(styles)(HoverPopoverPopupState)

Popper

import React from 'react'
import PropTypes from 'prop-types'
import { withStyles } from '@mui/material/styles'
import Typography from '@mui/material/Typography'
import Button from '@mui/material/Button'
import Popper from '@mui/material/Popper'
import PopupState, { bindToggle, bindPopper } from 'material-ui-popup-state'
import Fade from '@mui/material/Fade'
import Paper from '@mui/material/Paper'

const styles = (theme) => ({
  typography: {
    padding: theme.spacing.unit * 2,
  },
})

const PopperPopupState = ({ classes }) => (
  <PopupState variant="popper" popupId="demoPopper">
    {(popupState) => (
      <div>
        <Button variant="contained" {...bindToggle(popupState)}>
          Toggle Popper
        </Button>
        <Popper {...bindPopper(popupState)} transition>
          {({ TransitionProps }) => (
            <Fade {...TransitionProps} timeout={350}>
              <Paper>
                <Typography className={classes.typography}>
                  The content of the Popper.
                </Typography>
              </Paper>
            </Fade>
          )}
        </Popper>
      </div>
    )}
  </PopupState>
)

PopperPopupState.propTypes = {
  classes: PropTypes.object.isRequired,
}

export default withStyles(styles)(PopperPopupState)

Render Props API

Bind Functions

material-ui-popup-state exports several helper functions you can use to connect components easily:

  • anchorRef: creates a ref function to pass to the anchorEl (by default, the currentTarget of the mouse event that triggered the popup is used; only use anchorRef if you want a different element to be the anchor).
  • bindMenu: creates props to control a Menu component.
  • bindPopover: creates props to control a Popover component.
  • bindPopper: creates props to control a Popper component.
  • bindDialog: creates props to control a Dialog component.
  • bindTrigger: creates props for a component that opens the popup when clicked.
  • bindContextMenu: creates props for a component that opens the popup on when right clicked (contextmenu event). NOTE: bindPopover/bindMenu will position the Popover/Menu to the contextmenu event location. To position using the contextmenu target element instead, pass anchorReference="anchorEl" after {...bindPopover(popupState)}/{...bindMenu(popupState)}.
  • bindToggle: creates props for a component that toggles the popup when clicked.
  • bindHover: creates props for a component that opens the popup while hovered. NOTE: See this guidance if you are using bindHover with Popover or Menu.
  • bindFocus: creates props for a component that opens the popup while hovered.

To use one of these functions, you should call it with the props PopupState passed to your child function, and spread the return value into the desired element:

import * as React from 'react'
import Button from '@mui/material/Button'
import Menu from '@mui/material/Menu'
import MenuItem from '@mui/material/MenuItem'
import PopupState, { bindTrigger, bindMenu } from 'material-ui-popup-state'

const MenuPopupState = () => (
  <PopupState variant="popover" popupId="demoMenu">
    {(popupState) => (
      <React.Fragment>
        <Button variant="contained" {...bindTrigger(popupState)}>
          Open Menu
        </Button>
        <Menu {...bindMenu(popupState)}>
          <MenuItem onClick={popupState.close}>Cake</MenuItem>
          <MenuItem onClick={popupState.close}>Death</MenuItem>
        </Menu>
      </React.Fragment>
    )}
  </PopupState>
)

export default MenuPopupState

PopupState Props

variant ('popover', 'popper', or 'dialog', required)

Use 'popover' if your popup is a Popover or Menu; use 'popper' if your popup is a Popper.

Right now this only affects whether bindTrigger/bindToggle/bindHover return an aria-controls prop or an aria-describedby prop.

popupId (string, optional but strongly encouraged)

The id for the popup component. It will be passed to the child props so that the trigger component may declare the same id in an ARIA prop.

disableAutoFocus (boolean, optional)

If true, will not steal focus when the popup is opened. (And bindPopover/bindMenu) will inject disableAutoFocus, disableEnforceFocus, and disableRestoreFocus).

Defaults to true when the popup is opened by the bindHover or bindFocus element.

children ((popupState: InjectedProps) => ?React.Node, required)

The render function. It will be called with an object containing the following props (exported as the InjectedProps type):

  • open([eventOrAnchorEl]): opens the popup
  • close(): closes the popup
  • toggle([eventOrAnchorEl]): opens the popup if it is closed, or closes the popup if it is open.
  • setOpen(open, [eventOrAnchorEl]): sets whether the popup is open.
  • isOpen: true/false if the popup is open/closed
  • anchorEl: the current anchor element
  • anchorPosition: the current anchor position
  • setAnchorEl: sets the anchor element (the currentTarget of the triggering mouse event is used by default unless you have called setAnchorEl)
  • popupId: the popupId prop you passed to PopupState
  • variant: the variant prop you passed to PopupState

Using Popover and Menu with bindHover

MUI's Modal (used by Popover and Menu) blocks pointer events to all other components, interfering with bindHover (the popover or menu will open when the mouse enters the bindHover element, but won't close when the mouse leaves). You can use the following components to work around this:

import HoverMenu from 'material-ui-popup-state/HoverMenu'
import HoverPopover from 'material-ui-popup-state/HoverPopover'

These are just wrapper components that pass inline styles to prevent Modal from blocking pointer events.

Chaining event handlers

What if you need to perform additional actions in onClick, but it's being injected by {...bindTrigger(popupState)} etc?

There are two options:

Chaining event handlers manually

This is the most straightforward, explicit option.

const button = (
  <Button
    {...bindTrigger(popupState)}
    onClick={(e: React.MouseEvent) => {
      bindTrigger(popupState).onClick(e)
      performCustomAction(e)
    }}
  >
    Open Menu
  </Button>
)

Using material-ui-popup-state/chainEventHandlers

If you don't like the above option, you can use the provided material-ui-popup-state/chainEventHandlers helper:

import { chainEventHandlers } from 'material-ui-popup-state/chainEventHandlers'

const button = (
  <Button
    {...chainEventHandlers(bindTrigger(popupState), {
      onClick: (e: React.MouseEvent) => {
        bindTrigger(popupState).onClick(e)
        performCustomAction(e)
      },
    })}
  >
    Open Menu
  </Button>
)

chainEventHandlers accepts a variable number of props arguments and combines any function props of the same name into a function that invokes the chained functions in sequence. For all other properties the behavior is like Object.assign.

@storycopter/gatsby-starter-storycopterextension-chromereact-rogatioreact-rogatio-builderaudit-automator-clientdevelapp-regional-frontenddesignsystem_alphaabc-institute@cloudhub-ux/corecsr-iam-app-libcelfinet-rinnos-dash-energy@everything-registry/sub-chunk-2138backoffice-ui-labcrc-ui-1ncts5-lanaco-sharednpm-mui-react-table@andrydharmawan/bgs-component@andrydharmawan/bgs-form@andrydharmawan/component-react@astral-frontend/components@arkhn/uiautomedsys-reusable-ui-modules@3-shake/3design-ui@4design/for-ui@3design/3design-uiberry-free-react-material-admin-templateberry-material-react-freeasma-core-ui@azondo/ui@backstage/plugin-catalog-reactcloudhubuicloud-datagridcloudhub-components@backlight-dev/another-one.bricks-2htydw13pkxg0gsvc@backlight-dev/cent-undress-o9ffc.bricks-l6ngliw7@backlight-dev/power-drive-tgm7r.bricks-lo057i5n@backlight-dev/hdn-test.hdn-bricks@backlight-dev/human-apple-pfrt9.bricks-l0gqrlz3@backlight-dev/icky-boys-h9rid.bricks-lglce3zu@backlight-dev/rapid-sweater-1n8dt.bricks-l3yzvl9g@backstage/plugin-gcalendar@backstage/plugin-microsoft-calendarblondie-platform-webboomsourcingstorybookbrandshield-commonbridgeweave-ui@backlight-dev/various-trouble-hjmth.bricks-lgq27cpj@backlight-dev/veil-steal-hej64.bricks-l7gtkkiy@backlight-dev/various-trouble-hjmth.bricks-l6ngliw7-fork-lgq24azv@backlight-dev/setbrain.ink-forego-vnavb.bricks-l5gkcnmk@backlight-dev/sillyleo.bricks-lccvkr2p@backlight-dev/soap-distribute-dux20.rjidbxx-mui-avatarbxx-mui-formbxx-mui-layoutbxx-slate-editorcatalyst-canvasnubreed-widgetqubryx-react-componentpublic-filterreact-storefrontrt-state-formrtxalbertortxalberto1rtxalberto2rtxalberto3rtxalberto4rtxalberto5rtxalberto6semoss-componentssentral.rnvivify-ideas-react-scriptstranscriptpackageunity-fluent-libraryunofficial-dynamic-content-uiweb-whitelabelyoutrade-repoyoutrade-ui-core-componentstuten-top-barthe-handsomestnerd-internalwormhole-connect-clientshayan37shayan38shayan39shayan40site-tenuxtalentorg-profile-editortds-design-teststonex-uispacelabstrength-uitenjin-ui-kittest-datagrid777red-quick-template@cottage-software-inc/client-libraryclmm-masterscheckout-loadz@hashintel/type-editor@hedgehogcomputing/websitecra-template-red-quick
5.1.0

10 days ago

5.0.11

10 days ago

5.0.10

5 months ago

5.0.9

10 months ago

5.0.8

1 year ago

5.0.7

1 year ago

5.0.6

1 year ago

5.0.5

1 year ago

5.0.4

1 year ago

5.0.3

1 year ago

5.0.2

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

4.1.0

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.2

2 years ago

2.0.1

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.0.0

2 years ago

2.0.0-beta.1

3 years ago

1.9.3

3 years ago

1.9.1

3 years ago

1.9.2

3 years ago

1.9.0

3 years ago

1.8.4

3 years ago

1.8.3

3 years ago

1.8.2

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.2

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.4

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.2

4 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago