Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

fix: add copy button to docs for npm commands

Dillion Vermaf4cc5fe6ead9
diff --git a/components/copy-button.tsx b/components/copy-button.tsx
index 8ee54038..2dce508d 100644
--- a/components/copy-button.tsx
+++ b/components/copy-button.tsx
@@ -2,27 +2,26 @@
import * as React from "react";
import { DropdownMenuTriggerProps } from "@radix-ui/react-dropdown-menu";
+import { CheckIcon, ClipboardIcon } from "lucide-react";
+import { NpmCommands } from "types/unist";
import { Event, trackEvent } from "@/lib/events";
import { cn } from "@/lib/utils";
-import { Button } from "@/components/ui/button";
+import { Button, ButtonProps } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
-// import { NpmCommands } from "types/unist";
-import { Icons } from "@/components/icons";
-
-interface CopyButtonProps extends React.HTMLAttributes<HTMLButtonElement> {
+interface CopyButtonProps extends ButtonProps {
value: string;
src?: string;
event?: Event["name"];
}
-async function copyToClipboardWithMeta(value: string, event?: Event) {
+export async function copyToClipboardWithMeta(value: string, event?: Event) {
navigator.clipboard.writeText(value);
if (event) {
trackEvent(event);
@@ -33,6 +32,7 @@ export function CopyButton({
value,
className,
src,
+ variant = "ghost",
event,
...props
}: CopyButtonProps) {
@@ -47,9 +47,9 @@ export function CopyButton({
return (
<Button
size="icon"
- variant="ghost"
+ variant={variant}
className={cn(
- "relative z-10 size-6 text-zinc-50 hover:bg-background/70 hover:text-zinc-50",
+ "relative z-10 size-6 text-zinc-50 hover:bg-zinc-700 hover:text-zinc-50 [&_svg]:size-3",
className,
)}
onClick={() => {
@@ -59,7 +59,6 @@ export function CopyButton({
? {
name: event,
properties: {
- name: src!,
code: value,
},
}
@@ -70,11 +69,7 @@ export function CopyButton({
{...props}
>
<span className="sr-only">Copy</span>
- {hasCopied ? (
- <Icons.check className="size-3" />
- ) : (
- <Icons.copy className="size-3" />
- )}
+ {hasCopied ? <CheckIcon /> : <ClipboardIcon />}
</Button>
);
}
@@ -116,94 +111,97 @@ export function CopyWithClassNames({
)}
>
{hasCopied ? (
- <Icons.check className="size-3" />
+ <CheckIcon className="size-3" />
) : (
- <Icons.copy className="size-3" />
+ <ClipboardIcon className="size-3" />
)}
<span className="sr-only">Copy</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={() => copyToClipboard(value)}>
- <Icons.react className="mr-2 size-4" />
- <span>Component</span>
+ Component
</DropdownMenuItem>
<DropdownMenuItem onClick={() => copyToClipboard(classNames)}>
- <Icons.tailwind className="mr-2 size-4" />
- <span>Classname</span>
+ Classname
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}
-// interface CopyNpmCommandButtonProps extends DropdownMenuTriggerProps {
-// commands: Required<NpmCommands>;
-// }
-
-// export function CopyNpmCommandButton({
-// commands,
-// className,
-// ...props
-// }: CopyNpmCommandButtonProps) {
-// const [hasCopied, setHasCopied] = React.useState(false);
-
-// React.useEffect(() => {
-// setTimeout(() => {
-// setHasCopied(false);
-// }, 2000);
-// }, [hasCopied]);
-
-// const copyCommand = React.useCallback(
-// (value: string, pm: "npm" | "pnpm" | "yarn") => {
-// copyToClipboardWithMeta(value, {
-// name: "copy_npm_command",
-// properties: {
-// command: value,
-// pm,
-// },
-// });
-// setHasCopied(true);
-// },
-// []
-// );
-
-// return (
-// <DropdownMenu>
-// <DropdownMenuTrigger asChild>
-// <Button
-// size="icon"
-// variant="ghost"
-// className={cn(
-// "relative z-10 h-6 w-6 text-zinc-50 hover:bg-zinc-700 hover:text-zinc-50",
-// className
-// )}
-// >
-// {hasCopied ? (
-// <Icons.check className="h-3 w-3" />
-// ) : (
-// <Icons.copy className="h-3 w-3" />
-// )}
-// <span className="sr-only">Copy</span>
-// </Button>
-// </DropdownMenuTrigger>
-// <DropdownMenuContent align="end">
-// <DropdownMenuItem
-// onClick={() => copyCommand(commands.__npmCommand__, "npm")}
-// >
-// npm
-// </DropdownMenuItem>
-// <DropdownMenuItem
-// onClick={() => copyCommand(commands.__yarnCommand__, "yarn")}
-// >
-// yarn
-// </DropdownMenuItem>
-// <DropdownMenuItem
-// onClick={() => copyCommand(commands.__pnpmCommand__, "pnpm")}
-// >
-// pnpm
-// </DropdownMenuItem>
-// </DropdownMenuContent>
-// </DropdownMenu>
-// );
-// }
+interface CopyNpmCommandButtonProps extends DropdownMenuTriggerProps {
+ commands: Required<NpmCommands>;
+}
+
+export function CopyNpmCommandButton({
+ commands,
+ className,
+ ...props
+}: CopyNpmCommandButtonProps) {
+ const [hasCopied, setHasCopied] = React.useState(false);
+
+ React.useEffect(() => {
+ setTimeout(() => {
+ setHasCopied(false);
+ }, 2000);
+ }, [hasCopied]);
+
+ const copyCommand = React.useCallback(
+ (value: string, pm: "npm" | "pnpm" | "yarn" | "bun") => {
+ copyToClipboardWithMeta(value, {
+ name: "copy_npm_command",
+ properties: {
+ command: value,
+ pm,
+ },
+ });
+ setHasCopied(true);
+ },
+ [],
+ );
+
+ return (
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button
+ size="icon"
+ variant="ghost"
+ className={cn(
+ "relative z-10 size-6 text-zinc-50 hover:bg-zinc-700 hover:text-zinc-50",
+ className,
+ )}
+ >
+ {hasCopied ? (
+ <CheckIcon className="size-3" />
+ ) : (
+ <ClipboardIcon className="size-3" />
+ )}
+ <span className="sr-only">Copy</span>
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuItem
+ onClick={() => copyCommand(commands.__npmCommand__, "npm")}
+ >
+ npm
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ onClick={() => copyCommand(commands.__yarnCommand__, "yarn")}
+ >
+ yarn
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ onClick={() => copyCommand(commands.__pnpmCommand__, "pnpm")}
+ >
+ pnpm
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ onClick={() => copyCommand(commands.__bunCommand__, "bun")}
+ >
+ bun
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ );
+}
diff --git a/components/mdx-components.tsx b/components/mdx-components.tsx
index 3ede65d2..af04f888 100644
--- a/components/mdx-components.tsx
+++ b/components/mdx-components.tsx
@@ -19,7 +19,7 @@ import TweetCard from "@/registry/components/magicui/tweet-card";
import { ComponentInstallation } from "./component-installation";
import { ComponentPreview } from "./component-preview";
import { ComponentSource } from "./component-source";
-import { CopyButton } from "./copy-button";
+import { CopyButton, CopyNpmCommandButton } from "./copy-button";
const CustomLink = (props: any) => {
const href = props.href;
@@ -47,6 +47,11 @@ const components = {
TechStack,
RepoDownload,
TemplatePreview,
+ Image,
+ Tweet: ({ id }: { id: string }) => <TweetCard id={id} className="mx-auto" />,
+ ComponentPreview,
+ ComponentSource: (props: any) => <ComponentSource {...props} />,
+ ComponentInstallation: (props: any) => <ComponentInstallation {...props} />,
h1: ({ className, ...props }: React.HTMLAttributes<HTMLHeadingElement>) => (
<h1
className={cn(
@@ -101,11 +106,6 @@ const components = {
{...props}
/>
),
- Image,
- Tweet: ({ id }: { id: string }) => <TweetCard id={id} className="mx-auto" />,
- ComponentPreview,
- ComponentSource: (props: any) => <ComponentSource {...props} />,
- ComponentInstallation: (props: any) => <ComponentInstallation {...props} />,
a: ({ className, ...props }: React.HTMLAttributes<HTMLAnchorElement>) => (
<CustomLink
className={cn("font-medium underline underline-offset-4", className)}
@@ -216,9 +216,10 @@ const components = {
pre: ({
className,
__rawString__,
- // __npmCommand__,
- // __pnpmCommand__,
- // __yarnCommand__,
+ __npmCommand__,
+ __pnpmCommand__,
+ __yarnCommand__,
+ __bunCommand__,
__withMeta__,
__src__,
__event__,
@@ -228,6 +229,10 @@ const components = {
}: React.HTMLAttributes<HTMLPreElement> & {
// __style__?: Style["name"]
__rawString__?: string;
+ __npmCommand__?: string;
+ __pnpmCommand__?: string;
+ __yarnCommand__?: string;
+ __bunCommand__?: string;
__withMeta__?: boolean;
__src__?: string;
__event__?: Event["name"];
@@ -250,16 +255,20 @@ const components = {
className={cn("absolute right-4 top-4", __withMeta__ && "top-16")}
/>
)}
- {/* {__npmCommand__ && __yarnCommand__ && __pnpmCommand__ && (
- <CopyNpmCommandButton
- commands={{
- __npmCommand__,
- __pnpmCommand__,
- __yarnCommand__,
- }}
- 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/contentlayer.config.ts b/contentlayer.config.ts
index bb7438b9..bd38dfe7 100644
--- a/contentlayer.config.ts
+++ b/contentlayer.config.ts
@@ -11,6 +11,7 @@ import { BlogPosting, WithContext } from "schema-dts";
import { visit } from "unist-util-visit";
import { rehypeComponent } from "./lib/rehype-component";
+import { rehypeNpmCommand } from "./lib/rehype-npm-command";
/** @type {import('contentlayer/source-files').ComputedFields} */
const computedFields = {
@@ -197,6 +198,7 @@ export default makeSource({
}
});
},
+ rehypeNpmCommand,
[
rehypeAutolinkHeadings,
{
diff --git a/lib/rehype-npm-command.ts b/lib/rehype-npm-command.ts
new file mode 100644
index 00000000..46a85b00
--- /dev/null
+++ b/lib/rehype-npm-command.ts
@@ -0,0 +1,66 @@
+import { UnistNode, UnistTree } from "types/unist";
+import { visit } from "unist-util-visit";
+
+export function rehypeNpmCommand() {
+ return (tree: UnistTree) => {
+ visit(tree, (node: UnistNode) => {
+ if (node.type !== "element" || node?.tagName !== "pre") {
+ return;
+ }
+
+ // npm install.
+ if (node.properties?.["__rawString__"]?.startsWith("npm install")) {
+ const npmCommand = node.properties?.["__rawString__"];
+ node.properties["__npmCommand__"] = npmCommand;
+ node.properties["__yarnCommand__"] = npmCommand.replace(
+ "npm install",
+ "yarn add",
+ );
+ node.properties["__pnpmCommand__"] = npmCommand.replace(
+ "npm install",
+ "pnpm add",
+ );
+ node.properties["__bunCommand__"] = npmCommand.replace(
+ "npm install",
+ "bun add",
+ );
+ }
+
+ // npx create.
+ if (node.properties?.["__rawString__"]?.startsWith("npx create-")) {
+ const npmCommand = node.properties?.["__rawString__"];
+ node.properties["__npmCommand__"] = npmCommand;
+ node.properties["__yarnCommand__"] = npmCommand.replace(
+ "npx create-",
+ "yarn create ",
+ );
+ node.properties["__pnpmCommand__"] = npmCommand.replace(
+ "npx create-",
+ "pnpm create ",
+ );
+ node.properties["__bunCommand__"] = npmCommand.replace(
+ "npx",
+ "bunx --bun",
+ );
+ }
+
+ // npx.
+ if (
+ node.properties?.["__rawString__"]?.startsWith("npx") &&
+ !node.properties?.["__rawString__"]?.startsWith("npx create-")
+ ) {
+ const npmCommand = node.properties?.["__rawString__"];
+ node.properties["__npmCommand__"] = npmCommand;
+ node.properties["__yarnCommand__"] = npmCommand;
+ node.properties["__pnpmCommand__"] = npmCommand.replace(
+ "npx",
+ "pnpm dlx",
+ );
+ node.properties["__bunCommand__"] = npmCommand.replace(
+ "npx",
+ "bunx --bun",
+ );
+ }
+ });
+ };
+}
diff --git a/types/unist.ts b/types/unist.ts
index 871b09d4..4ddd8d45 100644
--- a/types/unist.ts
+++ b/types/unist.ts
@@ -27,4 +27,5 @@ export interface NpmCommands {
__npmCommand__?: string;
__yarnCommand__?: string;
__pnpmCommand__?: string;
+ __bunCommand__?: string;
}