I have tried many things but still it is not able to build up the logic of a terminal. I have used the xterm-for-react package as well but it was still giving the same black UI only
const Xterm = () => {
const termRef = useRef(null);
useEffect(() => {
const initTerminal = () => {
const terminal = new Terminal();
const fitAddon = new FitAddon();
terminal.loadAddon(fitAddon);
terminal.open(termRef.current);
fitAddon.fit();
terminal.onKey((e) => {
const printable = !e.domEvent.altKey && !e.domEvent.altGraphKey && !e.domEvent.ctrlKey && !e.domEvent.metaKey;
if (e.domEvent.key === 13) {
terminal.write('\r\n');
} else if (e.domEvent.key=== 8) {
// Do something when backspace is pressed
} else if (printable) {
terminal.write(e.key);
}
});
};
initTerminal();
}, []);
return <div ref={termRef}></div>;
};
export default Xterm;
xtermjs is only a visualization tool it doesn't have a built-in terminal logic. Check this in xtermjs official github page:
If you want to run processes like bash, vim etc. you should run a terminal process in the background and connect it with your js code. You can check this github project if you want to use it with React.