Files
heroui-inc--heroui/apps/docs/content/docs/handbook/animation.mdx
T
Junior Garcia 3c3f82432a Beta 1 [v3] (#5872)
* refactor: compound and named pattern (#5850)

* refactor(component): compound and named pattern

* refactor(component): tabs component naming consistency

* docs(changelog): v3.0.0-alpha-36

* fix(docs): correct broken storybook links (#5854)

* feat(components): checkbox (#5829)

* fix(component): support compound pattern in server component

* fix(component): keep accordion and chip in sync with compound pattern

* fix(component): type of forward refs

* fix(calendar): temporary workaround for ref error

* fix(storybook): imports

* feat(checkbox): initial draft

* refactor(styles): remove orientation

* refactor: migration from dot notation to separted components to support RSC

* refactor: migrate to React 19 ref pattern - remove forwardRef wrappers and explicit ref declarations from all components

* chore(checkbox): revise checkbox story

* feat(checkbox): checkmark & indeterminate

* chore(styles): checkmark & indeterminate styles

* refactor: provider context removed as it is not longer needed on react 19

* feat: made the migration smoother by still supporting the "dot" exports but adjusting the main compound component

* fix: compound patter, radio group api, ref on react 19

* refactor(switch): split switch and switch-group into separate components following radio/radio-group pattern

* chore(changelog): update v3.0.0-alpha.35 release notes and date

* fix(page): update version label to reflect RSC support

* chore(changelog): update examples to use new component names CardRoot and TabsRoot

* refactor(checkbox): adopt latest component structure

* chore: remove radio stories

* refactor(checkbox): adopt new api and revised stories

* refactor(checkbox-group): adopt new api and add stories

* chore(checkbox-group): revise examples

* chore(checkbox): separate checkbox group css

* chore(styles): revise checkbox styels

* fix(styles): add border for invalid

* feat(checkbox): add invalid story

* chore: sync changes from v3 redesign

* refactor(checkbox): remove slots

* refactor(checkbox): apply compound and named pattern

* feat(docs): checkbox page

* refactor(checkbox): remove use client

* chore(docs): revise based on the latest revamp

---------

Co-authored-by: Tianen Pang <32772271+tianenpang@users.noreply.github.com>
Co-authored-by: Junior Garcia <jrgarciadev@gmail.com>

* V3 Redesign (#5851)

* fix(docs): update Link component to use Link.Root for consistency with new component structure

* feat: light theme in progress, button, spearator & accordion adjusted to new default theme

* feat: button and chip done

* feat: enhance Skeleton component with new Grid and Single Shimmer previews; update card and disclosure styles for consistency

* feat: update Kbd component styles and add Abbr support in stories

* feat: avatar and chip updated

* fix(accordion): adjust separator styling for consistency in accordion items

* refactor: close button adjsuted

* feat(link): add underline variants and offset options to Link component documentation and implementation

* feat(link): enhance link component with hover state and transition effects for icon opacity

* refactor: surfaces variables are now calculated by default, card, button, disclosures updated

* refactor: update styles for various components to improve consistency and reduce motion effects

* feat(storybook): add welcome story and update story order; feat(styles): include variants in package config and copy script

* refactor: update accordion, card, and kbd components to enhance styling and introduce new variants

* refactor(link): update hover underline effect to use opacity transition instead of scale transformation

* refactor(styles): update background color variables and remove gradient backgrounds from layout components

* refactor(storybook): enhance color stories by adding calculated color variants and removing deprecated soft colors

* refactor(styles): introduce background quaternary color and update shadow variables for popover and tooltip components

* feat: alert component created

* refactor(alert-demo, x-profile-demo): simplify button usage and enhance layout alignment

* feat: input otp

* feat: inpuit otp docs

* refactor(switch): enhance styling with improved transition timings and add box-shadow for active states

* feat: home page design in progress

* feat: add Surface component with context-based on-surface styling

- Add Surface component with four variants (default, secondary, tertiary, quaternary)
- Implement SurfaceContext to provide variant to child components
- Update Input, TextArea, InputOTP, and RadioGroup to automatically detect Surface context
- Components now apply on-surface styling when wrapped in Surface component
- Add OnSurface stories for all affected components
- Create comprehensive Surface documentation with examples
- Add OnSurface demo examples to all component documentation pages
- Update CSS classes to support on-surface modifier variants

* feat: add Surface component documentation and update meta.json

* feat(card): implement form handling in WithForm story using Form, Input, and Label components

* feat(card): refactor WithForm to use Form component for improved form handling and submission

* refactor(accordion): remove background class from stories and enhance AccordionRoot with SurfaceContext for elevated variant

* refactor(accordion): update variant from elevated to surface across components and documentation

* feat: add checkbox-group component and checkbox enhancements

* feat(demo): add UIComponentsDemo to the DemoComponents

* refactor: migrate Component.Root to Component in docs and demos

- Update all component documentation files to use Component instead of Component.Root
- Update all demo files to use simplified component syntax
- Update API reference headers from Component.Root Props to Component Props
- Exclude changelog files from migration as requested

This change promotes the simpler and more elegant Component syntax while
maintaining backward compatibility (both patterns are still supported).

* style: update global styles and card component

* feat(storybook): enhance color stories with new "On Surface" color variants and remove obsolete field color states

* feat(demos): update chip component props from 'type' to 'color' and add "use client" directive in multiple demo files

* feat(demos): add "use client" directive to basic button demo

* refactor: theme variables update background color in dark mode

* feat(demos): add "use client" directive to multiple demo files

* feat(demos): add FieldError component to TextfieldDemo for improved validation feedback

* fix(demo): update avatar images and names in avatar-group-demo for consistency

* feat: listbox component preview created and documented

* feat: selecte component created

* feat: export React Aria components from rac module

* feat: slider component created, documentation styles and content updated. Home page completed

* fix(demo): correct spelling of "credits" in AlertDemo component

* refactor(docs): simplify component usage by using compound pattern (#5870)

* fix(component): checkbox build errors

* refactor(docs): simplify component usage by using compound pattern

* fix(component): checkbox build errors

* chore(docs): correct pattern description

* feat(docs): enhance component preview with intersection observer for lazy loading

* feat(docs): enhance code block styling and functionality with isolated rendering option

* chore(root): upgrade package manager

* fix(docs): update image sources and enhance styling across various components

---------

Co-authored-by: Tianen Pang <32772271+tianenpang@users.noreply.github.com>

* fix(docs): update Twitter card image and adjust styles in route component for improved readability

* refactor(docs): updated color documentation

* fix(docs): update icon import in home page and remove unused client directive from Iconify component

* chore(docs): update documentation to reflect beta status and adjust storybook links for components

* feat: fix alert responsive

* feat(docs): replace img tags with next/image component for improved performance and consistency in home page and demo components

* fix: improve Lighthouse performance scores

- Replace picture/source elements with Next.js Image component
- Add fetchPriority='high' and loading='eager' to LCP images
- Fix viewport: remove userScalable: false and increase maximumScale to 5
- Add robots metadata with index: true for proper SEO
- Update sitemap URL in robots.txt

* fix: explicitly set X-Robots-Tag header to ensure proper indexing

- Add headers function in next.config.ts to set X-Robots-Tag: index, follow
- This ensures the header is explicitly set and prevents noindex issues

* fix(docs): update Link component styling in home page for improved visibility

* feat: card demos adjusted

* fix(docs): update introduction images for consistency and improved presentation

* fix(docs): update image sources in introduction and README for consistency

* fix(docs): update card component to include icon property for enhanced documentation

* fix(docs): update icon property for ListBox, Select, Slider, and MCP Server components to 'new' for improved documentation clarity

* fix(docs): update card demo layout to use flex-wrap for improved responsiveness

* feat(docs): implement changelog filtering in search API response

* feat: beta-1 changelog (draft)

* fix(docs): update version label in home page to reflect major redesign

* feat(docs): update changelog for v3-0-0-beta-1 with new components and improved API details

* fix(docs): correct formatting in changelog for v3-0-0-beta-1

* chore(docs): minor changes on v3 beta doc (#5879)

* chore(docs): update release date

* chore(docs): point to v3 component doc instead of v2

* chore(docs): add updated icon

* fix(input): typo

* refactor(styles): reduce motion (#5873)

* refactor(styles): reduce motion

* refactor(styles): reduce motion

* docs(handbook): disabling animations in animation section

* style(component): extend motion-reduce and adjust motion priority

* docs(handbook): disabling animations in animation section

* chore(docs): update design principles and changelog for v3-0-0-beta-1

* feat(docs): enhance HomePage and DocsImage components with static rendering and improved image handling

* fix(docs): update background colors for input, textarea, skeleton components and tabs in showcase

---------

Co-authored-by: Tianen Pang <32772271+tianenpang@users.noreply.github.com>
Co-authored-by: WK <wingkwong.code@gmail.com>
2025-11-06 11:08:05 -03:00

269 lines
5.7 KiB
Plaintext

---
title: Animation
description: Add smooth animations and transitions to HeroUI v3 components
icon: copy-transparent
---
## Overview
HeroUI components are built on React Aria and support multiple animation approaches:
- **Built-in animations** - Components come with default CSS transitions
- **CSS animations** - Customize with Tailwind utilities or custom CSS
- **JavaScript libraries** - Integrate with Framer Motion, React Spring, etc.
## Built-in Animations
HeroUI components use data attributes to expose their state for animation:
```css
/* Popover entrance/exit */
.popover[data-entering] {
@apply animate-in zoom-in-90 fade-in-0 duration-200;
}
.popover[data-exiting] {
@apply animate-out zoom-out-95 fade-out duration-150;
}
/* Button press effect */
.button:active,
.button[data-pressed="true"] {
transform: scale(0.97);
}
/* Accordion expansion */
.accordion__panel[aria-hidden="false"] {
@apply h-[var(--panel-height)] opacity-100;
}
```
### State Attributes
Components expose these states for styling:
- `[data-hovered="true"]` - Hover state
- `[data-pressed="true"]` - Active/pressed state
- `[data-focus-visible="true"]` - Keyboard focus
- `[data-disabled="true"]` - Disabled state
- `[data-entering]` / `[data-exiting]` - Transition states
- `[aria-expanded="true"]` - Expanded state
## CSS Animations
### Using Tailwind Utilities
```tsx
// Pulse on hover
<Button className="hover:animate-pulse">
Hover me
</Button>
// Fade in entrance
<Alert className="animate-fade-in">
Welcome message
</Alert>
// Staggered list
<div className="space-y-2">
<Card className="animate-fade-in animate-delay-100">Item 1</Card>
<Card className="animate-fade-in animate-delay-200">Item 2</Card>
</div>
```
### Custom Transitions
Override default component animations:
```css
/* Slower accordion */
.accordion__panel {
@apply transition-all duration-500;
}
/* Bouncy button */
.button:active {
animation: bounce 0.3s;
}
@keyframes bounce {
50% { transform: scale(0.95); }
}
```
## Framer Motion
HeroUI components work seamlessly with Framer Motion for advanced animations.
### Basic Usage
```tsx
import { motion } from 'framer-motion';
import { Button } from '@heroui/react';
const MotionButton = motion(Button);
<MotionButton
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
>
Animated Button
</MotionButton>
```
### Entrance Animations
```tsx
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5 }}
>
<Alert>
<Alert.Title>Welcome!</Alert.Title>
</Alert>
</motion.div>
```
### Layout Animations
```tsx
import { AnimatePresence, motion } from 'framer-motion';
function Tabs({ items, selected }) {
return (
<div className="flex gap-2">
{items.map((item, i) => (
<Button key={i} onPress={() => setSelected(i)}>
{item}
{selected === i && (
<motion.div
layoutId="active"
className="absolute inset-0 bg-accent"
transition={{ type: "spring", bounce: 0.2 }}
/>
)}
</Button>
))}
</div>
);
}
```
## Render Props
Use render props to apply dynamic animations based on component state:
```tsx
<Button>
{({ isPressed, isHovered }) => (
<motion.span
animate={{
scale: isPressed ? 0.95 : isHovered ? 1.05 : 1
}}
>
Interactive Button
</motion.span>
)}
</Button>
```
## Accessibility
### Respecting Motion Preferences
HeroUI components automatically respect user motion preferences using Tailwind's `motion-reduce:` utility. All built-in transitions and animations are disabled when users enable "reduce motion" in their system settings.
HeroUI extends Tailwind's `motion-reduce:` variant to support both the native `prefers-reduced-motion` media query and the `data-reduce-motion` attribute, providing flexible control over animations.
```css
/* HeroUI pattern - uses Tailwind's motion-reduce: */
.button {
@apply transition-colors motion-reduce:transition-none;
}
/* Expands to support both approaches: */
@media (prefers-reduced-motion: reduce) {
.button {
transition: none;
}
}
[data-reduce-motion="true"] .button {
transition: none;
}
```
With Framer Motion:
```tsx
import { useReducedMotion } from 'framer-motion';
function AnimatedCard() {
const shouldReduceMotion = useReducedMotion();
return (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: shouldReduceMotion ? 0 : 0.5 }}
>
<Card>Content</Card>
</motion.div>
);
}
```
### Disabling Animations Globally
Disable all animations by adding `data-reduce-motion="true"` to the `<html>` or `<body>` tag:
```html
<!DOCTYPE html>
<html data-reduce-motion="true">
<!-- All HeroUI animations will be disabled -->
</html>
```
HeroUI automatically detects the user's `prefers-reduced-motion: reduce` setting and disables animations accordingly. By extending Tailwind's native `motion-reduce:` variant, HeroUI provides a consistent API that works seamlessly with both system preferences and manual control via the data attribute.
## Performance Tips
### Use GPU-Accelerated Properties
Prefer `transform` and `opacity` for smooth animations:
```css
/* Good - GPU accelerated */
.slide-in {
transform: translateX(-100%);
transition: transform 0.3s;
}
/* Avoid - Triggers layout */
.slide-in {
left: -100%;
transition: left 0.3s;
}
```
### Will-Change Optimization
Use `will-change` to optimize animations, but remove it when not animating:
```css
.button {
will-change: transform;
}
.button:not(:hover) {
will-change: auto;
}
```
## Next Steps
- Learn about [Styling](/docs/handbook/styling) approaches
- Explore [Component](/docs/components) examples
- View [Theming](/docs/handbook/theming) documentation