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:
Escapeconst 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-describedbyOverlay 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 Overlays behave as modals.See the W3C accessibility recommendations for modals here.
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.
| Name | Type | Default | Description | 
|---|---|---|---|
| returnFocusRef Required | React.RefObject<HTMLElement> | Ref for the element to focus when the Overlayis closed. | |
| onClickOutside Required | function | Function to call when clicking outside of the Overlay. Typically this function sets theOverlayvisibility state tofalse. | |
| onEscape Required | function | Function to call when user presses Escape. Typically this function sets theOverlayvisibility state tofalse. | |
| 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 Overlayis opened. If nothing is provided, the first focusable element in theOverlaybody is focused. | |
| width | | 'small' | 'medium' | 'large' | 'xlarge' | 'xxlarge' | 'auto' | 'auto' | Sets the width of the Overlay, pick from our set list of widths, or passautoto automatically set the width based on the content of theOverlay.smallcorresponds to256px,mediumcorresponds to320px,largecorresponds to480px,xlargecorresponds to640px,xxlargecorresponds to960px. | 
| height | | 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge' | 'auto' | 'auto' | Sets the height of the Overlay, pick from our set list of heights, or passautoto automatically set the height based on the content of theOverlay.xsmallcorresponds to192px,smallcorresponds to256px,mediumcorresponds to320px,largecorresponds to432px,xlargecorresponds to600px. | 
| maxHeight | | 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge' | Sets the maximum height of the Overlay, pick from our set list of heights.xsmallcorresponds to192px,smallcorresponds to256px,mediumcorresponds to320px,largecorresponds to432px,xlargecorresponds to600px. | |
| 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 | Vertical position of the overlay, relative to its closest positioned ancestor (often its Portal). | 
| left | number | 0 | Horizontal position of the overlay, relative to its closest positioned ancestor (often 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. |