Popover
A contextual overlay that displays content when a user interacts with a trigger element. Supports click or hover triggers with multiple positioning options.
Installation
Add Popover to your project with the CK-UI CLI. It copies the source into your project, so the code is yours to change.
npx ck-ui-cli@latest add popover- Adds
Popover.tsxThemeScope.tsxto your CK-UI folder (src/components/ck-ui by default)- Installs
@radix-ui/react-popover@radix-ui/react-slotif your project does not have them yet
First time with CK-UI in this project? Run npx ck-ui-cli@latest init once before adding components. The installation guide explains what it sets up.
Manual installationInstall the packages and copy the files yourself.
Install the following dependencies
Run these commands in your terminal to install the required packages
npm install @radix-ui/react-popover @radix-ui/react-slotAdd the ThemeScope component
Popover imports ./ThemeScope, so add this file next to it
"use client";
import * as React from "react";
import { cn } from "@/lib/utils";
/*
* A local light/dark boundary. Everything inside renders in `theme`, whatever the site theme is: the semantic tokens
* (`bg-background`, `text-foreground`, …) and every `dark:` utility resolve to the nearest boundary, not to <html>.
* Overlays that portal out of the tree (Select, Popover, Tooltip, Dropdown, Modal, AlertDialog, Drawer, Toast) render
* into a layer that carries the same theme, so a menu opened inside a dark scope is dark too.
*
* It never touches <html>, the site's theme state or its storage. Without `theme` it renders no theme class and
* follows the surrounding theme. Nest at most one scope below the site theme (see `darkMode` in tailwind.config.ts).
*/
export type ScopeTheme = "light" | "dark";
const PortalContainerContext = React.createContext<HTMLElement | null>(null);
/** Where overlays portal to: the nearest themed ThemeScope's layer, or null for the default (document.body). */
export function usePortalContainer() {
return React.useContext(PortalContainerContext);
}
// A themed layer at the end of <body> for portalled overlays. Only created while the scope sets a theme, so a scope
// that follows the site leaves the document exactly as it was.
function usePortalLayer(theme: ScopeTheme | undefined) {
const [layer, setLayer] = React.useState<HTMLElement | null>(null);
const themed = theme !== undefined;
React.useEffect(() => {
if (!themed) return;
const element = document.createElement("div");
element.setAttribute("data-theme-scope-layer", "");
document.body.appendChild(element);
setLayer(element);
return () => {
element.remove();
setLayer(null);
};
}, [themed]);
React.useEffect(() => {
if (!layer || !theme) return;
// text-foreground: portalled text that sets no colour of its own resolves against the layer's tokens.
layer.className = `${theme} text-foreground`;
}, [layer, theme]);
return layer;
}
export interface RootProps extends React.HTMLAttributes<HTMLDivElement> {
/** Theme for this subtree. Leave undefined to follow the surrounding (site) theme. */
theme?: ScopeTheme;
}
const Root = React.forwardRef<HTMLDivElement, RootProps>(({ theme, className, children, ...props }, ref) => {
const layer = usePortalLayer(theme);
// A scope that follows its surroundings keeps the surrounding scope's layer.
const outerLayer = usePortalContainer();
return (
<div ref={ref} data-theme-scope={theme} className={cn(theme && [theme, "text-foreground"], className)} {...props}>
<PortalContainerContext.Provider value={theme ? layer : outerLayer}>{children}</PortalContainerContext.Provider>
</div>
);
});
Root.displayName = "ThemeScope.Root";
export { Root };
Create or update your Popover component
Copy this code to create your Popover component
"use client";
import * as React from "react";
import * as PopoverPrimitive from "@radix-ui/react-popover";
import { Slottable } from "@radix-ui/react-slot";
import { cn } from "@/lib/utils";
import { usePortalContainer } from "./ThemeScope";
const useIsomorphicLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
/** Open state that works both controlled (`open` + `onOpenChange`) and uncontrolled (`defaultOpen`). */
function useControllableOpen(open: boolean | undefined, defaultOpen: boolean | undefined, onOpenChange: ((open: boolean) => void) | undefined) {
const [uncontrolledOpen, setUncontrolledOpen] = React.useState(defaultOpen ?? false);
const isControlled = open !== undefined;
const value = isControlled ? open : uncontrolledOpen;
const latest = React.useRef({ value, isControlled, onOpenChange });
useIsomorphicLayoutEffect(() => {
latest.current = { value, isControlled, onOpenChange };
});
const setOpen = React.useCallback((next: boolean) => {
const current = latest.current;
if (next === current.value) return;
if (!current.isControlled) setUncontrolledOpen(next);
current.onOpenChange?.(next);
}, []);
return [value, setOpen] as const;
}
/** Calls the consumer handler first; ours runs unless the consumer prevented the default. */
function composeHandlers<E extends { defaultPrevented: boolean }>(theirs: ((event: E) => void) | undefined, ours: ((event: E) => void) | undefined) {
if (!ours) return theirs;
return (event: E) => {
theirs?.(event);
if (!event.defaultPrevented) ours(event);
};
}
function setRef<T>(ref: React.Ref<T> | undefined, value: T | null) {
if (typeof ref === "function") ref(value);
else if (ref) (ref as React.MutableRefObject<T | null>).current = value;
}
const TABBABLE = 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
// ---------------------------------------------------------------------------------------------
// Sizes
// ---------------------------------------------------------------------------------------------
export type PopoverSize = "sm" | "md" | "lg";
/** @deprecated Use `sm` / `md` / `lg`. */
type LegacyPopoverSize = "small" | "medium" | "large";
const LEGACY_SIZES: Record<LegacyPopoverSize, PopoverSize> = { small: "sm", medium: "md", large: "lg" };
export function resolvePopoverSize(size?: PopoverSize | LegacyPopoverSize | null): PopoverSize {
if (!size) return "md";
return size in LEGACY_SIZES ? LEGACY_SIZES[size as LegacyPopoverSize] : (size as PopoverSize);
}
// `md` keeps the original look. The footer bleeds to the panel edges, so its offsets follow the padding.
const sizeStyles: Record<PopoverSize, { content: string; bodyWithFooter: string; footer: string }> = {
sm: { content: "p-3 size-p-xs", bodyWithFooter: "mb-3", footer: "-mx-3 -mb-3 mt-3 px-3 py-3" },
md: { content: "p-5 size-p-xs", bodyWithFooter: "mb-5", footer: "-mx-5 -mb-5 mt-5 px-5 py-4" },
lg: { content: "p-6 size-p-sm", bodyWithFooter: "mb-6", footer: "-mx-6 -mb-6 mt-6 px-6 py-4" },
};
// ---------------------------------------------------------------------------------------------
// Context
// ---------------------------------------------------------------------------------------------
type OpenReason = "click" | "hover" | "focus";
interface PopoverRootContextValue {
triggerMode: "click" | "hover";
contentRef: React.MutableRefObject<HTMLDivElement | null>;
onTriggerPointerEnter: (event: React.PointerEvent) => void;
onTriggerPointerLeave: (event: React.PointerEvent) => void;
onTriggerFocus: () => void;
onTriggerBlur: (event: React.FocusEvent) => void;
onTriggerClick: (event: React.MouseEvent) => void;
onContentPointerEnter: (event: React.PointerEvent) => void;
onContentPointerLeave: (event: React.PointerEvent) => void;
onContentFocus: () => void;
onContentOpenAutoFocus: (event: Event) => void;
onContentCloseAutoFocus: (event: Event) => void;
}
const PopoverRootContext = React.createContext<PopoverRootContextValue | null>(null);
const PopoverContext = React.createContext<{
hasFooter: boolean;
setHasFooter: React.Dispatch<React.SetStateAction<boolean>>;
size: PopoverSize;
} | null>(null);
// ---------------------------------------------------------------------------------------------
// Root
// ---------------------------------------------------------------------------------------------
export interface RootProps extends React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Root> {
/**
* `click` (default): the trigger toggles the popover and focus moves into it.
* `hover`: opens on mouse hover and on keyboard focus of the trigger without moving focus;
* clicking (or Enter/Space on) the trigger moves focus into the popover.
*/
triggerMode?: "click" | "hover";
/** Delay in ms before opening/closing on mouse hover (hover mode). @default 200 */
hoverDelay?: number;
}
/** Works controlled (`open` + `onOpenChange`) or uncontrolled (`defaultOpen`) in both trigger modes. */
const Root = ({ children, triggerMode = "click", hoverDelay = 200, open: openProp, defaultOpen, onOpenChange, modal }: RootProps) => {
const [open, setOpen] = useControllableOpen(openProp, defaultOpen, onOpenChange);
const openRef = React.useRef(open);
useIsomorphicLayoutEffect(() => {
openRef.current = open;
});
const contentRef = React.useRef<HTMLDivElement | null>(null);
const timerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
const openReasonRef = React.useRef<OpenReason>("click");
const focusInsideRef = React.useRef(false);
const ignoreNextFocusRef = React.useRef(false);
const clearTimer = React.useCallback(() => {
if (timerRef.current) clearTimeout(timerRef.current);
timerRef.current = null;
}, []);
React.useEffect(() => clearTimer, [clearTimer]);
const show = React.useCallback(
(reason: OpenReason) => {
clearTimer();
if (openRef.current) return;
openReasonRef.current = reason;
focusInsideRef.current = false;
setOpen(true);
},
[clearTimer, setOpen]
);
const hide = React.useCallback(() => {
clearTimer();
setOpen(false);
}, [clearTimer, setOpen]);
// Changes coming from Radix: trigger click (click mode), Escape, outside interaction, Popover.Close.
const handleOpenChange = React.useCallback(
(next: boolean) => {
if (next) show("click");
else hide();
},
[show, hide]
);
const context = React.useMemo<PopoverRootContextValue>(() => {
const scheduleShow = () => {
clearTimer();
if (openRef.current) return;
timerRef.current = setTimeout(() => show("hover"), hoverDelay);
};
const scheduleHide = () => {
clearTimer();
timerRef.current = setTimeout(hide, hoverDelay);
};
const focusContent = () => {
const content = contentRef.current;
if (!content) return;
const target = content.querySelector<HTMLElement>(TABBABLE) ?? content;
target.focus();
};
return {
triggerMode,
contentRef,
onTriggerPointerEnter: (event) => {
if (event.pointerType === "mouse") scheduleShow();
},
onTriggerPointerLeave: (event) => {
if (event.pointerType === "mouse") scheduleHide();
},
onTriggerFocus: () => {
if (ignoreNextFocusRef.current) {
ignoreNextFocusRef.current = false;
return;
}
show("focus");
},
onTriggerBlur: (event) => {
const next = event.relatedTarget as Node | null;
if (next && contentRef.current?.contains(next)) return;
hide();
},
onTriggerClick: (event) => {
// Replace Radix's toggle: open (focus moves in) or, when already open, move focus into the content.
event.preventDefault();
if (openRef.current) focusContent();
else show("click");
},
onContentPointerEnter: (event) => {
if (event.pointerType === "mouse") clearTimer();
},
onContentPointerLeave: (event) => {
if (event.pointerType === "mouse") scheduleHide();
},
onContentFocus: () => {
focusInsideRef.current = true;
},
onContentOpenAutoFocus: (event) => {
// Opened by hover or keyboard focus: keep focus where it is.
if (triggerMode === "hover" && openReasonRef.current !== "click") event.preventDefault();
},
onContentCloseAutoFocus: (event) => {
if (triggerMode !== "hover") return;
if (!focusInsideRef.current) {
// Focus never entered the popover: do not move it.
event.preventDefault();
return;
}
// Radix returns focus to the trigger; that focus must not reopen the popover.
ignoreNextFocusRef.current = true;
setTimeout(() => {
ignoreNextFocusRef.current = false;
}, 0);
},
};
}, [triggerMode, hoverDelay, clearTimer, show, hide]);
return (
<PopoverRootContext.Provider value={context}>
<PopoverPrimitive.Root open={open} onOpenChange={handleOpenChange} modal={modal}>
{children}
</PopoverPrimitive.Root>
</PopoverRootContext.Provider>
);
};
Root.displayName = "Popover.Root";
// ---------------------------------------------------------------------------------------------
// Trigger / Anchor
// ---------------------------------------------------------------------------------------------
const Trigger = React.forwardRef<React.ElementRef<typeof PopoverPrimitive.Trigger>, React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Trigger>>(
({ onPointerEnter, onPointerLeave, onFocus, onBlur, onClick, ...props }, ref) => {
const root = React.useContext(PopoverRootContext);
const hover = root?.triggerMode === "hover" ? root : null;
return (
<PopoverPrimitive.Trigger
ref={ref}
{...props}
onPointerEnter={composeHandlers(onPointerEnter, hover?.onTriggerPointerEnter)}
onPointerLeave={composeHandlers(onPointerLeave, hover?.onTriggerPointerLeave)}
onFocus={composeHandlers(onFocus, hover?.onTriggerFocus)}
onBlur={composeHandlers(onBlur, hover?.onTriggerBlur)}
onClick={composeHandlers(onClick, hover?.onTriggerClick)}
/>
);
}
);
Trigger.displayName = "Popover.Trigger";
const Anchor = PopoverPrimitive.Anchor;
// ---------------------------------------------------------------------------------------------
// Content
// ---------------------------------------------------------------------------------------------
type PopoverPosition = "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right" | "left" | "right";
type Side = "top" | "right" | "bottom" | "left";
type Align = "start" | "center" | "end";
const PLACEMENT_BY_POSITION: Record<PopoverPosition, { side: Side; align: Align }> = {
"top-left": { side: "top", align: "start" },
"top-center": { side: "top", align: "center" },
"top-right": { side: "top", align: "end" },
"bottom-left": { side: "bottom", align: "start" },
"bottom-center": { side: "bottom", align: "center" },
"bottom-right": { side: "bottom", align: "end" },
left: { side: "left", align: "center" },
right: { side: "right", align: "center" },
};
const ANIMATION_CLASSES: Record<"fade" | "scale" | "slide", string> = {
fade: "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
scale: "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95",
slide: [
"data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
"data-[side=bottom]:data-[state=open]:slide-in-from-top-2 data-[side=bottom]:data-[state=closed]:slide-out-to-top-2",
"data-[side=left]:data-[state=open]:slide-in-from-right-2 data-[side=left]:data-[state=closed]:slide-out-to-right-2",
"data-[side=right]:data-[state=open]:slide-in-from-left-2 data-[side=right]:data-[state=closed]:slide-out-to-left-2",
"data-[side=top]:data-[state=open]:slide-in-from-bottom-2 data-[side=top]:data-[state=closed]:slide-out-to-bottom-2",
].join(" "),
};
export interface ContentProps extends React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Content> {
showArrow?: boolean;
/** Removes the default surface, padding and typography. */
unstyled?: boolean;
animation?: "fade" | "scale" | "slide";
/** Padding and text size. `small` / `medium` / `large` are deprecated aliases. @default "md" */
size?: PopoverSize | LegacyPopoverSize;
/** Placement shorthand. Explicit `side` / `align` props take precedence. @default "top-center" */
position?: PopoverPosition;
}
/** The popover panel (`role="dialog"`); `className` goes on it. */
const Content = React.forwardRef<React.ElementRef<typeof PopoverPrimitive.Content>, ContentProps>(
(
{
children,
className,
side: sideProp,
align: alignProp,
sideOffset = 12,
collisionPadding = 12,
arrowPadding = 12,
showArrow = true,
unstyled,
animation = "scale",
size,
position = "top-center",
onOpenAutoFocus,
onCloseAutoFocus,
onPointerEnter,
onPointerLeave,
onFocus,
...props
},
forwardedRef
) => {
const root = React.useContext(PopoverRootContext);
const [hasFooter, setHasFooter] = React.useState(false);
const resolvedSize = resolvePopoverSize(size);
const placement = PLACEMENT_BY_POSITION[position];
const hover = root?.triggerMode === "hover" ? root : null;
const ref = React.useCallback(
(node: HTMLDivElement | null) => {
if (root) root.contentRef.current = node;
setRef(forwardedRef, node);
},
[root, forwardedRef]
);
const contextValue = React.useMemo(() => ({ hasFooter, setHasFooter, size: resolvedSize }), [hasFooter, resolvedSize]);
const container = usePortalContainer();
return (
<PopoverContext.Provider value={contextValue}>
<PopoverPrimitive.Portal container={container}>
<PopoverPrimitive.Content
ref={ref}
side={sideProp ?? placement.side}
align={alignProp ?? placement.align}
sideOffset={sideOffset}
collisionPadding={collisionPadding}
arrowPadding={arrowPadding}
onOpenAutoFocus={composeHandlers(onOpenAutoFocus, root?.onContentOpenAutoFocus)}
onCloseAutoFocus={composeHandlers(onCloseAutoFocus, root?.onContentCloseAutoFocus)}
onPointerEnter={composeHandlers(onPointerEnter, hover?.onContentPointerEnter)}
onPointerLeave={composeHandlers(onPointerLeave, hover?.onContentPointerLeave)}
onFocus={composeHandlers(onFocus, root?.onContentFocus)}
className={cn(
"z-popover",
!unstyled && ["w-max rounded-2xl bg-background text-foreground shadow-medium ring-1 ring-border", sizeStyles[resolvedSize].content],
ANIMATION_CLASSES[animation],
// The footer bleeds to the bottom edge.
hasFooter && !unstyled && "pb-0",
className
)}
{...props}
>
<div className={cn(hasFooter && !unstyled && sizeStyles[resolvedSize].bodyWithFooter)}>
<Slottable>{children}</Slottable>
</div>
{showArrow && (
<PopoverPrimitive.Arrow asChild>
<div className="size-[11px] -translate-y-[calc(50%)] -rotate-45 rounded-bl-[3px] border border-border bg-background [clip-path:polygon(0_100%,0_0,100%_100%)]"></div>
</PopoverPrimitive.Arrow>
)}
</PopoverPrimitive.Content>
</PopoverPrimitive.Portal>
</PopoverContext.Provider>
);
}
);
Content.displayName = "Popover.Content";
// ---------------------------------------------------------------------------------------------
// Close / Footer
// ---------------------------------------------------------------------------------------------
const Close = React.forwardRef<
React.ElementRef<typeof PopoverPrimitive.Close>,
React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Close> & {
unstyled?: boolean;
}
>(({ className, unstyled, ...props }, forwardedRef) => (
<PopoverPrimitive.Close ref={forwardedRef} className={cn(!unstyled && "absolute right-4 top-4 rounded-lg focus-ring", className)} {...props} />
));
Close.displayName = "Popover.Close";
type FooterProps = React.HTMLAttributes<HTMLDivElement>;
const Footer = React.forwardRef<HTMLDivElement, FooterProps>(({ className, children, ...props }, ref) => {
const context = React.useContext(PopoverContext);
const setHasFooter = context?.setHasFooter;
const size = context?.size ?? "md";
useIsomorphicLayoutEffect(() => {
if (!setHasFooter) return;
setHasFooter(true);
return () => setHasFooter(false);
}, [setHasFooter]);
return (
<div ref={ref} className={cn("flex items-center justify-between gap-3 rounded-b-2xl border-t border-border bg-muted", sizeStyles[size].footer, className)} {...props}>
{children}
</div>
);
});
Footer.displayName = "Popover.Footer";
export { Root, Anchor, Trigger, Content, Close, Footer };
Test Your Component
- 1. Add the component to a test page
- 2. Check all props and variants work as expected
- 3. Verify responsive behavior
- 4. Test with different content/data
- 5. Ensure accessibility features work correctly
Events
Popovers can be triggered by different events like click or hover, allowing for flexible user interaction patterns.
Positions
Popovers can be positioned in various directions around the trigger element to best fit your layout needs.
API Reference
The Popover component is built on top of the Radix UI Popover primitive and extends its functionality with additional styling options and features.
Popover.Root
The main container component that manages the popover state.
See Radix UI docs for base props. Only extended props shown below.
| Prop | Type | Default | Description |
|---|---|---|---|
triggerMode | "click""hover" | "click" | click: the trigger toggles the popover and focus moves into it. hover: opens on mouse hover and on keyboard focus of the trigger (focus stays on the trigger); click, Enter or Space on the trigger moves focus into the popover. |
hoverDelay | "number" | "200" | Delay in ms before showing/hiding on mouse hover (when triggerMode is 'hover') |
Popover.Trigger
The element that triggers the popover to open when interacted with.
See Radix UI docs for base props.
| Prop | Type | Default | Description |
|---|---|---|---|
asChild | "boolean" | "false" | Merges props onto child element |
Popover.Content
The content displayed when the popover is open.
See Radix UI docs for base props. Only extended props shown below.
| Prop | Type | Default | Description |
|---|---|---|---|
showArrow | "boolean" | "true" | Whether to show the arrow indicator |
unstyled | "boolean" | "false" | Removes the default surface, padding and typography |
animation | "fade""scale""slide" | "scale" | Animation style when opening/closing |
size | "sm""md""lg" | "md" | Padding and text size. small / medium / large are deprecated aliases. |
position | "top-left""top-center""top-right""bottom-left""bottom-center""bottom-right""left""right" | "top-center" | Position relative to the trigger |
Popover.Close
A button that closes the popover when clicked.
See Radix UI docs for base props. Only extended props shown below.
| Prop | Type | Default | Description |
|---|---|---|---|
unstyled | "boolean" | "false" | Removes default styling |