In View
motion-primitives/in-viewFreeComponent
A component that triggers animations when elements enter the viewport.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/ibelick/motion-primitives/HEAD/public/c/in-view.jsonSource
1'use client';2import { ReactNode, useRef, useState } from 'react';3import {4 motion,5 useInView,6 Variant,7 Transition,8 UseInViewOptions,9} from 'motion/react';1011export type InViewProps = {12 children: ReactNode;13 variants?: {14 hidden: Variant;15 visible: Variant;16 };17 transition?: Transition;18 viewOptions?: UseInViewOptions;19 as?: React.ElementType;20 once?: boolean21};2223const defaultVariants = {24 hidden: { opacity: 0 },25 visible: { opacity: 1 },26};2728export function InView({29 children,30 variants = defaultVariants,31 transition,32 viewOptions,33 as = 'div',34 once35}: InViewProps) {36 const ref = useRef(null);37 const isInView = useInView(ref, viewOptions);3839 const [isViewed, setIsViewed] = useState(false)4041 const MotionComponent = motion[as as keyof typeof motion] as typeof as;4243 return (44 <MotionComponent45 ref={ref}46 initial='hidden'47 onAnimationComplete={() => {48 if (once) setIsViewed(true)49 }}50 animate={(isInView || isViewed) ? "visible" : "hidden"}5152 variants={variants}53 transition={transition}54 >55 {children}56 </MotionComponent>57 );58}
What it pulls in
npm packages
Files it writes
More from motion-primitives
All 33 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | motion-primitives | Components | Free | 1 dep | |
| Animated Backgroundanimated-background | motion-primitives | Components | Free | 1 dep | |
| Animated Groupanimated-group | motion-primitives | Components | Free | 1 dep | |
| Animated Numberanimated-number | motion-primitives | Components | Free | 1 dep | |
| Border Trailborder-trail | motion-primitives | Components | Free | 1 dep | |
| Carouselcarousel | motion-primitives | Components | Free | 1 dep | |
| Cursorcursor | motion-primitives | Components | Free | 1 dep | |
| Dialogdialog | motion-primitives | Components | Free | 1 dep · 2 files |
