--- productId: material-ui title: React Popper component components: Popper githubLabel: 'component: Popper' githubSource: packages/mui-material/src/Popper --- # Popper A Popper can be used to display some content on top of another. It's an alternative to react-popper. Some important features of the Popper component: - 🕷 Popper relies on the 3rd party library ([Popper.js](https://popper.js.org/docs/v2/)) for perfect positioning. - 💄 It's an alternative API to react-popper. It aims for simplicity. - Its child element is a [Portal](/material-ui/react-portal/) on the body of the document to avoid rendering problems. You can disable this behavior with `disablePortal`. - The scroll isn't blocked like with the [Popover](/material-ui/react-popover/) component. The placement of the popper updates with the available area in the viewport. - Clicking away does not hide the Popper component. If you need this behavior, you can use the [Click-Away Listener](/material-ui/react-click-away-listener/) - see the example in the [menu documentation section](/material-ui/react-menu/#composition-with-menu-list). - The `anchorEl` is passed as the reference object to create a new `Popper.js` instance. {{"component": "@mui/internal-core-docs/ComponentLinkHeader", "design": false}} ## Basic Popper ```tsx import * as React from 'react'; import Box from '@mui/material/Box'; import Popper from '@mui/material/Popper'; export default function SimplePopper() { const [anchorEl, setAnchorEl] = React.useState(null); const handleClick = (event: React.MouseEvent) => { setAnchorEl(anchorEl ? null : event.currentTarget); }; const open = Boolean(anchorEl); const id = open ? 'simple-popper' : undefined; return (
The content of the Popper.
); } ``` ## Transitions The open/close state of the popper can be animated with a render prop child and a transition component. This component should respect the following conditions: - Be a direct child descendent of the popper. - Call the `onEnter` callback prop when the enter transition starts. - Call the `onExited` callback prop when the exit transition is completed. These two callbacks allow the popper to unmount the child content when closed and fully transitioned. Popper has built-in support for [react-transition-group](https://github.com/reactjs/react-transition-group). ```tsx import * as React from 'react'; import Box from '@mui/material/Box'; import Popper from '@mui/material/Popper'; import Fade from '@mui/material/Fade'; export default function TransitionsPopper() { const [open, setOpen] = React.useState(false); const [anchorEl, setAnchorEl] = React.useState(null); const handleClick = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); setOpen((previousOpen) => !previousOpen); }; const canBeOpen = open && Boolean(anchorEl); const id = canBeOpen ? 'transition-popper' : undefined; return (
{({ TransitionProps }) => ( The content of the Popper. )}
); } ``` Alternatively, you can use [react-spring](https://github.com/pmndrs/react-spring). ```tsx import * as React from 'react'; import Box from '@mui/material/Box'; import Popper from '@mui/material/Popper'; import { useSpring, animated } from '@react-spring/web'; interface FadeProps { children?: React.ReactElement; in?: boolean; onEnter?: () => void; onExited?: () => void; } const Fade = React.forwardRef(function Fade(props, ref) { const { in: open, children, onEnter, onExited, ...other } = props; const style = useSpring({ from: { opacity: 0 }, to: { opacity: open ? 1 : 0 }, onStart: () => { if (open && onEnter) { onEnter(); } }, onRest: () => { if (!open && onExited) { onExited(); } }, }); return ( {children} ); }); export default function SpringPopper() { const [open, setOpen] = React.useState(false); const [anchorEl, setAnchorEl] = React.useState(null); const handleClick = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); setOpen((previousOpen) => !previousOpen); }; const canBeOpen = open && Boolean(anchorEl); const id = canBeOpen ? 'spring-popper' : undefined; return (
{({ TransitionProps }) => ( The content of the Popper. )}
); } ``` ## Positioned popper ```tsx import * as React from 'react'; import Box from '@mui/material/Box'; import Popper, { PopperPlacementType } from '@mui/material/Popper'; import Typography from '@mui/material/Typography'; import Stack from '@mui/material/Stack'; import Button from '@mui/material/Button'; import Fade from '@mui/material/Fade'; import Paper from '@mui/material/Paper'; export default function PositionedPopper() { const [anchorEl, setAnchorEl] = React.useState(null); const [open, setOpen] = React.useState(false); const [placement, setPlacement] = React.useState(); const handleClick = (newPlacement: PopperPlacementType) => (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); setOpen((prev) => placement !== newPlacement || !prev); setPlacement(newPlacement); }; return ( {({ TransitionProps }) => ( The content of the Popper. )} ); } ``` ## Scroll playground ```jsx import * as React from 'react'; import { styled } from '@mui/material/styles'; import FormControlLabel from '@mui/material/FormControlLabel'; import Grid from '@mui/material/Grid'; import Typography from '@mui/material/Typography'; import Box from '@mui/material/Box'; import Button from '@mui/material/Button'; import MuiPopper from '@mui/material/Popper'; import Paper from '@mui/material/Paper'; import DialogActions from '@mui/material/DialogActions'; import DialogContent from '@mui/material/DialogContent'; import DialogContentText from '@mui/material/DialogContentText'; import DialogTitle from '@mui/material/DialogTitle'; import Switch from '@mui/material/Switch'; import TextField from '@mui/material/TextField'; import FormGroup from '@mui/material/FormGroup'; import { HighlightedCode } from '@mui/internal-core-docs/HighlightedCode'; const Popper = styled(MuiPopper, { shouldForwardProp: (prop) => prop !== 'arrow', })(({ theme }) => ({ zIndex: 1, '& > div': { position: 'relative', }, '&[data-popper-placement*="bottom"]': { '& .MuiPopper-arrow': { top: 0, left: 0, marginTop: '-0.9em', width: '3em', height: '1em', '&::before': { borderWidth: '0 1em 1em 1em', borderColor: `transparent transparent ${theme.palette.background.paper} transparent`, }, }, }, '&[data-popper-placement*="top"]': { '& .MuiPopper-arrow': { bottom: 0, left: 0, marginBottom: '-0.9em', width: '3em', height: '1em', '&::before': { borderWidth: '1em 1em 0 1em', borderColor: `${theme.palette.background.paper} transparent transparent transparent`, }, }, }, '&[data-popper-placement*="right"]': { '& .MuiPopper-arrow': { left: 0, marginLeft: '-0.9em', height: '3em', width: '1em', '&::before': { borderWidth: '1em 1em 1em 0', borderColor: `transparent ${theme.palette.background.paper} transparent transparent`, }, }, }, '&[data-popper-placement*="left"]': { '& .MuiPopper-arrow': { right: 0, marginRight: '-0.9em', height: '3em', width: '1em', '&::before': { borderWidth: '1em 0 1em 1em', borderColor: `transparent transparent transparent ${theme.palette.background.paper}`, }, }, }, variants: [ { props: ({ arrow }) => arrow, style: { '&[data-popper-placement*="bottom"]': { '& > div': { marginTop: 2, }, }, }, }, { props: ({ arrow }) => !arrow, style: { '&[data-popper-placement*="bottom"]': { '& > div': { marginTop: 0, }, }, }, }, { props: ({ arrow }) => arrow, style: { '&[data-popper-placement*="top"]': { '& > div': { marginBottom: 2, }, }, }, }, { props: ({ arrow }) => !arrow, style: { '&[data-popper-placement*="top"]': { '& > div': { marginBottom: 0, }, }, }, }, { props: ({ arrow }) => arrow, style: { '&[data-popper-placement*="right"]': { '& > div': { marginLeft: 2, }, }, }, }, { props: ({ arrow }) => !arrow, style: { '&[data-popper-placement*="right"]': { '& > div': { marginLeft: 0, }, }, }, }, { props: ({ arrow }) => arrow, style: { '&[data-popper-placement*="left"]': { '& > div': { marginRight: 2, }, }, }, }, { props: ({ arrow }) => !arrow, style: { '&[data-popper-placement*="left"]': { '& > div': { marginRight: 0, }, }, }, }, ], })); const Arrow = styled('div')({ position: 'absolute', fontSize: 7, width: '3em', height: '3em', '&::before': { content: '""', margin: 'auto', display: 'block', width: 0, height: 0, borderStyle: 'solid', }, }); export default function ScrollPlayground() { const anchorRef = React.useRef(null); const [open, setOpen] = React.useState(false); const [placement, setPlacement] = React.useState('bottom'); const [disablePortal, setDisablePortal] = React.useState(false); const [flip, setFlip] = React.useState({ enabled: true, altBoundary: true, rootBoundary: 'document', }); const [preventOverflow, setPreventOverflow] = React.useState({ enabled: true, altAxis: true, altBoundary: true, tether: true, rootBoundary: 'document', }); const [arrow, setArrow] = React.useState(false); const [arrowRef, setArrowRef] = React.useState(null); const handleClickButton = () => { setOpen((prevOpen) => !prevOpen); }; const centerScroll = (element) => { if (!element) { return; } const container = element.parentElement; container.scrollTop = element.clientHeight / 4; container.scrollLeft = element.clientWidth / 4; }; const jsx = ` `; const id = open ? 'scroll-playground' : null; return (
Scroll around this container to experiment with flip and preventOverflow modifiers.
{arrow ? ( ) : null} {"Use Google's location service?"} Let Google help apps determine location.
Appearance { setPlacement(event.target.value); }} variant="standard" slotProps={{ select: { native: true, inputProps: { 'aria-labelledby': 'scroll-playground-placement-label', }, }, inputLabel: { id: 'scroll-playground-placement-label', }, }} > { setDisablePortal(event.target.checked); }} value="disablePortal" /> } label="Disable portal" /> (the children stay within their parent DOM hierarchy) Modifiers (options from Popper.js) Prevent Overflow { setPreventOverflow((old) => ({ ...old, enabled: event.target.checked, })); }} value="arrow" /> } label="Enable" /> { setPreventOverflow((old) => ({ ...old, altAxis: event.target.checked, })); }} value="alt-axis" /> } label="Alt axis" /> { setPreventOverflow((old) => ({ ...old, altBoundary: event.target.checked, })); }} value="alt-boundary" /> } label="Alt Boundary" /> { setPreventOverflow((old) => ({ ...old, tether: event.target.checked, })); }} value="tether" /> } label="Tether" /> { setPreventOverflow((old) => ({ ...old, rootBoundary: event.target.value, })); }} variant="standard" slotProps={{ select: { native: true, inputProps: { 'aria-labelledby': 'scroll-playground-prevent-overflow-root-boundary', }, }, inputLabel: { id: 'scroll-playground-prevent-overflow-root-boundary', }, }} > Flip { setFlip((old) => ({ ...old, enabled: event.target.checked, })); }} value="enabled" /> } label="Enable" /> { setFlip((old) => ({ ...old, altBoundary: event.target.checked, })); }} value="alt-boundary" /> } label="Alt Boundary" /> { setFlip((old) => ({ ...old, rootBoundary: event.target.value, })); }} variant="standard" slotProps={{ select: { native: true, inputProps: { 'aria-labelledby': 'scroll-playground-flip-root-boundary', }, }, inputLabel: { id: 'scroll-playground-flip-root-boundary', }, }} > Arrow { setArrow(event.target.checked); }} value="arrow" /> } label="Enable" />
); } ``` ## Virtual element The value of the `anchorEl` prop can be a reference to a fake DOM element. You need to create an object shaped like the [`VirtualElement`](https://popper.js.org/docs/v2/virtual-elements/). Highlight part of the text to see the popper: ```tsx import * as React from 'react'; import Popper, { PopperProps } from '@mui/material/Popper'; import Typography from '@mui/material/Typography'; import Fade from '@mui/material/Fade'; import Paper from '@mui/material/Paper'; export default function VirtualElementPopper() { const [open, setOpen] = React.useState(false); const [anchorEl, setAnchorEl] = React.useState(null); const previousAnchorElPosition = React.useRef(undefined); React.useEffect(() => { if (anchorEl) { if (typeof anchorEl === 'object') { previousAnchorElPosition.current = anchorEl.getBoundingClientRect(); } else { previousAnchorElPosition.current = anchorEl().getBoundingClientRect(); } } }, [anchorEl]); const handleClose = () => { setOpen(false); }; const handleMouseUp = () => { const selection = window.getSelection(); // Resets when the selection has a length of 0 if (!selection || selection.anchorOffset === selection.focusOffset) { handleClose(); return; } const getBoundingClientRect = () => { if (selection.rangeCount === 0 && previousAnchorElPosition.current) { setOpen(false); return previousAnchorElPosition.current; } return selection.getRangeAt(0).getBoundingClientRect(); }; setOpen(true); setAnchorEl({ getBoundingClientRect }); }; const id = open ? 'virtual-element-popper' : undefined; return (
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ipsum purus, bibendum sit amet vulputate eget, porta semper ligula. Donec bibendum vulputate erat, ac fringilla mi finibus nec. Donec ac dolor sed dolor porttitor blandit vel vel purus. Fusce vel malesuada ligula. Nam quis vehicula ante, eu finibus est. Proin ullamcorper fermentum orci, quis finibus massa. Nunc lobortis, massa ut rutrum ultrices, metus metus finibus ex, sit amet facilisis neque enim sed neque. Quisque accumsan metus vel maximus consequat. Suspendisse lacinia tellus a libero volutpat maximus. {({ TransitionProps }) => ( The content of the Popper. )}
); } ``` ## Supplementary projects For more advanced use cases you might be able to take advantage of: ### material-ui-popup-state ![stars](https://img.shields.io/github/stars/jcoreio/material-ui-popup-state?style=social&label=Star) ![npm downloads](https://img.shields.io/npm/dm/material-ui-popup-state.svg) The package [`material-ui-popup-state`](https://github.com/jcoreio/material-ui-popup-state) that takes care of popper state for you in most cases. ```tsx 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'; export default function PopperPopupState() { return ( {(popupState) => (
{({ TransitionProps }) => ( The content of the Popper. )}
)}
); } ``` # Popper API ## Demos For examples and details on the usage of this React component, visit the component demo pages: - [Autocomplete](https://mui.com/material-ui/react-autocomplete/) - [Menu](https://mui.com/material-ui/react-menu/) - [Popper](https://mui.com/material-ui/react-popper/) ## Import ```jsx import Popper from '@mui/material/Popper'; // or import { Popper } from '@mui/material'; ``` ## Props | Name | Type | Default | Required | Description | |------|------|---------|----------|-------------| | open | `bool` | - | Yes | | | anchorEl | `HTML element \| object \| func` | - | No | | | children | `node \| func` | - | No | | | component | `elementType` | - | No | | | container | `HTML element \| func` | - | No | | | disablePortal | `bool` | `false` | No | | | keepMounted | `bool` | `false` | No | | | modifiers | `Array<{ data?: object, effect?: func, enabled?: bool, fn?: func, name?: any, options?: object, phase?: 'afterMain' \| 'afterRead' \| 'afterWrite' \| 'beforeMain' \| 'beforeRead' \| 'beforeWrite' \| 'main' \| 'read' \| 'write', requires?: Array, requiresIfExists?: Array }>` | - | No | | | placement | `'auto-end' \| 'auto-start' \| 'auto' \| 'bottom-end' \| 'bottom-start' \| 'bottom' \| 'left-end' \| 'left-start' \| 'left' \| 'right-end' \| 'right-start' \| 'right' \| 'top-end' \| 'top-start' \| 'top'` | `'bottom'` | No | | | popperOptions | `{ modifiers?: array, onFirstUpdate?: func, placement?: 'auto-end' \| 'auto-start' \| 'auto' \| 'bottom-end' \| 'bottom-start' \| 'bottom' \| 'left-end' \| 'left-start' \| 'left' \| 'right-end' \| 'right-start' \| 'right' \| 'top-end' \| 'top-start' \| 'top', strategy?: 'absolute' \| 'fixed' }` | `{}` | No | | | popperRef | `ref` | - | No | | | slotProps | `{ root?: func \| object }` | `{}` | No | | | slots | `{ root?: elementType }` | `{}` | No | | | sx | `Array \| func \| object` | - | No | The system prop that allows defining system overrides as well as additional CSS styles. | | transition | `bool` | `false` | No | | > **Note**: The `ref` is forwarded to the root element (HTMLDivElement). > Any other props supplied will be provided to the root element (native element). ## CSS ### Rule name | Global class | Rule name | Description | |--------------|-----------|-------------| | - | root | Class name applied to the root element. | ## Source code If you did not find the information on this page, consider having a look at the implementation of the component for more detail. - [/packages/mui-material/src/Popper/Popper.tsx](https://github.com/mui/material-ui/tree/HEAD/packages/mui-material/src/Popper/Popper.tsx)