A semantic command line element that allows for both inline and block commands and supports copying commands to the clipboard.
<Text className="mt-4"> Here's the inline component in action: <CommandLine command={`npx cowsay Hello world!`} /></Text>
Here's the inline component, with a string, in action: npx cowsay Hello world!
<Text className="mt-4">Here's the inline component with an array in action:{' '}<CommandLine command={[ { type: 'command', text: 'npx cowsay Hello world!' }, { type: 'log', text: ` ______________< Hello world! > -------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||`, }, { type: 'command', text: 'npx prisma db seed' }, { type: 'log', text: `Need to install the following packages: prisma@5.3.0Ok to proceed? (y) y`, }, { type: 'command', text: 'npm run dev' }, ]}/>
Here's the inline component, with an array, in action: npx cowsay Hello world!
npx prisma db seed
npm run dev
Notice output is ignored in this mode.
<Text className="mt-4">Here's the block component in action.</Text><CommandLine variant="block" command={`npx cowsay Hello world!`} />
Here's the block component, with a string, in action.
~npx cowsay Hello world!
<Text className="mt-4">Here's the block component with an array in action.</Text> <CommandLine variant="block" command={[ { type: 'command', text: 'npx cowsay Hello world!' }, { type: 'log', text: ` ______________< Hello world! > -------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||`, }, { type: 'command', text: 'npx prisma db seed' }, { type: 'log', text: `Need to install the following packages: prisma@5.3.0Ok to proceed? (y) y`, }, { type: 'command', text: 'npm run dev' }, ]} />
Here's the block component, with an array, in action.
~npx cowsay Hello world!
______________ < Hello world! > -------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
~npx prisma db seed
Need to install the following packages: prisma@5.3.0 Ok to proceed? (y) y
~npm run dev
Prop | Type | Default |
---|---|---|
variant | 'inline' | 'block' | 'inline' |
command | string | { type: 'command' | 'log'; text: string }[] | '' |
import { useState, type HTMLAttributes, useRef, useEffect } from 'react'import { cva, type VariantProps } from 'class-variance-authority'import { cn } from '~/utils/tailwind-merge.ts'import { useCopyToClipboard } from 'usehooks-ts'import { AccessibleIcon } from '~/components/ui/accessible-icon.tsx'import { Tooltip } from '~/routes/_marketing+/ui+/components/ui/tooltip.tsx'
const commandLineVariants = cva('notranslate rounded-lg overflow-hidden text-white bg-[rgb(24,46,63)]', { variants: { variant: { inline: 'inline px-1 py-0.5', block: '', }, },})
export type CommandAndLogArray = { type: 'command' | 'log'; text: string }[]
type CommandLineProps = { command: string | CommandAndLogArray } & VariantProps<typeof commandLineVariants> & HTMLAttributes<HTMLPreElement> & { children?: never // Prevent receiving children }
export const CommandLine = ({ command = '', variant = 'inline', className, ...props }: CommandLineProps) => { // Copy to clipboard const [showCopyIcon, setShowCopyIcon] = useState(true) const [, copy] = useCopyToClipboard() // Extract commands const lines = Array.isArray(command) ? command : [{ type: 'command', text: command }] const linesWithoutLogs = lines.filter(line => line.type === 'command') const allCommandsString = linesWithoutLogs.map(line => line.text).join('\n') // Code overflows const ref = useRef<HTMLDivElement>(null) const [codeOverflows, setCodeOverflows] = useState(false)
const handleCopy = (commandToCopy: string) => { copy(commandToCopy) if (showCopyIcon) { setShowCopyIcon(false) setTimeout(() => setShowCopyIcon(true), 1000) } }
useEffect(() => { if (ref.current) { setCodeOverflows(ref.current.offsetHeight < ref.current.scrollHeight || ref.current.offsetWidth < ref.current.scrollWidth) } }, [ref])
return variant === 'block' ? ( <pre className={cn(commandLineVariants({ variant }), className)} {...props}> <div className="flex overflow-hidden"> <div ref={ref} className="flex-grow overflow-x-scroll"> {lines.map(({ type, text }, i) => ( <div key={i} className="flex gap-4"> {type === 'command' ? ( <div className="flex select-none items-stretch justify-stretch"> <div className="bg-blue-500 pl-2 pr-1.5">~</div> <div> <svg viewBox="0 0 30 100" className="h-full"> <polygon points="0,0 30,50 0,100" className="fill-blue-500" /> </svg> </div> </div> ) : null} <code className="pr-4">{text}</code> </div> ))} </div> <div className={codeOverflows ? `shadow-[rgba(0,0,15,0.5)_-10px_0_5px_0]` : ``}> {linesWithoutLogs.length > 0 ? ( <button className={`min-h-tap min-w-tap rounded-lg px-4 hover:bg-muted-100/10 ${lines.length > 1 ? 'py-2' : ''}`} onClick={() => handleCopy(allCommandsString)}> {showCopyIcon ? <AccessibleIcon name="copy" label="Copy to clipboard" /> : <AccessibleIcon name="check" label="Saved to clipboard" />} </button> ) : null} </div> </div> </pre> ) : ( linesWithoutLogs.map(({ text }, i) => ( <Tooltip key={text} content={showCopyIcon ? 'Click to copy to clipboard' : 'Saved to clipboard!'}> <span onClick={() => handleCopy(text)} className="cursor-pointer"> <pre className={cn(commandLineVariants({ variant }), className)} {...props}> <code>{text}</code> </pre> {linesWithoutLogs.length > 1 && i < linesWithoutLogs.length - 1 ? ' ' : ''} </span> </Tooltip> )) )}
This component is made for developers, so the styling is not based on CSS variables a designer can control, but it's built-in into the component and you can change it however you like. I made it look like the Cobalt2 powerline, which is what I use in my VS Code.