xterm-react
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

XTerm-React

React component wrapper for XTerm.js to simplify integration into any React project supporing React 18.

This project takes heavy insperation from xterm-for-react by robert-harbison and also this Gist.

Getting Started

Installation

You can install XTerm-React using the following commands:

NPM:

npm install xterm-react

Yarn:

yarn add xterm-react

Simple Echo Example

  • Clone the repo git clone https://github.com/reubenmorgan/xterm-react.
  • Open a terminal change to the examples directory in the repo.
  • Run npm i to install the required packages.
  • Start the example with npm start.

Basic Terminal Example

// Import React
import React, { useState } from 'react';

// Import XTerm-React
import { Xterm } from 'xterm-react';

function App() {
	const [Terminal, setTerminal] = useState(null);
	const [input, setInput] = useState('');

	const onTermInit = (term) => {
		setTerminal(term);
		term.reset();
		term.write('Hello from \x1B[1;3;31mxterm.js\x1B[0m $ ');
	};

	const onTermDispose = (term) => {
		setTerminal(null);
	};

	const handleData = (data) => {
		if (Terminal) {
			const code = data.charCodeAt(0);
			// If the user hits empty and there is something typed echo it.
			if (code === 13 && input.length > 0) {
				Terminal.write("\r\nYou typed: '" + input + "'\r\n");
				Terminal.write('echo> ');
				setInput('');
			} else if (code < 32 || code === 127) {
				console.log('Control Key', code);
				// Disable control Keys such as arrow keys
				return;
			} else {
				// Add general key press characters to the terminal
				Terminal.write(data);
				setInput(input + data);
			}
		}
	};

	return (
		<div className="App">
			<header className="App-header">
				<Xterm
					onInit={onTermInit}
					onDispose={onTermDispose}
					onData={handleData}
				/>
			</header>
		</div>
	);
}

export default App;

License

Distributed under the MIT License. See LICENSE for more information.

/xterm-react/

    Package Sidebar

    Install

    npm i xterm-react

    Weekly Downloads

    377

    Version

    1.0.0

    License

    ISC

    Unpacked Size

    38.9 kB

    Total Files

    9

    Last publish

    Collaborators

    • reubenmorgan