import {Overlay} from '@primer/react'
The Overlay
component handles all behaviors needed by overlay UIs as well as the common styles that all overlays should have. Overlay
is the base component for many of our overlay-type components. Overlays use shadows to express elevation.
Behaviors include:
Escape
const Example = () => {// you must manage your own open stateconst [isOpen, setIsOpen] = React.useState(false)const noButtonRef = React.useRef(null)const anchorRef = React.useRef(null)return (<><Button ref={anchorRef} onClick={() => setIsOpen(!isOpen)}>open overlay</Button>{/* be sure to conditionally render the Overlay. This helps with performance and is required. */}{isOpen && (<OverlayinitialFocusRef={noButtonRef}returnFocusRef={anchorRef}ignoreClickRefs={[anchorRef]}onEscape={() => setIsOpen(!isOpen)}onClickOutside={() => setIsOpen(false)}aria-labelledby="title"><Box display="flex" flexDirection="column" p={2}><Text id="title">Are you sure you would like to delete this item?</Text><Button>yes</Button><Button ref={noButtonRef}>no</Button></Box></Overlay>)}</>)}render(<Example />)
Overlay
must either have:aria-labelledby
attribute that refers to a visible title.aria-label
attributeOverlay
should also have a longer description, use aria-describedby
Overlay
component has a role="dialog"
set on it, if you are using Overlay
for alerts, you can pass in role="alertdialog"
instead. Please read the W3C guidelines to determine which role is best for your use caseOverlay
component has aria-modal
set to true
by default and should not be overridden as all Overlay
s behave as modals.See the W3C accessibility recommendations for modals.
Overlay
renders its children
within a div positioned absolutely within a portal within the default portal root. The overlay will not update its positioning if the portal root's positioning changes (e.g., if the portal root is statically positioned after some DOM element that dynamically resizes). You may consider using the AnchoredOverlay component or customizing the portal root to achieve different positioning behavior.
The position of the Overlay can be customized by using the position
prop in conjunction with the top|left|right|bottom
props.
Name | Type | Default | Description |
---|---|---|---|
returnFocusRef Required | React.RefObject<HTMLElement> | Ref for the element to focus when the `Overlay` is closed. | |
onClickOutside Required | function | Function to call when clicking outside of the `Overlay`. Typically this function sets the `Overlay` visibility state to `false`. | |
onEscape Required | function | Function to call when user presses `Escape`. Typically this function sets the `Overlay` visibility state to `false`. | |
ignoreClickRefs | React.RefObject<HTMLElement> [] | An array of ref objects to ignore clicks on in the onOutsideClick behavior. This is often used to ignore clicking on the element that toggles the open/closed state for the Overlay to prevent the Overlay from being toggled twice. | |
initialFocusRef | React.RefObject<HTMLElement> | Ref for the element to focus when the `Overlay` is opened. If nothing is provided, the first focusable element in the `Overlay` body is focused. | |
width | | 'small' | 'medium' | 'large' | 'xlarge' | 'xxlarge' | 'auto' | 'auto' | Sets the width of the `Overlay`, pick from our set list of widths, or pass `auto` to automatically set the width based on the content of the `Overlay`. `small` corresponds to `256px`, `medium` corresponds to `320px`, `large` corresponds to `480px`, `xlarge` corresponds to `640px`, `xxlarge` corresponds to `960px`. |
height | | 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge' | 'auto' | 'auto' | Sets the height of the `Overlay`, pick from our set list of heights, or pass `auto` to automatically set the height based on the content of the `Overlay`. `xsmall` corresponds to `192px`, `small` corresponds to `256px`, `medium` corresponds to `320px`, `large` corresponds to `432px`, `xlarge` corresponds to `600px`. |
maxHeight | | 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge' | Sets the maximum height of the `Overlay`, pick from our set list of heights. `xsmall` corresponds to `192px`, `small` corresponds to `256px`, `medium` corresponds to `320px`, `large` corresponds to `432px`, `xlarge` corresponds to `600px`. | |
visibility | | 'visible' | 'hidden' | 'visible' | Sets the visibility of the `Overlay`. |
anchorSide | | 'inside-top' | 'inside-bottom' | 'inside-left' | 'inside-right' | 'inside-center' | 'outside-top' | 'outside-bottom' | 'outside-left' | 'outside-right' | If provided, the Overlay will slide into position from the side of the anchor with a brief animation | |
top | number | 0 | The top CSS property of the Overlay — affects the vertical position. |
left | number | 0 | The left CSS property of the Overlay — affects the horizontal position. |
right | number | The right CSS property of the Overlay — affects the horizontal position. | |
bottom | number | The bottom CSS property of the Overlay — affects the vertical position. | |
position | | 'static' | 'relative' | 'absolute' | 'fixed' | 'sticky' | absolute | The position CSS property of the Overlay — sets how the Overlay is positioned relative to its Portal |
portalContainerName | string | If defined, Overlays will be rendered in the named portal. See also `Portal`. | |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. |