The Tailwind Toggle component allows users to easily toggle the visibility of elements, providing seamless control over hiding and showing content.
Examples
Basic Usage
Utilize the data-kt-toggle="#my_toggle_element" attribute with an ID selector and data-kt-toggle-class="hidden" ttribute to theme the referenced element's visibilty.
<div class="space-y-3"> <button class="kt-btn" data-kt-toggle="#toggle_element" data-kt-toggle-class="hidden" > Toggle Element </button> <div id="toggle_element" class="max-w-[325px] p-4 rounded-lg border border-border text-sm" > Toggling content is a popular technique in web development that enhances user interaction and engagement. </div></div>
Hidden
Set the referenced element to be hidden by default by applying the hidden class
<div class="space-y-2.5"> <button class="kt-btn" data-kt-toggle="#toggle_element" data-kt-toggle-class="hidden" > Toggle Element </button> <div id="toggle_element" class="hidden max-w-[325px] p-4 rounded-lg border border-border text-sm" > Toggling content is a popular technique in web development that enhances user interaction and engagement. </div></div>
Appearance
Utilize data-kt-toggle-class="border border-red-600 bg-green-700" attribute with Tailwind classes to change the referenced element's appearance.
<div class="space-y-2.5"> <button class="kt-btn" data-kt-toggle="#toggle_element" data-kt-toggle-class="border-red-600" > Theme Element </button> <div id="toggle_element" class="text-sm max-w-[325px] p-4 rounded-lg border border-border" > Toggling content is a popular technique in web development that enhances user interaction and engagement. </div></div>
Component API
Options
These data attributes allow you to set options for the toggle component during auto initialization.
Name
Type
Default
Description
data-kt-toggle-class
string
-
The Tailwind class to apply for the toggled state of elements.
data-kt-toggle-attribute
Selectors
This table details the custom classes and data attributes used by the toggle component.
Name
Description
Data Attributes
data-kt-toggle"
Used to auto-initialize KTToggle instances on page load and provides a string value serving as an ID selector, "#content_id" for a toggle content element.
Tailwind Modifiers
Custom modifiers to control the toggle’s style and behavior with Tailwind classes.
Name
Description
kt-toggle-active:*
A modifier that applies specific Tailwind classes when the section becomes active.
Methods
Use KTToggle component's API methods to programmatically control its behavior.
Method
Description
new KTToggle(element, options)
Creates an object instance of KTToggle class for the given DOM element and configuration options .
show(element)
Shows the toggle section that corresponds to the given DOM element .
hide(element)
Hides the toggle section that corresponds to the given DOM .
Manage and interact with KTToggle instances using these static methods of KTToggle JavaScript class.
| Method | Description |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------ | --- |
| init() | Automatically initializes KTToggle object for all elements with the data-kt-toggle attribute on page load. |
| createInstances() | Allows to create KTToggle instances for all elements that have been dynamically added to the DOM but haven't been activated yet. | |
| getInstance(element) | Returns the KTToggleobject associated with the given DOM element element . |
| getOrCreateInstance(element) | Returns the existing KTToggle object for the provided DOM element element , or creates a new instance if none exists, then returns the same. |
// Initialize all toggleesKTToggle.init();// Initialzie pending toggleesKTToggle.createInstances();// Get toggle objectconst togglerEl = document.querySelector('#my_toggler');const toggle = KTToggle.getInstance(togglerEl);
Events
KTToggle 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 an toggle section is shown.
shown
Triggered immediately after an toggle section is shown.
hide
Triggered immediately before an toggle section is hidden.
The Tailwind attribute with true value to apply for the toggled state of elements.
data-kt-toggle-active-class
string
"active"
Tailwind class to use for the active state of the toggler elements.
element
toggle(element)
Toggles the toggle section that corresponds to the given DOM element .
getOption(name)
Retrieves the value of a configuration option by name parameter from a KTToggle instance.
getElement()
Retrieves the DOM element linked to a specific KTToggle instance.
on(eventName, handler)
Allows attaching event listeners to the KTToggle custom events using the eventName and eventId string parameters. These events enable programmatic interaction based on user actions or internal state changes of KTToggle. 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 KTToggle instance from an element, including any associated data stored on the DOM element.
hidden
Triggered immediately after an toggle section is hidden.
toggle
Triggered immediately before an toggle section is toggled(shown/hidden).