React Aria Elements (RAC) is a library from Adobe that will provide you with completely to be had, production-ready React components—then again without any default styling. This makes RAC best possible for pairing with a styling framework like TailwindCSS, because you’ll have the ability to design the entire thing exactly the way in which by which you want without fighting towards preset sorts.

I’ve been using RAC in relatively a large number of duties, and one thing that I like about RAC is how it handles the phase states. As a substitute of merely using CSS pseudo-classes like :hover
or :vigorous
which don’t always behave repeatedly on touch gadgets or with keyboards, it uses data-
attributes like data-hovered
, data-pressed
, and data-selected
.
The usage of with TailwindCSS
TailwindCSS is one in every of my favourite techniques to style components. While React Aria Components artwork merely smartly with TailwindCSS, there’s a catch on the subject of styling phase states. On account of RAC uses data attributes, you’ll have the ability to’t merely use Tailwind’s same old hover:
or focus:
variants. As a substitute, you wish to have to write down out the entire feature using Tailwind’s arbitrary variant syntax, for example:
This works glorious then again it can be redundant. The class names can in short get long and messy, which makes our code tougher to be told and scan. We moreover lose out on Tailwind’s handy editor autocompletion, so typos grow to be a lot more most probably.
This is exactly the problem that the tailwindcss-react-aria-components plugin is built to unravel. Let’s see how it works.
Arrange
Setting up the plugin is understated. We will be able to add it with NPM with this command underneath:
npm arrange tailwindcss-react-aria-components
Configuring the plugin depends upon the Tailwind type you’re using on the problem.
On Tailwind v3, add it on your tailwind.config.js
:
/** @type {import('tailwindcss').Config} */ module.exports = { //... plugins: [ require('tailwindcss-react-aria-components') ], }
On Tailwind v4, use the new @plugin
directive on your number one CSS record:
@import "tailwindcss"; @plugin "tailwindcss-react-aria-components";
Once installed, styling the phase is additional simplified. Verbose data attributes like data-[pressed]:
grow to be clean variants very similar to pressed:
, and data-[selected]:
becomes selected:
. Even non-boolean states are shorter, for example data-orientation="vertical"
now becomes orientation-vertical:
.
Proper right here’s a quick comparison of the two approaches for one of the vital states in RAC:
RAC State | Tailwind Function Selector | Simplified Class Selector |
---|---|---|
isHovered
|
data-[hovered]
|
hovered:
|
isPressed
|
data-[pressed]
|
pressed:
|
isSelected
|
data-[selected]
|
selected:
|
isDisabled
|
data-[disabled]
|
disabled:
|
isFocusVisible
|
data-[focus-visible]
|
focus-visible:
|
isPending
|
data-[pending]
|
pending:
|
Prefixing
By way of default, the plugin’s modifiers are unprefixed, so that you’ll have the ability to use variants like disabled:
straight away, for example:
import { Button } from 'react-aria-components'; function App() { return ( ); }
On the other hand while you need a clearer naming convention, you’ll have the ability to set a prefix throughout the config. This may also be in particular handy in upper duties where you want to steer clear of clashes with other plugins or custom designed utilities.
Another time, the setup depends upon the Tailwind type you’re using.
On Tailwind v3, you’ll have the ability to add the prefix chance when requiring the plugin in tailwind.config.js
:
// tailwind.config.js /** @type {import('tailwindcss').Config} */ module.exports = { //... plugins: [ require('tailwindcss-react-aria-components')({ prefix: 'rac' }) ], }
On Tailwind v4, you’ll have the ability to move the prefix chance throughout the @plugin
directive on your number one CSS record, like underneath:
@import "tailwindcss"; @plugin "tailwindcss-react-aria-components" { prefix: hk };
Now, the entire variants can also be prefixed with rac-
, so disabled:
becomes rac-disabled:
, and selected:
becomes rac-selected:
.
Proper right here’s the identical example as forward of, then again with the prefix applied:
import { Button } from 'react-aria-components'; function App() { return ( ); }
Right here’s a quick demo of the plugin in movement.
Wrapping Up
Styling React Aria Components with TailwindCSS doesn’t must be tough. With the tailwindcss-react-aria-components plugin, you’ll have the ability to skip the verbose syntax and artwork with clean, intuitive variants that truly really feel similar to native Tailwind utilities. This makes it much more simple to stick your code readable, your workflow blank, and your components to be had by the use of default.
In this article, we excited by styling. On the other hand that’s just the beginning. Inside the next one, we’ll take problems a step further and uncover how you can animate React Aria Components with TailwindCSS, together with blank motion to make your UI truly really feel a lot more polished and attractive.
Stay tuned!
The publish Find out how to Taste React Aria Elements with TailwindCSS gave the impression first on Hongkiat.
Supply: https://www.hongkiat.com/blog/react-aria-tailwindcss-styling/
Contents
0 Comments