Terminal
@adecore/terminal is a terminal pane on xterm.js in the theme of @adecore/ui. It fits its box, opens links and takes typing or only shows output. The app owns the program behind it. It writes output and reads the screen through a handle.
tsx
import { TerminalView, type TerminalViewHandle } from '@adecore/terminal';
import { useEffect, useRef } from 'react';
const PROMPT = '\x1b[32m~\x1b[0m $ ';
/* A pretend shell that echoes what is typed, since a demo has no program behind it. */
export default function TerminalViewDemo() {
const view = useRef<TerminalViewHandle>(null);
const line = useRef('');
useEffect(() => {
view.current?.write(`\x1b[1mbun test\x1b[0m\r\n\x1b[32m 236 pass\x1b[0m\r\n\x1b[2m 0 fail\x1b[0m\r\nDocs: https://adecore.dev\r\n\r\n${PROMPT}`);
}, []);
const type = (data: string): void => {
for (const key of data) {
if (key === '\r') {
view.current?.write(`\r\n${line.current === '' ? '' : `${line.current}\r\n`}${PROMPT}`);
line.current = '';
} else if (key === '\x7f') {
if (line.current !== '') {
line.current = line.current.slice(0, -1);
view.current?.write('\b \b');
}
} else if (key >= ' ') {
line.current += key;
view.current?.write(key);
}
}
};
return <TerminalView ref={view} label="Demo shell" onData={type} className="h-56 w-full max-w-xl rounded-lg" />;
}What it does
- Fits the grid to its container and reports every new grid, debounced during a drag.
- Follows the theme and the accent of the page, light and dark, without a prop.
- Opens web links and OSC 8 hyperlinks through one handler the app can replace.
- Draws with WebGL when asked, within a budget the page shares between all its terminals.
- Keeps clicks on the right cell under a scaled ancestor, such as a zooming canvas.
What it leaves to the app
The package runs no program and opens no pty. The app starts the process, sends what the person types to it and writes its output to the view. Key bindings, clipboard escapes and a registry of open terminals go through the xterm instance on the handle, so each app wires its own.
Where to next
- Getting started installs the package and wires a first terminal to a process.
- Input and output covers writing, resetting, sizing and several windows on one program.
- Theme and font, Links and WebGL cover the colors and font, link handling and the WebGL renderer.
- Going past the props shows how to reach xterm for what the props leave out.
- Reference lists every prop, every member of the handle and every export.
The package is licensed under FSL-1.1-MIT.