Installation
Setting up Preline UI in a Phoenix LiveView project using Tailwind CSS.
Phoenix LiveView framework with real-time, interactive user interfaces. If you haven't set up Tailwind CSS yet, check out Phoenix Tailwind CSS installation guides.
Install preline via npm or yarn in the assets directory.
cd assets
npm install preline
Please note, Preline UI uses Tailwind CSS Forms plugin in all form components. Don't forget to install it, if you haven't done so already: npm install -D @tailwindcss/forms
Import the Preline UI CSS Variants file variants.css into your app.css file, ensuring it comes after the tailwindcss import and
add source for Preline UI JavaScript.
@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";
Check out the Theme docs to learn more about Preline Themes.
Modify your app.js file to handle Phoenix LiveView's dynamic updates and re-initialize Preline UI components after DOM changes.
// 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();
});
Please note, Preline UI comes with some opinionated styles that are applied to components by default. If you want these styles in your project, you may include them into your CSS file. These styles used to come by default in Tailwind v3, so we decided to keep them 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);