# stimp

> Small lib for organize your JS by modules with state management and DOM elements autoselection

Latest version **1.3.4** (published 2020-06-08) · MIT license · 0 weekly downloads

## Install

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

## 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.3.4 |
| Published | 2020-06-08 |
| First published | 2018-03-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 38.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ilya Pomazkin |
| Maintainers | ilya_pomazkin |
| Keywords | state, state management, modules, vanilla js, dom, dom selection, microframework, dependencies |

## Links

- npm: https://www.npmjs.com/package/stimp
- Repository: https://github.com/ipomazkin/stimp.js
- Homepage: https://github.com/ipomazkin/stimp.js#readme
- Issues: https://github.com/ipomazkin/stimp.js/issues
- npm.io page: https://npm.io/package/stimp

## Dependencies (2)

- [redux](https://npm.io/package/redux.md) ^4.0.5
- [toposort](https://npm.io/package/toposort.md) ^2.0.2

## 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.3.4 (latest) — 2020-06-08
- 1.3.3 — 2020-06-08
- 1.3.2 — 2020-06-08
- 1.3.1 — 2020-06-08
- 1.3.0 — 2020-06-08
- 1.2.10 — 2020-06-08
- 1.2.9 — 2020-06-07
- 1.2.8 — 2020-06-07
- 1.2.7 — 2020-05-13
- 1.2.6 — 2020-05-10
- 1.2.5 — 2020-05-10
- 1.2.4 — 2020-05-10
- 1.2.3 — 2020-05-10
- 1.2.2 — 2020-05-10
- 1.2.1 — 2020-05-10
- … 7 more at https://npm.io/package/stimp/versions

## README

![stimp.js logo](https://github.com/ipomazkin/stimp.js/raw/master/stimp.png "stimp.js")

# stimp.js
Small lib for organize your JS by modules with state management and DOM elements autoselection

JS docs [here](https://ipomazkin.github.io/stimp.js/)

## Install
via NPM
```
npm i stimp
```
or via Yarn
```
yarn add stimp
```
or from CDN:
```html
<script src="https://unpkg.com/stimp@latest/dist/stimp.js"></script>
```

## Features
### Apps and modules for project organization
```javascript
import Stimp from 'stimp';

window.app = new Stimp.App('main'); // create app with name "main"

const helloModule = app.addModule(Stimp.Module, 'hello'); // add module with name "hello"
helloModule.onInit(() => { // add some module code
  console.log('I have just init!');
});

app.init(); // init the app
```
You can access the module by "app.modules.hello".

### State management in React.js style
```javascript
const statefulModule = app.addModule(Stimp.Module, 'statefulModule', { // add stateful module
  isShow: false, // initial state
});

statefulModule.onInit(() => {
  setInterval(() => {
    statefulModule.setState({ // setting the new state
      isShow: !statefulModule.state.isShow
    });
  }, 1000);
});

statefulModule.onUpdateIsShow = (currentVal, prevVal) => { // add reaction on state change
  console.log(`now isShow is ${currentVal}, but before it was ${prevVal}`);
};
```

### DOM elements auto selection for each module
On frontend you often need to interact with DOM. To do that you need to select elements (by jQuery or native JS functions).
Stimp can do it for you without any JS-code. You just need to add some HTML-attributes to target tags.
For example, we have this html:
```html
<div -m="menu" class="menu">
  <div class="menu__cont">

    <button -ch="burger" class="menu__burger">open</button>

    <nav -ch="nav">
      <ul>
        <li -ch="navEl" -i="link1"><a href="#">link 1</a></li>
        <li -ch="navEl" -i="link2"><a href="#">link 2</a></li>
        <li -ch="navEl" -i="link3"><a href="#">link 3</a></li>
        <li -ch="navEl" -i="link4"><a href="#">link 4</a></li>
        <li -ch="navEl" -i="link5"><a href="#">link 5</a></li>
        <li -ch="navEl" -i="link6"><a href="#">link 6</a></li>
        <li -ch="navEl" -i="link7"><a href="#">link 7</a></li>
      </ul>
    </nav>

  </div>
</div>
```
There are 3 custom attributes:
- "-m" for module scope - use here exact module name
- "-ch" for module children - use here any value
- "-i" for module's children list, but with separate it by ID - use here any value

In JS we add this code:
```javascript
const menu = app.addModule(Stimp.DOMInteractModule, 'menu', { // add dom interactive module
  isShow: false, // initial state
});

menu.onAfterDomInit(() => { // after DOM was selected
  let dom = menu.dom; // access to the DOM elements
  let root = menu.dom.root; // access to the root module element with attribute "-m"
  let burger = menu.dom.burger; // access to the child element with attribute "-ch='burger'"
  let navEls = menu.dom.navEl; // access to the child elements with attribute -ch="navEl" -i="*"
  let firstNavEl = navEls.link1; // access to the child elements with attribute -ch="navEl" -i="link1"

  burger.addEventListener('click', () => { // add some state changing on burger button click
    menu.setState({ isShow: !menu.state.isShow });
  });
});

menu.onUpdateIsShow = (currentVal, prevVal) => { // add reaction on state change
  if (currentVal) { // if isShow is true - add class 'show' to the module root
    menu.dom.root.classList.add('show')
  } else { // if isShow is false - remove class 'show'
    menu.dom.root.classList.remove('show')
  }
};
```

### Modules init by their depends (when module "A" should be initialized before module "B")
In some cases you need to init modules in exact order. To do that you can use module dependencies.

```javascript
const a = app.addModule(Stimp.DOMInteractModule, 'a', {}); // module a
const b = app.addModule(Stimp.DOMInteractModule, 'b', {}, ['a']); // module b with module a in dependencies
const c = app.addModule(Stimp.DOMInteractModule, 'c', {}, ['a']); // module c with module b in dependencies
const d = app.addModule(Stimp.DOMInteractModule, 'd', {}, ['c', 'a']); // module d with module c and a in dependencies
```

The modules will init in this order:
- module a
- module b and module c in any order
- module d after module a and c was init

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