3.1.0 • Published 12 days ago

react-native-story-view v3.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 days ago

Story View - Simform

react-native-story-view

react-native-story-view on npm react-native-story-view downloads react-native-story-view install size Android iOS MIT


This library provides status/stories features like Instagram/WhatsApp or other social media, It is simple to use and fully customizable. It works on both android and iOS platforms.

Quick Access

Installation | StoryView | Usage | Props | Example | License

Installation

1. Install Story View
$ npm install react-native-story-view
# --- or ---
$ yarn add react-native-story-view
2. Install peer dependencies
$ npm install react-native-video react-native-reanimated react-native-gesture-handler react-native-video-cache-control @shopify/flash-list lodash
# --- or ---
$ yarn add react-native-video react-native-reanimated react-native-gesture-handler react-native-video-cache-control @shopify/flash-list lodash

Note: If you already have these libraries installed and at the latest version, you are done here!

3. Install cocoapods in the ios project
cd ios && pod install

Note: Make sure to add Reanimated's babel plugin to your babel.config.js

module.exports = {
      ...
      plugins: [
          ...
          'react-native-reanimated/plugin',
      ],
  };

Android Cache control dependency (Mandatory)

In your project's android/app/build.gradle

dependencies {
    implementation 'com.danikula:videocache:2.7.1'
    // Your rest of the code
}

In your project's android/build.gradle

buildscript {
    // Your rest of the code
}
allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
        jcenter()
    }
}

Extra Step

Android: If you're facing issue related to 'android-scalablevideoview' or 'videocache' module not found. Add this code in android's build.gradle

jcenter() {
    content {
        includeModule("com.yqritc", "android-scalablevideoview")
        includeModule("com.danikula", "videocache")
    }
}
Know more about react-native-video, react-native-reanimated, react-native-gesture-handler, react-native-video-cache-control, @shopify/flash-list and lodash

StoryView

🎬 Preview



Usage

StoryView is divided into several components, MultiStory is the root component. ProfileHeader and Footer are individual components for header and footer. StoryContainer internally used for rendering Story. We'll look usage and customization of all these.

Story Data Format

Define the users' stories array in the below format. There will be multiple users and multiple stories inside.

const userStories = [
    {
      id: 1, //unique id (required)
      username: 'Alan', //user name on header
      title: 'Albums', //title below username
      profile: 'https://sosugary.com/wp-content/uploads/2022/01/TheWeeknd_001.jpg', //user profile picture
      stories: [
        {
          id: 0, //unique id (required)
          url: 'https://i1.sndcdn.com/artworks-IrhmhgPltsdrwMu8-thZohQ-t500x500.jpg', // story url
          type: 'image', //image or video type of story
          duration: 5, //default duration
          storyId: 1,
          isSeen: false,
        },
        {
          id: 1,
          url: 'https://storage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
          type: 'video',
          duration: 15,
          storyId: 1,
          isSeen: false,
        },
      ],
    },
    {
      id:2,
      username: 'Weekend',
      ...
    }
]

MultiStory

This is the root component of StoryView package. It displays horizontal list of users with pre-defined ui from StoryAvatar component and uses animated flatlist under the hood to display stories. StoryContainer is used under the hood for story so all customization can be done from storyContainerProps.

Basic Usage

const multiStoryRef = useRef<MultiStoryRef>(null);

<MultiStory
  stories={stories}
  ref={multiStoryRef}
  avatarProps={{
    userNameStyle: { fontSize: 16 },
  }}
  // all StoryContainer props applies here
  storyContainerProps={{
    renderHeaderComponent: ({ userStories, progressIndex, userStoryIndex }) => (
      <Header {...{ userStories, progressIndex, multiStoryRef }} />
    ),
    renderFooterComponent: ({ userStories, progressIndex, userStoryIndex }) => (
      <Footer {...{ userStories }} />
    ),
    barStyle: {
      barActiveColor: Colors.red,
    },
  }}
/>;

ProfileHeader

