Skip to content

Commit 6519a94

Browse files
committed
fix(demo): improve terminal resizing to fit container
- Add overflow: hidden and position: relative to #terminal container so the container constrains the canvas size rather than expanding to fit it - Call fitAddon.observeResize() for automatic resize on container changes - Use term.onResize event to notify PTY of dimension changes (works with both observeResize and manual fit calls) - Keep window resize handler for browsers that don't trigger ResizeObserver on window resize This ensures the terminal properly sizes itself to fill the container on initial load and responds to container/window size changes. fix: expose renderer publicly for FitAddon to access FitAddon.proposeDimensions() was returning undefined because it couldn't access the terminal's renderer to get font metrics. The renderer was private, so FitAddon couldn't calculate the correct terminal dimensions. Changes: - Make Terminal.renderer public so FitAddon can access getMetrics() - Add observeResize() call to demo/index.html - Make initTerminal async and await term.open() in demo/index.html - Update demo/bin/demo.js CSS to use absolute positioning for #terminal The root cause was that FitAddon needs renderer.getMetrics() to calculate how many cols/rows fit in the container, but the renderer field was private.
1 parent d3c37df commit 6519a94

4 files changed

Lines changed: 22 additions & 11 deletions

File tree

demo/bin/demo.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,16 @@ const HTML_TEMPLATE = `<!doctype html>
160160
padding: 0;
161161
min-height: 400px;
162162
height: 60vh;
163+
position: relative;
163164
}
164165
165166
#terminal {
166-
width: 100%;
167-
height: 100%;
167+
position: absolute;
168+
top: 0;
169+
left: 0;
170+
right: 0;
171+
bottom: 0;
172+
overflow: hidden;
168173
}
169174
</style>
170175
</head>
@@ -203,6 +208,7 @@ const HTML_TEMPLATE = `<!doctype html>
203208
const container = document.getElementById('terminal');
204209
await term.open(container);
205210
fitAddon.fit();
211+
fitAddon.observeResize(); // Auto-fit when container resizes
206212
207213
// Status elements
208214
const statusDot = document.getElementById('status-dot');
@@ -249,13 +255,17 @@ const HTML_TEMPLATE = `<!doctype html>
249255
}
250256
});
251257
252-
// Handle resize
253-
window.addEventListener('resize', () => {
254-
fitAddon.fit();
258+
// Handle resize - notify PTY when terminal dimensions change
259+
term.onResize(({ cols, rows }) => {
255260
if (ws && ws.readyState === WebSocket.OPEN) {
256-
ws.send(JSON.stringify({ type: 'resize', cols: term.cols, rows: term.rows }));
261+
ws.send(JSON.stringify({ type: 'resize', cols, rows }));
257262
}
258263
});
264+
265+
// Also handle window resize (for browsers that don't trigger ResizeObserver on window resize)
266+
window.addEventListener('resize', () => {
267+
fitAddon.fit();
268+
});
259269
</script>
260270
</body>
261271
</html>`;

demo/bun.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
let ws;
139139
let fitAddon;
140140

141-
function initTerminal() {
141+
async function initTerminal() {
142142
term = new Terminal({
143143
cursorBlink: true,
144144
fontSize: 14,
@@ -153,10 +153,11 @@
153153
fitAddon = new FitAddon();
154154
term.loadAddon(fitAddon);
155155

156-
term.open(document.getElementById('terminal-container'));
156+
await term.open(document.getElementById('terminal-container'));
157157
fitAddon.fit();
158+
fitAddon.observeResize(); // Auto-fit when container resizes
158159

159-
// Handle window resize
160+
// Handle window resize (for browsers that don't trigger ResizeObserver on window resize)
160161
window.addEventListener('resize', () => {
161162
fitAddon.fit();
162163
});

lib/terminal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class Terminal implements ITerminalCore {
6767
// Components (created on open())
6868
private ghostty?: Ghostty;
6969
public wasmTerm?: GhosttyTerminal; // Made public for link providers
70-
private renderer?: CanvasRenderer;
70+
public renderer?: CanvasRenderer; // Made public for FitAddon
7171
private inputHandler?: InputHandler;
7272
private selectionManager?: SelectionManager;
7373
private canvas?: HTMLCanvasElement;

0 commit comments

Comments
 (0)