2.4.0 • Published 2 months ago

accessible-minimodal v2.4.0

Weekly downloads
76
License
MIT
Repository
github
Last release
2 months ago

accessible-minimodal

Accessible, lightweight (≈ 3 kB gzip), stylish JavaScript (TypeScript) modal library.

Example

https://imhvost.github.io/accessible-minimodal.html

Install

npm install accessible-minimodal --save

Your html

Open button:

<!-- Open Button -->
<button id="modal-open-btn" data-modal-open="modal">Open Modal</button>

<!-- Modal -->
<div id="modal" aria-hidden="true" class="modal">
  <div tabindex="-1" class="modal-wrapp">
    <div
      role="dialog"
      aria-modal="true"
      aria-labelledby="modal-open-btn"
      class="modal-body"
    >
      <!-- Close Button -->
      <button
        type="button"
        class="modal-close-btn"
        aria-label="Close Modal"
        data-modal-close
      ></button>
      <!-- Modal Content -->
    </div>
  </div>
</div>

js

import { AccessibleMinimodal } from 'accessible-minimodal';

const Modal = new AccessibleMinimodal(/* { options } */);

Or include scripts

<script src="accessible-minimodal.umd.js"></script>
<script>
  const Modal = new AccessibleMinimodal(/* { options } */);
</script>

Options

interface AccessibleMinimodalSettings {
  animationDuration?: number /* defult: 400 */;
  classes?: {
    modal?: string /* defult: modal */;
    wrapp?: string /* defult: modal-wrapp */;
    body?: string /* defult: modal-body */;
    active?: string /* defult: active */;
    open?: string /* defult: open */;
    close?: string /* defult: close */;
  };
  disableScroll?:{
    use: boolean, /* defult: true */
    jumpingElements?: string | HTMLElement[]; /* defult: '' */;
  };
  focus?: {
    use?: boolean /* defult: true */;
    selectors?: string[] /* defult:
                            [
                              'button:not([disabled])',
                              '[href]',
                              'input:not([disabled]):not([type="hidden"]):not([aria-hidden])',
                              'select:not([disabled]):not([aria-hidden])',
                              'textarea:not([disabled]):not([aria-hidden])',
                              '[tabindex]:not([tabindex="-1"])',
                              '[contenteditable="true"]',
                            ]
                          */;
  };
  hash?: {
    open?: boolean /* defult: false */;
    add?: boolean /* defult: false */;
    remove?: boolean /* defult: false */;
  };
  multiple?: {
    use?: boolean /* defult: false */;
    closePrevModal?: boolean /* defult: false */;
  };
  on?: {
    beforeOpen?: (instance?: object) => void /* defult: () => true
                                                use return false for prevent open
                                              */;
    afterOpen?: (instance?: object) => void /* defult: () => ({}) */;
    beforeClose?: (instance?: object) => void /* defult: () => true
                                                  use return false for prevent close
                                              */;
    afterClose?: (instance?: object) => void /* defult: () => ({}) */;
  };
  outsideClose?: boolean /* defult: true */;
  style?: {
    use?: boolean /* defult: false */;
    width?: number /* defult: 400 */;
    valign?: string /* defult: center */;
    animation?: /* defult: from-bottom */
    | 'from-bottom'
      | 'from-top'
      | 'from-left'
      | 'from-right'
      | 'zoom-in'
      | 'zoom-out'
      | 'fade';
  };
  triggersAttrs?: {
    open?: string /* defult: data-modal-open */;
    close?: string /* defult: data-modal-close */;
    closeAll?: string /* defult: data-modal-close-all */;
  };
}

Methods

Open by id:

Modal.openModal('#my-modal');

Close by id:

Modal.closeModal('#my-modal');

Close current modal:

Modal.closeModal();

Close all modals:

Modal.closeAllModals();

Get scrollbar width:

Example of preventing displacement of position:fixed elements when scrolling is disabled:

const Modal = new AccessibleMinimodal({
  disableScroll: {
    jumpingElements: '.fixed-elements',
  },
});

Events

  • accessible-minimodal:before-open
  • accessible-minimodal:after-open
  • accessible-minimodal:before-close
  • accessible-minimodal:after-close

Example:

document
  .getElementById('modal')
  ?.addEventListener('accessible-minimodal:before-close', e => {
    console.log('accessible-minimodal:before-close');
    // use e.preventDefault() for prevent close
  });
2.4.0

2 months ago

2.3.0

2 months ago

2.3.1

2 months ago

2.2.3

4 months ago

2.2.2

5 months ago

2.2.1

7 months ago

2.2.0

7 months ago

2.1.0

9 months ago

2.0.23

9 months ago

2.0.15

11 months ago

2.0.16

11 months ago

2.0.14

12 months ago

2.0.19

11 months ago

2.0.17

11 months ago

2.0.22

11 months ago

2.0.20

11 months ago

2.0.21

11 months ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.11

1 year ago

2.0.7

1 year ago

2.0.12

1 year ago

2.0.6

1 year ago

2.0.9

1 year ago

2.0.10

1 year ago

2.0.8

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

1.2.0

3 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.13

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.1

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.11

3 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago