npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xaro/micro-dom

v0.2.2

Published

[![DeepScan grade](https://deepscan.io/api/teams/11657/projects/14878/branches/287086/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=11657&pid=14878&bid=287086)

Downloads

30

Readme

DeepScan grade

@xaro/micro-dom

The DOM control micro-library has several functions for manipulating the classes, styles, and attributes of elements. (See all methods below)

Also has all the array functions and actually inherits from it

Install

$ npm install @xaro/micro-dom

Usage

import _, { I_MicroDOM } from '@xaro/micro-dom';

const els: I_MicroDOM = _('.test-1', document.querySelector('.test-2'), ...document.querySelectorAll('.test-3') /* ... */);

els.addClass('class-A', 'class-B' /* ... */)
  .removeClass('class-A', 'class-B' /* ... */)
  .toggleClass('class-A')
  .css({
    color: 'red',
    'font-size': '15px',
    backgroundColor: 'blue'
    /* ... */
  })
  .attr({
    'data-test-1': 'test-1',
    'data-test-2': 'test-2',
    /* ... */
  })
  .append('content');
Other methods description see below in the section Interface

Additional features

The nextTick method for sequential execution of functions through setTimeout with a delay of 0, like Vue's $nextTick, but for an array of functions. Useful for adding or removing classes when you need to wait for the browser to render, like in the example below:

import _ from '@xaro/micro-dom';

const els = _('.test');

const cbs = [
  () => els.addClass('class-A'),
  () => els.addClass('class-B')
];

els.nextTick(...cbs);

// Result:
// In setTimeout, the first passed callback function is started, and a new setTimeout for the next function, and so on until all functions are executed

Also, you can only import the nextTick function, which returns void, but also not tied to the MicroDOM instance.

import { nextTick } from '@xaro/micro-dom';

nextTick(() => console.log('I\'m call in setTimeout(cb, 0);'));

Files

Sources

  • src/MicroDOM.ts

    Main class

  • src/entry.ts

    Entry function: _(...)

  • src/helpers.ts

    nextTick(...cb: Function[]) helper: setTimeout wrap with recursive feature (every next callback run in new timer)

Bundles

  • micro-dom.es.js

    export entry function, MicroDOM class and nextTick helper

  • micro-dom.js & micro-dom.umd.js

    export only entry function: _(...);

Interface

// File: dist/micro-dom.d.ts
export default function _<T extends Element = Element>(...args: Array<string | T | MicroDOM<T>>): MicroDOM<T>;

export class MicroDOM<T extends Element = Element> extends Array<T> {
  get<U extends Element = Element>(...args: Array<string | U | MicroDOM<U>>): MicroDOM<U>;  // Returns a new instance containing the elements with the passed selectors and elements (or from the document if the current instance is empty)
  create<U extends Element = Element>(...entities: Array<
    string |
    {
      tagName?: string,
      content?: string | U | Array<string | U> | MicroDOM<U>
    }
  >): MicroDOM<U>;                                                                          // Returns a new instance with new created elements according to the passed parameters
  empty(): MicroDOM<T>;                                                                     // Clears the contents of each element in the set
  text(text?: string): MicroDOM<T>;                                                         // Sets the textContent property for each collection item
  append(...append: Array<string | T> | MicroDOM<T>): MicroDOM<T>;                          // Inserts a set of Node objects or DOMString objects after the last child of each array element
  addClass(...classes: string[]): MicroDOM<T>;                                              // Adds a class or classes to all array elements
  removeClass(...classes: string[]): MicroDOM<T>;                                           // Removes a class or classes from all array elements
  toggleClass(classname: string): MicroDOM<T>;                                              // Adds or removes a class for each element of the array, depending on its presence
  hasClass(classname: string, reqtForAll?: boolean): boolean;                               // Returns new MicroDOM instance with element which has passed css class. If you pass "true" as the second argument, then returns all elements has passed class in set (default: reqtForAll = false)
  addEventListener<K extends keyof HTMLElementEventMap>(
    type: K,
    listener: EventListenerOrEventListenerObject,
    options?: boolean | AddEventListenerOptions
  ): MicroDOM<T>;                                                                           // Calls the "addEventListener" method for each set item
  removeEventListener<K extends keyof HTMLElementEventMap>(
    type: K,
    listener: EventListenerOrEventListenerObject,
    options?: boolean | EventListenerOptions
  ): MicroDOM<T>;                                                                           // Calls the "removeEventListener" method for each set item
  fireEvent(type: string): MicroDOM<T>;                                                     // Calls dispatchEvent with an event of the specified type for each item in the set
  css(obj: object): MicroDOM<T>;                                                            // Sets the style attribute property passed in the object by key
  attr(obj: object): MicroDOM<T>;                                                           // Sets the attribute property passed in the object by key
  nextTick(...cbs: Array<Function | [ Function, number? ]>): MicroDOM<T>;                   // Recursively calls each passed function in a new setTimeout(() => {}, 0)
}

export function nextTick(...cbs: Array<Function | [ Function, number? ]>): void;

License

MIT