voko
Done. This project is maintained but there's no more development needed
Hyperscript reviver that uses CSS selector syntax to shorthand element creation. Supports DOM and SVGs. JSX compatible.
1.72kB minified and exactly 1000B gzipped when exported as window.v
.
Reduce your bundle size by replacing verbose DOM APIs with voko.
Intro
Replaces:
const header = documentheaderclassName = 'header'header const text = documentheader const input = documentinputstyle = 'padding: 10px'inputinput header
Heavily based on Mithril, but also inspired by the hyperscript project, Preact and other React-like libraries, and Val.
Scope
There's no virtual DOM, handling of state or updates, or mutating existing DOM nodes. It only simplifies DOM APIs for creating elements and fragments. Allows for HTML components as functions. JSX compatible so drop it into any React-like projects.
I wrote this for my co-workers who were writing all DOM API calls by hand in a
very messy and unmaintainable way (sorry but you know it's true). This is meant
to be dropped into a <script>
tag to be plug-and-play.
SVGs/Namespaces
There are many nuances to writing namespaced elements like SVGs and MathML. DOM
APIs like svgTextEl.x = 10
won't work as expected. They need methods which are
namespaced like setAttribute()
.
document.createElement()
won't work since it'll be namespaced to XHTML.
Here's some standard JS for creating SVGs:
document document const size = 30const svg = documentsvg; const box = documentboxboxboxbox svg
In voko, you'd write them as expected. Use :
to denote the namespace. In this
case, :svg
. Replace the above code with:
const size = 30
Add arbitrary namespaces such as MathML by adding them to the v.ns
object.
Only SVG is included by default. You can rename namespaces too, if you're into
that: v.ns.s = v.ns.svg
Examples
Example of its flexibility, supporting different writing styles for attributes, CSS-style syntax, and looping:
const header = const existingNode = document const ButtonComponent = const tags = Pictures: 'Some content' 'Another post' Videos: 'Hello! <code>Escaped HTML</code>' 123 // track references to DOM nodes using `ref:`const live = {} documentbody // expect `{ Button: HTMLElement {} }`console // expect `{ click: [λ: onClick] }`console // append without extra tags or calls to `appendChild`:document
Read docs/rationale.md on what makes a reviver (and components more generally) useful, and docs/scope.md for the feature set and design decisions for voko.
Note: This is a purely ESM script, so it won't support require()
. Use the
minified version to bind to window
in the browser, and ESM for your bundler or
on Pika/Snowpack