1. Frameworks
  2. Astro

Installation

Install Preline UI with Astro using Tailwind CSS

Setting up Preline UI in a Astro project using Tailwind CSS.

Installation

Please note that the plugin has been tested with the 6.0.2 version of the framework. The framework was installed using the standard npm create astro@latest command.
If you are using your own project structure or a different version, pay attention to the file paths and features of your version!

Quick Astro setup

Astro is the all-in-one web framework designed for speed. If you haven't set up Tailwind CSS yet, check out Astro Tailwind CSS installation guides.

Preline UI + Astro

Some components require third-party libraries. The settings below assume full use of Preline UI, including preloaded dependencies. If you do not intend to use those components that rely on third-party libraries, you may exclude them from your configuration.

    1. Install Preline UI

      Install Preline using your preferred package manager.

      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. Include Preline CSS

      Include Preline in your global.css file, e.g. projects_root_directory/src/styles/global.css.

      global.css
                              
                                @import "tailwindcss";
      
                                @source "../../node_modules/preline";
      
                                /* Preline UI */
                                @import 'preline/variants.css';
      
                                /* Optional Preline UI Datepicker Plugin */
                                /* @import 'preline/src/plugins/datepicker/styles.css'; */
      
                                /* Plugins */
                                /* @plugin "@tailwindcss/forms"; */
      
                                /* Preline Themes */
                                @import "preline/css/themes/theme.css";
                              
                            

      Check out the Theme docs to learn more about Preline Themes.

    3. Add type definitions for Preline

      Create the global.d.ts file in the project root directory, e.g. projects_root_directory/global.d.ts

      global.d.ts
                              
                                import type { JQueryStatic } from 'jquery';
                                import type _ from 'lodash';
                                import type Dropzone from 'dropzone';
                                import type noUiSlider from 'nouislider';
                                import type DataTables from 'datatables.net';
                                import type { Calendar } from 'vanilla-calendar-pro';
      
                                declare global {
                                  interface Window {
                                    $: JQueryStatic
                                    jQuery: JQueryStatic
                                    _: typeof _
                                    Dropzone: typeof Dropzone
                                    noUiSlider: typeof noUiSlider
                                    DataTable: typeof DataTables
                                    VanillaCalendarPro: typeof Calendar
                                    HSStaticMethods?: {
                                      autoInit: (collection?: string[]) => void
                                    }
                                  }
                                };
      
                                export {};
                              
                            
    4. Create Preline UI JavaScript loader

      Create the Preline UI JavaScript loader inside the project: projects_root_directory/src/scripts/preline.ts

      preline.tsx
                              
                                import $ from 'jquery';
                                import _ from 'lodash';
                                import Dropzone from 'dropzone';
                                import noUiSlider from 'nouislider';
      
                                window.$ = $;
                                window.jQuery = $;
                                window._ = _;
                                window.Dropzone = Dropzone;
                                window.noUiSlider = noUiSlider;
      
                                async function initPreline() {
                                  try {
                                    const dtModule = await import('datatables.net');
                                    window.DataTable = dtModule.default ?? dtModule;
      
                                    const vcModule = await import('vanilla-calendar-pro');
                                    window.VanillaCalendarPro = vcModule.Calendar ?? vcModule.default ?? vcModule;
      
                                    await import('preline');
                                    window.HSStaticMethods?.autoInit();
                                  } catch (e) {
                                    console.warn('[preline] initialization error:', e);
                                  }
                                }
      
                                if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', initPreline);
                                else initPreline();
      
                                document.addEventListener('astro:page-load', initPreline);
                              
                            
    5. Add the Preline UI JavaScript

      Include Preline UI JavaScript to the projects_root_directory/src/layouts/Layout.astro file.

      Layout.astro
                              
                            

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);
                    
                  

Community Workarounds

Check out this section for community-shared tips and tricks. It's a spot for finding those handy hacks and solutions that enhance your experience with Preline UI.