Crel: Fast DOM Creation Utility
Crel is a lightweight, minimalist JavaScript utility designed to simplify and accelerate the creation of DOM elements, serving as a more ergonomic alternative to directly using `document.createElement`. It enables developers to construct complex DOM trees with a concise, declarative syntax. The current stable version is 4.2.1, with the project demonstrating an active development and maintenance cadence over its lifetime, having evolved through multiple major versions. Key differentiators include its exceptionally small footprint (under 1KB minified and 500 bytes gzipped), its performance which rivals native `document.createElement` calls, and its avoidance of interference with existing in-DOM event listeners. It also provides a modern Proxy-based API for even cleaner syntax in environments supporting ES6 Proxies, making it suitable for modern evergreen browser applications.
Common errors
-
ReferenceError: crel is not defined
cause The `crel` library was not correctly imported, loaded, or is out of the current scope, preventing access to its primary function.fixEnsure `crel` is properly imported using `import crel from 'crel';` (ESM), `const crel = require('crel');` (CommonJS), or that `crel.min.js` is loaded via a `<script>` tag in the HTML before use. -
TypeError: crel.proxy is not a function / crel.proxy is undefined
cause This error occurs when attempting to use the `crel.proxy` API without properly accessing it from the default export, or if the runtime environment does not support ES6 Proxies.fixVerify that your runtime environment supports ES6 Proxies. For ESM, access the proxy API correctly via `import crelBase from 'crel'; const crelProxy = crelBase.proxy;`. For CommonJS, use `const crelProxy = require('crel').proxy;`.
Warnings
- gotcha Crel is designed for efficient creation of *new* DOM elements. While it's technically possible to pass existing DOM elements to `crel` for modification or rearrangement, this usage pattern is explicitly discouraged by the library's authors and can lead to unexpected behavior or inefficient DOM operations compared to native methods or libraries optimized for manipulation.
- gotcha The standard `crel` API handles event listeners via a custom `crel.attrMap['on']` definition. Direct `onclick` or `oninput` attributes within the attribute object for the standard API will *not* automatically attach event handlers without this custom mapping being set up beforehand.
- gotcha Crel extensively utilizes modern ES6 features (e.g., Proxies for its advanced API). Consequently, it is specifically intended for use in modern 'evergreen' browser environments. Applications targeting older or legacy browsers will require transpilation (e.g., via Babel) to ensure full compatibility and functionality.
Install
-
npm install crel -
yarn add crel -
pnpm add crel
Imports
- crel
import { crel } from 'crel';import crel from 'crel';
- crel (CommonJS)
import crel from 'crel';
const crel = require('crel'); - crel.proxy
import { proxy } from 'crel';import crelBase from 'crel'; const crelProxy = crelBase.proxy;
Quickstart
import crel from 'crel';
// Standard API usage
const standardButton = crel('button', {
type: 'button',
'data-action': 'standard-click' // Direct event attributes like 'onclick' won't work without crel.attrMap['on'] setup
}, 'Click Me (Standard)!');
const infoParagraph = crel('p', 'This element was created using the standard `crel` function syntax.');
const standardDiv = crel('div',
{ 'class': 'standard-example', style: 'padding: 15px; border: 1px solid #ccc; background-color: #f9f9f9;' },
crel('h2', 'Standard Crel API'),
infoParagraph,
standardButton
);
// Attach event listener for the standard button (common alternative if attrMap['on'] isn't configured)
standardButton.addEventListener('click', () => alert('Standard API button clicked (via addEventListener)!'));
// Accessing the Proxy API from the default export
const crelProxy = crel.proxy;
// Proxy API usage
const proxyButton = crelProxy.button({
type: 'button',
onclick: () => alert('Proxy API button clicked!') // Proxy API handles 'onclick' directly as an attribute
}, 'Proxy Click Me!');
const proxyInfoParagraph = crelProxy.p('This element was created using the modern Proxy API for cleaner syntax.');
const proxyDiv = crelProxy.div(
{ 'class': 'proxy-example', style: 'margin-top: 20px; padding: 15px; border: 1px solid #cce; background-color: #f0f8ff;' },
crelProxy.h2('Proxy Crel API'),
proxyInfoParagraph,
proxyButton
);
// Append elements to the document body (or a specific container)
document.body.appendChild(standardDiv);
document.body.appendChild(proxyDiv);
console.log('DOM elements created and appended to the document body.');