Skip to main content

Preview poster

Overview

The PreviewPoster component is a React Native component designed for use within a carousel. It displays a poster image that gradually fades out and scales away while a video starts playing in the background when the card is focused. It includes a mute/unmute button that persists user preferences across all instances.

Features

  • Displays a poster image before the video starts playing.
  • Plays the video when the card remains focused for a specified delay.
  • Stops the video when the card loses focus or is no longer visible.
  • Detects visibility changes using onLayout.
  • Stops playback when switching to a different tab or screen.
  • Includes a mute/unmute button with persistent state across instances.
  • Shows a progress bar for the video playback.
  • Includes a play/pause button with persistent state across instances
  • Automatically pauses playback when the component scrolls out of view, and resumes when it comes back into view — unless manually paused by the user.

Props

Prop NameTypeDefaultDescription
posterUristringRequiredThe URI of the poster image displayed before the video starts.
videoUristringRequiredThe URI of the video to be played when focused.
isFocusedbooleanRequiredDetermines whether the card is currently focused.
focusDelayMsnumber1000Delay (in milliseconds) before the video starts playing when focused.
repeatbooleantrueDetermines if the video should loop after finishing.
showVolumebooleantrueDisplays the mute/unmute button.
showPlayPausebooleantrueDisplays the play/pause button.
showProgressbooleantrueDisplays the progress bar at the top or bottom.
progressPosition'top' | 'bottom''bottom'Position of the progress bar.
tintColorstring'white'Tint color for progress bar and mute button icon.
muteButtonStyleStyleProp<ViewStyle>undefinedOptional style override for the mute/unmute button container.
pauseButtonStyleStyleProp<ViewStyle>undefinedOptional style override for the play/pause button container.
progressContainerStyleStyleProp<ViewStyle>undefinedOptional style override for the progress bar container.
progressBarStyleStyleProp<ViewStyle>undefinedOptional style override for the visual progress bar.
overlayViewStyleProp<ViewStyle>undefinedOptional overlay view rendered on top of the Video / Poster.
overlayPosition'Poster' | 'Video'VideoPosition of the overlay view. Rendered on top of Video by default.
isVideoPlayingbooleanRequiredControls whether the video should currently be playing.
setPlaybackState(play: boolean, isUserAction: boolean) => voidRequiredFunction to update the video’s play/pause state.
• play: true to play, false to pause.
• isUserAction (optional): Set to true if triggered by a user action (e.g., button press), or leave out for automatic control (e.g., scroll-based).

Usage

import { PreviewPoster } from './PreviewPoster';

const MyComponent = () => {
const [isVideoPlaying, setVideoPlayState] = useState(true);
/**
* Function to update the video play state.
* 'play' determines whether the video should play (true) or pause (false)
* 'isUserAction' is optional and defaults to 'false'; it tells whether the action was initiated by the user(button Press)
*/
const setPlaybackState = (play: boolean = true, isUserAction: boolean = false) => {
setVideoPlayState(play); // Updates the state to play or pause the video
};
return (
<PreviewPoster
posterUri="https://example.com/poster.jpg"
videoUri="https://example.com/video.mp4"
isFocused={true}
isVideoPlaying={isVideoPlaying} //'isVideoPlaying' tells the PreviewPoster whether to play or pause the video
setPlaybackState={setPlaybackState}
/>
);
};

Behavior

  • The video starts playing after focusDelayMs if the card remains focused.
  • The poster fades out and scales out while the video starts playing.
  • The video stops playing when the card loses focus, user switches tabs, or the component goes out of view.
  • Mute/unmute state is stored in SessionStorage, ensuring consistency across multiple cards.

Dependencies

  • react-native-video for video playback.
  • react-navigation hooks (useFocusEffect).
  • react-native-svg.

References