Minimal DOM Utility Toolbelt
min-dom is a focused and lightweight JavaScript library, currently at version 5.3.0, providing a collection of essential DOM manipulation utilities. It emphasizes a minimal footprint, weighing approximately 2KB gzipped, making it suitable for environments where bundle size is critical. The library maintains an active release cadence, with recent updates ensuring compatibility and minor feature enhancements. Key differentiators include its small size, its inspiration from the `component` project's utilities, and its provision of common helpers like `assignStyle`, `attr`, `classes`, `clear`, `closest`, `delegate`, `domify`, `event`, `matches`, `query`, and `remove`. It is designed to be library-friendly and ships with TypeScript types for improved developer experience in typed projects.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'on')
cause Attempting to call `on(element, 'event', handler)` directly instead of `event.on(...)`.fixCorrect your event binding to `import { event } from 'min-dom'; event.on(element, 'event', handler);` -
Uncaught SyntaxError: The requested module 'min-dom' does not provide an export named 'default'
cause Attempting to import `min-dom` with a default import statement (`import MinDom from 'min-dom'`).fixUse named imports for all utilities: `import { query, domify } from 'min-dom';` -
Element.closest: The selector is not a valid selector.
cause Passing an invalid or empty string as the selector argument to `closest` or `query`.fixEnsure the selector string is a valid CSS selector and not empty. For example, `closest(element, '.my-class')` is correct, but `closest(element, '')` will fail.
Warnings
- breaking min-dom v5.0.0 updated its internal dependency to `domify@2`, which may introduce breaking changes if you relied on specific internal behaviors or undocumented APIs of `domify` directly. Always review the `domify` changelog when upgrading `min-dom` across major versions.
- gotcha The `event` utility bundles methods `on`, `off`, and `bind`. When listening for events, you must pass the `event` object itself to access these methods, e.g., `event.on(element, 'click', handler)`. Do not destructure `on` directly from `min-dom` as a top-level export.
- gotcha When using `query` or `closest`, providing an invalid selector string (e.g., an empty string or malformed selector) can lead to unexpected behavior or errors, as these methods often rely on native browser APIs like `querySelector` or `closest`.
- gotcha The `domify` function takes an HTML string. If this string contains untrusted user input, it can lead to Cross-Site Scripting (XSS) vulnerabilities, as the HTML will be parsed and potentially inserted into the DOM.
Install
-
npm install min-dom -
yarn add min-dom -
pnpm add min-dom
Imports
- query
const query = require('min-dom').query;import { query } from 'min-dom'; - domify
import domify from 'min-dom/lib/domify';
import { domify } from 'min-dom'; - event
import { on } from 'min-dom.event';import { event } from 'min-dom'; - closest
import { findClosest } from 'min-dom';import { closest } from 'min-dom';
Quickstart
import { domify, query, classes, event } from 'min-dom';
// 1. Create a DOM element from an HTML string
const myElement = domify('<div class="container"><p id="greeting">Hello, <span class="name">World</span>!</p><button>Click me</button></div>');
// Append it to the body for demonstration
document.body.appendChild(myElement);
// 2. Query for elements within the created element
const greetingParagraph = query('#greeting', myElement);
const nameSpan = query('.name', greetingParagraph);
console.log('Greeting paragraph text:', greetingParagraph.textContent);
// 3. Manipulate classes
classes(myElement).add('active');
classes(myElement).remove('container');
console.log('myElement classes:', myElement.className);
// 4. Attach an event listener
const button = query('button', myElement);
const handler = (e) => {
console.log('Button clicked!', e.target.tagName);
classes(button).toggle('clicked');
event.off(button, 'click', handler); // Remove after first click
};
event.on(button, 'click', handler);
// Clean up after a few seconds
setTimeout(() => {
if (myElement.parentNode) {
myElement.parentNode.removeChild(myElement);
console.log('Element removed from DOM.');
}
}, 3000);