Default Modal
Click the below toggle button to reveal the default modal example.
<button class="btn btn-primary" data-modal-toggle="#modal_1">
Show Modal
</button>
<div class="modal" data-modal="true" id="modal_1">
<div class="modal-content max-w-[600px] top-[20%]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal components are commonly used for various purposes such as displaying login forms, confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through specific tasks or actions while maintaining the context of the underlying webpage.
</div>
</div>
</div>
Footer
Table
This example demonstrates a modal containing a table.
<button class="btn btn-primary" data-modal-toggle="#modal_1_3">
Show Modal
</button>
<div class="modal" data-modal="true" id="modal_1_3">
<div class="modal-content max-w-[600px] top-[10%]">
<div class="modal-header border-b-0">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-table scrollable-auto">
<table class="table table-border align-middle text-gray-700 font-medium text-sm" id="my_table_2">
<thead>
<tr>
<th class="min-w-[250px]">
<span class="sort asc">
<span class="sort-label">
Person
</span>
<span class="sort-icon">
</span>
</span>
</th>
<th class="min-w-[165px]">
<span class="sort">
<span class="sort-label">
Browser
</span>
<span class="sort-icon">
</span>
</span>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="flex items-center gap-2.5">
<img alt="" class="h-9 rounded-full" src="/static/metronic/tailwind/docs/dist/assets/media/avatars/300-1.png"/>
<a class="leading-none font-semibold text-gray-900 hover:text-primary" href="#">
Esther Howard
</a>
</div>
</td>
<td>
<div class="flex items-center gap-2">
<i class="ki-outline ki-chrome text-gray-500 text-lg">
</i>
<span class="text-gray-700">
Chrome on Mac OS X
</span>
</div>
</td>
</tr>
<tr>
<td>
<div class="flex items-center gap-2.5">
<img alt="" class="h-9 rounded-full" src="/static/metronic/tailwind/docs/dist/assets/media/avatars/300-2.png"/>
<a class="leading-none font-semibold text-gray-900 hover:text-primary" href="#">
Tyler Hero
</a>
</div>
</td>
<td>
<div class="flex items-center gap-2">
<i class="ki-outline ki-chrome text-gray-500 text-lg">
</i>
<span class="text-gray-700">
Chrome on Windows 7
</span>
</div>
</td>
</tr>
<tr>
<td>
<div class="flex items-center gap-2.5">
<img alt="" class="h-9 rounded-full" src="/static/metronic/tailwind/docs/dist/assets/media/avatars/300-5.png"/>
<a class="leading-none font-semibold text-gray-900 hover:text-primary" href="#">
Leslie Alexander
</a>
</div>
</td>
<td>
<div class="flex items-center gap-2">
<i class="ki-outline ki-chrome text-gray-500 text-lg">
</i>
<span class="text-gray-700">
Chrome on Mac OS X
</span>
</div>
</td>
</tr>
<tr>
<td>
<div class="flex items-center gap-2.5">
<img alt="" class="h-9 rounded-full" src="/static/metronic/tailwind/docs/dist/assets/media/avatars/300-6.png"/>
<a class="leading-none font-semibold text-gray-900 hover:text-primary" href="#">
Brooklyn Simmons
</a>
</div>
</td>
<td>
<div class="flex items-center gap-2">
<i class="ki-outline ki-chrome text-gray-500 text-lg">
</i>
<span class="text-gray-700">
Chrome on Windows 10
</span>
</div>
</td>
</tr>
<tr>
<td>
<div class="flex items-center gap-2.5">
<img alt="" class="h-9 rounded-full" src="/static/metronic/tailwind/docs/dist/assets/media/avatars/300-7.png"/>
<a class="leading-none font-semibold text-gray-900 hover:text-primary" href="#">
Darlene Robertson
</a>
</div>
</td>
<td>
<div class="flex items-center gap-2">
<i class="ki-outline ki-chrome text-gray-500 text-lg">
</i>
<span class="text-gray-700">
Chrome on Mac OS X
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer border-t-0 justify-end">
<div class="flex gap-4">
<button class="btn btn-light" data-modal-dismiss="true">
Cancel
</button>
<button class="btn btn-primary">
Submit
</button>
</div>
</div>
</div>
</div>
Center
Utilize the
modal-center
class to both, vertically and horizontally center the modal.
<button class="btn btn-primary" data-modal-toggle="#modal_2">
Show Modal
</button>
<div class="modal" data-modal="true" id="modal_2">
<div class="modal-content modal-center-y max-w-[600px]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
</div>
</div>
</div>
Scrollable
Apply the
scrollable-y
class to the
modal-body
element to enable vertical scrolling within the modal body.
<button class="btn btn-primary" data-modal-toggle="#modal_3">
Show Modal
</button>
<div class="modal" data-modal="true" id="modal_3">
<div class="modal-content max-w-[600px] top-[10%]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body scrollable-y py-0 my-5 pl-6 pr-3 mr-3 h-[300px] max-h-[95%]">
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
</div>
<div class="modal-footer justify-end">
<div class="flex gap-4">
<button class="btn btn-light" data-modal-dismiss="true">
Cancel
</button>
<button class="btn btn-primary">
Submit
</button>
</div>
</div>
</div>
</div>
Long Content
Apply the
scrollable-y
class to the
modal-body
element to enable vertical scrolling within the modal body.
<button class="btn btn-primary" data-modal-toggle="#modal_3_1">
Show Modal
</button>
<div class="modal" data-modal="true" id="modal_3_1">
<div class="modal-content modal-center-y max-w-[600px] max-h-[95%]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body scrollable-y py-0 my-5 pl-6 pr-3 mr-3">
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
</div>
<div class="modal-footer justify-end">
<div class="flex gap-4">
<button class="btn btn-light" data-modal-dismiss="true">
Cancel
</button>
<button class="btn btn-primary">
Submit
</button>
</div>
</div>
</div>
</div>
Static Backdrop
Use the attribute
data-modal-backdrop-static="true"
to display a modal with a static backdropn that prevents modal closure on click on it.
<button class="btn btn-primary" data-modal-toggle="#modal_4">
Show Modal
</button>
<div class="modal" data-modal="true" data-modal-backdrop-static="true" id="modal_4">
<div class="modal-content max-w-[600px] top-[10%]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal components are commonly used for various purposes such as displaying login forms, confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through specific tasks or actions while maintaining the context of the underlying webpage.
</div>
</div>
</div>
No Backdrop
Use the attribute
data-modal-backdrop="false"
to display a modal without the backdrop layer.
<button class="btn btn-primary" data-modal-toggle="#modal_5">
Show Modal
</button>
<div class="modal modal-dialog modal-center max-w-[600px]" data-modal="true" data-modal-backdrop="false" id="modal_5">
<div class="modal-content shadow-lg border">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal components are commonly used for various purposes such as displaying login forms, confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through specific tasks or actions while maintaining the context of the underlying webpage.
</div>
</div>
</div>
Toggle Modals
Switch between multiple modals for different actions.
<button class="btn btn-primary" data-modal-toggle="#modal_6_1">
Show Modal
</button>
<div class="modal" data-modal="true" id="modal_6_1">
<div class="modal-content modal-center max-w-[600px]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title 1
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal components are commonly used for various purposes such as displaying login forms, confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through specific tasks or actions while maintaining the context of the underlying webpage.
</div>
<div class="modal-footer justify-end">
<div class="flex gap-4">
<button class="btn btn-light" data-modal-dismiss="true">
Cancel
</button>
<button class="btn btn-primary" data-modal-toggle="#modal_6_2">
Launch Another Modal
</button>
</div>
</div>
</div>
</div>
<div class="modal" data-modal="true" id="modal_6_2">
<div class="modal-content modal-center max-w-[600px]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title 2
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal components are commonly used for various purposes such as displaying login forms, confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through specific tasks or actions while maintaining the context of the underlying webpage.
Modal components are commonly used for various purposes such as displaying login forms, confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through specific tasks or actions while maintaining the context of the underlying webpage.
</div>
<div class="modal-footer justify-end">
<div class="flex gap-4">
<button class="btn btn-light" data-modal-dismiss="true">
Cancel
</button>
<button class="btn btn-primary" data-modal-toggle="#modal_6_1">
Launch Previouse Modal
</button>
</div>
</div>
</div>
</div>
Persistent
Use the
data-modal-persistent="true"
attribute to prevent modal closure when clicking outside of it.
<button class="btn btn-primary" data-modal-toggle="#modal_6">
Show Modal
</button>
<div class="modal modal-dialog modal-center max-w-[600px]" data-modal="true" data-modal-backdrop="false" data-modal-persistent="true" id="modal_6">
<div class="modal-content shadow-lg border">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal components are commonly used for various purposes such as displaying login forms, confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through specific tasks or actions while maintaining the context of the underlying webpage.
</div>
</div>
</div>
Input Autofocus
To automatically set focus on an input element within a modal when it appears,
apply the
data-modal-autofocus="true"
attribute to that input.
<button class="btn btn-primary" data-modal-toggle="#modal_7">
Show Modal
</button>
<div class="modal" data-modal="true" id="modal_7">
<div class="modal-content max-w-[600px] top-[10%]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
<input class="input" data-modal-input-focus="true" placeholder="Modal input..." type="text"/>
<p class="pt-5">
Modal components are commonly used for various purposes such as displaying login forms, confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through specific tasks or actions while maintaining the context of the underlying webpage.
</p>
</div>
</div>
</div>
Page Scroll
Apply the
data-modal-disable-scroll="false"
attribute to allow the page scrollbar to remain enabled.
<button class="btn btn-primary" data-modal-toggle="#modal_8">
Show Modal
</button>
<div class="modal" data-modal="true" data-modal-disable-scroll="false" id="modal_8">
<div class="modal-content max-w-[600px] max-h-full top-[10%]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
</div>
</div>
</div>
Full Width
The example below demonstrates the implementation of a full-width modal.
<button class="btn btn-primary" data-modal-toggle="#modal_9">
Show Modal
</button>
<div class="modal" data-modal="true" id="modal_9">
<div class="modal-content modal-center-y">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
</div>
</div>
</div>
Overlay
The example below illustrates an overlay modal that spans the entire screen size.
<button class="btn btn-primary" data-modal-toggle="#modal_10">
Show Modal
</button>
<div class="modal" data-modal="true" id="modal_10">
<div class="modal-content modal-overlay">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body scrollable-y-auto">
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
</div>
</div>
</div>
Draggable
Description goes here.
<button class="btn btn-primary" data-modal-toggle="#modal_draggable">
Show Modal
</button>
<div class="modal" data-modal="true" id="modal_draggable">
<div class="modal-content max-w-[600px] top-[10%]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body scrollable-y-auto">
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
</div>
</div>
</div>
Sizes
Explore various modal width sizes set using Tailwind CSS width classes.
<button class="btn btn-light" data-modal-toggle="#modal_11_1">
Small
</button>
<button class="btn btn-light" data-modal-toggle="#modal_11_2">
Medium
</button>
<button class="btn btn-light" data-modal-toggle="#modal_11_3">
Large
</button>
<div class="modal" data-modal="true" id="modal_11_1">
<div class="modal-content modal-center-y max-w-[400px]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
</div>
</div>
</div>
<div class="modal" data-modal="true" id="modal_11_2">
<div class="modal-content modal-center-y lg:max-w-[800px]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
</div>
</div>
</div>
<div class="modal" data-modal="true" id="modal_11_3">
<div class="modal-content modal-center-y lg:max-w-[1100px]">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal components are commonly used for various purposes such as displaying login forms,
confirming actions, presenting multimedia content, or showing detailed information.
They provide a non-intrusive way to engage users and guide them through
specific tasks or actions while maintaining the context of the underlying webpage.
</div>
</div>
</div>
Source Code
The following source files manage the JavaScript behavior and CSS styles of the Tailwind CSS Modal component.
Upon building the assets using
Webpack Build Tools
,
these files are compiled and added into the
dist/assets/css/styles.css
and
dist/assets/js/core.bundle.js
bundles, making them available globally across all pages.
File | Description |
---|---|
src/core/components/modal/modal.ts
|
The TypeScript source file that manages the JavaScript behavior of the Tailwind CSS Modal component. |
src/core/plugins/components/modal.js
|
The Tailwind CSS compatible plugin written in PostCSS for applying CSS styles to the Tailwind CSS Modal component. |
HTML Markup
The following markup outlines the core structure of the Tailwind CSS Modal component.
<button class="btn btn-primary" data-modal-toggle="#my_modal">
Show Modal
</button>
<div class="modal max-w-[600px] top-[10%]" data-modal="true" id="my_modal">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal body goes here.
</div>
</div>
</div>
Attributes
The following HTML attributes are utilized by Tailwind CSS Modal component to control its JavaScript behavior.
HTML Attribute | Description |
---|---|
data-modal-dismiss="true"
|
Identifies an element that, upon click, hides the modal. |
data-modal-input-focus="true"
|
Specifies an input that gains focus when the modal opened. |
Classes
This table outlines the custom CSS classes utilized for styling the default components of the Tailwind CSS Modal.
Class | Description |
---|---|
modal
|
Base class for the modal component. It wraps the entire modal content and provides basic styling and behavior. |
modal-content
|
Represents the main content area of the modal. It contains the header, body, and footer sections of the modal content. |
modal-header
|
Styles the header section of the modal. Typically contains the title and any close button or other controls. |
modal-title
|
Applied to the title element within the modal header. Used to style and position the title text. |
modal-body
|
Represents the body section of the modal content. Contains the main content or form elements displayed within the modal. |
open
|
Applied to a specific modal to indicate that it should be displayed or opened. Typically triggers the modal to become visible. |
hidden
|
Sets a specific modal to be hidden initially. Can be used to hide the modal until it needs to be displayed. |
Data Attribute Initialization
Auto-initialize all KTModal instances on page load by adding the
data-modal="true"
attribute to your modal elements. This triggers on the fly initialization by the KTModal JavaScript component.
<button class="btn btn-primary" data-modal-toggle="#my_modal">
Show Modal
</button>
<div class="modal max-w-[600px] top-[10%]" data-modal="true" id="my_modal">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal body goes here.
</div>
</div>
</div>
These attributes allow you to set options for the KTModal component during initialization.
The values you provide as strings are automatically converted to the appropriate
KTModal Options
.
HTML Attribute | Type | Default | Description |
---|---|---|---|
data-modal
|
boolean
|
true
|
Used to auto-initialize KTModal instances on page load. Alternatively, you can remove it and perform initialization using JavaScript. |
data-modal-zindex
|
string
|
"auto"
|
Specifies the z-index of the modal. |
data-modal-backdrop
|
boolean
|
true
|
Controls whether a backdrop is displayed behind the modal. |
data-modal-backdrop-class
|
string
|
"transition-all duration-300 fixed inset-0 bg-gray-900 opacity-25"
|
Defines the CSS classes applied to the modal backdrop. |
data-modal-backdrop-static
|
boolean
|
false
|
Specifies whether the modal backdrop is static, preventing modal closure on click. |
data-modal-keyboard
|
boolean
|
false
|
Controls whether the modal can be closed by pressing the Escape key. |
data-modal-disable-scroll
|
boolean
|
true
|
Specifies whether scrolling on the page is disabled when the modal is open. |
data-modal-persistent
|
boolean
|
false
|
Controls whether the modal remains open after clicking outside of it. |
data-modal-focus
|
boolean
|
true
|
Specifies whether an input defined with the
data-modal-input-focus="true"
attribute gains focus when the modal opened.
|
data-modal-hidden-class
|
string
|
"hidden"
|
Defines the CSS class applied to hide the modal. |
JavaScript Initialization
To initialize a new modal component, pass the corresponding DOM element and configuration options to the KTModal class constructor.
// Modal toggle element
const modalEl = document.querySelector('#my_modal');
// Configuration options(optional)
const options = {
backdropClass: 'transition-all duration-300 fixed inset-0 bg-gray-900 opacity-25',
backdrop: true,
disableScroll: true,
persistent: true
};
// Initialize object
const modal = new KTModal(modalEl, options);
<div class="modal max-w-[600px] top-[10%]" data-modal="false" id="my_modal">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal body goes here.
</div>
</div>
</div>
To initialize the modal with JavaScript, use
data-modal="false"
attribute instead.
This prevents automatic initialization on page load.
Options
This table outlines the configuration options for initialization of Tailwind CSS Modal instances, using the KTModal JavaScript component.
Option | Type | Default | Description |
---|---|---|---|
zindex
|
string
|
"auto"
|
Specifies the z-index of the modal. |
backdrop
|
boolean
|
true
|
Controls whether a backdrop is displayed behind the modal. |
backdropClass
|
string
|
"transition-all duration-300 fixed inset-0 bg-gray-900 opacity-25"
|
Defines the CSS classes applied to the modal backdrop. |
backdropStatic
|
boolean
|
false
|
Specifies whether the modal backdrop is static, preventing modal closure on click. |
keyboard
|
boolean
|
false
|
Controls whether the modal can be closed by pressing the Escape key. |
disableScroll
|
boolean
|
true
|
Specifies whether scrolling on the page is disabled when the modal is open. |
persistent
|
boolean
|
false
|
Controls whether the modal remains open after clicking outside of it. |
focus
|
boolean
|
true
|
Specifies whether an input defined with the
input-focus="true"
attribute gains focus when the modal opened.
|
hiddenClass
|
string
|
"hidden"
|
Defines the CSS class applied to hide the modal. |
Utilities
Manage and interact with KTModal instances using these static methods of
KTModal
class.
Method | Description |
---|---|
init()
|
Automatically initializes KTModal object for all elements with the
data-modal="true"
attribute on page load.
|
createInstances()
|
Allows to create KTModal instances for all elements that have been dynamically added to the DOM but haven't been activated yet. |
getInstance(element)
|
Returns the
KTModal
object associated with the given DOM element
element
.
|
getOrCreateInstance(element)
|
Returns the existing
KTModal
object for the provided DOM element
element
, or creates a new instance if none exists, then returns the same.
|
// Initialize all modals
KTModal.init()
// Initialzie pending modals
KTModal.createInstances();
// Get modal object
const modalEl = document.querySelector('#my_modal');
const modal = KTModal.getInstance(modalEl);
<button class="btn btn-primary" data-modal-toggle="#my_modal">
Show Modal
</button>
<div class="modal max-w-[600px] top-[10%]" data-modal="true" id="my_modal">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal body goes here.
</div>
</div>
</div>
Methods
Use KTModal component's API methods to programmatically control its behavior.
Method | Description |
---|---|
new KTModal(element, options)
|
Creates an object instance of KTModal class for the given DOM
element
and configuration
options
.
|
show()
|
Shows a modal element. |
hide()
|
Hides a modal element. |
toggle()
|
Toggles a modal state to shown or hidden. |
getOption(name)
|
Retrieves the value of a configuration option by
name
parameter from a KTModal instance.
|
getElement()
|
Retrieves the DOM element linked to a specific KTModal instance. |
getTargetElement()
|
Returns the DOM element that triggered the display of the KTModal instance. |
on(eventName, handler)
|
Allows attaching event listeners to the KTModal custom events using the
eventName
and
eventId
string parameters. These events enable programmatic interaction based on user actions or
internal state changes of KTModal. The function returns
string
as a unique identifier for the registered listener, allowing you to remove it later if needed.
|
off(eventName, eventId)
|
Removes an event listener for the
eventName
and
eventId
parameters attached with the
on
method.
|
dispose()
|
Removes the KTModal instance from an element, including any associated data stored on the DOM element. |
const modalEl = document.querySelector('#my_modal');
const modal = KTModal.getInstance(modalEl);
modal.show();
modal.hide();
modal.toggle();
<div class="modal max-w-[600px] top-[10%]" data-modal="true" id="my_modal">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">
Modal Title
</h3>
<button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
<i class="ki-outline ki-cross">
</i>
</button>
</div>
<div class="modal-body">
Modal body goes here.
</div>
</div>
</div>
Events
KTModal
custom events allows you to register callback functions(event listeners)
that will be invoked automatically whenever specific custom events are triggered within the component.
Event | Description |
---|---|
show
|
Triggered immediately before a modal element is shown. |
shown
|
Triggered immediately after a modal element is shown. |
hide
|
Triggered immediately before a modal element is hidden. |
hidden
|
Triggered immediately after a modal element is hidden. |
toggle
|
Triggered immediately before a modal element is toggled(shown/hidden). |
const modalEl = document.querySelector('#my_modal');
const modal = KTModal.getInstance(modalEl);
modal.on('show', () => {
console.log('show event');
});
modal.on('shown', () => {
console.log('shown event');
});
modal.on('hide', (detail) => {
detail.cancel = true;
console.log('hide action canceled');
});