JSPM

xterm-react

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 75
  • Score
    100M100P100Q80598F
  • License ISC

React component wrapper for xterm

Package Exports

  • xterm-react
  • xterm-react/dist/index.js
  • xterm-react/dist/index.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (xterm-react) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.