snippet
kibo-ui-registry/snippetFreeComponent
Snippet is a component that allows you to display and copy code in a tabbed interface.
Live preview
live · rendered by the registry
Rendered by Kibo UI Registry on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://www.kibo-ui.com/r/snippet.jsonSource
1"use client";23import { CheckIcon, CopyIcon } from "lucide-react";4import {5 type ComponentProps,6 cloneElement,7 type HTMLAttributes,8 type ReactElement,9 useState,10} from "react";11import { Button } from "@/components/ui/button";12import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";13import { cn } from "@/lib/utils";1415export type SnippetProps = ComponentProps<typeof Tabs>;1617export const Snippet = ({ className, ...props }: SnippetProps) => (18 <Tabs19 className={cn(20 "group w-full gap-0 overflow-hidden rounded-md border",21 className22 )}23 {...props}24 />25);2627export type SnippetHeaderProps = HTMLAttributes<HTMLDivElement>;2829export const SnippetHeader = ({ className, ...props }: SnippetHeaderProps) => (30 <div31 className={cn(32 "flex flex-row items-center justify-between border-b bg-secondary p-1",33 className34 )}35 {...props}36 />37);3839export type SnippetCopyButtonProps = ComponentProps<typeof Button> & {40 value: string;41 onCopy?: () => void;42 onError?: (error: Error) => void;43 timeout?: number;44};4546export const SnippetCopyButton = ({47 asChild,48 value,49 onCopy,50 onError,51 timeout = 2000,52 children,53 ...props54}: SnippetCopyButtonProps) => {55 const [isCopied, setIsCopied] = useState(false);5657 const copyToClipboard = () => {58 if (59 typeof window === "undefined" ||60 !navigator.clipboard.writeText ||61 !value62 ) {63 return;64 }6566 navigator.clipboard.writeText(value).then(() => {67 setIsCopied(true);68 onCopy?.();6970 setTimeout(() => setIsCopied(false), timeout);71 }, onError);72 };7374 if (asChild) {75 return cloneElement(children as ReactElement, {76 // @ts-expect-error - we know this is a button77 onClick: copyToClipboard,78 });79 }8081 const icon = isCopied ? <CheckIcon size={14} /> : <CopyIcon size={14} />;8283 return (84 <Button85 className="opacity-0 transition-opacity group-hover:opacity-100"86 onClick={copyToClipboard}87 size="icon"88 variant="ghost"89 {...props}90 >91 {children ?? icon}92 </Button>93 );94};9596export type SnippetTabsListProps = ComponentProps<typeof TabsList>;9798export const SnippetTabsList = TabsList;99100export type SnippetTabsTriggerProps = ComponentProps<typeof TabsTrigger>;101102export const SnippetTabsTrigger = ({103 className,104 ...props105}: SnippetTabsTriggerProps) => (106 <TabsTrigger className={cn("gap-1.5", className)} {...props} />107);108109// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from Kibo UI Registry
All 41 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| announcementannouncement | Kibo UI Registry | Components | Free | no deps | |
| avatar-stackavatar-stack | Kibo UI Registry | Components | Free | no deps | |
| bannerbanner | Kibo UI Registry | Components | Free | 2 deps | |
| calendarcalendar | Kibo UI Registry | Components | Free | 3 deps | |
| choiceboxchoicebox | Kibo UI Registry | Components | Free | 2 deps | |
| code-blockcode-block | Kibo UI Registry | Components | Free | 5 deps · 2 files | |
| color-pickercolor-picker | Kibo UI Registry | Components | Free | 3 deps | |
| comboboxcombobox | Kibo UI Registry | Components | Free | 2 deps |
