Search for a command to run...
Displays rich content in a portal, triggered by a button.
npx shadcn@latest add @zaidan/popoverpnpx shadcn add @zaidan/popoveryarn dlx shadcn@latest add @zaidan/popoverbunx shadcn@latest add @zaidan/popoverCopy and paste the following code into your project.
1import type { PolymorphicProps } from "@kobalte/core/polymorphic";2import * as PopoverPrimitive from "@kobalte/core/popover";3import type { ComponentProps, ValidComponent } from "solid-js";4import { mergeProps, splitProps } from "solid-js";5import { cn } from "~/lib/utils";6
7type PopoverProps = PopoverPrimitive.PopoverRootProps;8
9const Popover = (props: PopoverProps) => {10 const mergedProps = mergeProps({ gutter: 4, placement: "bottom" } as const, props);11 return <PopoverPrimitive.Root data-slot="popover" {...mergedProps} />;12};13
14type PopoverTriggerProps<T extends ValidComponent = "button"> = PolymorphicProps<15 T,16 PopoverPrimitive.PopoverTriggerProps<T>17>;18
19const PopoverTrigger = <T extends ValidComponent = "button">(props: PopoverTriggerProps<T>) => {20 return <PopoverPrimitive.Trigger data-slot="popover-trigger" {...props} />;21};22
23type PopoverAnchorProps<T extends ValidComponent = "div"> = PolymorphicProps<24 T,25 PopoverPrimitive.PopoverAnchorProps<T>26>;27
28const PopoverAnchor = <T extends ValidComponent = "div">(props: PopoverAnchorProps<T>) => {29 return <PopoverPrimitive.Anchor data-slot="popover-anchor" {...props} />;30};31
32type PopoverContentProps<T extends ValidComponent = "div"> = PolymorphicProps<33 T,34 PopoverPrimitive.PopoverContentProps<T>35> &36 Pick<ComponentProps<T>, "class" | "children">;37
38const PopoverContent = <T extends ValidComponent = "div">(props: PopoverContentProps<T>) => {39 const [local, others] = splitProps(props as PopoverContentProps, ["class", "children"]);40
41 return (42 <PopoverPrimitive.Portal>43 <PopoverPrimitive.Content44 data-slot="popover-content"45 class={cn(46 "z-50 z-popover-content w-72 origin-(--kb-popover-content-transform-origin) outline-hidden",47 local.class,48 )}49 {...others}50 >51 {local.children}52 </PopoverPrimitive.Content>53 </PopoverPrimitive.Portal>54 );55};56
57type PopoverCloseButtonProps<T extends ValidComponent = "button"> = PolymorphicProps<58 T,59 PopoverPrimitive.PopoverCloseButtonProps<T>60> &61 Pick<ComponentProps<T>, "class">;62
63const PopoverCloseButton = <T extends ValidComponent = "button">(64 props: PopoverCloseButtonProps<T>,65) => {66 const [local, others] = splitProps(props as PopoverCloseButtonProps, ["class"]);67 return (68 <PopoverPrimitive.CloseButton69 data-slot="popover-close-button"70 class={cn("z-popover-close-button", local.class)}71 {...others}72 />73 );74};75
76type PopoverHeaderProps = ComponentProps<"div"> & {77 class?: string | undefined;78};79
80const PopoverHeader = (props: PopoverHeaderProps) => {81 const [local, others] = splitProps(props, ["class"]);82 return <div data-slot="popover-header" class={cn("z-popover-header", local.class)} {...others} />;83};84
85type PopoverTitleProps<T extends ValidComponent = "h2"> = PolymorphicProps<86 T,87 PopoverPrimitive.PopoverTitleProps<T>88> &89 Pick<ComponentProps<T>, "class">;90
91const PopoverTitle = <T extends ValidComponent = "h2">(props: PopoverTitleProps<T>) => {92 const [local, others] = splitProps(props as PopoverTitleProps, ["class"]);93 return (94 <PopoverPrimitive.Title95 data-slot="popover-title"96 class={cn("z-popover-title", local.class)}97 {...others}98 />99 );100};101
102type PopoverDescriptionProps<T extends ValidComponent = "p"> = PolymorphicProps<103 T,104 PopoverPrimitive.PopoverDescriptionProps<T>105> &106 Pick<ComponentProps<T>, "class">;107
108const PopoverDescription = <T extends ValidComponent = "p">(props: PopoverDescriptionProps<T>) => {109 const [local, others] = splitProps(props as PopoverDescriptionProps, ["class"]);110 return (111 <PopoverPrimitive.Description112 data-slot="popover-description"113 class={cn("z-popover-description", local.class)}114 {...others}115 />116 );117};118
119type PopoverArrowProps<T extends ValidComponent = "div"> = PolymorphicProps<120 T,121 PopoverPrimitive.PopoverArrowProps<T>122> &123 Pick<ComponentProps<T>, "class">;124
125const PopoverArrow = <T extends ValidComponent = "div">(props: PopoverArrowProps<T>) => {126 const [local, others] = splitProps(props as PopoverArrowProps, ["class"]);127 return (128 <PopoverPrimitive.Arrow129 data-slot="popover-arrow"130 class={cn("z-popover-arrow", local.class)}131 {...others}132 />133 );134};135
136export {137 Popover,138 PopoverAnchor,139 PopoverArrow,140 PopoverCloseButton,141 PopoverContent,142 PopoverDescription,143 PopoverHeader,144 PopoverTitle,145 PopoverTrigger,146};Here are the source code of all the examples from the preview page:
import { Button } from "~/components/ui/button";import { Popover, PopoverContent, PopoverDescription, PopoverHeader, PopoverTitle, PopoverTrigger,} from "~/components/ui/popover";function PopoverBasic() { return ( <Example title="Basic"> <Popover placement="bottom-start"> <PopoverTrigger as={Button} variant="outline" class="w-fit"> Open Popover </PopoverTrigger> <PopoverContent class="w-64"> <PopoverHeader> <PopoverTitle>Dimensions</PopoverTitle> <PopoverDescription>Set the dimensions for the layer.</PopoverDescription> </PopoverHeader> </PopoverContent> </Popover> </Example> );}import { Button } from "~/components/ui/button";import { Field, FieldGroup, FieldLabel } from "~/components/ui/field";import { Input } from "~/components/ui/input";import { Popover, PopoverContent, PopoverDescription, PopoverHeader, PopoverTitle, PopoverTrigger,} from "~/components/ui/popover";function PopoverWithForm() { return ( <Example title="With Form"> <Popover> <PopoverTrigger as={Button} variant="outline"> Open Popover </PopoverTrigger> <PopoverContent class="w-64"> <PopoverHeader> <PopoverTitle>Dimensions</PopoverTitle> <PopoverDescription>Set the dimensions for the layer.</PopoverDescription> </PopoverHeader> <FieldGroup class="gap-4"> <Field orientation="horizontal"> <FieldLabel for="width" class="w-1/2"> Width </FieldLabel> <Input id="width" value="100%" /> </Field> <Field orientation="horizontal"> <FieldLabel for="height" class="w-1/2"> Height </FieldLabel> <Input id="height" value="25px" /> </Field> </FieldGroup> </PopoverContent> </Popover> </Example> );}import { Button } from "~/components/ui/button";import { Popover, PopoverContent, PopoverTrigger,} from "~/components/ui/popover";function PopoverAlignments() { return ( <Example title="Alignments"> <div class="flex gap-6"> <Popover placement="bottom-start"> <PopoverTrigger as={Button} variant="outline" size="sm"> Start </PopoverTrigger> <PopoverContent class="w-40">Aligned to start</PopoverContent> </Popover> <Popover placement="bottom"> <PopoverTrigger as={Button} variant="outline" size="sm"> Center </PopoverTrigger> <PopoverContent class="w-40">Aligned to center</PopoverContent> </Popover> <Popover placement="bottom-end"> <PopoverTrigger as={Button} variant="outline" size="sm"> End </PopoverTrigger> <PopoverContent class="w-40">Aligned to end</PopoverContent> </Popover> </div> </Example> );}import { Button } from "~/components/ui/button";import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger,} from "~/components/ui/dialog";import { Popover, PopoverContent, PopoverDescription, PopoverHeader, PopoverTitle, PopoverTrigger,} from "~/components/ui/popover";function PopoverInDialog() { return ( <Example title="In Dialog"> <Dialog> <DialogTrigger as={Button} variant="outline"> Open Dialog </DialogTrigger> <DialogContent> <DialogHeader> <DialogTitle>Popover Example</DialogTitle> <DialogDescription>Click the button below to see the popover.</DialogDescription> </DialogHeader> <Popover placement="bottom-start"> <PopoverTrigger as={Button} variant="outline" class="w-fit"> Open Popover </PopoverTrigger> <PopoverContent> <PopoverHeader> <PopoverTitle>Popover in Dialog</PopoverTitle> <PopoverDescription> This popover appears inside a dialog. Click the button to open it. </PopoverDescription> </PopoverHeader> </PopoverContent> </Popover> </DialogContent> </Dialog> </Example> );}