Overlay
Overlay2 renders content above the page, with an optional backdrop, focus management and escape handling. Dialog, Drawer, Alert and Popover are built on it. Use it directly only when none of them fits.
Usage#
import { Overlay2 } from "@clawscale/react";Overlay2 is controlled. It renders its children while isOpen is true and calls onClose when the user presses escape or clicks outside. Update your state there to close it.
It needs an OverlaysProvider above it. ClawscaleProvider renders one.
Examples#
Basic#
Overlay content is position: fixed. Place it with your own styles.
import { Button, Card, H5, Overlay2 } from "@clawscale/react";
import { useState } from "react";
export default function OverlayBasic() {
const [isOpen, setIsOpen] = useState(false);
const close = () => setIsOpen(false);
return (
<>
<Button text="Show run summary" onClick={() => setIsOpen(true)} />
<Overlay2 isOpen={isOpen} onClose={close}>
<Card
elevation={4}
style={{ left: 0, right: 0, top: "15vh", margin: "0 auto", width: "min(440px, calc(100vw - 32px))" }}
>
<H5>Run 4812 finished with warnings</H5>
<p>ingest-orders loaded 1,284,112 rows in 6 min 42 s. 312 rows failed schema validation.</p>
<div style={{ display: "flex", gap: 8, justifyContent: "flex-end" }}>
<Button text="Close" onClick={close} />
<Button intent="primary" text="Open run" onClick={close} />
</div>
</Card>
</Overlay2>
</>
);
}Scrolling#
Content taller than the viewport is clipped. Add Classes.OVERLAY_SCROLL_CONTAINER to make the overlay scroll. Dialog adds it for you.
<Overlay2 className={Classes.OVERLAY_SCROLL_CONTAINER} isOpen={isOpen} onClose={close}>
<Card>{/* long content */}</Card>
</Overlay2>Child refs#
Overlay2 attaches a ref to each child to run its enter and exit transitions. Children must be DOM elements or components that forward their ref, like Card.
- A single child without its own
refneeds nothing. - If the child has a
ref, pass the same ref object tochildRef. - With several children, pass
childRefs, keyed by each child'skey.
Interactive playground#
Props interface#
interface Overlay2Props extends OverlayProps, React.RefAttributes<OverlayInstance>@clawscale/react| Props | Description |
|---|---|
autoFocus | boolean = trueWhether the overlay should acquire application focus when it first opens. Inherited from OverlayableProps |
backdropClassName | stringCSS class names to apply to backdrop element. Inherited from BackdropProps |
backdropProps | React.HTMLProps<HTMLDivElement>HTML props for the backdrop element. Inherited from BackdropProps |
canEscapeKeyClose | boolean = trueWhether pressing the Inherited from esc key should invoke onClose.OverlayableProps |
canOutsideClickClose | boolean = trueWhether clicking outside the overlay element (either on backdrop when present or on document) should invoke Inherited from onClose.BackdropProps |
childRef | React.RefObject<HTMLElement>If you provide a single child element to Overlay2 and attach your own ref to the node, you must pass the same value here (otherwise, Overlay2 won't be able to render CSSTransition correctly). |
childRefs | Record<string, React.RefObject<HTMLElement>>If you provide a _multiple child elements_ to Overlay2, you must enumerate and generate a collection of DOM refs to those elements and provide it here. The object's keys must correspond to the child React element key values. |
children | React.ReactNodeElement to overlay. Inherited from OverlayProps |
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
enforceFocus | boolean = trueWhether the overlay should prevent focus from leaving itself. That is, if the user attempts to focus an element outside the overlay and this prop is enabled, then the overlay will immediately bring focus back to itself. If you are nesting overlay components, either disable this prop on the "outermost" overlays or mark the nested ones Inherited from usePortal={false}.OverlayableProps |
hasBackdrop | boolean = trueWhether a container-spanning backdrop element should be rendered behind the contents. When Inherited from false, users will be able to scroll through and interact with overlaid content.BackdropProps |
isOpenrequired | booleanToggles the visibility of the overlay and its children. This prop is required because the component is controlled. Inherited from OverlayProps |
lazy | boolean = trueIf Inherited from true and usePortal={true}, the Portal containing the children is created and attached to the DOM when the overlay is opened for the first time; otherwise this happens when the component mounts. Lazy mounting provides noticeable performance improvements if you have lots of overlays at once, such as on each row of a table.OverlayableProps |
onClose | (event: React.SyntheticEvent<HTMLElement>) => voidA callback that is invoked when user interaction causes the overlay to close, such as clicking on the overlay or pressing the Inherited from esc key (if enabled).OverlayableProps |
onClosed | (node: HTMLElement) => voidLifecycle method invoked just after the CSS _close_ transition ends but before the child has been removed from the DOM. Receives the DOM element of the child being closed. Inherited from OverlayLifecycleProps |
onClosing | (node: HTMLElement) => voidLifecycle method invoked just before the CSS _close_ transition begins on a child. Receives the DOM element of the child being closed. Inherited from OverlayLifecycleProps |
onOpened | (node: HTMLElement) => voidLifecycle method invoked just after the CSS _open_ transition ends. Receives the DOM element of the child being opened. Inherited from OverlayLifecycleProps |
onOpening | (node: HTMLElement) => voidLifecycle method invoked just after mounting the child in the DOM but just before the CSS _open_ transition begins. Receives the DOM element of the child being opened. Inherited from OverlayLifecycleProps |
portalClassName | stringSpace-delimited string of class names applied to the Inherited from Portal element if usePortal={true}.OverlayableProps |
portalContainer | HTMLElement = document.bodyThe container element into which the overlay renders its contents, when Inherited from usePortal is true. This prop is ignored if usePortal is false.OverlayableProps |
portalStopPropagationEventsdeprecated | Array<keyof HTMLElementEventMap>A list of DOM events which should be stopped from propagating through the Portal. This prop is ignored if usePortal is false.this prop's implementation no longer works in React v17+ Inherited from OverlayableProps |
shouldReturnFocusOnClose | boolean = trueWhether the application should return focus to the last active element in the document after this overlay closes. Inherited from OverlayableProps |
transitionDuration | number = 300Indicates how long (in milliseconds) the overlay's enter/leave transition takes. This is used by React Inherited from CSSTransition to know when a transition completes and must match the duration of the animation in CSS. Only set this prop if you override Blueprint's default transitions with new transitions of a different length.OverlayableProps |
transitionName | string = Classes.OVERLAYName of the transition for internal Inherited from CSSTransition. Providing your own name here will require defining new CSS transition properties.OverlayProps |
usePortal | boolean = trueWhether the overlay should be wrapped in a Inherited from Portal, which renders its contents in a new element attached to portalContainer prop.OverlayableProps |