apps/www/content/docs/components/popover.mdx
import { Popover } from "@chakra-ui/react"
<Popover.Root>
<Popover.Trigger />
<Popover.Positioner>
<Popover.Content>
<Popover.CloseTrigger />
<Popover.Arrow>
<Popover.ArrowTip />
</Popover.Arrow>
<Popover.Body>
<Popover.Title />
</Popover.Body>
</Popover.Content>
</Popover.Positioner>
</Popover.Root>
The Popover provides a shortcuts for common use cases.
The Popover.Arrow renders the Popover.ArrowTip component within in by
default.
This works:
<Popover.Arrow>
<Popover.ArrowTip />
</Popover.Arrow>
This might be more concise, if you don't need to customize the arrow tip.
<Popover.Arrow />
Use the open and onOpenChange to control the visibility of the popover.
Use the size prop to change the size of the popover component.
Use the lazyMounted and/or unmountOnExit prop to defer the mounting of the
popover content until it's opened.
Use the positioning.placement prop to configure the underlying floating-ui
positioning logic.
Use the positioning.offset prop to adjust the position of the popover content.
Use the positioning.sameWidth prop to make the popover content the same width
as the trigger.
When nesting floating elements like popover, select, menu, inside of the popover, avoid portalling them to the document's body.
-<Portal>
<Popover.Positioner>
<Popover.Content>
</Popover.Content>
</Popover.Positioner>
-</Portal>
Use the initialFocusEl prop to set the initial focus of the popover content.
Here's an example of a popover with a form inside.
<ExampleTabs name="popover-with-form" />Use the --popover-bg CSS variable to change the background color of the
popover content and its arrow.
To use the Popover within a Dialog, you need to avoid portalling the
Popover.Positioner to the document's body.
-<Portal>
<Popover.Positioner>
<Popover.Content>
</Popover.Content>
</Popover.Positioner>
-</Portal>
If you have set scrollBehavior="inside" on the Dialog, you need to:
fixed to avoid the popover from being clipped
by the dialog.hideWhenDetached to true to hide the popover when the trigger is
scrolled out of view.<Popover.Root positioning={{ strategy: "fixed", hideWhenDetached: true }}>
</Popover.Root>
Use usePopoverContext to access the popover's state and methods from any
component inside the popover.
import { usePopoverContext } from "@chakra-ui/react"
const PopoverStatus = () => {
const popover = usePopoverContext()
return <div>Popover is {popover.open ? "open" : "closed"}</div>
}
const MyPopover = () => (
<Popover.Root>
<Popover.Trigger>Open</Popover.Trigger>
<Popover.Positioner>
<Popover.Content>
<PopoverStatus />
</Popover.Content>
</Popover.Positioner>
</Popover.Root>
)
Use setOpen(false) from the context to close the popover programmatically.
import { usePopoverContext } from "@chakra-ui/react"
const CloseButton = () => {
const popover = usePopoverContext()
return <Button onClick={() => popover.setOpen(false)}>Close Popover</Button>
}
const MyPopover = () => (
<Popover.Root>
<Popover.Trigger>Open</Popover.Trigger>
<Popover.Positioner>
<Popover.Content>
<CloseButton />
</Popover.Content>
</Popover.Positioner>
</Popover.Root>
)
Use positioning.getAnchorRect() to position the popover based on a custom
element ref.
import { useRef } from "react"
const MyPopover = () => {
const anchorRef = useRef<HTMLDivElement>(null)
return (
<>
<div ref={anchorRef}>Anchor Element</div>
<Popover.Root
positioning={{
getAnchorRect() {
return anchorRef.current?.getBoundingClientRect()
},
}}
>
<Popover.Trigger>Open</Popover.Trigger>
<Popover.Positioner>
<Popover.Content>
<Popover.Body>
This popover is anchored to the div above
</Popover.Body>
</Popover.Content>
</Popover.Positioner>
</Popover.Root>
</>
)
}
Explore the Popover component parts interactively. Click on parts in the
sidebar to highlight them in the preview.