A JavaScript class for creating Bootstrap 5 Modals

Overview

BS5ModalJS

A JavaScript class for creating Bootstrap 5 Modals

Why BS5ModalJS?

BS5ModalJS removes the hassle of writing the bootstrap 5 modal html elements manually in your document. It will create those elements for you automatically when you instantiate it and prepends to your document's <body>. Useful if you intend to add modals throughout your website.

Prerequisite

In order for BS5ModalJS to work, you must refer bootstrap 5 main framework in your document.

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">

You may also include bootstrap 5 javascript library if you prefer to use javascript in toggling your modals.

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>

Usage

To use BS5ModalJS, just instantiate BSModal class and pass the required parameters. The constructor will create the necessary elements and prepends the created modal in your document's <body> automatically. The modal will be hidden by default and must be toggled to be visible.

Constructor Parameters

The parameters must be in the following order:

  1. id {string} required
    • Unique identifier of the modal. BSModal will use this parameter to toggle the visibility of the modal.
  2. title {string} required
    • The header of the modal.
  3. message {string} required
    • The main message of the modal. This parameter accepts HTML strings. String escaping is a must when passing html tags with attribute values.
  4. callBack {function} optional
    • A callback function to be executed when the user clicks the confirm button of the modal. This is where you should execute the necessary actions when the user clicks confirm.

Properties

modalElement {node}
A reference to the created modal element.
elementId {string}
Stores the unique identifier of the modal element.
elementTitle {string}
Stores the title of the modal element.
elementMessage {string}
Stores the message of the modal element.
modalDialog {node}
A reference to the modal dialog element.
cancelBtn {node}
A reference to the modal's cancel button element.
You can change the cancel button's text by changing the `innerText` property of this element
confirmBtn {node}
A reference to the modal's confirm button element.
You can change the confirm button's text by changing the `innerText` property of this element

Methods

toggleButton(innerText, classes)

Creates and returns a <button> element that toggles the modal when clicked. You must append this element manually in your document to be visible.

Parameters

Parameters must be in order:

  1. innerText {string} optional
    The text displayed in the button. Default is "Submit".
  2. classes {string} optional
    Specifies the class of the button. Can accept space-separated class names. Default is "btn btn-btn-outline-primary".
staticBackdrop()

Call this method if you don't want the modal to dismiss when clicking the background.

scrollableDialog()

Call this method to turn the modal's dialog into a scorallable dialog. This means that the header and footer will not scroll with the modal's content.

verticallyCentered()

Call this method to center the modal vertically

changeSize(size)

Call this method if you want to change the modal's size.

Parameters

size {string} required

  • xl - Extra large 1140px
  • lg - Large 800px
  • sm - Small 300px
removeAnimation()

Removes the animation (fade effect). The modal will simply appear / disappear insteading of fading to view.

Example

Here is an example of instantiating the class.

let myModal = new BSModal("myModal", "Test Modal", "Hi. I am a test modal! Are you sure you want to test me?", function() {
    console.log("Modal is working!");
});

The code above generates modal element that becomes the first child of your document's body. Now, let's create a toggle button.

let toggleBtn = myModal.toggleButton("Try me!");

Then, just append the toggleBtn element anywhere in the document. In this example, we'll append it to the document's body.

document.body.appendChild(toggleBtn);

Important Notes

BS5ModalJS works only with Bootstrap v5.0

Live Demo

Here is a live demo of this class in action.

Author

Written by: Eleazer Jr. Ababa
Email: [email protected]
Website: http://ababagaming.com

You might also like...

Pure JavaScript library that add .is-hover class instead of css :hover for mobile and desktop.

 Pure JavaScript library that add .is-hover class instead of css :hover for mobile and desktop.

MultiDeviceHover Pure JavaScript library that add .is-hover class instead of css :hover for mobile and desktop. Usage Install Using npm, install multi

Oct 5, 2021

Toaster is a Pure Javascript plugin for displaying toast notifications. It comes with different options e.g custom text or HTML message, duration, custom class, toggle close button, position, custom close icon and backgorund color.

Pure Javascript Toaster Requires Nothing Demo Toaster is a Pure Javascript plugin for displaying toast notifications. It comes with different options

Jun 21, 2022

A pure javascript class for paginating through any number of DOM elements

