Style Library
In View
AnimationanimationEasily animate elements when they come into view. You can apply animations to elements when they enter the viewport, or when they are fully visible.
Prompt
Paste into any AI assistant before describing what to build — or attach it to a project below.
# Animation pattern — In View
Easily animate elements when they come into view. You can apply animations to elements when they enter the viewport, or when they are fully visible.
Build it in React with `motion` (motion/react) and Tailwind CSS. Respect `prefers-reduced-motion` (render the final state without movement), keep it under ~400 ms for UI feedback, and animate only transform and opacity where possible.
Reference implementation (Motion-Primitives, MIT):
```tsx
'use client';
import { ReactNode, useRef, useState } from 'react';
import {
motion,
useInView,
Variant,
Transition,
UseInViewOptions,
} from 'motion/react';
export type InViewProps = {
children: ReactNode;
variants?: {
hidden: Variant;
visible: Variant;
};
transition?: Transition;
viewOptions?: UseInViewOptions;
as?: React.ElementType;
once?: boolean
};
const defaultVariants = {
hidden: { opacity: 0 },
visible: { opacity: 1 },
};
export function InView({
children,
variants = defaultVariants,
transition,
viewOptions,
as = 'div',
once
}: InViewProps) {
const ref = useRef(null);
const isInView = useInView(ref, viewOptions);
const [isViewed, setIsViewed] = useState(false)
const MotionComponent = motion[as as keyof typeof motion] as typeof as;
return (
<MotionComponent
ref={ref}
initial='hidden'
onAnimationComplete={() => {
if (once) setIsViewed(true)
}}
animate={(isInView || isViewed) ? "visible" : "hidden"}
variants={variants}
transition={transition}
>
{children}
</MotionComponent>
);
}
```Use it in an app
Create a free account to attach this style to a project: the hub adds it to the prompt your AI assistant gets, so it builds every page in this look.
From Motion-Primitives (MIT, commit 120f64f).View source