Preline JavaScript
Learn what Preline JavaScript is, how it powers Tailwind CSS UI components, and how autoInit, TypeScript static methods, and initialization work.
Helper for dynamic added components
Preline JavaScript plugins include autoInit static method, it's useful when you need to reinitialize all elements on the page.
window.HSStaticMethods.autoInit();
This method can also be used with certain collections of initialized elements.
window.HSStaticMethods.autoInit(['carousel', 'dropdown']);
AJAX example:
Usage of static methods inside TypeScript (TS) files
To use static methods inside TS files, it is necessary to declare the interface inside the files where method is called, this will prevent possible warnings and errors.
...
import { IStaticMethods } from "preline/preline";
declare global {
interface Window {
HSStaticMethods: IStaticMethods;
}
}
...
window.HSStaticMethods.autoInit();
Prevent auto initialize
To prevent an element from auto-initializing, you can add the --prevent-on-load-init CSS class.
This can be useful if you want to initialize elements using an event other than load.