Popup

Maintainer:
amje
GitHub
open
hasArrow
placement
import {Popup} from '@gravity-ui/uikit';

You can use a Popup to display floating content above the page. Technically, it is a wrapper around Floating UI with some default values. To manage Popup visibility, use the open property. The Popup child components are rendered inside the Portal component. To disable this behavior, use the disablePortal property.

Anchor

To specify the anchor of a floating element, you can use the anchorElement property.

const [buttonElement, setButtonElement] = React.useState(null);
const [open, setOpen] = React.useState(false);

<Button ref={setButtonElement} onClick={() => setOpen((prevOpen) => !prevOpen)}>
  Toggle Popup
</Button>
<Popup anchorElement={buttonElement} open={open} placement="bottom">
  Content
</Popup>

Placement

Use the placement property to manage the Popup position around the anchor element. By default, Popup uses flip middleware to prevent overflow. If the property is set to an array, the first element will be used as the default placement value, the rest will be used as fallback placements. It is also acceptable to use the values auto, auto-start, auto-end to use autoPlacement middleware instead of flip.

Anchor
Top Start
Top
Top End
Right Start
Right
Right End
Bottom End
Bottom
Bottom Start
Left End
Left
Left Start
const [boxElement, setBoxElement] = React.useState(null);

<div ref={setBoxElement} />
<Popup open anchorElement={boxElement} placement="top-start">Top Start</Popup>
<Popup open anchorElement={boxElement} placement="top">Top</Popup>
<Popup open anchorElement={boxElement} placement="top-end">Top End</Popup>
<Popup open anchorElement={boxElement} placement="right-start">Right Start</Popup>
<Popup open anchorElement={boxElement} placement="right">Right</Popup>
<Popup open anchorElement={boxElement} placement="right-end">Right End</Popup>
<Popup open anchorElement={boxElement} placement="bottom-end">Bottom End</Popup>
<Popup open anchorElement={boxElement} placement="bottom">Bottom</Popup>
<Popup open anchorElement={boxElement} placement="bottom-start">Bottom Start</Popup>
<Popup open anchorElement={boxElement} placement="left-end">Left End</Popup>
<Popup open anchorElement={boxElement} placement="left">Left</Popup>
<Popup open anchorElement={boxElement} placement="left-start">Left Start</Popup>

Properties

NameDescriptionTypeDefault
anchorElementAnchor element. Can also be a VirtualElementPopupAnchorElement
aria-describedbyaria-describedby attribute. Use it if you have both label and description nodesstring
aria-labelaria-label attribute. Use it only if you do not have any visible captionstring
aria-labelledbyaria-labelledby attribute. Preferable if you have visible captionstring
childrenAny React contentReact.ReactNode
classNameclass HTML attribute for the root nodestring
disableEscapeKeyDownDisables triggering close on Escbooleanfalse
disableFocusOutDisables triggering close on focusoutbooleanfalse
disableOutsideClickDisables triggering close on outside clicksbooleanfalse
disablePortalDisables using Portal for childrenbooleanfalse
disableTransitionDisables animation of popup appearing/disappearingbooleanfalse
floatingClassNameAdditional class to apply to the Floating UI elementstring
floatingContextFloating UI context to provide interactionsFloatingRootContext
floatingInteractionsOverride Floating UI interactionsArray<ElementProps>
floatingMiddlewaresFloating UI middlewares. If set, they will completely overwrite the default middlewares.Array<Middleware>
floatingStylesStyles to apply to the Floating UI elementReact.CSSProperties
focusOrderThe order in which focus circleArray<'reference' | 'floating' | 'content'>['content']
hasArrowRenders arrow pointing to the anchorbooleanfalse
idid HTML attributestring
initialFocusInitial element to be focused. Positive number is the index of tabbable element.number React.Ref<HTMLElement>
keepMountedPopup will not be removed from the DOM upon hidingbooleanfalse
modalEnables focus trapping behaviourbooleanfalse
offsetFloating UI offset valuePopupOffset4
onOpenChangeHandles Popup open change eventFunction
onTransitionInOn start open popup animationFunction
onTransitionInCompleteOn finish open popup animationFunction
onTransitionOutOn start close popup animationFunction
onTransitionOutCompleteOn finish close popup animationFunction
openManages Popup visibilitybooleanfalse
placementFloating UI placementPlacement Array<Placement> auto auto-start auto-end
qaTest attribute (data-qa)string
returnFocusElement to be focused on closingboolean React.Ref<HTMLElement>true
roleAccessibility role for popupstring
strategyFloating UI positioning strategyabsolute fixedabsolute
stylestyle HTML attribute for root nodeReact.CSSProperties

CSS API

NameDescription
--g-popup-background-colorBackground color
--g-popup-border-colorBorder color
--g-popup-border-widthBorder width