UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
feat: new command copy component (#535)
Dillion Verma8e55ea31f73f
diff --git a/app/layout.tsx b/app/layout.tsxindex 55de6c03..b3502678 100644--- a/app/layout.tsx+++ b/app/layout.tsx@@ -1,10 +1,11 @@-import { fontMono, fontSans } from "@/lib/fonts";-import { absoluteUrl, cn, constructMetadata } from "@/lib/utils";-import { Toaster } from "@/components/ui/sonner";-import { TooltipProvider } from "@/components/ui/tooltip";import { Analytics } from "@/components/analytics";import { PHProvider } from "@/components/posthog-provider";import { ThemeProvider } from "@/components/theme-provider";+import { Toaster } from "@/components/ui/sonner";+import { TooltipProvider } from "@/components/ui/tooltip";+import { fontMono, fontSans } from "@/lib/fonts";+import { absoluteUrl, cn, constructMetadata } from "@/lib/utils";+import { Provider as JotaiProvider } from "jotai";import "@/styles/globals.css";import "@/styles/mdx.css";@@ -42,15 +43,17 @@ export default function RootLayout({fontMono.variable,)}>- <PHProvider>- <ThemeProvider attribute="class" defaultTheme="light">- <TooltipProvider>- {children}- <Toaster />- <Analytics />- </TooltipProvider>- </ThemeProvider>- </PHProvider>+ <JotaiProvider>+ <PHProvider>+ <ThemeProvider attribute="class" defaultTheme="light">+ <TooltipProvider>+ {children}+ <Toaster />+ <Analytics />+ </TooltipProvider>+ </ThemeProvider>+ </PHProvider>+ </JotaiProvider></body></html>);diff --git a/components/code-block-command.tsx b/components/code-block-command.tsxnew file mode 100644index 00000000..2aa91532--- /dev/null+++ b/components/code-block-command.tsx@@ -0,0 +1,113 @@+"use client";++import { copyToClipboardWithMeta } from "@/components/copy-button";+import { Button } from "@/components/ui/button";+import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";+import { useConfig } from "@/hooks/use-config";+import { useMounted } from "@/hooks/use-mounted";+import { NpmCommands } from "@/types/unist";+import { CheckIcon, ClipboardIcon } from "lucide-react";+import * as React from "react";++export function CodeBlockCommand({+ __npmCommand__,+ __yarnCommand__,+ __pnpmCommand__,+ __bunCommand__,+}: React.ComponentProps<"pre"> & NpmCommands) {+ const [config, setConfig] = useConfig();+ const [hasCopied, setHasCopied] = React.useState(false);+ const mounted = useMounted();++ React.useEffect(() => {+ if (hasCopied) {+ const timer = setTimeout(() => setHasCopied(false), 2000);+ return () => clearTimeout(timer);+ }+ }, [hasCopied]);++ const packageManager = config.packageManager || "pnpm";+ const tabs = React.useMemo(() => {+ return {+ pnpm: __pnpmCommand__,+ npm: __npmCommand__,+ yarn: __yarnCommand__,+ bun: __bunCommand__,+ };+ }, [__npmCommand__, __pnpmCommand__, __yarnCommand__, __bunCommand__]);++ const copyCommand = React.useCallback(() => {+ const command = tabs[packageManager];++ if (!command) {+ return;+ }++ copyToClipboardWithMeta(command, {+ name: "copy_npm_command",+ properties: {+ command,+ pm: packageManager,+ },+ });+ setHasCopied(true);+ }, [packageManager, tabs]);++ if (!mounted) {+ return null;+ }++ return (+ <div className="relative mt-6 max-h-[650px] overflow-x-auto rounded-xl bg-zinc-950 dark:bg-zinc-900">+ <Tabs+ defaultValue={packageManager}+ onValueChange={(value) => {+ console.log("value", value, packageManager);+ setConfig({+ ...config,+ packageManager: value as "pnpm" | "npm" | "yarn" | "bun",+ });+ }}+ >+ <div className="flex items-center justify-between border-b border-zinc-800 bg-zinc-900 px-3 pt-2.5">+ <TabsList className="h-7 translate-y-[2px] gap-3 bg-transparent p-0 pl-1">+ {Object.entries(tabs).map(([key, value]) => {+ return (+ <TabsTrigger+ key={key}+ value={key}+ className="rounded-none border-b border-transparent bg-transparent p-0 pb-1.5 font-mono text-zinc-400 data-[state=active]:border-b-zinc-50 data-[state=active]:bg-transparent data-[state=active]:text-zinc-50"+ >+ {key}+ </TabsTrigger>+ );+ })}+ </TabsList>+ </div>+ {Object.entries(tabs).map(([key, value]) => {+ return (+ <TabsContent key={key} value={key} className="mt-0">+ <pre className="px-4 py-5">+ <code+ className="relative font-mono text-sm leading-none"+ data-language="bash"+ >+ {value}+ </code>+ </pre>+ </TabsContent>+ );+ })}+ </Tabs>+ <Button+ size="icon"+ variant="ghost"+ className="absolute right-2.5 top-2 z-10 h-6 w-6 text-zinc-50 hover:bg-zinc-700 hover:text-zinc-50 [&_svg]:h-3 [&_svg]:w-3"+ onClick={copyCommand}+ >+ <span className="sr-only">Copy</span>+ {hasCopied ? <CheckIcon /> : <ClipboardIcon />}+ </Button>+ </div>+ );+}diff --git a/components/mdx-components.tsx b/components/mdx-components.tsxindex 756a87ba..c80a3f8f 100644--- a/components/mdx-components.tsx+++ b/components/mdx-components.tsx@@ -1,4 +1,5 @@import { Callout } from "@/components/callout";+import { CodeBlockCommand } from "@/components/code-block-command";import RepoDownload from "@/components/repo-download";import TechStack from "@/components/tech-stack";import TemplatePreview from "@/components/template-preview";@@ -17,7 +18,7 @@ import Image from "next/image";import Link from "next/link";import { ComponentPreview } from "./component-preview";import { ComponentSource } from "./component-source";-import { CopyButton, CopyNpmCommandButton } from "./copy-button";+import { CopyButton } from "./copy-button";const CustomLink = (props: any) => {const href = props.href;@@ -242,11 +243,9 @@ const components = {__withMeta__,__src__,__event__,- // __style__,__name__,...props}: React.HTMLAttributes<HTMLPreElement> & {- // __style__?: Style["name"]__rawString__?: string;__npmCommand__?: string;__pnpmCommand__?: string;@@ -257,6 +256,20 @@ const components = {__event__?: Event["name"];__name__?: string;}) => {+ const isNpmCommand =+ __npmCommand__ && __yarnCommand__ && __pnpmCommand__ && __bunCommand__;++ if (isNpmCommand) {+ return (+ <CodeBlockCommand+ __npmCommand__={__npmCommand__}+ __yarnCommand__={__yarnCommand__}+ __pnpmCommand__={__pnpmCommand__}+ __bunCommand__={__bunCommand__}+ />+ );+ }+return (<><pre@@ -274,20 +287,6 @@ const components = {className={cn("absolute right-4 top-4", __withMeta__ && "top-16")}/>)}- {__npmCommand__ &&- __yarnCommand__ &&- __pnpmCommand__ &&- __bunCommand__ && (- <CopyNpmCommandButton- commands={{- __npmCommand__,- __pnpmCommand__,- __yarnCommand__,- __bunCommand__,- }}- className={cn("absolute right-4 top-4", __withMeta__ && "top-16")}- />- )}</>);},diff --git a/components/toc.tsx b/components/toc.tsxindex 67a94a0a..fd7be2ed 100644--- a/components/toc.tsx+++ b/components/toc.tsx@@ -2,8 +2,8 @@import { useEffect, useMemo, useState } from "react";+import { useMounted } from "@/hooks/use-mounted";import type { TableOfContents } from "@/lib/toc";-import { useMounted } from "@/lib/use-mounted";import { cn } from "@/lib/utils";interface TocProps {diff --git a/hooks/use-config.ts b/hooks/use-config.tsnew file mode 100644index 00000000..57c1d44a--- /dev/null+++ b/hooks/use-config.ts@@ -0,0 +1,14 @@+import { useAtom } from "jotai";+import { atomWithStorage } from "jotai/utils";++type Config = {+ packageManager: "npm" | "yarn" | "pnpm" | "bun";+};++const configAtom = atomWithStorage<Config>("config", {+ packageManager: "pnpm",+});++export function useConfig() {+ return useAtom(configAtom);+}diff --git a/lib/use-mounted.ts b/hooks/use-mounted.tssimilarity index 100%rename from lib/use-mounted.tsrename to hooks/use-mounted.tsdiff --git a/package.json b/package.jsonindex 3314b73d..3ab2559e 100644--- a/package.json+++ b/package.json@@ -56,6 +56,7 @@"date-fns": "^2.30.0","geist": "^1.2.2","gray-matter": "^4.0.3",+ "jotai": "^2.11.3","lodash.template": "^4.5.0","lucide-react": "^0.401.0","motion": "^11.15.0",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex bb54b222..b6f97f52 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -104,6 +104,9 @@ importers:gray-matter:specifier: ^4.0.3version: 4.0.3+ jotai:+ specifier: ^2.11.3+ version: 2.11.3(@types/[email protected])([email protected])lodash.template:specifier: ^4.5.0version: 4.5.0@@ -3704,6 +3707,18 @@ packages:resolution: {integrity: sha512-gFqAIbuKyyso/3G2qhiO2OM6shY6EPP/R0+mkDbyspxKazh8BXDC5FiFsUjlczgdNz/vfra0da2y+aHrusLG/Q==}hasBin: true+ resolution: {integrity: sha512-B/PsewAQ0UOS5e2+TTWegUPQ3SCLPCjPY24LYUjfn2EorGlluTA2dFjVLgF1+xHLjK9Jit3y5mKHyMG3Xq/GZg==}+ engines: {node: '>=12.20.0'}+ peerDependencies:+ '@types/react': '>=17.0.0'+ react: '>=17.0.0'+ peerDependenciesMeta:+ '@types/react':+ optional: true+ react:+ optional: true+resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==}@@ -9649,6 +9664,11 @@ snapshots:+ optionalDependencies:+ '@types/react': 18.2.12+ react: 18.3.1+