DOM Rendering
Knowing all the basics, we are already able to build up our whole styling environment. But we still do not know how to actually render our styles into the DOM. Luckily this is really simple as all we have to do is call a single function.
The
fela-dom
package is used for any DOM specific methods.
It provides a render
function which takes the renderer as a parameter.Automatic Updates
Once rendered to the DOM, a change listener will subscribe to changes. The DOM nodes will automatically be updated on changes.
It uses an optimized rendering mechanism based on
(new tab) to update as performant as possible.CSSStyleSheet.insertRule
Depending on the browser, it automatically removes unsupported rules.
Example
import { createRenderer } from 'fela'import { render } from 'fela-dom'
const rule = ({ size }) => ({ backgroundColor: 'red', fontSize: size, color: 'blue',})
const renderer = createRenderer()
renderer.renderRule(rule, { size: '12px' }) // => a b c
render(renderer)
// automatically adds the rule to the respective style noderenderer.renderRule(rule, { size: '16px' }) // => a d c
Advanced Usage
Unless working in a plain JavaScript environment, you are usually not calling
render
by yourself.Most bindings like listed below will automatically do that for you.