Web Terminal Component
info
This is a jQuery-free web terminal component built with xterm.js. It supports light/dark mode and uses the WaveDash color scheme.
Basic Terminal
Advanced Terminal with Commands
Source Code Examples
HTML Structure
<div id="terminal" class="terminal-container"></div>
CSS Styling
.terminal-container {
height: 400px;
width: 100%;
background: var(--bs-body-bg);
border-radius: 0.375rem;
}
JavaScript Implementation
// Initialize terminal
const terminal = new Terminal({
cursorBlink: true,
fontFamily: 'Ubuntu Sans Mono, monospace',
fontSize: 14,
theme: {
background: getComputedStyle(document.documentElement)
.getPropertyValue('--bs-body-bg'),
foreground: getComputedStyle(document.documentElement)
.getPropertyValue('--bs-body-color'),
cursor: getComputedStyle(document.documentElement)
.getPropertyValue('--bs-primary'),
selection: getComputedStyle(document.documentElement)
.getPropertyValue('--bs-primary'),
}
});
// Add addons
const fitAddon = new FitAddon();
const webLinksAddon = new WebLinksAddon();
terminal.loadAddon(fitAddon);
terminal.loadAddon(webLinksAddon);
// Open terminal
const terminalContainer = document.getElementById('terminal');
terminal.open(terminalContainer);
fitAddon.fit();
// Demo commands
const commands = {
help: 'Available commands: help, hello, date, clear, theme',
hello: 'Hello from WaveDash Terminal!',
date: new Date().toString(),
clear: () => terminal.clear(),
theme: 'Current theme: ' + document.documentElement.getAttribute('data-bs-theme')
};
// Handle input
let currentInput = '';
terminal.onKey(e => {
const char = e.key;
if (char === '\r') {
terminal.write('\r\n');
const cmd = currentInput.trim();
if (cmd.length > 0) {
const output = typeof commands[cmd] === 'function'
? commands[cmd]()
: commands[cmd] || `Command not found: ${cmd}`;
if (output) terminal.writeln(output);
}
terminal.write('$ ');
currentInput = '';
} else if (char === '\u007F') {
if (currentInput.length > 0) {
currentInput = currentInput.slice(0, -1);
terminal.write('\b \b');
}
} else {
currentInput += char;
terminal.write(char);
}
});
// Resize on window resize
window.addEventListener('resize', () => fitAddon.fit());