# domodule

> Class-based JavaScript modules accessible via the DOM.

Latest version **8.2.1** (published 2024-07-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install domodule
pnpm add domodule
yarn add domodule
bun add domodule
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 8.2.1 |
| Published | 2024-07-23 |
| First published | 2016-05-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 128.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | First+Third |
| Maintainers | jga, dawnerd, ecwillis, orthagonal1, alaguna, aleperez92 |
| Keywords | module, browser, javascript, typescript |

## Links

- npm: https://www.npmjs.com/package/domodule
- Repository: https://github.com/firstandthird/domodule
- Homepage: https://github.com/firstandthird/domodule#readme
- Issues: https://github.com/firstandthird/domodule/issues
- npm.io page: https://npm.io/package/domodule

## Dependencies (3)

- [aug](https://npm.io/package/aug.md) ^4.0.0
- [attrobj](https://npm.io/package/attrobj.md) ^3.1.0
- [domassist](https://npm.io/package/domassist.md) ^2.2.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 8.2.1 (latest) — 2024-07-23
- 8.2.0 — 2024-07-22
- 8.1.0 — 2021-01-07
- 8.0.1 — 2020-12-04
- 8.0.0 — 2020-11-18
- 7.0.0 — 2020-01-24
- 6.2.0 — 2020-01-13
- 6.1.0 — 2019-06-26
- 6.0.0 — 2019-02-22
- 5.2.0 — 2018-10-03
- 5.1.3 — 2017-04-19
- 5.1.2 — 2017-04-06
- 5.1.1 — 2017-02-16
- 5.1.0 — 2017-02-09
- 5.0.3 — 2017-02-09
- … 18 more at https://npm.io/package/domodule/versions

## README

<h1 align="center">domodule</h1>

<p align="center">
  <a href="https://github.com/firstandthird/domodule/actions/workflows/test.yaml">
    <img src="https://img.shields.io/github/actions/workflow/status/firstandthird/domodule/test.yaml?branch=main&label=Tests&style=for-the-badge" alt="Test Status"/>
  </a>
  <a href="https://github.com/firstandthird/domodule/actions/workflows/lint.yaml">
    <img src="https://img.shields.io/github/actions/workflow/status/firstandthird/domodule/lint.yaml?branch=main&label=Lint&style=for-the-badge" alt="Lint Status"/>
  </a>
  <a href="https://www.npmjs.com/package/domodule">
    <img src="https://img.shields.io/npm/v/domodule.svg?label=npm&style=for-the-badge" alt="NPM Version" />
  </a>
</p>

Domodule is a helper that allows you to create JavaScript modules with minimal effort while keeping code size down. It automatically binds to elements using the `data-module` attribute.

## Installation

```sh
npm install domodule
```

_or_

```sh
yarn add domodule
```

## Example usage

```html
<div data-module="ExampleModule" data-module-title="Example Module">
  <div data-name="title"></div>
  <button type="button" data-action="click">Show Title</button>
</div>
```

```js
class ExampleModule extends Domodule {
  click() {
    this.els.title.textContent = this.options.title;
  }
}
```

### Inherited methods

Each module has access to these helper methods.

- `find(<selector>)` - Returns an array of matched elements inside of the module.
- `findOne(<selector>)` - Returns the first matched element inside of the module.
- `findByName(<element name>)` - Alternative to `this.els[name]`.
- `getOption(<option>)` - Returns value of an option (`data-module-*`).
- `setupActions()` - Used to bind actions. Useful if the module adds elements in after Domodule inits. **Note:** Called by default. Calling again wont re-process elements.
- `setupNamed()` - Same as `setupActions()` but binds to named elements. **Note:** Called by default. Calling again wont re-process elements.

### Static Methods

- `Domodule.getInstance(<element>)` - Returns an instance of the module.
- `Domodule.discover(<dom node, array of nodes, selector>)` - Looks for `data-module` inside of matched elements. Will skip elements already processed. Calling just `Domodule.discover()` will search for all modules in the body.

### Named elements

Adding `data-name=<name>` to an element will bind it to `this.els.<name>`.
Adding the same `data-name` to multiple elements will change `this.els.<name>` to an `Array<HTMLElement>`, sorted in DOM order.

### Actions

Adding `data-action=<name>` to an element binds it to click (or optionally `data-action-type=<touch|mouseover|etc>`). Values can be passed through the event by adding data attributes starting with `data-action-`.

Create a method in the class matching the name given in the data attribute. Method will be passed: (the element, event object, values)

### Properties

- `this.el` - References the module element.
- `this.els` - Object containing all `data-name` elements
- `this.options` - Object containing anything passed in after `data-module-` (similar to action values).

#### constructor

A constructor method can be used but you will need to call `super(el)`. Constructor method gets el as it's only (and required) parameter. `super(el)` should be called before your code unless you need to modify core behavior. Element binding happens only when super is called.

### Required options

A module can pass an array of required options to the `super()` method. Module will fail to init if any number of the required options are not present. Example: `super(el, ['someOption', 'anotherOption'])`

---

<a href="https://firstandthird.com"><img src="https://firstandthird.com/_static/ui/images/safari-pinned-tab-62813db097.svg" height="32" width="32" align="right"></a>

_A [First + Third](https://firstandthird.com) Project_

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