1. Frameworks
  2. Ruby on Rails

Installation

Install Preline UI with Ruby on Rails using Tailwind CSS

Setting up Preline UI in a Ruby on Rails project using Tailwind CSS and Importmap.

Quick Ruby on Rails setup

A web application framework written in Ruby. If you haven't set up Tailwind CSS yet, check out Rails Tailwind CSS installation guides.

  1. Install Preline UI

    Install preline via npm or yarn.

    Terminal
                          
                            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

  2. Configure Tailwind CSS

    Update tailwind.config.js files with Preline UI configuration.

    tailwind.config.js
                          
                            /** @type {import('tailwindcss').Config} */
                            module.exports = {
                              content: [
                                './app/helpers/**/*.rb',
                                './app/javascript/**/*.js',
                                './app/views/**/*',
                                './app/views/**/*.erb',
                                './app/views/**/*.html',
                                './node_modules/preline/**/*.js'
                              ],
                              theme: {
                                extend: {},
                              },
                              plugins: [
                                require('preline/plugin'),
                              ],
                            }
                          
                        
  3. Configure CSS and JavaScript

    Update your CSS and JavaScript files to include Preline UI with Turbo integration.

    app/assets/stylesheets/application.css
                          
                            @import 'tailwindcss';
                            @import 'preline/variants.css';
    
                            /* Preline Themes */
                            @import "./themes/theme.css";
                          
                        

    Check out 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) {
                                window.HSStaticMethods.autoInit();
                              }
                            });
                          
                        
  4. Configure Importmap and Copy Preline Files

    Configure Rails Importmap to serve Preline JavaScript and copy the file to the vendor directory.

    config/importmap.rb
                          
                            pin "preline", to: "preline.js", preload: true
                          
                        
    Terminal
                          
                            mkdir -p vendor/javascript
                            cp node_modules/preline/dist/preline.js vendor/javascript/
                          
                        

    Please note, Rails Importmap cannot serve files directly from node_modules/ directory due to security and architectural constraints. Copying to vendor/javascript/ ensures proper integration with Rails' asset pipeline.

Optional Preline UI Styles

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.

CSS
                    
                      /* 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);