Minimal DOM Utility Toolbelt

5.3.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating elements with `domify`, querying the DOM using `query`, manipulating CSS classes with `classes`, and handling events with `event.on`/`event.off`.

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);

view raw JSON →