The Tailwind Theme component enables users to switch theme mode between, `dark`, `light`, and `system` preferences.
Examples
Toggle
Utilize the data-kt-theme-switch-toggle="light" and data-kt-theme-switch-toggle="dark" attributes on an element to toggle the theme mode upon clicking.
Menu
The following example illustrates a menu offering the option to select a theme mode from dark , light , and system preferences.
Manage and interact with KTThemeSwitch instances using these static methods.
| Method | Description |
| ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------- | --- |
| init() | Automatically initializes KTThemeSwitch object for all elements with the data-kt-theme-switch attribute on page load. |
| createInstances() | Allows to create KTThemeSwitch instances for all elements that have been dynamically added to the DOM but haven't been activated yet. | |
| getInstance(element) | Returns the KTThemeSwitch object associated with the given DOM element element . |
| getOrCreateInstance(element) | Returns the existing KTThemeSwitch object for the provided DOM element element , or creates a new instance if none exists, then returns the same. |
// Initialize all instancesKTTheme.init();// Initialzie pending instancesKTTheme.createInstances();// Get theme objectconst themeEl = document.querySelector('body');const theme = KTTheme.getInstance(themeEl);
Events
KTThemeSwitch 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 sticky section is shown.
shown
Triggered immediately after an sticky section is shown.
hide
Triggered immediately before an sticky section is hidden.
Hides the theme-switch section that corresponds to the given DOM element .
hide(element)
Toggles the theme-switch section that corresponds to the given DOM element .
toggle(element)
Retrieves the value of a configuration option by name parameter from a KTThemeSwitch instance.
getOption(name)
Retrieves the DOM element linked to a specific KTThemeSwitch instance.
on(eventName, handler)
Allows attaching event listeners to the KTThemeSwitch custom events using the eventName and eventId string parameters. These events enable programmatic interaction based on user actions or internal state changes of KTThemeSwitch. 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 KTThemeSwitch instance from an element, including any associated data stored on the DOM element.
hiden
Triggered immediately after an sticky section is hidden.
toggle
Triggered immediately before an sticky section is toggled(shown/hidden).