aiellie

Search

Find a component, template or page

GitHub
Components

Code Snippet

A command to copy, with tabs for npm, pnpm, yarn and bun.

npx shadcn@latest add "@aiellie/code-snippet"
Open full screen
fig. 01 · open it full screen from the corner

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

  1. Give it the command

    app/install.tsx
    import { CodeSnippet } from "@/components/aiellie/code-snippet"
    
    export function Install() {
      return <CodeSnippet command="shadcn@latest add button" />
    }
  2. Remember the reader's manager

    Pass manager and onManagerChange to keep the tab, so every snippet on the page agrees.

    app/install.tsx
    const [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.git

API reference

CodeSnippet

PropTypeDefaultDescription
commandstringrequiredWhat follows the runner. With no tabs, the whole line.
managersPackageManager[]["npm", "pnpm", "yarn", "bun"]The tabs. [] for none.
managerPackageManagerThe open tab, to control it.
defaultManagerPackageManagerthe first
onManagerChange(manager: PackageManager) => void

All other div props are forwarded. runWith(manager, command) gives the full line, for copying it elsewhere.