Scrim
A light, unblurred veil over the page behind a panel that is not a modal.
Installation
$
npx shadcn@latest add https://ui.mattdowney.com/r/scrim.jsonUsage
import { Scrim } from "@/components/ui/scrim"Examples
Default
Keep it mounted and drive open from the panel's own state, so it fades out as well as in. While open it catches clicks, so a click on the page closes the panel instead of pressing a dimmed control.
API Reference
| Prop | Type | Default | Description |
|---|---|---|---|
| open* | boolean | None | Shown and catching clicks while true; faded out and click-through while false. Fades on the motion roles; reduced motion drops the fade. |
| onClick | (event) => void | None | Optional. For a panel with no outside-click handling of its own, close it here. |
| open*booleanShown and catching clicks while true; faded out and click-through while false. Fades on the motion roles; reduced motion drops the fade. |
| onClick(event) => voidOptional. For a panel with no outside-click handling of its own, close it here. |
import * as React from "react"
import { cn } from "@/registry/lib/utils"
// @use-when a light veil over the page behind a panel that is not a modal: a
// confirm popover or a side panel that should read as the one live surface
// while the page stays visible and unblurred. Not DialogOverlay or the other
// overlays Dialog, AlertDialog, Sheet and Drawer draw, which blur and come with
// their popup; reach for those when the panel is a real modal.
function Scrim({
open,
className,
...props
}: React.ComponentProps<"div"> & {
/** Shown and catching clicks while true. Keep the Scrim mounted and flip
* this, so the veil fades out as well as in. While open it swallows clicks,
* so a click on the page closes the panel (through the panel's own
* outside-click handling, or an `onClick` here) instead of landing on a
* dimmed control. */
open: boolean
}) {
return (
<div
data-slot="scrim"
data-open={open ? "" : undefined}
aria-hidden
className={cn(
// The kit backdrop's tint (`blurredOverlayClass`, black at 10%) without
// its blur: this is emphasis, not a modal boundary, so the page behind
// stays legible. Black is a primitive, so the alpha is allowed; there
// is no semantic token to fade. Higher contrast settings get a darker
// veil, as the blurred backdrop does.
//
// `z-40`: over the page and its sticky chrome, under the portaled
// popups at `z-50`. Put the panel it sets off at `z-50` too.
//
// The fade is the large role in and the small role out, the same pair
// as the blurred backdrop. Reduced motion drops it.
"fixed inset-0 z-40 bg-black/10 contrast-more:bg-black/30 transition-opacity motion-reduce:transition-none",
open
? "opacity-100 duration-large ease-large"
: "pointer-events-none opacity-0 duration-small ease-small",
className
)}
{...props}
/>
)
}
export { Scrim }