-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvanilla-fp.js
52 lines (49 loc) · 1.61 KB
/
vanilla-fp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// A helper for creating html elements functionally
export const createElement =
(type) =>
({ className, text, onClick, disabled, ...props }, children) => {
//For HTML
const div = document.createElement(type);
// For SVG
//const el = document.createElementNS("http://www.w3.org/2000/svg", type)
if (className) {
div.classList.add(className);
}
if (text) {
let textNode = document.createTextNode(text);
el.appendChild(textNode);
}
if (children) {
el.replaceChildren(...children.filter((c) => c !== undefined));
}
if (onClick) {
el.addEventListener("click", onClick);
}
if (disabled) {
el.setAttribute("disabled", true);
}
Object.keys(props).forEach((propName) => {
el.setAttribute(propName, props[propName]);
});
return el;
}
// Functions for creating html elements
// (Right now they create DOM elements directly, but they
// can be made to work with with some virtual DOM lib
export const div = createElement('div')
export const button = createElement('button')
export const span = createElement('span')
// Function for rendering the root component calls the
// component function and renders the output.
// It also provides implementations of the 'setState'
// function that triggers a rerender.
export const renderComponent = (component, state = {}, params) => {
console.log('Rendering app with state', state)
document.getElementById("vanilla-fp")
.replaceChildren(component({
state,
setState: (state) => renderComponent(component, state, params),
fetch,
...params
}))
}