react-insta-stories

repository·master·Indexed 23 days ago

https://github.com/mohitk05/react-insta-stories

A React component for creating Instagram-like stories. Version 2.8.0 supports image and video stories, custom durations, and interactive controls such as tap-to-navigate and tap-and-hold to pause. It features a flexible API allowing for custom renderers, story objects with metadata, and Higher Order Components like WithHeader and WithSeeMore for enhanced UI customization.

Tokens
4.6K
Snippets
12
Records
28
Agent score
79%

What's inside react-insta-stories

  1. Interactions and behavior of react-insta-stories

    master

    The Stories component includes built-in touch/click interactions:

    • Tap on the right side: Move to the next story.
    • Tap on the left side: Move to the previous story.
    • Tap and hold: Pause the story progression.
    • Custom duration: You can provide a custom time duration for individual stories within the story objects.
  2. Override default story content styles

    master

    The component applies default styles to the story content. You can override these globally using the storyStyles prop or per-story using the styles property in a story object.

    The default storyContent object is:

    storyContent: {
        width: 'auto',
        maxWidth: '100%',
        maxHeight: '100%',
        margin: 'auto'
    }
  3. Set up local development for react-insta-stories

    master

    To develop the package locally with a hot-reloading setup and a live example, follow these steps:

    1. Clone the repository.
    2. Run npm install in the root.
    3. Navigate to the example directory: cd example && npm install.
    4. Return to the root: cd ...
    5. Start the main process: npm start.
    6. In a separate terminal, run the example: npm run example.
    npm install
    cd example && npm install
    cd ..
    npm start
    npm run example
  4. Create custom renderers using the renderers prop

    master

    You can pass an array of renderer objects to the renderers prop to allow custom UI components to handle specific stories. Each renderer object must contain:

    1. renderer: A UI component that receives 4 props: story, action, isPaused, and config (which contains width, height, loader, and storyStyles).
    2. tester: A function that receives the current story and returns an object with:
      • condition: A boolean indicating if this renderer should be used.
      • priority: A number used to resolve conflicts if multiple renderers return condition: true. Higher priority wins.

    Example Renderer

    // Renderer.js
    
    export const renderer = ({ story, action, isPaused, config }) => {
    	return <div>Hello!</div>;
    };
    
    export const tester = (story) => {
    	return {
    		// Use this renderer only when the story type is video
    		condition: story.type === 'video',
    		priority: 3,
    	};
    };
  5. Render custom JSX as a story using the `content` property

    master

    You can render arbitrary React components as stories by providing a content property in the story object. When content is present, all other media-related properties (like url or type) are ignored. The duration property still applies.

    The content function receives an object containing:

    • action: A function to trigger 'play' or 'pause' actions.
    • isPaused: A boolean indicating if the story is currently paused.
    const stories = [
    	{
    		content: ({ action, isPaused }) => {
    			useEffect(() => {
    				setTimeout(() => {
    					action('pause');
    					setTimeout(() => {
    						action('play');
    					}, 2000);
    				}, 2000);
    			}, []);
    			return (
    				<div style={{ background: 'pink', padding: 20 }}>
    					<h1>{isPaused ? 'Paused' : 'Playing'}</h1>
    				</div>
    			);
    		},
    		duration: 5000,
    	},
    ];
  6. Customize stories using the story object

    master

    To add more control, pass an array of objects instead of strings. When using objects, use the url key to specify the media source. You can customize individual stories with properties like duration, header, seeMore, type, and styles.

    const stories = [
    	'https://example.com/pic.jpg',
    	{
    		url: 'https://example.com/pic2.jpg',
    		duration: 5000,
    		header: {
    			heading: 'Mohit Karekar',
    			subheading: 'Posted 30m ago',
    			profileImage: 'https://picsum.photos/100/100',
    		},
    		seeMore: SeeMoreComponent, // A component to show on click
    		type: 'video', // Loads a video player; duration is ignored in favor of video length
    	},
    ];
  7. Implement basic image stories with string URLs

    master

    For a minimal setup that only displays images, pass an array of image URL strings to the stories prop of the Stories component. This will render each string as an individual story.

    import Stories from 'react-insta-stories';
    
    const stories = [
    	'https://example.com/pic.jpg',
    	'data:image/jpg;base64,R0lGODl....',
    	'https://mohitkarekar.com/icon.png',
    ];
    
    return () => <Stories stories={stories} />;
  8. Basic usage of the Stories component

    master

    To use the component, import Stories from react-insta-stories and pass it a stories array. You can also configure the defaultInterval (in milliseconds), and the width and height of the stories container.

    import React, { Component } from 'react';
    
    import Stories from 'react-insta-stories';
    
    const App = () => {
    	return (
    		<Stories
    			stories={stories}
    			defaultInterval={1500}
    			width={432}
    			height={768}
    		/>
    	);
    };
  9. Use the WithHeader Higher Order Component

    master

    The WithHeader HOC allows you to include the header UI (containing profile image, heading, etc.) on any custom story component.

    const { WithHeader } = 'react-insta-stories';
    
    const CustomStoryContent = ({ story, config }) => {
    	return <WithHeader story={story} globalHeader={config.header}>
    		<div>
    			<h1>Hello!</h1>
    			<p>This story would have the configured header!</p>
    		</div>
    	</WithHeader>
    }
  10. Use the WithSeeMore Higher Order Component

    master

    The WithSeeMore HOC provides the UI and logic for a 'See More' link at the bottom of a story. It can be used within a custom content component.

    Basic Usage

    Wrap your custom content with WithSeeMore. It receives story and action props.

    const { WithSeeMore } from 'react-insta-stories';
    
    const CustomStoryContent = ({ story, action }) => {
    	return <WithSeeMore story={story} action={action}>
    		<div>
    			<h1>Hello!</h1>
    			<p>This story would have a 'See More' link at the bottom ✨</p>
    		</div>
    	</WithSeeMore>
    }

    Custom Collapsed State

    Pass a customCollapsed prop to WithSeeMore to define what is shown when the 'See More' section is not expanded. The custom component receives toggleMore and action props.

    const { WithSeeMore } from 'react-insta-stories';
    
    const customCollapsedComponent = ({ toggleMore, action }) =>
    	<h2 onClick={() => {
    		action('pause');
    		window.open('https://mywebsite.url', '_blank');
    	}}>
    		Go to Website
    	</h2>
    
    const CustomStoryContent = ({ story, action }) => {
    	return <WithSeeMore
    		story={story}
    		action={action}
    		customCollapsed={customCollapsedComponent}
    	>
    		<div>
    			<h1>Hello!</h1>
    			<p>This story would have a 'See More' link at the bottom and will open a URL in a new tab.</p>
    		</div>
    	</WithSeeMore>
    }

    Using via Story Object

    You can also provide the components directly in the story object using seeMore and seeMoreCollapsed keys.