1.0.2 • Published 2 years ago

cg-modal v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

CgModal

CgModal - is simple and lightweight JS plugin of displaying modal windows which does not depend on jQuery or others.

For examples and demos, see link

Installation

  • Via npm npm install --save cg-modal
  • Via script tag (change @latest to latest version ex: @1.0.1)
<link
  rel="stylesheet"
  href="https://unpkg.com/cg-modal@latest/dist/cg-modal.min.css"
/>
<script src="https://unpkg.com/cg-modal@latest/dist/cg-modal.min.js"></script>
<!-- or -->
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/cg-modal@latest/dist/cg-modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/cg-modal@latest/dist/cg-modal.min.js"></script>

Usage

If you are using import/export syntax, you need to import library:

import "cg-modal";
// or
import "cg-modal/dist/cg-modal.min.js";
import "cg-modal/dist/cg-modal.min.css";

Append to document following HTML:

<!-- HTML of open modal button -->
<button type="button" data-open-modal="modal">Button</button>

<!-- HTML of modal window -->
<div class="modal" data-modal="modal">
  <div class="modal__wrapper">
    <div class="modal__content">
      <button class="modal__close-btn" type="button">Close</button>
      <!-- modal content -->
    </div>
  </div>
</div>

Note: Button which open modal window should contains data attribute (data-open-modal) and modal window should contains (data-modal)

Then, create an instance of class

const modals = new CgModal({
  // Options
});

API

Parameters

NameTypeDefaultDescription
selectorstring.modalSelector HTML element that will act as a modal window
openButtonSelectorstring[data-open-modal]Selector HTML element that will act as the button that opens the modal window
closeButtonSelectorstring.modal__close-btnThe HTML element selector that will act as the modal's close button. Should be inside this window.
wrapperClassstringmodal__wrapperClass of wrapper container inside modal
contentClassstringmodal__contentClass of content part of modal window
effectnullstring|nullModal open and close effect. There are following effects: "fade" \| "scaleCenter" \| "transformLeft" \| "transformBottom" \| "transformRight" \|"transformTop"
speednumber300Modal opening and closing animation speed
positionstringcenterModal position on screen. There are following values: "center" \| "left" \| "right"
addTechClassesbooleantrueAdding technical classes for the main elements of the modal window so that the default styles work. If the value is false, then you need to write all the styles for the modal window yourself. Classes such as "cg-modal", "cg-modal-wrapper", "cg-modal-content"

Methods

  • open(modalData) - open modal by data-attribute (data-modal)
  • close(modal) - close modal by Node Element
  • closeActive() - close active modal
  • update() - update instance

Events

You can listen different useful events. Using on parameter on initialization

const modals = new CgModal({
  // ...
  on: {
    update: function () {
      console.log("updated");
    },
  },
});

List of events:

  • beforeOpen - Event before open modal
  • afterOpen - Event after open modal
  • beforeClose - Event before close modal
  • afterClose - Event after close modal
  • openBtnClick - Event at moment of click on open button
  • update - Event at moment of update instance

License

The code and the documentation are released under the MIT License.