A pure javascript class for paginating through any number of DOM elements

PurePajinate A pure javascript class for paginating through any number of DOM elements. Inspired by Pajinate, a plugin for jQuery. Options Option Type

Nov 21, 2022

A refined tool for exploring open-source projects on GitHub with a file tree, rich Markdown and image previews, multi-pane multi-tab layouts and first-class support for Ink syntax highlighting.

A refined tool for exploring open-source projects on GitHub with a file tree, rich Markdown and image previews, multi-pane multi-tab layouts and first-class support for Ink syntax highlighting.

Ink codebase browser, "Kin" 🔍 The Ink codebase browser is a tool to explore open-source code on GitHub, especially my side projects written in the In

Oct 30, 2022

A simple class to embed a panoramic view in a website from six pictures

htmlcubemap A simple class to embed a panoramic view in a website from six pictures. It uses CSS with transforms. Demo here Usage Just call from javas

Dec 17, 2022

This simple project, show how work with async Fetch, function component and class component

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Feb 17, 2022

Modern Spatial Reference System Class. Supports EPSG Codes, PROJ4 String, and Well-Known Text.

spatial-reference-system Modern Spatial Reference System Class. supports EPSG Codes PROJ4 Strings ESRI and OGC Well-Known Text PRJ File install npm in

Jul 22, 2022

Small library to create classes without using class syntax.

Clazz.js Small library to create classes without using class syntax. Compatibility For internet explorer 11 or higher. Example script src="Clazz.js"

Dec 25, 2021

All five assignments and the final group project is done in class CSCI5410 (Serverless Data Processing) Fall 2021 of MACS at Dalhousie University.

Dalhousie University | Fall 2021 | CSCI5410 | SDP (Serverless Data Processing) All five assignments and the final group project is done in class CSCI5

Dec 26, 2021
Releases(v1.0.0)
Owner
null
Hemsida för personer i Sverige som kan och vill erbjuda boende till mÀnniskor pÄ flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Extends Bootstrap Tooltips and Popovers by adding custom classes. Available for Bootstrap 3 and Bootstrap 4.

Bootstrap Tooltip Custom Class Extends Bootstrap Tooltips and Popovers by adding custom classes. Available for Bootstrap 3 and Bootstrap 4. Define you

Andrei Victor Bulearca 14 Feb 10, 2022
A simple Form Validation Utility for Bootstrap 3, Bootstrap 4, and Bootstrap 5 for Humans.

bootstrap-validate A simple Form Validation Utility for Bootstrap 3, Bootstrap 4, and Bootstrap 5 for Humans. ?? Support us with Developer Merchandise

null 138 Jan 2, 2023
Demodal is a browser extension that automatically removes content blocking modals including paywalls, discount offers, promts to sign up or enter your email address and more.

Demodal Demodal is a browser extension that automatically removes content blocking modals including paywalls, discount offers, promts to sign up or en

Elbert Alias 225 Jan 4, 2023
A package that allows your bot of discord.js v13 & v14 to create the new awesome Discord Modals and interact with them

A package that allows your bot of discord.js v13 & v14 to create the new awesome Discord Modals and interact with them

MateoDeveloper 85 Dec 23, 2022
A utility for creating toggleable items with JavaScript. Inspired by bootstrap's toggle utility. Implemented in vanillaJS in a functional style.

LUX TOGGLE Demo: https://jesschampion.github.io/lux-toggle/ A utility for creating toggleable dom elements with JavaScript. Inspired by bootstrap's to

Jess Champion 2 Oct 3, 2020
Bootstrap Colorpicker is a modular color picker plugin for Bootstrap.

Bootstrap Colorpicker Bootstrap Colorpicker is a modular color picker plugin for Bootstrap 4. THIS PROJECT IS NOT MAINTAINED ANYMORE. After almost 10

Javi Aguilar 1.4k Dec 22, 2022
A JavaScript lib with more functions for the normal Date class.

A JavaScript lib with more functions for the normal Date class.

Ícaro Gabriel 4 Jan 26, 2022
It's a javascript Class which contains utility methods that simplify working with google maps web SDK

About GoogleMapsJSHelper It's a javascript Class which contains utility methods that simplify working with google maps web SDK Note: i used ES7 Class

Sami Alateya 6 Jul 23, 2022