shadcn.io is an independent project built on shadcn/ui
On this page5 sections
Installation
Usage
import React from "react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { Presence } from "@/components/ui/presence";const PresenceDemo = () => {
const [present, setPresent] = React.useState(false);
return (
<div className="relative">
<Button
clickEffect={false}
onClick={() => setPresent(!present)}
variant="outline"
>
Toggle
</Button>
<Presence asChild present={present}>
<div
className={cn(
"absolute bottom-full left-1/2 mb-2 -translate-x-1/2",
"px-4 py-2",
"bg-muted",
"text-sm",
"rounded-md border",
"origin-bottom",
"data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-[98%] data-[state=closed]:slide-out-to-bottom-5 data-[state=closed]:animate-out",
"data-[state=open]:fade-in-0 data-[state=open]:zoom-in-[98%] data-[state=open]:slide-in-from-bottom-5 data-[state=open]:animate-in"
)}
>
Content
</div>
</Presence>
</div>
);
};Accessibility
- Enter
- Activate the focused control.
- Space
- Activate the focused control.
FAQ
What does Presence do?
It keeps an element mounted while its exit animation plays, and removes it when the animation ends. Pass present as true or false, and style the data-state of open and closed with your animation classes.
Does Presence render its children before they are shown?
No. lazyMount and unmountOnExit both default to true here, so the content is not rendered until present is first true, and it is removed after the exit animation. Pass lazyMount={false} or unmountOnExit={false} to keep it in the page.
Which animation does it wait for?
The CSS animation on the element while it is in its closed state (data-state="closed"). If the element has no animation there, it is removed at once. The demo uses animate-out classes for the fade and zoom.
How do I animate my own element?
Pass asChild and give Presence one child. It puts its state on that element instead of adding a wrapper, which keeps absolute positioning and the DOM tree as you wrote them.
API Reference
Presence
lazyMount?boolean
Mount the content only once it's first present.
- Default
true
onExitComplete?() → void
Runs once the exit animation has finished.
- Returns
void
present?boolean
Whether the content is shown.
- Default
false
skipAnimationOnMount?boolean
Skip the enter animation when it starts present.
- Default
false
unmountOnExit?boolean
Unmount the content after it animates out; otherwise it stays, hidden.
- Default
true
render?ReactElement | ((props: object, state: object) => ReactElement)
Renders your own element, which gets the state attributes.
- Type
ReactElement | ((props: object, state: object) => ReactElement)