--- name: animated-focus description: This document captures learnings from fixing keyboard navigation issues when floating components (Select, DropdownMenu, Popover) have CSS open/close animations. --- # Focus Management with CSS Animations This document captures learnings from fixing keyboard navigation issues when floating components (Select, DropdownMenu, Popover) have CSS open/close animations. ## The Problem When floating content elements have CSS animations that start at `opacity: 0` (like Tailwind's `animate-in fade-in-0`), the browser may reject `element.focus()` calls because the element is invisible. ### Symptoms - Component opens correctly with mouse clicks - Keyboard navigation (arrow keys, Escape) doesn't work after opening with keyboard - Works fine in demos without animation classes - Broken in demos with animation classes ### Root Cause 1. CSS animations like `fade-in-0` start the element at `opacity: 0` 2. When `focus()` is called immediately after render, the element is still invisible 3. Browser rejects focus on invisible elements 4. Focus stays on the trigger button instead of moving to content 5. Keyboard events go to trigger (which does nothing when open) instead of content ### Evidence from Console Debugging ```javascript // After opening select, keyboard events go to trigger, not content: Document keydown: ArrowDown Target: