- Docs
- Modal
Modal Component
Learn how to create customizable modal dialogs with animations and advanced configuration options.
The Modal component allows you to easily create accessible, customizable modals in your web application. It provides flexible options for animations, content control, and more, giving you full control over how the modal behaves.
Installation
To get started with the modal component, install the
@flexilla/modal
package: Structure
Markup Structure
-
modal-trigger
: Element that trigger the modal, with thedata-modal-target
the value must be the targeted modal. -
modal-element
: The modal element, must have aid
-
data-modal-content
: The modal content -
data-close-modal
: any element inside themodal-element
to close the modal
Here is a basic example of how to structure your modal and trigger elements in HTML:
To add overlay
- Adding a overlay element
- Let it being create by the library
Example
Initialization
Modal Options
The modal component provides a range of configuration options through the
ModalOptions
object. Here’s a breakdown of the available options: defaultState
Specifies whether the modal should be open or closed by default.
- Type:
"open" | "close"
- Default:
"close"
animateContent
Define CSS animations for the modal content when showing and hiding the modal. You can specify both
enterAnimation
and exitAnimation
(optional). -
Type:
ModalContentAnimations
-
Example:
-
Requirement : to make work you need to set this CSS to the modal content element
- Animate on Enter only
Setting in Options
setting in data-attribute
- Animate on enter and exit
Setting Option
setting in data-attribute
overlayClass
Add a custom class for the modal overlay (background layer).
- Type:
string
- Example:
preventCloseModal
Disable close modal behavior while clicking outside modal content or pressing
esc
.
If true
, prevents the modal from being closed (useful for alerts or confirmations). - Type:
boolean
- Default:
false
allowBodyScroll
If set to
false
, prevents scrolling of the body when the modal is open. - Type:
boolean
- Default:
false
enableStackedModals
Allows multiple modals to be stacked on top of each other.
- Type:
boolean
- Default:
false
beforeHide
A function that executes before the modal is hidden. You can return an object with
cancelAction: true
to prevent the modal from closing. - Type:
() => { cancelAction?: boolean; } | void
Example
onShow
Callback function that triggers when the modal is shown.
- Type:
() => void
onHide
Callback function that triggers when the modal is hidden.
- Type:
() => void
onToggle
Callback function that triggers when the modal is toggled (open/close). It provides the
isHidden
state to indicate whether the modal is currently hidden. - Type:
({ isHidden }: { isHidden: boolean }) => void
Initialization Example
Here’s an example of initializing a modal with custom options:
Methods
open()
Programmatically opens the modal.
close()
Programmatically closes the modal.