.dom 
A tiny (512 byte) virtual DOM template engine for embedded projects
IE / Edge |
Firefox |
Chrome |
Safari |
Opera |
iOS Safari |
Chrome for Android |
| Edge 14+ |
45+ |
49+ |
10+ |
37+ |
10.2+ |
55+ |
.dom borrows some concepts from React.js (such as the re-usable Components and the Virtual DOM) and tries to replicate them with the smallest possible footprint, exploiting the ES6 javascript features.
Why? Because with such library you can create powerful GUIs in tight space environments, such as IoT devices, where saving even an extra byte actually matters!
Features
-
Tiny by design : The library should never exceed the 512 bytes in size. The goal is not to have yet another template engine, but to have as many features as possible in 512 bytes. If a new feature is needed, an other must be sacraficed or the scope must be reduced.
-
Built for the future : The library is heavily exploiting the ES6 specifications, meaning that it's not supported by older browsers. Currently it's supported by the 90% of the browsers in the market, but expect this to be close to 100% within the next year.
-
Declarative : Describe your HTML DOM in a structured, natural manner, helping you create powerful yet readable user interfaces.
-
Component-Oriented : Just like React.js, .dom promotes the use of functional components.
-
"Write less" accelerators : The library API is designed specifically to have short function names and accelerators, allowing you to describe your views with less code.
Projects Using .dom
Are you using .dom in your project? Fork this repository and add yours on the list!
Installation
For minimum footprint, include dotdom.min.js.gz (512b) to your project.
<script src="dotdom.min.js.gz" />
Alternatively you can just include the minified version of the library directly before your script. Just copy-paste the minified code.
Examples
If you already know React.js, the following examples can help you understand how
the .dom primitives relate to React.
1. Plain DOM
Rendering a very simple DOM structure.
| React |
.dom |
ReactDOM.render(
React.createElement('div', null, 'Hello world'),
document.body
);
|
R(
H('div', 'Hello world'),
document.body
)
|
2. Stateless Component
Creating a component on which you can pass properties.
| React |
.dom |
function Hello(props) {
return React.createElement(
'div', null, `Hello ${props.toWhat}`
);
}
ReactDOM.render(
React.createElement(
Hello, {toWhat: 'World'}, null
),
document.body
);
|
function Hello(props) {
return H('div', `Hello ${props.toWhat}`);
}
R(
H(Hello, {toWhat: 'World'}),
document.body
)
|
3. Stateful Component
Creating components that can maintain their own state.
| React |
.dom |
class Clickable extends React.Component {
constructor() {
super(...arguments);
this.state = {
clicks: 0
};
}
render() {
const {clicks} = this.state;
return React.createElement(
'button', {
onClick() {
this.setState({clicks: clicks+1})
}
}, `Clicked ${clicks} times`
);
}
}
ReactDOM.render(
React.createElement('div', null,
React.createElement(Clickable, null, null),
React.createElement(Clickable, null, null)
),
document.body
);
|
function Clickable(props, state, setState) {
const {clicks=0} = state;
return H('button',
{
onclick() {
setState({clicks: clicks+1})
}
},
`Clicked ${clicks} times`
);
}
R(
H('div',
H(Clickable),
H(Clickable)
),
document.body
)
|
4. Life-Cycle Component Events
The component can also subscribe to life-cycle events:
| React |
.dom |
class WithLifeCycle extends React.Component {
constructor() {
super(...arguments);
this.state = {
mounted: "no"
};
}
componentDidMount() {
this.setState({ mounted: "yes" })
}
render() {
const {mounted} = this.state;
return React.createElement(
'div', null, `mounted = ${mounted}`
);
}
}
ReactDOM.render(
React.createElement('div', null,
React.createElement(WithLifeCycle, null, null),
),
document.body
);
|
function WithLifeCycle(props, state, setState, hooks) {
const {mounted = "no"} = state;
hooks.m.push(() => {
setState({ mounted: "yes" })
});
return H('div',
`mounted = ${mounted}`
);
}
R(
H('div', H(WithLifeCycle)),
document.body
)
|
5. Keyed Updates
Keyed updates is a useful reconciliation feature from React that enables the rendering engine to take smart decisions on which elements to update.
A particularly useful case is when you are rendering a dynamic list of elements. Since the rendering engine does not understand which element has changed, it ends-up with wrong updates.
To solve this issue, the VDOM engines use a key property that uniquely identifies an element in the tree. However .dom solves it, by keeping a copy of the element state in the VDom element instance itself.
This means that you don't need any key