Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
react-gallery-carousel
Advanced tools
Mobile-friendly Carousel with batteries included (supporting touch, mouse emulation, lazy loading, thumbnails, fullscreen, RTL, keyboard navigation and customisations).
Mobile-friendly Carousel with batteries included (supporting touch, mouse emulation, lazy loading, thumbnails, fullscreen, RTL, keyboard navigation and customisations).
The first version was published on 31st March 2021.
Try Live Demo
I have used and carefully analyzed a lot of carousel/slider components. I summarized that their issues are:
I wanted to write my own detail-oriented and exquisite carousel component that is easy to use yet solves/supports all these things above under the hood. 🤓
I wanted to take my understanding of JavaScript events, DOM manipulation, browser APIs, cross-browser compatibility and performance debugging to the next level. 🤓
I wanted to master React functional components, hooks, custom hooks and reconciliation. 🤓
I wanted to learn more, place more care and attention to accessibility. I want to give focus outlines to the right users, support keyboard navigation, support screen reader, and follow W3C accessible carousel tutorials.
My carousel should support: touch, mouse emulation, keyboard navigation, modal (lightbox), thumbnails, autoplay (and auto pause), RTL (right to left for internationalization), image lazy loading (and preloading), responsive images, fallback image, reduced motion settings, instantaneous velocity detection, responsive design, images with any sizes, custom elements in a slide, pinch to zoom, customization and great accessibility. 😎
(e.g. To solve the last issue (#20) in the list above, my carousel should be able to detect a mostly vertical swipe and then fix the slides horizontally in the carousel. ✅ It should also be able to detect a mostly horizontal swipe and then fix the carousel vertically in the page. ✅)
Try Live Demo
This lighthouse report is conducted on https://yifaneye.github.io/react-gallery-carousel/ in an incognito window on Chrome version 89.0.4389.114 (Official Build) (x86_64) on MacBook Pro with macOS version 10.15.7 (19H2) on 12th of April 2021.
npm install --save react-gallery-carousel
OR
yarn add react-gallery-carousel
import React from 'react';
import Carousel from 'react-gallery-carousel';
import 'react-gallery-carousel/dist/index.css';
const App = () => {
const images = [9, 8, 7, 6, 5].map((number) => ({
src: `https://placedog.net/${number}00/${number}00?id=${number}`
}));
return (
<Carousel images={images} style={{ height: 500, width: 800 }} />
);
};
export default App;
To customize the carousel, use the following props:
Name | Type | Default | Description |
---|---|---|---|
images | Array | undefined | Array of image(s) to be placed in the carousel. Each image object (e.g. example object) in the array has a required attribute 'src'. |
children | node or Array of nodes | undefined | HTML element(s) to be placed into the carousel for user-managed slides. It (they) will be placed only if the 'images' prop is falsy. |
thumbnails | Array of nodes | undefined | HTML element(s) to be placed into the thumbnail view at the bottom. If not provided, the slides will be used. If provided, it must have the same length as the slides. |
index | Number | undefined | Initial (0-indexed) index of the slides of the carousel as a whole number starting from 0. |
isRTL | Boolean | false | If true, the slides of the carousel starts from the right (and also auto plays from the right to the left). |
isLoop | Boolean | true | If true, the carousel form a loop (i.e. going left from the left-most slide lands at the right-most slide, and vice versa) from the ribbon of slides. |
isMaximized | Boolean | false | If true, the carousel is maximized initially. |
shouldLazyLoad | Boolean | true | If true, images that are not yet in the viewport of the carousel will be lazy loaded, except the 2 adjacent images from either side of the carousel which will be preloaded. |
canAutoPlay | Boolean | true | If true, the carousel has auto play capability. |
isAutoPlaying | Boolean | false | If true, the carousel auto plays initially. |
autoPlayInterval | Number | 5000 | Interval of the auto play (in milliseconds). |
hasTransition | Boolean | true | If false, the carousel does not have transition in moving between slides. |
swipeThreshold | Number | 0.1 | Threshold swipe distance (in percentage of the width of the viewport of the carousel) to move to the previous or the next slide. |
swipeRollbackSpeed | Number | 0.1 | Speed of the transition (in pixels per millisecond) in moving back to the current slide after a swipe smaller than swipeThreshold. |
transitionSpeed | Number | 1 | Speed of the transition (in pixels per millisecond) in moving to the previous or the next slide on non-swipe updates on the carousel. |
transitionDurationLimit | Number | 750 | Limit of transition duration (in milliseconds). The limit is used to flatten transition duration, where the maximum transition duration infinitely approaches this value. |
transitionDurationMin | Number | 250 | Minimum transition duration (in milliseconds). Transition duration can be set to be a constant with transitionDurationMin === transitionDurationMax. |
transitionDurationMax | Number | undefined | Maximum transition duration (in milliseconds). It will have precedence over transitionDurationMin, if transitionDurationMin > transitionDurationMax. |
widgetsHasShadow | Boolean | false | If true, the following widgets (with *) have shadows. (Note: if true, the transition will tend to drop frames when there are a large number of slides.) |
hasLeftButton * | Boolean or String | 'centerLeft' | If false, the carousel does not show left button. Its position can be specified by one of small widget positions. |
hasRightButton * | Boolean or String | 'centerRight' | If false, the carousel does not show right button. Its position can be specified by one of small widget positions. |
hasMediaButton * | Boolean or String | 'topLeft' | If false, the carousel does not show media button (i.e. play/pause button). Its position can be specified by one of small widget positions. If the value of 'canAutoPlay' is falsy, media button will not be shown on the carousel. |
hasSizeButton * | Boolean or String | 'topRight' | If false, the carousel does not show size button (i.e. maximize/minimize button). Its position can be specified by one of small widget positions. |
hasIndexBoard * | Boolean or String | 'topCenter' | If false, the carousel does not show index board (i.e. currentIndex / totalNumberOfSlides). Its position can be specified by one of small widget positions. |
hasDotButtons * | Boolean or String | false | If false, the carousel does not show dot buttons (i.e. array of dots indicating the current slide in relation to other slides). Its position can be specified by one of large widget positions. |
hasCaptions * | Boolean or String | false | If false, the carousel does not show caption for each image. Its position can be specified by one of large widget positions. |
hasThumbnails | Boolean | true | If false, the carousel does not show thumbnails at the bottom. |
hasLeftButtonAtMax * | Boolean or String | undefined | If false, the maximized carousel does not show left button. Its position can be specified by one of small widget positions. It overrides 'hasLeftButton' prop for the maximized carousel. If not specified, the value of 'hasLeftButton' will be used. |
hasRightButtonAtMax * | Boolean or String | undefined | If false, the maximized carousel does not show right button. Its position can be specified by one of small widget positions. It overrides 'hasRightButton' prop for the maximized carousel. If not specified, the value of 'hasRightButton' will be used. |
hasMediaButtonAtMax * | Boolean or String | undefined | If false, the maximized carousel does not show media button (i.e. play/pause button). Its position can be specified by one of small widget positions. It overrides 'hasMediaButton' prop for the maximized carousel. If not specified, the value of 'hasMediaButton' will be used. If the value of 'canAutoPlay' is falsy, media button will not be shown on the maximized carousel. |
hasSizeButtonAtMax * | Boolean or String | undefined | If false, the maximized carousel does not show size button (i.e. maximize/minimize button). Its position can be specified by one of small widget positions. It overrides 'hasSizeButton' prop for the maximized carousel. If not specified, the value of 'hasSizeButton' will be used. |
hasIndexBoardAtMax * | Boolean or String | undefined | If false, the maximized carousel does not show index board (i.e. currentIndex / totalNumberOfSlides). Its position can be specified by one of small widget positions. It overrides 'hasIndexBoard' prop for the maximized carousel. If not specified, the value of 'hasIndexBoard' will be used. |
hasDotButtonsAtMax * | Boolean or String | undefined | If false, the maximized carousel does not show dot buttons (i.e. array of dots indicating the current slide in relation to other slides). Its position can be specified by one of large widget positions. It overrides 'hasDotButtons' prop for the maximized carousel. If not specified, the value of 'hasDotButtons' will be used. |
hasCaptionsAtMax * | Boolean or String | undefined | If false, the maximized carousel does not show caption for each image. Its position can be specified by one of large widget positions. It overrides 'hasCaptions' prop for the maximized carousel. If not specified, the value of 'hasCaptions' will be used. |
hasThumbnailsAtMax | Boolean | undefined | If false, the maximized carousel does not show thumbnails at the bottom. It overrides 'hasThumbnails' for the maximized carousel. If not specified, the value of 'hasThumbnails' will be used. |
leftIcon | node | undefined | Left icon (HTML element) to be placed into the left ArrowButton. |
rightIcon | node | undefined | Right icon (HTML element) to be placed into the right ArrowButton. |
playIcon | node | undefined | Play icon (HTML element) to be placed into the MediaButton. |
pauseIcon | node | undefined | Pause icon (HTML element) to be placed into the MediaButton. |
minIcon | node | undefined | Minimize icon (HTML element) to be placed into the SizeButton. |
maxIcon | node | undefined | Maximize icon (HTML element) to be placed into the SizeButton. |
activeIcon | node | undefined | Active dot icon (HTML element) to be placed into the active DotButton indicating the current slide. |
passiveIcon | node | undefined | Passive dot icon (HTML element) to be placed into the passive DotButton indicating all non-current slide(s). |
elements | node | undefined | Custom elements (HTML element) to be placed into the Carousel. |
shouldSwipeOnMouse | Boolean | true | If true, the carousel can be swiped by the cursor using a mouse or a track pad. |
shouldMaximizeOnClick | Boolean | false | If true, the carousel can be maximized by clicking. |
shouldMinimizeOnClick | Boolean | false | If true, the carousel can be minimized by clicking. |
shouldMinimizeOnSwipeDown | Boolean | true | If true, the carousel can be minimized by touch swiping down. |
onIndexChange | Function | ({curIndex, curIndexForDisplay}) => {} | Callback function invoked when the current index of the slides of the carousel is being updated. (Note: it is called regardless of whether index value's before and after are the same. curIndex is 0-indexed whilst curIndexForDisplay is 1-indexed) |
onSwipeMoveX | Function | (displacementX) => {} | Callback function invoked when the carousel is being swiped in a horizontal swipe. (Note: available from v0.2.0) |
onSwipeMoveY | Function | (displacementX, displacementY) => {} | Callback function invoked when the carousel is being swiped in a vertical swipe. (Note: available from v0.2.0) |
onSwipeEndDown | Function | () => {} | Callback function invoked when the carousel is being swiped in a downward swipe. (Note: available from v0.2.0) |
onTap | Function | () => {} | Callback function invoked when the carousel is being tapped (i.e. from mousedown to mouseup without mousemove, or from touchstart to touchend without touchmove. (Note: available from v0.2.0) |
objectFit | String | 'cover' | CSS 'object-fit' style to be placed on each image, on the non-maximized carousel. |
objectFitAtMax | String | 'contain' | CSS 'object-fit' style to be placed on each image, on the maximized carousel. |
zIndexAtMax | Number | undefined | CSS 'z-index' attribute to be placed on the maximized carousel. |
thumbnailWidth | String | '10%' | Width of each thumbnail. e.g. '20%', '20vw', '200px', 'auto'. |
thumbnailHeight | String | '10%' | Height of each thumbnail (as well as the height of thumbnails). e.g. '20%', '20vh', '200px', 'auto'. |
className | String | undefined | Class name(s) to be placed on the non-maximized carousel. |
style | Object | undefined | Inline style(s) to be placed on the non-maximized carousel. |
To customize the carousel in a declarative manner, use the props above (e.g. isAutoPlaying
, isMaximized
, index
).
To customize the carousel in an imperative manner, use the following handlers (on the forwarded ref):
Name | Description |
---|---|
play() | Start the autoplay by setting the isPlaying state to true. (Note: need >= v0.2.0) |
pause() | Pause the autoplay by setting the isPlaying state to false. (Note: need >= v0.2.0) |
toggleIsPlaying() | Toggle the isPlaying state. (Note: need >= v0.2.0) |
maximize() | Maximize the carousel by setting the isMaximized state to true. (Note: need >= v0.2.0) |
minimize() | Minimize the carousel by setting the isMaximized state to false. (Note: need >= v0.2.0) |
toggleIsMaximized() | Toggle the isMaximized state. (Note: need >= v0.2.0) |
goLeft() | Go to the left slide. (Note: need >= v0.2.0) |
goRight() | Go to the right slide. (Note: need >= v0.2.0) |
goToIndex(index) | Go to the specified index. (Note: need >= v0.2.0) |
{
src: `https://placedog.net/700/420?id=1`, // required
srcset: `https://placedog.net/400/240?id=1 400w, https://placedog.net/700/420?id=1 700w, https://placedog.net/1000/600?id=1 1000w`,
sizes: '(max-width: 1000px) 400px, (max-width: 2000px) 700px, 1000px',
alt: `Dogs are domesticated mammals, not natural wild animals. They were originally bred from wolves. They have been bred by humans for a long time, and were the first animals ever to be domesticated.`,
thumbnail: `https://placedog.net/100/60?id=1`
}
[
'hasLeftButton',
'hasRightButton',
'hasMediaButton',
'hasSizeButton',
'hasDotButtons',
'hasIndexBoard'
],
[
'topLeft', 'topCenter', 'topRight',
'centerLeft', 'centerCenter', 'centerRight',
'bottomLeft', 'bottomCenter','bottomRight'
]
['top', 'bottom']
A: Custom elements in slides (called user-managed slides) can be placed using the children
prop. Example: demo, code. Note: it (they) will be placed only if the 'images' prop is falsy.
A: There are some browser default styles. Set a font, some global styles and/or use normalize.css will help. Example: code.
A: Yes. In node_modules/microbundle-crl/dist/microbundle.js
, Change useHash: true
to useHash: false
.
A: Yes. @types/react-gallery-carousel
package contains type definitions for react-gallery-carousel
through DefinitelyTyped.
Issues and pull requests are welcomed.
(Note: please use Prettier for code formatting.)
src/
directory and to automatically compile the local version of react-gallery-carousel
into the dist/
directory. (Note: the compilation does not minify the package for readability. The decision on minification is left to the developer users).yarn install
yarn start
example/
directory, which is dependent on the local version of react-gallery-carousel
.cd example
yarn start
(Note: it is not helpful to run either of these commands in the background, because you will miss out on errors and warnings.)
MIT © Yifan Ai
v0.4.0 (2023-01-28)
Add support for custom elements.
Improve format in the README file.
FAQs
Mobile-friendly Carousel with batteries included (supporting touch, mouse emulation, lazy loading, thumbnails, fullscreen, RTL, keyboard navigation and customisations).
The npm package react-gallery-carousel receives a total of 1,300 weekly downloads. As such, react-gallery-carousel popularity was classified as popular.
We found that react-gallery-carousel demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.