react-xterm-shell 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +44 -13
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,12 +1,14 @@
1
1
  # react-xterm-shell
2
2
 
3
+ [npm package](https://www.npmjs.com/package/react-xterm-shell)
4
+
3
5
  > **Beta** — under active development; the API may change between minor versions until 1.0.
4
6
 
5
7
  A small React shell around [xterm.js](https://xtermjs.org/). It is a **React shell around xterm, not a React renderer for terminal cells** — xterm owns the grid, parsing, and rendering; this gives you the lifecycle, a stable imperative controller, automatic fitting, and opt-in addons as ordinary React.
6
8
 
7
9
  - `useXTerm()` — creates and owns the xterm instance behind a stable controller.
8
10
  - `<XTerm />` — the DOM mount point.
9
- - `TerminalProvider` / `useTerminalController()` — drive the terminal from chrome (toolbars) without prop drilling.
11
+ - `TerminalProvider` / `useTerminalController()` — drive the terminal from surrounding UI without prop drilling.
10
12
  - Automatic sizing via `FitAddon` + `ResizeObserver`.
11
13
  - Opt-in `webgl` (with DOM fallback), `web-links`, and `unicode11` addons, on by default.
12
14
 
@@ -27,48 +29,77 @@ import "@xterm/xterm/css/xterm.css";
27
29
  ## Quick start
28
30
 
29
31
  ```tsx
30
- import { useXTerm, XTerm } from "react-xterm-shell";
32
+ import { useEffect, useRef } from "react";
33
+ import { useXTerm, XTerm, type XTermHandle } from "react-xterm-shell";
31
34
  import "@xterm/xterm/css/xterm.css";
32
35
 
33
- function TerminalPanel({ socket }: { socket: WebSocket }) {
36
+ export function TerminalPanel() {
37
+ const terminalRef = useRef<XTermHandle | null>(null);
34
38
  const terminal = useXTerm({
35
- onData: (data) => socket.send(JSON.stringify({ type: "input", data })),
36
- onResize: ({ cols, rows }) =>
37
- socket.send(JSON.stringify({ type: "resize", cols, rows })),
39
+ onData: (data) => terminalRef.current?.write(data === "\r" ? "\r\n$ " : data),
38
40
  theme: { background: "#1a1b26", foreground: "#a9b1d6" }
39
41
  });
42
+ terminalRef.current = terminal;
40
43
 
41
- // Stream server output into the terminal:
42
- socket.onmessage = (e) => terminal.write(e.data);
44
+ useEffect(() => {
45
+ terminal.write("react-xterm-shell\r\n$ ");
46
+ terminal.focus();
47
+ }, [terminal]);
43
48
 
44
49
  return <XTerm terminal={terminal} style={{ width: "100%", height: 420 }} />;
45
50
  }
46
51
  ```
47
52
 
48
- ## Composition
53
+ ## External controls
49
54
 
50
- `useXTerm` returns a stable controller, so a toolbar can drive it imperatively
55
+ `useXTerm` returns a stable controller, so surrounding UI can drive it imperatively
51
56
  without re-rendering as bytes stream:
52
57
 
53
58
  ```tsx
54
59
  import { useXTerm, XTerm, TerminalProvider, useTerminalController } from "react-xterm-shell";
55
60
 
56
- function Toolbar() {
61
+ function SessionActions() {
57
62
  const term = useTerminalController();
58
- return <button onClick={term.clear}>Clear</button>;
63
+ return (
64
+ <div>
65
+ <button onClick={() => term.write("deploy --target staging\r\n")}>Insert command</button>
66
+ <button onClick={term.clear}>Clear</button>
67
+ <button onClick={term.focus}>Focus</button>
68
+ </div>
69
+ );
59
70
  }
60
71
 
61
72
  function Panel() {
62
73
  const terminal = useXTerm({ onData: send });
63
74
  return (
64
75
  <TerminalProvider value={terminal}>
65
- <Toolbar />
76
+ <SessionActions />
66
77
  <XTerm terminal={terminal} className="h-[420px]" />
67
78
  </TerminalProvider>
68
79
  );
69
80
  }
70
81
  ```
71
82
 
83
+ ## Backend wiring
84
+
85
+ The package does not include a transport. Connect `onData` and `onResize` to a
86
+ PTY, SSH, container, or WebSocket service, and stream backend output into
87
+ `terminal.write()`:
88
+
89
+ ```tsx
90
+ function RemoteShell({ socket }: { socket: WebSocket }) {
91
+ const terminal = useXTerm({
92
+ onData: (data) => socket.send(JSON.stringify({ type: "input", data })),
93
+ onResize: ({ cols, rows }) =>
94
+ socket.send(JSON.stringify({ type: "resize", cols, rows }))
95
+ });
96
+
97
+ socket.onmessage = (event) => terminal.write(event.data);
98
+
99
+ return <XTerm terminal={terminal} className="h-[420px]" />;
100
+ }
101
+ ```
102
+
72
103
  ## API
73
104
 
74
105
  ### `useXTerm(options?) => XTermHandle`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-xterm-shell",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "A small React shell around xterm.js: a useXTerm hook + XTerm component with a stable imperative controller, automatic fitting, and opt-in webgl/web-links/unicode11 addons.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -34,7 +34,7 @@
34
34
  "author": "Noah Wardlow",
35
35
  "repository": {
36
36
  "type": "git",
37
- "url": "https://github.com/noah-wardlow/react-xterm-shell"
37
+ "url": "git+https://github.com/noah-wardlow/react-xterm-shell.git"
38
38
  },
39
39
  "bugs": {
40
40
  "url": "https://github.com/noah-wardlow/react-xterm-shell/issues"