Install Preline UI with Next.js using Tailwind CSS
Install Preline UI with Tailwind CSS in Next.js projects, including JavaScript plugin setup, client components, app layout, and optional dependencies.
Installation
Please note that the plugin has been tested with the 16.3.0 version of the framework. The framework was installed using the standard npx create-next-app@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!
Next.js quick setup
If Tailwind CSS is not set up yet, start with the official Next.js Tailwind CSS guide first.
Some components rely on third-party libraries. The setup below assumes full use of Preline UI, including those optional dependencies. If you do not need those components, you can trim the related packages and configuration.
-
Install Preline UI
Install
prelinewith your preferred package manager.Terminalnpm install prelinePreline UI uses the Tailwind CSS Forms plugin across form components. If you have not added it yet, install it with
npm install -D @tailwindcss/forms.The steps below wire up the full set of Preline UI components, including the ones that rely on third-party libraries (Datatable, Datepicker, Range Slider, File Upload). Install those libraries and their TypeScript types now, so the
global.d.tsandPrelineClient.tsxsteps below have everything they import:Terminalnpm install jquery lodash nouislider dropzone datatables.net-dt vanilla-calendar-pro npm install -D @types/jquery @types/lodash @types/dropzoneIf you don't use Datatable, Datepicker, Range Slider, or File Upload, skip whichever of these your project doesn't need, along with the matching lines in the steps below.
-
Include Preline CSS
Import Preline into your
global.cssfile, for exampleproject_root_directory/app/globals.css.global.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; } */See the Theme docs to learn more about Preline Themes.
-
Add type definitions for Preline
Create a
global.d.tsfile for the shared window typings, for exampleproject_root_directory/global.d.ts.global.d.tsimport type { JQueryStatic } from "jquery"; import type _ from "lodash"; import type Dropzone from "dropzone"; import type noUiSlider from "nouislider"; import type DataTable from "datatables.net-dt"; import type { Calendar } from "vanilla-calendar-pro"; declare global { interface Window { // Optional third-party libraries $: JQueryStatic jQuery: JQueryStatic _: typeof _ Dropzone: typeof Dropzone noUiSlider: typeof noUiSlider DataTable: typeof DataTable VanillaCalendarPro: typeof Calendar } }; export {}; -
Create Preline UI JavaScript loader
Create a client-side Preline UI loader component inside
project_root_directory/app/components/PrelineClient.tsx. Use thepreline/non-autoentry, not the bareprelineentry: it exposesHSStaticMethodsas a normal named export, so nothing needs to read it back offwindow.PrelineClient.tsx"use client"; import { usePathname } from "next/navigation"; import { useEffect } from "react"; // Optional third-party libraries import $ from "jquery"; import _ from "lodash"; import Dropzone from "dropzone"; import noUiSlider from "nouislider"; import DataTable from "datatables.net-dt"; import { Calendar } from "vanilla-calendar-pro"; window.$ = $; window.jQuery = $; window._ = _; window.Dropzone = Dropzone; window.noUiSlider = noUiSlider; window.DataTable = DataTable; window.VanillaCalendarPro = Calendar; // Preline UI export default function PrelineClient() { const pathname = usePathname(); useEffect(() => { let cancelled = false; import("preline/non-auto").then(({ HSStaticMethods }) => { if (cancelled) return; HSStaticMethods.cleanCollection(); HSStaticMethods.autoInit(); }); return () => { cancelled = true; }; }, [pathname]); return null; }No
next/dynamicwrapper orssr: falseis needed here."use client"alone is enough to keep this component's code out of the server render; everywindowaccess already lives insideuseEffector the dynamicimport("preline/non-auto"), both of which only run in the browser after mount.cleanCollection()beforeautoInit()matters on every pathname change, not just the first run. See Using Preline UI with Next.js for why. -
Add the Preline UI JavaScript loader
Add the Preline UI loader to your app entry point, for example
project_root_directory/app/layout.tsx.layout.tsx... import PrelineClient from './components/PrelineClient'; ... export default function RootLayout({ children }: LayoutProps<"/">) { return ( <html> <body> {children} <PrelineClient /> </body> </html> ); }LayoutProps<"/">is the typed-routes helper the currentcreate-next-appscaffold generates for the root layout's props. If your project predates it or has typed routes turned off, use{ children: React.ReactNode }instead: both describe the same shape.
Optional Preline UI styles
Preline UI ships with a small set of opinionated base styles that can be applied to components by default. If you want those defaults in your project, include them in your CSS. These styles shipped by default in Tailwind CSS v3, so they remain 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.