ktuiktui
ktuiktui
ComponentsDocsProducts

Getting Started

IntroductionInstallationJavaScriptDark modeRTLReferencesChangelog

Components

AccordionAvatarAlertBadgeBreadcrumbButtonCardCheckboxCollapseDatatableDatepickerDismissDrawerDropdownImage InputInputKbdLinkModalPaginationProgressRadio GroupReparentScrollableScrollspyScrolltoSelectSeparatorSkeletonStepperStickySwitchTabsTextareaTheme SwitchToggleToggle GroupToggle PasswordTooltip
©2025 KTUI. All rights reserved.
A project by Keenthemes
Privacy Policy
Docs
Tailwind Toggle

Tailwind Toggle

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.

NameTypeDefaultDescription
data-kt-toggle-classstring-The Tailwind class to apply for the toggled state of elements.
data-kt-toggle-attributestring-The Tailwind attribute with true value to apply for the toggled state of elements.
data-kt-toggle-active-classstring"active"Tailwind class to use for the active state of the toggler elements.

Selectors

This table details the custom classes and data attributes used by the toggle component.

NameDescription
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.

NameDescription
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.

MethodDescription
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 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.
const togglerEl = document.querySelector('#my_toggler');
const toggle = KTToggle.getInstance(togglerEl);
 
toggle.toggle();

To initialize Tailwind Toggle with JavaScript, use data-toggle="false" attribute instead. This prevents automatic initialization on page load.

Utilities

Manage and interact with KTToggle instances using these static methods of KTToggle JavaScript class.

MethodDescription
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 togglees
KTToggle.init()
 
// Initialzie pending togglees
KTToggle.createInstances();
 
// Get toggle object
const 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.

EventDescription
showTriggered immediately before an toggle section is shown.
shownTriggered immediately after an toggle section is shown.
hideTriggered immediately before an toggle section is hidden.
hiddenTriggered immediately after an toggle section is hidden.
toggleTriggered immediately before an toggle section is toggled(shown/hidden).
const togglerEl = document.querySelector('#my_toggler');
const toggle = KTToggle.getInstance(togglerEl);
 
toggle.on('toggle', () => {
	detail.cancel = true;
	console.log('toggle action canceled');
});
 
toggle.on('toggled', () => {
	console.log('toggled event');
});
PreviouseTheme SwitchNextToggle Group

On This Page

  • Examples
    • Basic Usage
    • Hidden
    • Appearance
  • Component API
    • Options
    • Selectors
    • Tailwind Modifiers
    • Methods
    • Utilities
    • Events