skip to content
mpkcn

~/components/terminal-shell

Terminal Shell

An interactive fake shell: you pass the commands, it handles the prompt, history, tab completion, help, clear and Ctrl+L.

guest@mpkcn: ~
Welcome. Type "help" to list commands.
⏎ run · tab complete · ↑↓ history · ctrl+l clear

Installation

terminal
npx mpkcn add terminal-shell
deps:none beyond react· tested with react@19.3.0

It installs Terminal Window too, which draws the frame.

Usage

Give it your commands. Each one gets the words typed after its name and returns what to print: a string, a React node, or an array of lines.

tsx
"use client";

import { TerminalShell } from "@/components/mpkcn/terminal-shell";

export function Shell() {
  return (
    <TerminalShell
      title="guest@site: ~"
      commands={{
        whoami: { description: "who you are", run: () => "guest" },
        echo: { description: "print text", usage: "echo <text>", run: (args) => args.join(" ") },
        about: () => ["A terminal-style site.", "Type help for more."],
      }}
    />
  );
}

help, clear and history are built in (turn them off with builtins={false}). The prompt handles ↑ ↓ history, Tab completion of command names, Ctrl+L to clear and Ctrl+C to drop the line.

Examples

Async commands

run can return a promise. The input waits until it settles; use ctx.print to show progress first. A thrown error is printed as name: message.

tsx
<TerminalShell
  commands={{
    weather: {
      description: "today's weather",
      run: async ([city = "Tunis"], ctx) => {
        ctx.print(`fetching ${city}…`);
        const res = await fetch(`/api/weather?city=${encodeURIComponent(city)}`);
        return (await res.json()).summary;
      },
    },
  }}
/>

Hidden commands

hidden: true keeps a command out of help and Tab completion: room for easter eggs.

tsx
commands={{ sudo: { hidden: true, run: () => "nice try." } }}

In a popup

The shell renders inline. To open it over the page, put it in your dialog and pass onClose: the title bar gets an "esc" button and Esc in the input calls it too.

tsx
<TerminalShell autoFocus onClose={() => setOpen(false)} commands={commands} />

Props

PropTypeDefaultDescription
commandsRecord<string, ShellCommand | ShellCommand["run"]>{}Your commands by name: { description?, usage?, hidden?, run(args, ctx) }, or just the run function.
builtinsbooleantrueAdd help, clear and history. Yours win on a name clash.
welcomeReactNode[]['Type "help" to list commands.']Lines printed when the shell starts.
promptstring"$"Prompt in front of the input and past commands.
titleReactNode"~ — sh"Text in the title bar.
notFound(name: string) => ShellOutputcommand not foundOutput for an unknown command.
inputLabelstring"Terminal input"Accessible name of the input.
placeholderstring—Placeholder in the empty input.
autoFocusbooleanfalseFocus the input on mount.
hintsbooleantrueShow the key-hint footer.
maxLinesnumber300Most lines kept on screen.
onClose() => void—Adds an "esc" button; Esc in the input calls it.
classNamestring—Extra classes for the window.
screenClassNamestring"h-72"Extra classes for the scrolling screen; set its height here.

run receives ctx: { history: string[]; clear(): void; print(output): void }.

Accessibility

  • The input has a real label ("Terminal input" by default) and the window shows a focus border when it is focused.
  • Output lands in a role="log" region, so screen readers announce new lines as they print.
  • Tab only completes when there is a partial command name; on an empty input it moves focus as usual, so keyboard users are never trapped.
  • Clicking the screen focuses the input, unless you are selecting text to copy.