Skip to content

Terminal

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

Import

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

Usage

Try command hello and then clear in the terminal below:

Terminal

T E __ __ __ R / \ / \ ____ | | ____ ____ _____ ____ M \ \/\/ // __ \| | _/ ___\/ _ \ / \_/ __ \ I \ /\ ___/| |_\ \__( <_> ) Y Y \ ___/ N \__/\ / \___ >____/\___ >____/|__|_| /\___ > A \/ \/ \/ \/ \/ L Try command hello and then clear:

$

Handle Command

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.

Default Commands

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

Helpers

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

Helpers that ship with the commandHandler prop:
printNode - prints a React node
println - prints a string with a newline
clearHistory - clears the terminal history
Try command print or p to log messages, and then clear.

$

You can also access these helpers via ref as well.

printNode

Prints a ReactNode to Terminal history section.

println

Prints a string with a newline.

clearHistory

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

Customization

Title & greatings

The greeting prop accept a ReactNode.

¯\_(ツ)_/¯

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

Theme

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

Terminal (window) | Lorem ipsum dolor, sit amet consectetur adipisicing elit. Excepturi eum ab deleniti, non praesentium aliquid voluptas eos illo corrupti corporis minus

This is window theme
>
styling.tsx
<Terminal theme="window" />

Hide window controls

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

Styling Elements

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

Terminal Title ✨

🎨 You can use classNames prop to style <Terminal /> component like this:
<Terminal
  classNames={{
    windowHeader: '...',
    historySection: '...',
    commandForm: '...',
  }}
/>
$
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-yellow-600',
}}
/>

Special Types

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.

<Terminal> Props

NameTypeDefaultDescription
classNamesPartial<Record<TerminalStylingSelectors, string>>Applies className to related selector element.
stylesPartial<Record<TerminalStylingSelectors, CSSProperties>>Applies inline styles to related selector element.
titlestringTerminal 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.