Git Product home page Git Product logo

xterm-react's Introduction

XTerm for React

This project provides a React component that integrates the xterm.js terminal emulator library. It aims to offer a more up-to-date and flexible alternative to existing solutions(last commit Jul 8, 2022), with a focus on performance, code style, and additional functionality.

Live Example

Usage

Installation

To install the component, use package manager like npm, yarn or pnpm:

npm install xterm-react
# or
yarn add xterm-react
# or
pnpm add xterm-react

Code Example

Import the XTerm component and use it within your React application:

import React from "react";
import { XTerm } from "@pablo-lion/xterm-react";

export default function App() {
  return <XTerm />;
}

Docs

For the documentation of the XTerm component, check XTerm-React Docs.

Props

The XTerm component accepts several props to customize its behavior, including options for terminal options, addons for loading XTerm addons, and various event handlers like onData, onResize, and more. I'll add a full docs later.

Motivations

  • Personal Use: Tailored for my own personal requirements and projects.
  • Updated Dependencies:
    • Utilizes the latest @xterm/xterm version than xterm to address issues with deprecated add-ons, such as the non-functional xterm-addon-fit in resizing terminals.
    • Newer react and typescript for peer dependencies.
  • Code Style Improvements:
    • Adopting XTerm over Xterm for naming consistency.
    • Exposing terminal methods like .write(), .blur(), etc., directly from the XTerm class, enhancing developer experience and code readability.
    • Transitioning to @typescript-eslint for linting over tslint.
  • Enhanced Functionality:
    • Integration of new event listeners from XTerm like onBell() and onWriteParsed().
    • Exposure of terminal methods such as .blur(), .focus(), and more for direct interaction.
    • Introduction of props for custom event handlers like attachCustomWheelEventHandler(), registerLinkProvider(), etc.
    • Implementation of a new link provider for registerLinkProvider().
  • Performance Optimization: Removal of dynamic type checking to improve performance.
  • Documentation Enhancements:
    • Updated documentation to align with @xterm/xterm, including new types like IEventListener.
    • Detailed comments for methods such as attachCustomWheelEventHandler, registerLinkProvider, registerCharacterJoiner, and others.
  • Development and Testing: Adoption of vite for faster end-to-end testing, with plans to test add-ons thoroughly.

Development

For development purposes, this project uses vite for a streamlined and efficient workflow.

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues to discuss potential improvements or features.

  • For dev, pnpm is recommended.

Credits

Honorable mentions

I found many many packages after finishing this project... I felt like I wasted 2 days but it seems to me that mine has the most up-to-date xterm.js version and the most features added to xterm-for-react.

Decisions

Do we need a useXterm hook?

I can wrap up this to a useXterm hook like the example below, but I think it's better to just keep it as a component. If you want to use it as a hook, make an issue or shoot me a message. Definition of useXterm hook can be like this:

import { useRef, useEffect } from "react";
import XTerm from "./XTerm";

const useXTerm = () => {
  const xtermRef = useRef();

  useEffect(() => {
    if (xtermRef.current) {
      xtermRef.current.write = xtermRef.current.write.bind(xtermRef.current);
      xtermRef.current.focus = xtermRef.current.focus.bind(xtermRef.current);
      xtermRef.current.blur = xtermRef.current.blur.bind(xtermRef.current);
    }
  }, []);

  return {
    setRef: xtermRef,
    write: (data) => xtermRef.current.write(data),
    focus: () => xtermRef.current.focus(),
    blur: () => xtermRef.current.blur(),
  };
};

export default useXTerm;

Then, in use it is like this

import React, { useEffect } from "react";
import XTerm from "./XTerm";
import useXTerm from "./useXTerm";

const App = () => {
  const { setRef, write, focus, blur } = useXTerm<XTerm>(null);

  useEffect(() => {
    write("Hello, XTerm!\n");
    focus();
  }, []);

  return (
    <div>
      <h1>Using XTerm with Custom Hook</h1>
      <XTerm ref={setRef} />
      <button onClick={() => write("Button clicked!\n")}>
        Write to Terminal
      </button>
      <button onClick={focus}>Focus Terminal</button>
      <button onClick={blur}>Blur Terminal</button>
    </div>
  );
};

export default App;

License

This project is licensed under the MIT License. See the LICENSE file for more details.

xterm-react's People

Contributors

dependabot[bot] avatar pablolion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

xterm-react's Issues

Xterm not working

Dear @PabloLION ,

Based on Documentation the simple minimum component not working, would you provide the working example
Also the import should be changed to import { XTerm } from "@pablo-lion/xterm-react";

Thanks

Use all the handlers provided by @xterm/xterm

For Copilot Workspace

Like the props.customKeyEventHandler for attachCustomKeyEventHandler,

Add a new customWheelEventHandler should be to the props for custom wheel event handling, and then process it like customKeyEventHandler

For these methods below in @xterm/xterm,

attachCustomKeyEventHandler
attachCustomWheelEventHandler
registerLinkProvider
registerCharacterJoiner
deregisterCharacterJoiner
registerMarker
registerDecoration

add according properties like linkProvider, characterJoiner, etc. to the class property and try to process them. When adding props, don't forget to include docstring.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.