One of the most requested tutorials we are asked for is how to integrate Tailwind CSS, the still-contentious CSS utility framework that warrants no introduction, into the Payload admin panel.
As of 3.0, the Payload admin panel integrates directly into your Next.js App Router project. This lends us the flexibility and some of the integrations Next.js already comes with, making the addition of Tailwind or other libraries easier than in previous versions.
Step 0 is to have Payload installed using this command:
From here on we refer to the official installation guide, so let's add the Tailwind dependencies.
And the init command can do the rest:
Now let's edit the generated tailwind.config.js
to add the content
configuration for our files.
Lastly we need to import Tailwind directives into a global stylesheet for our frontend, let's create a globals.css
file next to our layout in (app)
.
And we need to make sure this is imported in our layout.tsx
component.
From here on, you can use it in your frontend as you need, but we're also going to setup a custom component for Payload and use it there.
We're going to setup a simple UI
field in Payload but any custom component will work; in our collection let's add the field.
And in /components/AlertBox.tsx
we'll create a simple placeholder component.
You won't be seeing the styles updated just yet as we need to add the Tailwind directives to our admin panel's CSS as well. So locate custom.scss
in your (payload)
route group and we'll add the same @tailwind
directives in there
Do not add the base styles here as they contain a lot of style resets that may interfere with the admin panel.
And now you can use Tailwind in any of your components! Open your admin panel and go to the collection using your AlertBox UI field to see it in action.
Shadcn/ui is a library of components to be installed and used locally and it uses Tailwind CSS so please make sure it's working before running the following command
This will take you through the installation steps, and pretty much all aspects can be left as the default however you need to make sure that the globals.css
path is configured to your actual file's inside (app)
.
This will generate and override certain files for you including the tailwind.config.js and globals.css files.
Now let's add the Inter font for shadcn but you can replace this with any font that you want. Our layout.tsx
in (app)
should look something like this
Since the installation process sets up everything for us, we need to make a few more adjustments to work with Payload's admin panel.
In tailwind.config.js
we need to adjust the dark mode selector to include data-theme which is what Payload uses instead of a class.
And in (payload)/custom.scss
we need to copy over the generated values for root:
CSS variables so that the shadcn components can utilise the styles out of the box.
We do this by copying the generated code from globals.css
but note that we won't be using base
here so we can remove the @layer base
directives and we need to replace the .dark
selector with a [data-theme='dark']
selector for the admin panel.
That's a lot of code. o please check out the example repo for a reference.
Now we can add the alert
component, this will install it in a ui
directory under components
And we can go back to our AlertBox
component and let's use this new alert instead. Import the dependencies
And we'll use it as is.
the final component for AlertBox
will look like this:
And that should be it, you've now got the full power of shadcn components in Payload, follow along with the full example.
Combine it with our React hooks to read and manipulate data in your fields!