Install Preline UI with Svelte using Tailwind CSS
Install Preline UI with Tailwind CSS in Svelte projects, including JavaScript plugin setup, SvelteKit navigation, actions, and optional dependencies.
Installation
Please note that the plugin has been tested with the 5.0.0 version of the framework. The framework was installed using the standard npx sv create <project-name> command.
If you are using your own project structure or a different version, pay attention to the file paths and features of your version!
Svelte quick setup
If Tailwind CSS is not set up yet, start with the official SvelteKit + Tailwind CSS guide first.
Some components rely on third-party libraries. The setup below assumes full Preline UI usage with those dependencies preloaded. If you do not plan to use those components, you can remove the related libraries from your configuration.
-
Install Preline UI
Install
prelinewith your preferred package manager.Terminalnpm install preline -
Include Preline CSS
Import Preline into
projects_root_directory/src/app.css.app.css@import "tailwindcss"; @import "preline/variants.css"; @source "../node_modules/preline/dist/*.js"; /* Optional plugins */ /* @import "preline/src/plugins/datepicker/styles.css"; */ /* Third-party plugins */ /* @plugin "@tailwindcss/forms"; */ /* Preline Themes */ @import "./themes/theme.css";See the Theme docs to learn more about Preline Themes.
-
Add type definitions for Preline
Create an
app.d.tsfile for the shared window typings, for exampleprojects_root_directory/src/app.d.ts.app.d.tsimport type { IStaticMethods } from "preline/dist"; declare global { interface Window { // Optional plugins _; $: typeof import("jquery"); jQuery: typeof import("jquery"); DataTable; Dropzone; VanillaCalendarPro; // Preline UI HSStaticMethods: IStaticMethods; } namespace App { ... } } export {}; -
Add Preline UI to the client entry
Import and attach the required external libraries in
projects_root_directory/src/lib/client/init.ts, then load Preline UI.init.ts// Optional plugins import $ from "jquery"; import _ from "lodash"; import noUiSlider from "nouislider"; import "datatables.net"; import "dropzone/dist/dropzone-min.js"; import * as VanillaCalendarPro from "vanilla-calendar-pro"; window._ = _; window.$ = $; window.jQuery = $; window.DataTable = $.fn.dataTable; window.noUiSlider = noUiSlider; window.VanillaCalendarPro = VanillaCalendarPro; // Preline UI import("preline/dist");Import that client initializer in
projects_root_directory/src/hooks.client.ts.hooks.client.tsimport "./lib/client/init"; -
Reinitialize on navigation
Re-run Preline UI initialization after client-side navigation in
projects_root_directory/src/routes/+layout.svelte.+layout.svelte<script lang="ts"> import { afterNavigate } from "$app/navigation"; ... afterNavigate(() => { window.HSStaticMethods.autoInit(); }); </script> ...
Optional Preline UI styles
Preline UI ships with a small set of opinionated base styles. If you want them in your project, add them to your CSS file. These defaults used to come bundled with Tailwind CSS v3, so they are still available as an optional layer in Preline UI.
/* Adds pointer cursor to buttons */
@layer base {
button:not(:disabled),
[role="button"]:not(:disabled) {
cursor: pointer;
}
}
/* Defaults hover styles on all devices */
@custom-variant hover (&:hover);
Hints and tips
When passing configuration through data attributes, wrap the object in curly braces and escape the quotes and slashes inside the string value.
Specific guide
Continue with a focused guide for wiring Preline UI JavaScript plugins into this framework.