Skip to content

Utilities

The helpers WebComponent uses internally, exported so you can use them directly. Import from the utils entry point or each module separately:

import { serialize, getKebabCase } from 'web-component-base/utils'
// or
import { serialize } from 'web-component-base/utils/serialize.js'

See it live: Just the parts demo ↗ builds a component from these helpers without extending the base class.

Converts a kebab-case attribute name to its camelCase prop key.

ParameterType
kebabstringthe attribute name
returnsstringthe prop key
getCamelCase('max-count') // 'maxCount'

Converts a camelCase prop key to its kebab-case attribute name. This is the mapping observedAttributes uses.

ParameterType
strstringthe prop key
returnsstringthe attribute name
getKebabCase('maxCount') // 'max-count'

Consecutive capitals are treated as one word, so parseHTML becomes parse-html.

Converts a value to its attribute string form.

ParameterType
valueanythe value to serialize
returnsstringthe attribute value

Numbers, booleans and objects go through JSON.stringify; strings and everything else pass through unchanged.

Parses an attribute string back into a value of the given declared type — the inverse of serialize().

ParameterType
valuestringthe attribute value
typestring'boolean', 'number', 'object', 'undefined' or 'string'
returnsanythe parsed value

'boolean' always returns true — strict HTML boolean-attribute semantics, where any present value is true. Absence is handled by the caller and never reaches here. 'number', 'object' and 'undefined' use JSON.parse and throw on malformed input; strings pass through.

Builds real DOM from a vnode tree.

ParameterType
treeanya vnode, an array of vnodes, or a text value
returnsNodean element, a DocumentFragment, or a text node

An array becomes a DocumentFragment; a value with no type becomes a text node. Props are applied with applyProp() and children are created recursively.

Applies a single vnode prop to an element, using the rule described in html.

ParameterType
elElementthe element to apply the prop to
propstringthe prop name as written in the vnode
valueanythe prop value

Shared with the reconciler, so a patched element gets props by exactly the same rule as a freshly created one.

These power the in-place re-render described in Template vs Render. Matching is index-based and non-keyed.

patchChildren(parent, oldChildren, newChildren)

Section titled “patchChildren(parent, oldChildren, newChildren)”

Reconciles a parent node’s children from one vnode list to another, patching matches in place and trimming leftovers.

ParameterType
parentNodethe parent node to patch into
oldChildrenanythe previous vnode children, or previous tree
newChildrenanythe new vnode children, or new tree

patchNode(parent, dom, oldVnode, newVnode)

Section titled “patchNode(parent, dom, oldVnode, newVnode)”

Reconciles a single node position. Reuses dom when the vnode type matches, otherwise replaces it.

ParameterType
parentNodethe parent node being patched
domNode | nullthe existing node at this index, if any
oldVnodeanythe vnode that produced dom, if known
newVnodeanythe vnode to render