Skip to content

Instantly share code, notes, and snippets.

// server.js
const http = require('http');
const fs = require('fs');
require('./components');
const PORT = 4000;
const styles = fs.readFileSync('./styles.css');
const components = fs.readFileSync('./components.js');
.box {
width: 30px;
height: 30px;
background: #ccc;
}
.box_big {
width: 100px;
height: 100px;
}
function App({ url }) {
return `
<!doctype html>
<html class="app">
<head>
...
</head>
<body class="app__body">
${NameEditor({ instanceIndex: 0 })}
${NameEditor({ instanceIndex: 1 })}
function NameEditor() {
return `
<div class="nameEditor">
<div>Enter you name:</div>
<input onKeyUp="NameEditor.onKeyUp(this)" />
<div>
Hello, <span></span>
</div>
</div>
`;
function NameEditor({ instanceIndex }) {
return `
<div class="nameEditor">
<div>Enter you name:</div>
<input onKeyUp="NameEditor.onKeyUp(this, ${instanceIndex})" />
<div>
Hello, <span id="nameEditor__name-${instanceIndex}"></span>
</div>
</div>
`;
function Box({ text }) {
return `
<div class="box" onClick="Box.onClick()">
${text}
</div>
`;
}
Box.onClick = () => {
alert('box was clicked');
function App({ url }) {
return `
...
${NameEditor({ id: 0 })}
${NameEditor({ id: 1 })}
${NameEditor({ id: 2 })}
...
`
}
function NameEditor({ id }) {
// Add color to distinguish name editors
const color = {
0: '#b6e1c1',
1: '#b7c4ed',
}[id];
return `
<div class="nameEditor" style="background: ${color}">
...
const Box = () => '<div class="box"></div>';
const boxes = document.getElementsByClassName('box');
document.body.innerHTML = `${Box()}${Box()}${Box()}`;
console.log(boxes.length); // 3
document.body.innerHTML = `${Box()}`;
console.log(boxes.length); // 1
const nameElems = {};
function getNameElems(id) {
return nameElems[id] || (
nameElems[id] = document.getElementsByClassName(`nameEditor__name-${id}`)
);
}
NameEditor.onKeyUp = (input, id) => {
for (const elem of getNameElems(id)) {