react-xtermjs - a React Library to Build Terminals
Recently, at Qovery, we added a terminal to our UI Console but didn’t find a React library that fit our needs. So, we built react-xtermjs based on the popular XTerm.js library. In this article, I'll explain what XTerm.js is and how to use our library.
XTermJS is an open-source terminal emulator for web applications. It allows users to interact with a shell directly from the browser and supports features like Unicode, ANSI escape codes, and custom keybindings.
Existing Libraries and Their Limitations
There are already a couple of libraries, the most notable being xterm-for-react and react-xterm. However, these libraries have not been updated for many years and do not support hooks and components, limiting their flexibility and usability.
By building react-xtermjs, we wanted to provide an easy-to-use solution that leverages the full capabilities of XTerm.js integrating with React.
How to Use the Library
Here is an example where I load an XTerm addon using react-xtermjs useXTerm hook. XTerm.js provides many add-ons, which you can find here.
JAVASCRIPT
import { FitAddon } from '@xterm/addon-fit'import { useEffect } from 'react'import { useXTerm } from 'react-xtermjs'const TerminalComponent = () => { const { instance, ref } = useXTerm() const fitAddon = new FitAddon() useEffect(() => { // Load the fit addon instance?.loadAddon(fitAddon) const handleResize = () => fitAddon.fit() // Write custom message on your terminal instance?.writeln('Welcome react-xtermjs!') instance?.writeln('This is a simple example using an addon.') // Handle resize event window.addEventListener('resize', handleResize) return () => { window.removeEventListener('resize', handleResize) } }, [ref, instance])
return
}
If you prefer, you can also use the component approach. Check the example here.
Note that the useXTerm hook allows more flexibility with the underlying XTerm.js instance. However, it is more verbose than the XTerm component. You can choose whichever option fits your needs.
Agents ship fast. Guardrails keep them safe.
Qovery ensures every agent action is scoped, audited, and policy-checked. Start deploying in under 10 minutes.
At Qovery, we built a Kubernetes management platform. We needed a terminal in our Web Console (UI) to allow users to connect directly to their running applications via a shell.