# uc-dom

> The only DOM abstraction you will need

Latest version **1.6.0** (published 2018-11-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install uc-dom
pnpm add uc-dom
yarn add uc-dom
bun add uc-dom
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.6.0 |
| Published | 2018-11-17 |
| First published | 2018-06-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Alexey Novikov |
| Maintainers | velocityzen |
| Keywords | unchained, ui, browser, dom, kiss |

## Links

- npm: https://www.npmjs.com/package/uc-dom
- Repository: https://github.com/unchainedui/dom
- Homepage: https://github.com/unchainedui/dom#readme
- Issues: https://github.com/unchainedui/dom/issues
- npm.io page: https://npm.io/package/uc-dom

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.6.0 (latest) — 2018-11-17
- 1.5.0 — 2018-11-09
- 1.4.0 — 2018-07-03
- 1.3.0 — 2018-06-26
- 1.2.0 — 2018-06-26
- 1.1.1 — 2018-06-25
- 1.1.0 — 2018-06-25
- 1.0.0 — 2018-06-21

## README

# Unchained UI

## DOM

[![NPM Version](https://img.shields.io/npm/v/uc-dom.svg?style=flat-square)](https://www.npmjs.com/package/uc-dom)
[![NPM Downloads](https://img.shields.io/npm/dt/uc-dom.svg?style=flat-square)](https://www.npmjs.com/package/uc-dom)

The only DOM abstraction you will need

### Usage

Just import methods you need

```js
import { get } from 'uc-dom'
```

### Methods

#### ready(fn)

Runs the `fn` when DOM is ready

#### get(selector, root)

returns:

* `Element` if selector is an id
* `HTMLCollection` if selector is a class
* `NodeList` for everything else

Parameters:

* `selector` — css selector
* `root` — default is _document_. root element to look in

#### closest(el, selector)

 Returns the closest ancestor of the current element (or the current element itself) which matches the selector

#### create(selector, content)

Creates and return an element from `selector`. If `content` is present, it will be added as innerHTML.

#### createNs(namespaceURI, selector)

Creates an element with the specified namespace URI and qualified selector.

#### attr(el, name, value)

Sets the value of an attribute on the specified element. If value is `undefined` returns the value of the attribute.

#### append(parent, el) / prepend(parent, el)

appends or prepends the `el` to the `parent`

#### appendTo(el, parent) / prependTo(el, parent)

appends or prepends the `el` to the `parent`

#### insertBefore(el, node)

Inserts a `node` before the `el` as a child of its parent node

#### insertAfter(el, node)

Inserts a `node` after the `el` as a child of its parent node

#### empty(el)

Removes all the child elements from the `el` element

#### remove(el)

Removes `el` from the DOM

#### on(el, event, selector, handler, options)

Sets up a `handler` function to be called whenever the specified event is delivered to the `el`. If `selector` is present will call the function if event target matches the `selector`

Returns a `handler` function

#### once(el, event, handler, options)

Sets up a `handler` function to be called whenever the specified event is delivered to the `el`. The `handler` function will be called only once.

Returns a `handler` function

#### off(el, event, handler, options)

Removes previously registered an `event` listener from the `el`. Returns a `handler` function.

#### onEvents(ctx, events)

Addes all event handlers from `events` object to `ctx.el`.

#### offEvents(ctx)

Removes all event handlers for `ctx.el` added with `onEvents` method.

#### addClass(el, string[, string])

Adds classes to the `el`.

#### removeClass(el, string[, string])

Removes classes from the `el`.

#### toggleClass(el, cls, force)

Toggle `cls` value. When the `force` is present, if is is evaluates to true, adds specified class value, and if it evaluates to false, removes it.

#### replaceClass(el, rx, newClass)

Replace all classes that match the `rx` regexp with `newClass`. Returns the number of new classes.

#### addDelayRemoveClass(el, cls, delay[, cb])

Adds `cls`, waits `delay` ms and then `removes` it. Returns timeoutID, it can be passed to clearTimeout() to cancel the timeout. The optional `cb` function will be called after class is removed.

### UI component methods

```js
import compose from 'uc-compose';
import domMethods from 'uc-dom/methods';

const MyUIComponent = function() {}

MyUIComponent.prototype = compose(
  domMethods,
  {
    componentMethod: function() {
      // add class active to this.el
      this.addClass('active');
    }
  }
)

```

This mixin adds following methods to your class (all the methods use `this.el`):

* addClass
* removeClass
* toggleClass
* replaceClass
* appendTo
* prependTo
* insertBefore
* insertAfter
* find
* attr

License MIT

© velocityzen

---
_Source: https://npm.io/package/uc-dom · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
