Install Preline UI with Phoenix LiveView using Tailwind CSS
Install Preline UI with Tailwind CSS in Phoenix LiveView projects, including JavaScript plugin setup, LiveView hooks, app scripts, and optional dependencies.
Phoenix LiveView quick setup
If Tailwind CSS is not set up yet, start with the official Phoenix + Tailwind CSS guide first.
-
Install Preline UI
Install
prelinewith your preferred package manager inside theassetsdirectory.Terminalcd assets npm install prelinePreline UI uses the Tailwind CSS Forms plugin across form components. Install it if you have not already:
npm install -D @tailwindcss/forms -
Import Preline CSS and source files
Import
variants.cssintoapp.cssafter thetailwindcssimport, then add the@sourceentry for Preline UI JavaScript.app.css@import "tailwindcss"; /* Preline UI */ @source "../node_modules/preline/dist/*.js"; @import "../node_modules/preline/variants.css"; /* Plugins */ /* @plugin "@tailwindcss/forms"; */ /* Preline Themes */ @import "./themes/theme.css";See the Theme docs to learn more about Preline Themes.
-
Reinitialize Preline UI after LiveView updates
Update
app.jsto initialize Preline UI on first load and re-run it after Phoenix LiveView updates.app.js// Include phoenix_html to handle method=PUT/DELETE in forms and buttons. import "phoenix_html" // Establish Phoenix Socket and LiveView configuration. import { Socket } from "phoenix" import { LiveSocket } from "phoenix_live_view" import { hooks as colocatedHooks } from "phoenix-colocated/preline_phoenix_test" import topbar from "../vendor/topbar" // Preline UI import "preline" // Initialize Preline components document.addEventListener('DOMContentLoaded', () => { window.HSStaticMethods.autoInit(); }); // Re-initialize Preline components after LiveView updates document.addEventListener('phx:update', () => { window.HSStaticMethods.autoInit(); });
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);