This is an individual component, To display user details on header like instagram/whatsapp. In renderHeaderComponent of StoryContainer, Custom component can be assigned. For MultiStory, renderHeaderComponent receives progressIndex, userStories, story and userStoryIndex for getting current user data.

const multiStoryRef = useRef(null);

<MultiStory
  ref={multiStoryRef}
  storyContainerProps={{
    renderHeaderComponent: ({
      userStories,
      story,
      progressIndex,
      userStoryIndex,
    }) => (
      <ProfileHeader
        userImage={{ uri: userStories?.profile ?? '' }}
        userName={userStories?.username}
        userMessage={userStories?.title}
        onClosePress={() => {
          multiStoryRef?.current?.close?.();
        }}
      />
    ),
  }}
/>;

Footer

This is an individual component, To display footer like instagram. Any TextInput props can be directly passed to Footer. In renderFooterComponent of StoryContainer, Custom component can be assigned.

<MultiStory
  storyContainerProps={{
    renderFooterComponent: ({
      userStories,
      story,
      progressIndex,
      userStoryIndex,
    }) => (
      <Footer
        onIconPress={() => {
          console.log('Share icon clicked');
        }}
        onSendTextPress={() => {
          console.log('Message sent');
        }}
        shouldShowSendImage={true}
        shouldShowTextInputSend={true}
        placeholder="Enter Message"
      />
    ),
  }}
/>

Additional Reference

StoryContainer

This is the core component of StoryView, which provides all functionality of story view and customization. It is used to render all stories in MultiStory. This component is just for reference how storyContainerProps in MultiStory being passed in this component internally.

const [isStoryViewVisible, setIsStoryViewShow] = useState(false);

<StoryContainer
  visible={isStoryViewVisible}
  extended={true}
  // extended enables play & pause feature on single story view default is "true"
  maxVideoDuration={10}
  stories={userStories[0].stories}
  renderFooterComponent={({ story, progressIndex }) => (
    <Footer
      onSendTextPress={() => {
        Alert.alert(`Current Story id ${story?.[progressIndex].id} `);
        Keyboard.dismiss();
      }}
      onIconPress={() => {
        Alert.alert('Current Story progress index' + progressIndex);
      }}
    />
  )}
  renderHeaderComponent={({ story, progressIndex }) => (
    <ProfileHeader
      userImage={{ uri: userStories[0]?.profile ?? '' }}
      userName={userStories[0]?.username}
      userMessage={userStories[0]?.title}
      onImageClick={() => {}}
      onClosePress={() => setIsStoryViewShow(false)}
    />
  )}
  //Callback when all stories completes
  onComplete={() => setIsStoryViewShow(false)}
/>;

MultiStoryContainer

MultiStory is wrapper on this component with extra horizontal user list UI of StoryAvatar. If MultiStory's horizontal list customisation is not sufficient for any use-case, use this base component and add your own customised horizontal user list UI.

Basic Usage

const [isStoryViewVisible, setIsStoryViewShow] = useState(false);
const [pressedIndex, setPressedIndex] = useState<number>(0);

const openStories = (index: number) => {
  setIsStoryViewShow(true);
  setPressedIndex(index);
};

  <View style={styles.container}>
    <FlatList
      horizontal
      data={userStories}
      keyExtractor={item => item?.id?.toString()}
      renderItem={({ item, index }) => (
        <Pressable onPress={() => openStories(index)}>
          <CustomStoryAvatar {...{ item, index }} />
        </Pressable>
      )}
    />
    {isStoryViewVisible && (
      // add other StoryContainer Props
      <MultiStoryContainer
        visible={isStoryViewVisible}
        onComplete={() => setIsStoryViewShow(false)}
        stories={userStories}
        renderHeaderComponent={...}
        renderFooterComponent={...}
        userStoryIndex={pressedIndex}
      />
    )}
  </View>

ProgressBar

ProgressBar customisation can be controlled through StoryContainer itself. enableProgress to make visible the progressbar.progressIndex to start story from any index. barStyle to customize look feel of progressbar.onChangePosition trigger when progressbar index will change returns current index.

