Skip to content

Terminal

Terminal UI component that allows users to interact with the terminal-like interface.

import {Terminal} from '@kaiverse/k/ui'

Try command hello and then clear in the terminal below:

Crafting demo...

You can provide your custom handler by using commandHandler prop.
Your function should returns "skip_default" if the command is manually handled and/or you wanna skip the default handler.
Otherwise, commands will be executed by it.

  • clear or cls - clear the terminal
  • Others - println: “command not found”.

commandHandler prop also has a collection of built-in helpers (2nd argument), you can call it to manipulate the terminal’s history (log) section.

Crafting demo...

Prints a ReactNode to Terminal history section.

Prints a string with a newline.

Clears the terminal history. Same as the default clear or cls command.

The greeting prop accept a ReactNode.

Crafting demo...
greetings.tsx
<Terminal
windowTitle="¯\_(ツ)_/¯"
greeting={<img width="120" height="120" src="https://i.redd.it/fktuppkre7p51.gif" />}
/>

Switch between 2 themes: macos (default) and window by using the theme prop.

Crafting demo...
styling.tsx
<Terminal theme="window" />

You can also hide the window controls by setting hideWindowCtrls to true.

classNames and styles are 2 more available options for styling the Terminal component.
See TerminalStylingSelectors in the Special Types section below for available selectors.

Crafting demo...
styling.tsx
<Terminal
className="bg-black dark:bg-slate-600"
classNames={{
windowHeader:
'bg-[radial-gradient(circle,rgba(34,193,195,0.4)_0%,rgba(253,187,45,0.2)_100%)] dark:bg-slate-700',
commandForm: '[&>span]:text-amber-600',
}}
/>
NameTypeDescription
PrintlnFn(text: string) => voidPrint the inputted string.
PrintNodeFn(node: ReactNode) => voidPrint the inputted ReactNode.
TerminalHelpers{println: PrintlnFn; printNode: PrintNodeFn; clearHistory: () => void}A collection of helper functions.
TerminalRefTerminalProps & TerminalHelpersAll accepted props and helper functions.
TerminalStylingSelectors'windowHeader' | 'historySection' | 'commandForm'Available selectors of special styles APIs: classNames, styles.
NameTypeDefaultDescription
classNamesPartial<Record<TerminalStylingSelectors, string>>Applies className to related selector element.
stylesPartial<Record<TerminalStylingSelectors, CSSProperties>>Applies inline styles to related selector element.
windowTitlestringTerminal window title.
greetingReactNodeGreating section. It will be printed on top of terminal’s history section.
theme'macos' | 'window''macos'
commandPrefixstring'$' ('>' if theme is 'window')
commandHandler(command: string, helper: TerminalHelpers) => 'skip_default' | voidProvide custom command handler. Returns "skip_default" if the command is manually handled and/or you wanna skip the default handler.
Otherwise, commands will be executed by default handler.
hideWindowCtrlsbooleanfalseHide window controls.
...htmlAttributesOmit<HTMLAttributes<HTMLDivElement>, 'onClick'>Others HTML attributes.
Note: onClick event handler is not supported.