Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dhmk/dom-utils

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dhmk/dom-utils

A collection of DOM related utils.

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@dhmk/dom-utils

A collection of DOM related utils.

list<T>(parent: Element, childFactory: (data: T, index: number) => Child, getKey?: (data: T) => Key): (newData: T[]) => void

type Child<T> = {
  node: Element;
  update?: (data: T, index: number) => void;
  dispose?: () => void;
  insert?: (parent: Element, node: Element, ref: Element | null) => void;
  remove?: (node: Element) => void;
};

const defaultKey = (x) => ("id" in x ? x.id : x);

Syncs data list with DOM elements.

Usage:

const sync = list(document.getElementById("items"), (data) => {
  const node = document.createElement('div')
  // ...set up node

  return {
    node, // required

    // optional:

    update(newData, index) {} // called when a data item with the same key is updated
    dispose() {} // called when a new data list doesn't contain an item with the key
    insert(parent, node, refNode) {} // customize node insertion; default: parent.insertBefore(node, refNode)
    remove(node) {} // customize node deletion; default: node.remove()
  }
})

sync([1, 2, 3]) // inserts nodes
sync([3, 1]) // moves `3` and deletes `2`

Keywords

FAQs

Package last updated on 01 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc