Media Between Text
A component that animates a media (image or video) between two text elements.
Artwork by Joffey
Installation
npx shadcn@latest add "https://fancycomponents.dev/r/media-between-text.json"
Usage
The component is extremely simple, and only consists of two text elements and a media element (either an image or a video). The trick for the smooth animation is to use layout
animations on the two texts, so they smoothly transition when the media element is revealed.
You can trigger the media reveal animation by hover
, ref
, or inView
:
hover
: The media will animate when you hover over the componentref
: You can call theanimate
andreset
methods exposed via a ref to manually control the animationinView
: The media will animate when the component is in view. You can pass anuseInViewOptionsProp
prop to customize the in view detection. Refer to the motion documentation for more details.
You can also customize the animation by passing a animationVariants
prop. Please use animate
and initial
variants. Refer to the motion documentation for more details.
Examples
Scroll
Scroll down to trigger the animation. In this case, you can pass down a containerRef prop to the component to track when elements come into view within that specific container, rather than the entire viewport. This is useful when you want to trigger animations based on scrolling within a specific scrollable container.
today's inspo
Scroll down ↓
Artworks by Tim Rodenböker, polyhop, Andreion de Castro, Lorraine Li
Vertical & Ref
You can also style the whole container, the media element, and the text elements separately. In this example, we use a column-layout to create a vertical effect. The animation can also be triggered from outside the component, by calling the animate
and reset
methods exposed via a ref. Click on the "Open" button to trigger the animation.
Artificial
Intelligence
Video from chrbutler.com
Props
Prop | Type | Default | Description |
---|---|---|---|
firstText* | string | - | The text to be displayed on the left |
secondText* | string | - | The text to be displayed on the right |
mediaUrl* | string | - | The URL of the media element |
mediaType | "image" | "video" | "image" | The type of media element |
mediaWidth | number | 100 | The width of the media element |
mediaHeight | number | 100 | The height of the media element |
mediaContainerClassName | string | - | Additional CSS classes for styling the media container |
fallbackUrl | string | - | The URL of the fallback image for the media element |
autoPlay | boolean | true | Whether to autoplay the media element |
loop | boolean | true | Whether to loop the media element |
muted | boolean | true | Whether to mute the media element |
playsInline | boolean | true | Whether to play the media element inline |
alt | string | - | The alt text for the media element |
triggerType | "hover" | "ref" | "inView" | "hover" | The trigger type for the animation |
containerRef | React.RefObject<HTMLDivElement> | - | Reference to the container for the animation |
useInViewOptionsProp | UseInViewOptions | - | Options for the useInView hook |
animationVariants | { initial: Variants["initial"]; animate: Variants["animate"] } | - | Animation configuration for the media container |
className | string | - | Additional CSS classes for styling the container of the component |
leftTextClassName | string | - | Additional CSS classes for styling the left text |
rightTextClassName | string | - | Additional CSS classes for styling the right text |