Published
Edited
Sep 25, 2020
Importers
Insert cell
md`# HTML Utils`
Insert cell
import { addDefaults } from '@nuuuwan/option-utils'
Insert cell
DEFAULT_STYLE = Object({
'font-family': 'Futura'
})
Insert cell
function _(tag, childListOrString = [], attributeMap = {}) {
let e = document.createElement(tag);
attributeMap = addDefaults(attributeMap, { style: DEFAULT_STYLE });

e = Object.entries(attributeMap).reduce(function(e, [k, v]) {
if (typeof v === 'object') {
v = Object.entries(v)
.map(([k, v]) => `${k}: ${v}`)
.join('; ');
}
e.setAttribute(k, v);
return e;
}, e);

if (typeof childListOrString === 'string') {
e.appendChild(document.createTextNode(childListOrString));
return e;
}

return childListOrString.reduce(function(e, child) {
e.appendChild(child);
return e;
}, e);
}
Insert cell
_('div', [
_('h1', 'This is a heading'),
_('p', 'This is a paragraph', { style: { color: 'red' } })
])
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more