Install Preline UI with Ruby on Rails using Tailwind CSS
Install Preline UI with Tailwind CSS in Ruby on Rails projects, including JavaScript plugin setup, Importmap, layout scripts, and optional dependencies.
Installation
Please note that the plugin has been tested with the 8.1.3.1 version of the framework. The framework was installed using the standard rails new <app-name> --css=tailwind command, which wires in Tailwind CSS through the tailwindcss-rails gem and JavaScript through Importmap with Propshaft.
If you are using your own project structure, a different asset pipeline such as Vite Ruby, or a different version, pay attention to the file paths and features of your version!
Ruby on Rails quick setup
If Tailwind CSS is not set up yet, start with the official Rails + Tailwind CSS guide first.
-
Install Preline UI
Install
prelinewith your preferred package manager.Terminalnpm install prelinePreline UI uses the Tailwind CSS Forms plugin across form components. Install it if you have not already:
npm install -D @tailwindcss/forms -
Configure CSS and JavaScript entry files
Update your CSS and JavaScript entry files to load Preline UI and reinitialize it after Turbo page loads.
app/assets/tailwind/application.css@import "tailwindcss"; @import "preline/variants.css"; @source "../../../node_modules/preline/dist/*.js"; /* Optional Preline UI Datepicker Plugin */ /* @import "preline/datepicker-styles-utility.css"; */ /* Plugins */ /* @plugin "@tailwindcss/forms"; */ /* Preline Themes */ @import "preline/theme.css"; /* Optional Preline UI Datatable Plugin: DataTables.net renders its own search/length/paging controls alongside Preline's own; hide the native ones since they can't be disabled through DataTables options alone. */ /* .dt-layout-row:has(.dt-search), .dt-layout-row:has(.dt-length), .dt-layout-row:has(.dt-paging) { display: none !important; } */The
tailwindcss-railsgem's default generator createsapp/assets/tailwind/application.cssas the Tailwind CSS source file, and that is the file thetailwindcss:build/tailwindcss:watchtasks compile.app/assets/stylesheets/application.cssis Propshaft's separate, uncompiled manifest, so anything placed there is not run through Tailwind CSS.See the Theme docs to learn more about Preline Themes.
app/javascript/application.js// ... other imports import "preline" // Initialize Preline components after Turbo page loads document.addEventListener('turbo:load', () => { if (window.HSStaticMethods) { // Turbo Drive replaces the whole page body on every visit, so // the previous page's instances are already gone from the DOM. // Clean the registry before re-running autoInit. window.HSStaticMethods.cleanCollection(); window.HSStaticMethods.autoInit(); } }); -
Configure Importmap and vendor files
Add the Preline pin to Importmap, then copy the JavaScript file into the vendor directory.
config/importmap.rbpin "preline", to: "preline.js", preload: trueTerminalmkdir -p vendor/javascript cp node_modules/preline/dist/index.mjs vendor/javascript/preline.jsRails Importmap cannot serve files directly from
node_modules/. Copying the file intovendor/javascript/keeps it available to the Rails asset pipeline. Vendordist/index.mjs, notdist/preline.js, because the package's ownexportsmap points theimportcondition atindex.mjs, and it is a real ES module (it ends inexport const HSStaticMethods = ...), whereasdist/preline.jsis the UMD build meant for classic<script>tags.
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);
Specific guide
Continue with a focused guide for wiring Preline UI JavaScript plugins into this framework.