<MultiStory
  storyContainerProps={{
    enableProgress: true,
    //Callback when progressbar index changes
    onChangePosition: position => {},
    barStyle: {
      barHeight: 2,
      barInActiveColor: 'green',
      barActiveColor: 'grey',
    },
    maxVideoDuration: 25,
    progressIndex: 0,
  }}
/>

Custom View

Pass any custom view in story view. It will be rendered on top of story view as it has an absolute position. In renderCustomView of StoryContainer, Any custom component can be assigned.

<MultiStory
  storyContainerProps={{
    renderCustomView: () => (
      <View
        style={{
          position: 'absolute',
          top: 40,
          right: 50,
        }}>
        <Image
          source={Images.star}
          style={{
            height: 25,
            width: 25,
            tintColor: 'green',
          }}
        />
      </View>
    ),
  }}
/>

Story Data

[{
  id: number;
  username: string;
  title: string;
  profile: string;
  stories:Array<Story>[
    {
      id: number;
      url: string;
      type: 'image' | 'video';
      duration: number
      isReadMore: boolean
      storyId: number,
      isSeen?: boolean,
    }
  ]
}]

Transitions

CubeScaleDefault

Props

MultiStory

NameDefaultTypeDescription
stories*undefinedStoriesType[]Array of multiple user stories
refnullMultiStoryRefTo access close story method
storyContainerProps{}StoryContainerPropsCustomize all story props, detailed props in below StoryContainer section
avatarProps{}StoryAvatarStylePropsCustomize avatar component styles
onChangePositionnull(progressIndex, storyIndex) => {}Callback when progress index changes
transitionModeTransitionMode.CubeTransitionMode: {Default, Cube, Scale}To customize user story transition, (TransitionMode.default : no transition, Transition.scale : zoomIn/zoomOut transition, Transition.Cube: 3D cube transition) cube
onCompletenull(viewedStories?: Array<boolean[]>) => voidCallback when stories closed or completes. viewedStories contains multi array of boolean whether story is seen or not
props-FlatListPropsPass any FlatList props to customize horizontal user list

StoryAvatarStyleProps

NameDefaultTypeDescription
userNameStyle-TextStyleTo change style of user name
userImageStyle-ImageStyleTo change style of user avatar
containerStyle-ViewStyleTo change style of image container
userImageProps-ImagePropsTo customize image props
userNameProps-ViewStyleTo customize text props
rootProps-PressablePropsTo customize root view props
viewedStoryContainerStyle-ViewStyleTo customize story avatar when all stories of it are seen

MultiStoryContainer

NameDefaultTypeDescription
stories*undefinedStoriesType[]Array of multiple user stories
visible*falsebooleanHide / show story view
userStoryIndex0numberPass clicked index of horizontal user list.
storyContainerProps{}StoryContainerPropsCustomize all story props, detailed props in below StoryContainer section
onChangePositionnull(progressIndex, userIndex) => {}Callback when progress index changes
transitionModeTransitionMode.CubeTransitionMode: {Default, Cube, Scale}To customize user story transition, (TransitionMode.default : no transition, Transition.scale : zoomIn/zoomOut transition, Transition.Cube: 3D cube transition) cube
onCompletenull() => {}Callback when stories closed or complete
props-StoryContainerPropsPass any StoryContainerProps props to customize story
pointers{ pageX: 0, pageY: 0 }PointerTypePointers will be used internally for modal opening and closing positions, You can pass profile avatar's position offsets here.

StoryContainer

