Code Snippet
A command to copy, with tabs for npm, pnpm, yarn and bun.
npx shadcn@latest add "@aiellie/code-snippet"Installation
npx shadcn@latest add "@aiellie/code-snippet"shadcn needs the @aiellie registry in components.json first. npx aiellie add code-snippet adds it on its first run, then installs the same way.
Code Snippet shows a command to copy, with tabs for npm, pnpm, yarn and bun. You write the command once, like shadcn@latest add button, and each tab puts its own runner in front: npx, pnpm dlx, yarn dlx or bunx. The copy button copies the line on the open tab.
The line is highlighted as shell and scrolls rather than wraps, so a long command stays one line to paste.
Getting started
Give it the command
app/install.tsximport { CodeSnippet } from "@/components/aiellie/code-snippet" export function Install() { return <CodeSnippet command="shadcn@latest add button" /> }Remember the reader's manager
Pass
managerandonManagerChangeto keep the tab, so every snippet on the page agrees.app/install.tsxconst [manager, setManager] = React.useState<PackageManager>("pnpm") <CodeSnippet command="shadcn@latest add button" manager={manager} onManagerChange={setManager} />
Anatomy
<div data-slot="code-snippet">
<div> {/* header */}
<div role="tablist">{/* a tab per manager */}</div>
<button data-slot="code-block-copy" />
</div>
<div role="tabpanel">
<span>$</span>
<code />
</div>
</div>The tabs are Base UI's, so the arrow keys move between them. The highlighting and the copy button come from code-block.
Examples
Without tabs
Pass managers={[]} for a command that isn't run through a package manager. The copy button moves onto the line.
git clone https://github.com/aiellie/elements.gitAPI reference
CodeSnippet
| Prop | Type | Default | Description |
|---|---|---|---|
command | string | required | What follows the runner. With no tabs, the whole line. |
managers | PackageManager[] | ["npm", "pnpm", "yarn", "bun"] | The tabs. [] for none. |
manager | PackageManager | The open tab, to control it. | |
defaultManager | PackageManager | the first | |
onManagerChange | (manager: PackageManager) => void |
All other div props are forwarded. runWith(manager, command) gives the full line, for copying it elsewhere.