profile

Build UI

🎥 A clever Tailwind trick

Published 4 months ago • 1 min read

​

I'm wrapping up our Radix course and came across a neat use case for Tailwind's new `has` modifier.

If you haven't used it yet, `has` lets you style parent elements based on the state of their children:

Here we're giving the root div a green border if it has any children in the :checked pseudostate. Pretty neat!

So this came up when I was working on the focus state for my Radix <Slider> demo. Typically sliders render a <Thumb> control, which is perfect for showing focus:

But this demo doesn't have a thumb, since we're recreating the iOS slider. So how should we style the focus state?

I went with the full track:

Looks good! Only one problem... the <Thumb> is still the element that receives focus, not the <Track>.

So how can we style it?

Here's the markup:

You might think we could use `peer`, since <Track> is a peer of <Thumb>:

but it turns out <Track> renders some extra elements, and the one that gets our classNames doesn't end up being a peer to <Thumb>.

Here's where it gets fun.

`has` lets us style the <Root> if the <Thumb> is focused:

...but we want to style the <Track>. If we put `group` on the root, we can then combine group and has to target the <Track>:

Voilà!

`group` lets us go up the tree from <Thumb> to <Root>, and `has` lets us go back down from <Root> to <Track>.

I love that with both of these utilities we can traverse up and down our tree to style any element based on the state of any other. Just stick `group` on the lowest common ancestor of all the elements involved and you're good to go!


Our Radix course is launching next week! We build this <Slider> and three other awesome components giving you a deep understanding of the library along the way.

You'll also learn a ton about React, Tailwind, and even some Framer Motion... but more on that next week. 😀

Have a great weekend!

– Sam and Ryan

113 Cherry St #92768, Seattle, WA 98104-2205
​Unsubscribe​

Build UI

by Sam Selikoff

Subscribe to stay updated on all the latest content from Build UI.

Read more from Build UI

Learn all about Search Params with React Server Components in our latest blog post, and check out our most recent podcast episode for our thoughts on React 19's upcoming features. Instant Search Params with React Server Components Read now → Out-of-order streaming in React Watch now → Asset loading in React Watch now → Podcast - Blog Post Club: React Labs – What We’ve Been Working On Watch now → Podcast - Instant URL search params in Next.js Watch now → Podcast - React Deep Dive:...

2 months ago • 1 min read

Dive into free videos on Radix, Remix, and Server Components, and catch our latest podcast episode where we talk through React's new cache function. Building an Elastic Slider with Framer Motion and Radix Watch now → Why you can't set cookies in Server Components Watch now → React's new cache function Watch now → Optimistic UI in Remix Watch now → Podcast - React Cache Deep Dive Watch now → Podcast - Advanced Radix UI and Blog Post Club React Server Watch now → And in case you missed it, last...

3 months ago • 1 min read

Hey there! I've got some exciting news to share today: Advanced Radix UI – my newest course – is now live! Clocking in at 2 hours and 13 minutes across 35 chapters, you’ll learn how to use Radix to build four completely custom UI components: a Switch, a Selector Group, a Toast, and a Slider: Each component has a completely custom design using Tailwind, and there's also some slick animation and interactions we build with React and Framer Motion to enhance what Radix gives you out of the box....

4 months ago • 1 min read
Share this post