NameDefaultTypeDescription
visible*falsebooleanHide / show story view
stories*undefinedStoryType[]Array of stories
extended*truebooleanEnables pause / play feature on single story view
backgroundColor#000000stringBackground color of story view
maxVideoDurationnullnumberOverride video progress duration (default is actual duration of video)
style{}ViewStyleStyle of story view
showSourceIndicatortruebooleanDisplay indicator while video loading
sourceIndicatorProps{}ActivityIndicatorPropsTo override indicator props
onCompletenull() => {}Callback when all stories completes
renderHeaderComponentnull(callback: CallbackProps) => JSX.ElementRender Header component (ProfileHeader) or custom component
renderFooterComponentnull(callback: CallbackProps) => JSX.ElementRender Footer component (Footer) or custom component
renderCustomViewnull(callback: CallbackProps) => JSX.ElementRender any custom view on Story
renderIndicatorComponent{}() => JSX.ElementRender loader when we press on Story, which represent loading state of story
storyContainerViewProps{}ViewPropsRoot story view props
headerViewProps{}ViewPropsHeader view wrapper props
footerViewProps{}ViewPropsFooter view wrapper props
customViewProps{}ViewPropsCustom view wrapper props
videoProps{}VideoPropertiesTo override video properties
ref{}StoryRefTo access 'pause' story method and 'viewedStories' stories object (Single Story)
customViewStyle{}ViewStyleStyle of custom view container
headerStyle{}ViewStyleStyle of header container
footerStyle{}ViewStyleStyle of footer container

CallbackProps

NameDefaultTypeDescription
progressIndex0numberCurrent progress index of story
storyundefinedStoryType[]Current story array
userStoriesundefinedStoriesTypeCurrent user story array (Only for Multi Story)
userStoryIndexundefinednumberCurrent user story index (Only for Multi Story)

StoryContainer: Progressbar

NameDefaultTypeDescription
progressIndex0numberTo start story with any index
barStyle{barActiveColor: #ffffff' barInActiveColor: #FFFFFF7F barHeight : 2}BarStylePropsProgressbar Style: (barActiveColor, barInActiveColor, barHeight)
enableProgresstruebooleanTo display progressbar
progressViewProps{}ViewPropsProgressBar view wrapper props
onChangePositionnull(position) => {}Callback when progress index changes

ProfileHeader

NameDefaultTypeDescription
userImage{}ImageSourcePropTypeCircular view image
userName''stringTo display username
userMessage''stringDisplay text below username
customCloseButtonnullanyTo render custom close button
closeIconProps{}ViewPropsProgressBar view wrapper props
onImageClicknull() => {}Callback on user image click
rootStyle{}ViewStyleroot view style changes
containerStyle{}ViewStylecontainer view style changes
userImageStyle{}ImageStyleTo change profile Image view style
userNameStyle{}TextStyleTo change profile name style
userMessageStyle{}TextStyleTo change profile message/subtext style
closeIconStyle{}ImageStyleTo change close icon style
userImageProps{}ImagePropsUser Image props
userMessageProps{}TextPropsUser Message Props
userNameProps{}TextPropsUser Name Props

Footer

NameDefaultTypeDescription
customInputnullTextInputRender any custom text input
shouldShowSendImagetrueboolShow/hide send icon image
onIconPressnull() => {}Callback on send icon press
sendIconProps{}ImagePropsAdditional props to customize 'send' image view
sendText'Send'stringTo change text 'send' with any other string
shouldShowTextInputSendtrueboolShow/hide send text inside text input (like instagram)
onSendTextPressnull() => {}Callback on send text press
sendTextProps{}TextPropsAdditional props to customize 'send' text view
sendTextStyle{}TextStyleTo change style of send text
sendIconStyle{}ImageStyleTo change style of send icon
inputStyle{}StylePropTo change style of input
containerStyle{}ViewStyleTo change style of root view
containerViewProps{}ViewPropsRoot view props
props-TextInputPropsPass any TextInput props on Footer component

Example

A full working example project is here Example

yarn
yarn example ios   // For ios
yarn example android   // For Android

TODO

  • Customize StoryAvatar in reference of Instagram
  • Customized Story example
  • Refactor Cube transition (make perfect cube in reference of Instagram)
  • Landscape support
  • Optimize video loading on android

Find this library useful? ❤️

Support it by joining stargazers for this repository.⭐

Bugs / Feature requests / Feedbacks

For bugs, feature requests, and discussion please use GitHub Issues, GitHub New Feature, GitHub Feedback

🤝 How to Contribute

We'd love to have you improve this library or fix a problem 💪 Check out our Contributing Guide for ideas on contributing.

Awesome Mobile Libraries

License