Overview
The focus gesture detects when an element receives focus. It respects the:focus-visible pseudo-class to only animate when keyboard focus is detected, not when clicking with a mouse.
Props
whileFocus
Properties or variant label to animate to while the focus gesture is recognized.Behavior
Focus Visible Detection
The focus gesture uses the:focus-visible CSS pseudo-class to determine when to apply animations:
- Animations trigger when the element receives keyboard focus
- Animations do NOT trigger when the element is clicked with a mouse or touch
- If the browser doesn’t support
:focus-visible, it falls back to standard:focusbehavior
Examples
Accessible form input
Focus ring animation
Combined with other gestures
Select dropdown with focus state
Accessibility Notes
- Always ensure focus states are visible for keyboard users
- The
whileFocusanimation respects:focus-visible, making it accessible by default - Combine with proper ARIA labels and semantic HTML for the best user experience
- Test keyboard navigation to ensure focus animations enhance rather than hinder usability
Browser Support
The:focus-visible detection works in all modern browsers. In browsers that don’t support :focus-visible, the feature gracefully falls back to standard focus behavior.