Clawscale

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.

Overlay is deprecated

Blueprint deprecated the original Overlay component. Use Overlay2.

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 ref needs nothing.
  • If the child has a ref, pass the same ref object to childRef.
  • With several children, pass childRefs, keyed by each child's key.

Interactive playground#

Props
View source on GitHub

Props interface#

interface Overlay2Props extends OverlayProps, React.RefAttributes<OverlayInstance>@clawscale/react
PropsDescription
autoFocus
boolean = true
Whether the overlay should acquire application focus when it first opens.
Inherited from OverlayableProps
backdropClassName
string
CSS 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 = true
Whether pressing the esc key should invoke onClose.
Inherited from OverlayableProps
canOutsideClickClose
boolean = true
Whether clicking outside the overlay element (either on backdrop when present or on document) should invoke onClose.
Inherited from 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.ReactNode
Element to overlay.
Inherited from OverlayProps
className
string
A space-delimited list of class names to pass along to a child element.
Inherited from Props
enforceFocus
boolean = true
Whether 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 usePortal={false}.
Inherited from OverlayableProps
hasBackdrop
boolean = true
Whether a container-spanning backdrop element should be rendered behind the contents. When false, users will be able to scroll through and interact with overlaid content.
Inherited from BackdropProps
isOpenrequired
boolean
Toggles the visibility of the overlay and its children. This prop is required because the component is controlled.
Inherited from OverlayProps
lazy
boolean = true
If 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.
Inherited from OverlayableProps
onClose
(event: React.SyntheticEvent<HTMLElement>) => void
A callback that is invoked when user interaction causes the overlay to close, such as clicking on the overlay or pressing the esc key (if enabled).
Inherited from OverlayableProps
onClosed
(node: HTMLElement) => void
Lifecycle 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) => void
Lifecycle 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) => void
Lifecycle method invoked just after the CSS _open_ transition ends. Receives the DOM element of the child being opened.
Inherited from OverlayLifecycleProps
onOpening
(node: HTMLElement) => void
Lifecycle 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
string
Space-delimited string of class names applied to the Portal element if usePortal={true}.
Inherited from OverlayableProps
portalContainer
HTMLElement = document.body
The container element into which the overlay renders its contents, when usePortal is true. This prop is ignored if usePortal is false.
Inherited from 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 = true
Whether the application should return focus to the last active element in the document after this overlay closes.
Inherited from OverlayableProps
transitionDuration
number = 300
Indicates how long (in milliseconds) the overlay's enter/leave transition takes. This is used by React 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.
Inherited from OverlayableProps
transitionName
string = Classes.OVERLAY
Name of the transition for internal CSSTransition. Providing your own name here will require defining new CSS transition properties.
Inherited from OverlayProps
usePortal
boolean = true
Whether the overlay should be wrapped in a Portal, which renders its contents in a new element attached to portalContainer prop.
Inherited from OverlayableProps