Docs
Media Between Text

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 component
  • ref: You can call the animate and reset methods exposed via a ref to manually control the animation
  • inView: The media will animate when the component is in view. You can pass an useInViewOptionsProp 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.

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

Artificial  Intelligence

Intelligence

Video from chrbutler.com

Props


PropTypeDefaultDescription
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
mediaWidthnumber100The width of the media element
mediaHeightnumber100The height of the media element
mediaContainerClassNamestring-Additional CSS classes for styling the media container
fallbackUrlstring-The URL of the fallback image for the media element
autoPlaybooleantrueWhether to autoplay the media element
loopbooleantrueWhether to loop the media element
mutedbooleantrueWhether to mute the media element
playsInlinebooleantrueWhether to play the media element inline
altstring-The alt text for the media element
triggerType"hover" | "ref" | "inView""hover"The trigger type for the animation
containerRefReact.RefObject<HTMLDivElement>-Reference to the container for the animation
useInViewOptionsPropUseInViewOptions-Options for the useInView hook
animationVariants{ initial: Variants["initial"]; animate: Variants["animate"] }-Animation configuration for the media container
classNamestring-Additional CSS classes for styling the container of the component
leftTextClassNamestring-Additional CSS classes for styling the left text
rightTextClassNamestring-Additional CSS classes for styling the right text