Installations
npm install react-insta-stories
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
16.20.2
NPM Version
8.19.4
Score
94.4
Supply Chain
99.6
Quality
82.9
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Languages
TypeScript (68.12%)
JavaScript (27.15%)
CSS (4%)
EJS (0.73%)
Developer
Download Statistics
Total Downloads
1,832,185
Last Day
4,100
Last Week
17,860
Last Month
83,179
Last Year
840,978
GitHub Statistics
1,387 Stars
295 Commits
248 Forks
11 Watching
67 Branches
19 Contributors
Bundle Size
43.43 kB
Minified
9.41 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.8.0
Package Id
react-insta-stories@2.8.0
Unpacked Size
113.59 kB
Size
22.44 kB
File Count
28
NPM Version
8.19.4
Node Version
16.20.2
Publised On
27 Jan 2025
Total Downloads
Cumulative downloads
Total Downloads
1,832,185
Last day
-12%
4,100
Compared to previous day
Last week
-20.2%
17,860
Compared to previous week
Last month
9.3%
83,179
Compared to previous month
Last year
56.7%
840,978
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
Dev Dependencies
20
react-insta-stories
A React component for Instagram like stories
Install
1npm install --save react-insta-stories
Demo
The component responds to actions like tap on right side for next story, on left for previous and tap and hold for pause. Custom time duration for each story can be provided. See it in action here: https://mohitk05.github.io/react-insta-stories/
Usage
1import React, { Component } from 'react'; 2 3import Stories from 'react-insta-stories'; 4 5const App = () => { 6 return ( 7 <Stories 8 stories={stories} 9 defaultInterval={1500} 10 width={432} 11 height={768} 12 /> 13 ); 14};
Here stories
is an array of story objects, which can be of various types as described below.
Props
Property | Type | Default | Description |
---|---|---|---|
stories | [String/Object] | required | An array of image urls or array of story objects (options described below) |
renderers ⚡️ | [Object] | [] | An array of renderer objects (options described below) |
defaultInterval | Number | 1200 | Milliseconds duration for which a story persists |
loader | Component | Ripple loader | A loader component as a fallback until image loads from url |
header | Component | Default header as in demo | A header component which sits at the top of each story. It receives the header object from the story object. Data for header to be sent with each story object. |
storyContainerStyles | Object | {} | Styles object for the outer container |
width | Number/String | 360 | Width of the component, e.g. 600 or '100vw' or 'inherit' |
height | Number/String | 640 | Height of the component, e.g. 1000 or '100%' or 'inherit' |
storyStyles | Object | none | Override the default story styles mentioned below. |
progressContainerStyles | Object | {} | Styles object for the container wrapping the progress bars |
progressWrapperStyles | Object | {} | Styles object for the container wrapping each progress bar bars |
progressStyles | Object | {} | Styles object for the progress bars |
loop | Boolean | false | The last story loop to the first one and restart the stories. |
New props | ⭐️ | ⭐️ | ⭐️ |
isPaused | Boolean | false | Toggle story playing state |
currentIndex | Number | undefined | Set the current story index |
onStoryStart | Function | - | Callback when a story starts |
onStoryEnd | Function | - | Callback when a story ends |
onAllStoriesEnd | Function | - | Callback when all stories in the array have ended |
onNext | Function | - | Callback when the user taps/press to proceed to the next story |
onPrevious | Function | - | Callback when the user taps/press to go back to the previous story |
keyboardNavigation | Boolean | false | Attaches arrow key listeners to navigate between stories if true. Also adds up arrow key listener for opening See More and Escape/down arrow for closing it |
preventDefault | Boolean | false | Disable the default behavior when user click the component |
preloadCount | number | 1 | Determines how many stories should be preloaded ahead of the current story index. |
Story object
Instead of simple string url, a comprehensive 'story object' can also be passed in the stories
array.
Property | Description |
---|---|
url | The url of the resource, be it image or video. |
type | Optional. Type of the story. `type: 'video' |
duration | Optional. Duration for which a story should persist. |
header | Optional. Adds a header on the top. Object with heading , subheading and profileImage properties. |
seeMore | Optional. Adds a see more icon at the bottom of the story. On clicking, opens up this component. (v2: updated to Function instead of element) |
seeMoreCollapsed | Optional. Send custom component to be rendered instead of the default 'See More' text. |
styles | Optional. Override the default story styles mentioned below. |
preloadResource | Optional. Whether to preload the resource or not, defaults to true for images and false for videos (video preloading is experimental) |
Default story styles
Following are the default story content styles. Override them by providing your own style object with each story or a global override by using the storyStyles
prop.
1storyContent: { 2 width: 'auto', 3 maxWidth: '100%', 4 maxHeight: '100%', 5 margin: 'auto' 6}
Renderers
To allow reusable components to display story UI, you can pass in pre-built or custom-built components in a special manner to leverage this behavior. Each renderer object has two properties:
renderer
- This is the UI component that will be rendered whenever the object matches certain conditions.tester
- This is a function that tests whether the renderer is suitable for the current story. It receives the current story object to render and returns an object with two properties:condition
- This states if the renderer matches the current story's criteria (a boolean).priority
- A number denoting the priority of the current renderer. E.g. priority of 2 is less than a 5, and if two renderers have condition =true
, their priorities will be compared and the one with higher priority will be selected.
So essentially a simple renderer would look like this: (you may also refer the inbuilt Image renderer)
1// Renderer.js 2 3export const renderer = ({ story, action, isPaused, config }) => { 4 return <div>Hello!</div>; 5}; 6 7export const tester = (story) => { 8 return { 9 // Use this renderer only when the story type is video 10 condition: story.type === 'video', 11 priority: 3, 12 }; 13};
Every renderer
component gets 4 props as shown above. Out of these the story
, action
and isPaused
are as their names suggest. The config
object contains certain global properties which were passed while initialising the component. It looks like this:
1const { width, height, loader, storyStyles } = config;
These props can be used to customize the entire UI as required, and then can be packaged as a Node module and shared. If someone else wishes to use your package as a renderer, they can simply pass it inside an array as the
renderers
prop to the main component. If you publish any such renderer, please raise a PR to add it to this list. A few suggestions would be a Markdown renderer, highlighted code renderer, etc.
List of public renderers:
- Add one here
Higher Order Components
WithSeeMore
This is a wrapper component which includes the UI and logic for displaying a 'See More' link at the bottom of the story. This is available as a named export from the package and can be used to easily add the functionality to a custom content story.
It takes in two props - story
and action
.
1const { WithSeeMore } from 'react-insta-stories'; 2 3const CustomStoryContent = ({ story, action }) => { 4 return <WithSeeMore story={story} action={action}> 5 <div> 6 <h1>Hello!</h1> 7 <p>This story would have a 'See More' link at the bottom ✨</p> 8 </div> 9 </WithSeeMore> 10}
You can also send custom 'See More' component for the collapsed state. While using WithSeeMore
, pass in a customCollapsed
prop with a value of your custom component. It will receive a toggleMore
and action
prop to handle clicks on the See More link.
1const { WithSeeMore } from 'react-insta-stories'; 2 3const customCollapsedComponent = ({ toggleMore, action }) => 4 <h2 onClick={() => { 5 action('pause'); 6 window.open('https://mywebsite.url', '_blank'); 7 }}> 8 Go to Website 9 </h2> 10 11const CustomStoryContent = ({ story, action }) => { 12 return <WithSeeMore 13 story={story} 14 action={action} 15 customCollapsed={customCollapsedComponent} 16 > 17 <div> 18 <h1>Hello!</h1> 19 <p>This story would have a 'See More' link at the bottom and will open a URL in a new tab.</p> 20 </div> 21 </WithSeeMore> 22}
If not implementing a custom UI, you can send the customCollapsedComponent
component inside the story object as seeMoreCollapsed
.
1const stories = [ 2 { 3 url: 'some.url', 4 seeMore: SeeMoreComponent, // when expanded 5 seeMoreCollapsed: customCollapsedComponent, // when collapsed 6 }, 7];
WithHeader
This named export can be used to include the header UI on any custom story. Simply wrap the component with this HOC and pass in some props.
1const { WithHeader } from 'react-insta-stories'; 2 3const CustomStoryContent = ({ story, config }) => { 4 return <WithHeader story={story} globalHeader={config.header}> 5 <div> 6 <h1>Hello!</h1> 7 <p>This story would have the configured header!</p> 8 </div> 9 </WithHeader> 10}
You may also use both these HOCs together, as in the Image renderer.
Common Usage
1. Basic implementation with string URLs
If you wish to have a bare minimum setup and only need to show image stories, you can simply pass the image urls inside the stories
array.
This will show all your images as stories.
1import Stories from 'react-insta-stories'; 2 3const stories = [ 4 'https://example.com/pic.jpg', 5 'data:image/jpg;base64,R0lGODl....', 6 'https://mohitkarekar.com/icon.png', 7]; 8 9return () => <Stories stories={stories} />;
2. Customising stories
If plain images does not suffice your usecase, you can pass an object instead of a string. This object supports all the properties mentioned above in the section story object. While using the object type, use url
to denote the source url in case of media.
These properties can be mixed in different ways to obtain desired output.
Duration
Each story can be set to have a different duration.
1const stories = [ 2 'https://example.com/pic.jpg', 3 { 4 url: 'https://example.com/pic2.jpg', 5 duration: 5000, 6 }, 7];
Header
Adds a header to the story.
1const stories = [ 2 'https://example.com/pic.jpg', 3 { 4 url: 'https://example.com/pic2.jpg', 5 duration: 5000, 6 header: { 7 heading: 'Mohit Karekar', 8 subheading: 'Posted 30m ago', 9 profileImage: 'https://picsum.photos/100/100', 10 }, 11 }, 12];
See More
Adds a click to see more option at the bottom of the story. When present, shows the arrow at the bottom and when clicked, shows the provided component.
1const stories = [ 2 'https://example.com/pic.jpg', 3 { 4 url: 'https://example.com/pic2.jpg', 5 duration: 5000, 6 seeMore: SeeMore, // some component 7 }, 8 { 9 url: 'https://example.com/pic3.jpg', 10 duration: 2000, 11 seeMore: ({ close }) => { 12 return <div onClick={close}>Hello, click to close this.</div>; 13 }, 14 }, 15];
Type
If provided type: video
, then the component loads a video player. All expected features come in automatically. Duration is ignored, if provided and actual video duration is considered.
1const stories = [ 2 'https://example.com/pic.jpg', 3 { 4 url: 'https://example.com/vid.mp4', 5 duration: 5000, // ignored 6 type: 'video', 7 }, 8];
Styles
Override default story element styles. Regular style object can be provided.
3. Custom JSX as a story
You can render custom JSX inside a story by sending a content
property inside the story object. If a content
property is present, all other media related properties are ignored. duration
holds true here.
1const stories = [ 2 'https://example.com/pic.jpg', 3 { 4 content: (props) => ( 5 <div style={{ background: 'pink', padding: 20 }}> 6 <h1 style={{ marginTop: '100%', marginBottom: 0 }}>🌝</h1> 7 <h1 style={{ marginTop: 5 }}>A custom title can go here.</h1> 8 </div> 9 ), 10 }, 11];
The content property can hold any React component. For further control, it receives two important props:
action
It allows you to fire play/pause actions.isPaused
Holds true is the story is currently paused, false otherwise.
1const stories = [ 2 'https://example.com/pic.jpg', 3 { 4 content: ({ action, isPaused }) => { 5 useEffect(() => { 6 setTimeout(() => { 7 action('pause'); 8 setTimeout(() => { 9 action('play'); 10 }, 2000); 11 }, 2000); 12 }, []); 13 return ( 14 <div style={{ background: 'pink', padding: 20 }}> 15 <h1 style={{ marginTop: '100%', marginBottom: 0 }}>🌝</h1> 16 <h1>{isPaused ? 'Paused' : 'Playing'}</h1> 17 </div> 18 ); 19 }, 20 }, 21];
In the code above, on render a timeout will be set which would fire a 'pause' action after 2 seconds. Again after 2 seconds, a 'play' action would be fired.
In the JSX, isPaused
is used to display the current play state.
Development
To develop this package locally, you can follo these steps:
- Clone the repo to your local.
- Run
npm install
. - Then
cd example && npm install
- Come back to the root directory
cd ..
- Run
npm start
- In a new command window/tab, run
npm run example
.
This will start a hot-reloading setup with a live example.
Thanks To
- @SamHambert for the default spinner SVG
Websites using react-insta-stories
Do you use react-insta-stories
too? Raise a PR to include your site in this list!
Contributors
This project exists thanks to all the people who contribute.
Like the project? Support me on Ko-fi
License
MIT © mohitk05
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 5/8 approved changesets -- score normalized to 6
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/publish.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/publish.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/mohitk05/react-insta-stories/publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/publish.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/mohitk05/react-insta-stories/publish.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/publish.yml:16
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Reason
22 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-8mmm-9v2q-x3f9
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
Score
3.5
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-insta-stories
react-insta-stories-rtl
A React component for Instagram like stories
@wecansync/react-insta-stories
A React component for stories like instagram
react-insta-stories-amela
A React component for Instagram like stories
@daverupp/react-insta-stories
A React component for Instagram like stories