# open-modal-js

> Vanilla JS modal/popup

Latest version **0.5.0** (published 2022-05-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install open-modal-js
pnpm add open-modal-js
yarn add open-modal-js
bun add open-modal-js
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2022-05-05 |
| First published | 2022-05-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=12 |
| Dependencies | 0 |
| Unpacked size | 34.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Alexand Plesca |
| Maintainers | kloshar4o |
| Keywords | modal, popup, vanillajs, vanilla-js, typescript, vite, prettier, eslint, cypress |

## Links

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

## Alternatives

- [eslint-plugin-sonarjs](https://npm.io/package/eslint-plugin-sonarjs.md) — 2.9M weekly downloads
- [eslint-config-expo](https://npm.io/package/eslint-config-expo.md) — 1.5M weekly downloads
- [@matter/protocol](https://npm.io/package/@matter/protocol.md) — 63.5K weekly downloads
- [@eventcatalog/linter](https://npm.io/package/@eventcatalog/linter.md) — 24.8K weekly downloads
- [@pandacss/eslint-plugin](https://npm.io/package/@pandacss/eslint-plugin.md) — 18.7K weekly downloads

## Recent versions

- 0.5.0 (latest) — 2022-05-05
- 0.3.4-beta.4 — 2022-05-05
- 0.3.4-beta.3 — 2022-05-05
- 0.3.4-beta.2 — 2022-05-05
- 0.3.4-beta.0 — 2022-05-05
- 0.3.5 — 2022-05-04
- 0.3.4 — 2022-05-04
- 0.3.3 — 2022-05-04
- 0.3.2 — 2022-05-04

## README

# JSModal

## Description

Vanilla JS modal, that simply adds a `.open` class to show the modal.

## Quick start

Download source files
- css from _assets/css/default-modal-styles.css_ (feel free to use your own style)
- js from _dist/open-modal-js.umd.js_

HTML:
```html

<div id="modal" class="modal">
  <div class="modal-overlay"></div>
  <div class="modal-card">
    <div class="modal-body">
      <div class="modal-content">Content</div>
      <div class="modal-footer">
        <button class="modal-close">Close</button>
      </div>
    </div>
  </div>
</div>
```
JavaScript:
```javascript
new OpenModalJs('modal')
```

## Details

<details>

<summary> 
Template 
</summary>

Include like in the example from /example/index.html

```html
<link rel="stylesheet" href="default-modal-styles.css">
<script src="open-modal-js.umd.js"></script>
<script> const modal = new OpenModalJs('modal', true) </script>
<button onclick="modal.openModal()">Open modal</button>

<div id="modal" class="modal">
    <div class="modal-overlay"></div>
    <div class="modal-card">
        <div class="modal-body">
            <div class="modal-header">ModalController</div>
            <div class="modal-content">Content</div>
            <div class="modal-footer">
                <button class="modal-close">Close</button>
            </div>
        </div>
    </div>
</div>
```
</details>

<details>
<summary>
Arguments
</summary>

```javascript
new OpenModalJs(modalId, config, callback)
```


###`modalId` HTML id Attribute (required)
### `config` OpenModalJs configurations

| Property           | Type      | Default           | Description                                                                                                                                       |
|--------------------|-----------|-------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
| `openOnInit`       | `boolean` | `false`           | _Make the modal open by default_                                                                                                                  |
| `openClass`        | `string`  | `"open"`          | _The css class that will be added and removed when opening closing modal_                                                                         |
| `overlayClass`     | `string`  | `"modal-overlay"` | _CSS class for the overlay which will close the modal on click by default. <br/> Set to empty string if you want to disable close on click_       |
| `closeButtonClass` | `string`  | `"modal-close"`   | _CSS class for the close buttons which will close the modal on click by default. <br/> Set to empty string if you want to disable close on click_ |

### `Callback`

Object of callbacks, which are fired when
- `callback.onOpening` - .open css class added ("transitionstart")
- `callback.onOpened` - opening css transition end or canceled ("transitionend" or "transitioncancel")
- `callback.onClosing` - .open css class removed ("transitionstart")
- `callback.onClosed` - closing css transition end ("transitionend" or "transitioncancel")

if the modal doesn't have css `transition-duration`both events will fire  
`onOpening` with `onOpened` or  
`onClosing` with `onClosed`

All props example:
```javascript
new OpenModalJs(
  'some-id', 
  {
    openOnInit: false,
    openClass: "open",
    overlayClass: "modal-overlay",
    closeButtonClass: "modal-close",
  }, 
  {
    onOpening: () => {},
    onOpened: () => {},
    onClosing: () => {},
    onClosed: () => {},
  }
)

```
</details>


<details>
<summary>
Open/Close
</summary>

```javascript
const modal = new OpenModalJs('some-element-id')

//Open the modal
modal.openModal()
//or
modal.isOpen = true

//Close modal
modal.closeModal()
//or
modal.isOpen = false
```
</details>

<details>
<summary>
Events
</summary>

**Listen for events**
```javascript
const handler = (event) => {
  const modal = event.detail; //<- access the modal object
    
  if(modal.modalId === 'some-element-id'){
    //Do something
  }
}

document.addEventListener('opening:modal', handler) // .open css class added ("transitionstart")
document.addEventListener('opened:modal', handler) // opening css transition end or canceled ("transitionend" or "transitioncancel")
document.addEventListener('closing:modal', handler) // .open css class removed ("transitionstart")
document.addEventListener('closed:modal', handler) // closing css transition end ("transitionend" or "transitioncancel")
```
if the modal doesn't have css `transition-duration`both events will fire  
`opening:modal` with `opened:modal` or  
`closing:modal` with `closed:modal`

</details>

## Dev env
Clone/download repo and install dependencies
```npm
npm i
```

### **Hot reload:**
```npm
npm run dev
```

### **Cypress:**
```npm
npm run cy:open
```
Cypress uses the dist folder,
you can reload build on change via:
```npm
npm run build:watch
```

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