β 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 |
Subscribe to stay updated on all the latest content from Build UI.
Hey there!We've been experimenting with some new AI components, and we thought you'd have fun seeing what we've been up to! This week, we've got two new videos showing off some of the React techniques we're using to handle streaming text from large language modelsβplus a code recipe for animating that text to make it look buttery smooth. Animate streaming text with a one-line hook Watch now β Distinguishing between human and programmatic scrolling Watch now β Recipe: useAnimatedText View now...
Hey! It's been a while since we've sent an update, but it was great meeting so many of you during conference season this spring. We're excited to be gearing up for our next course! More on that below... but first, here's what we've been up to: How to build a Recursive React Component Watch now β React Unpacked: A Roadmap to React 19 Watch now β SPAs in React 19 Watch now β Throw is about control flow β not error handling Watch now β We're super excited to share that we've started work on our...
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:...