Menu

1 / 2

I Stopped Checking relatedTarget. :focus-within Does It in CSS.

DEV Community: css·Parsa J·1 day ago
#ILo7EqTj
#devtocss#devto#dev#focus#input
Long article (1168 words) — no AI summary yet.
Reading 0:00
15s threshold

I built a search box with a clear button inside it — the little × that shows up once you've typed something. Standard stuff. The input had a nice blue ring when focused, so I matched it on the wrapper too: border-color: var(--focus-color) the moment the input gets focus, so the whole pill looks selected, not just the text caret.

It worked, until a teammate typed a query and clicked the ×. For one frame, the wrapper's border flashed back to gray — like the whole box had lost focus — before the input (correctly) still showing the cleared, empty state kept its own focus ring. It looked like a rendering glitch. It wasn't. It was onBlur doing exactly what onBlur does.

The fix everyone reaches for first

The wrapper's "focused" look was driven by state, toggled from the input's own focus and blur events:

function SearchBox ( ) { const [ focused , setFocused ] = useState ( false ) ;

return ( < div className = { focused ?…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.