0.2.2 • Published 3 years ago

@xaro/micro-dom v0.2.2

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

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

0.1.20

3 years ago

0.1.21

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.19

3 years ago

0.2.2

3 years ago

0.1.18

3 years ago

0.1.17

3 years ago

0.1.16

3 years ago

0.1.14

3 years ago

0.1.15

3 years ago

0.1.13

3 years ago

0.1.12

3 years ago

0.1.10

3 years ago

0.1.11

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.4

3 years ago

0.1.5

3 years ago

0.1.2

3 years ago

0.1.3

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago