Repository: akiran/react-slick
Branch: master
Commit: 97442318e9a4
Files: 101
Total size: 190.1 KB
Directory structure:
gitextract_zvxyx7ut/
├── .babelrc
├── .eslintrc
├── .github/
│ ├── FUNDING.yml
│ └── ISSUE_TEMPLATE.md
├── .gitignore
├── .npmignore
├── .prettierrc
├── .travis.yml
├── CHANGELOG.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── __tests__/
│ ├── SimpleSlider.test.js
│ ├── TestComponents.js
│ ├── afterChange.test.js
│ ├── arrows.js
│ ├── beforeChange.test.js
│ ├── lazyLoad.test.js
│ ├── observations.json
│ ├── regression/
│ │ ├── fix-1813.test.js
│ │ ├── fix-1874.test.js
│ │ ├── fix-2315.test.js
│ │ └── fix-2414.test.js
│ ├── sliderStyles.test.js
│ ├── testUtils.js
│ └── utils/
│ └── filterSettings.test.js
├── docs/
│ ├── api.md
│ ├── common.md
│ ├── demos.js
│ ├── docs.css
│ ├── docs.js
│ ├── index.html
│ ├── index.js
│ ├── routes.js
│ ├── scripts/
│ │ ├── generateExampleConfigs.js
│ │ └── generateExamples.js
│ ├── single-demo.js
│ ├── slick-theme.css
│ └── slick.css
├── examples/
│ ├── AdaptiveHeight.js
│ ├── AppendDots.js
│ ├── AsNavFor.js
│ ├── AutoPlay.js
│ ├── AutoPlayMethods.js
│ ├── CenterMode.js
│ ├── CustomArrows.js
│ ├── CustomPaging.js
│ ├── CustomSlides.js
│ ├── DynamicSlides.js
│ ├── Fade.js
│ ├── FocusOnSelect.js
│ ├── LazyLoad.js
│ ├── MultipleItems.js
│ ├── MultipleRows.js
│ ├── PauseOnHover.js
│ ├── PreviousNextMethods.js
│ ├── Resizable.js
│ ├── Responsive.js
│ ├── Rtl.js
│ ├── SimpleSlider.js
│ ├── SlickGoTo.js
│ ├── SlideChangeHooks.js
│ ├── SwipeToSlide.js
│ ├── UnevenSetsFinite.js
│ ├── UnevenSetsInfinite.js
│ ├── VariableWidth.js
│ ├── VerticalMode.js
│ ├── VerticalSwipeToSlide.js
│ ├── __tests__/
│ │ ├── CentreMode.test.js
│ │ ├── Fade.js
│ │ ├── FocusOnSelect.test.js
│ │ ├── MultipleItems.test.js
│ │ ├── SimpleSlider.test.js
│ │ ├── SlickGoTo.test.js
│ │ └── UnevenSets.test.js
│ └── config.js
├── gulpfile.js
├── jest.config.js
├── package.json
├── playwright/
│ ├── index.html
│ └── index.jsx
├── playwright-ct.config.js
├── playwright-tests/
│ ├── features/
│ │ └── responsive/
│ │ ├── responsive.spec.tsx
│ │ └── responsive.story.tsx
│ ├── regression/
│ │ └── fix-1930/
│ │ ├── fix-1930.spec.tsx
│ │ └── fix-1930.story.tsx
│ └── sample/
│ ├── sample.spec.tsx
│ └── sample.story.tsx
├── src/
│ ├── arrows.js
│ ├── default-props.js
│ ├── dots.js
│ ├── index.js
│ ├── initial-state.js
│ ├── inner-slider.js
│ ├── slider.js
│ ├── track.js
│ └── utils/
│ └── innerSliderUtils.js
├── test-setup.js
├── test-utils.js
├── webpack.config.dist.js
└── webpack.config.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .babelrc
================================================
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
================================================
FILE: .eslintrc
================================================
{
"rules": {
"no-extra-parens": 0,
"react/jsx-uses-vars": 1,
"strict": 0,
"no-underscore-dangle": 0,
"space-infix-ops": 0,
"no-alert": 0,
"react/prop-types": 0,
"react/no-find-dom-node": 0,
"react/display-name": 0,
"no-console": 0,
"no-prototype-builtins": 0
},
"env": {
"node": true,
"browser": true,
"es6": true,
"jasmine": true
},
"parser": "@babel/eslint-parser",
"parserOptions": {
"requireConfigFile": false
},
"plugins": [
"react",
"import"
],
"extends": [
"eslint:recommended",
"plugin:import/errors",
"plugin:react/recommended"
]
}
================================================
FILE: .github/FUNDING.yml
================================================
github: akiran
open_collective: react-slick
================================================
FILE: .github/ISSUE_TEMPLATE.md
================================================
### Guidelines for posting a new issue
* Please replicate your issue with this [CodeSandBox](https://codesandbox.io/s/ppwkk5l6xx) and provide a link to it along with the issue description
================================================
FILE: .gitignore
================================================
node_modules
bower_components
.sass-cache
build
demos/*
demos1
TODO.md
npm-debug.log
lib
*.sublime-*
.idea
dist
yarn.lock
.vscode
exampleslib
examples/scripts/configs.json
jquery.html
docs/fonts/
docs/ajax-loader.gif
package-lock.json
.DS_Store
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
src-jsx
================================================
FILE: .npmignore
================================================
bower_components
.sass-cache
build
demos
demos1
TODO.md
test
testlib
bower.json
gulpfile.js
karma.conf.js
LICENSE
webpack.config.dist.js
webpack.config.js
ISSUE_TEMPLATE.md
================================================
FILE: .prettierrc
================================================
trailingComma: none
================================================
FILE: .travis.yml
================================================
language: node_js
node_js:
- "stable"
================================================
FILE: CHANGELOG.md
================================================
# Change Log
## [Unreleased](https://github.com/akiran/react-slick/tree/HEAD)
## 0.22.0
**Release Changes**
- Internal Changes
- converted InnerSlider from createReactClass object to ES6 class
- removed all the mixins, created classMethods and pure utility functions instead
- changed autoplay from setTimeout to setInterval
- added images onload handlers to update dynamically
- added autoplaying state for the betterment of autoplay and pause
- removed usage of assign or Object.assign, using object spreading instead
- implemented effects of touchMove props
- fixed transition in opposite direction in case of continuous scrolling
- added separate onclick event listener for images
- added missing classes `regular` and `slider`
- renamed events
- edgeEvent => onEdge
- init => onInit
- reInit => onReInit
- implemented `pauseOnDotsHover` property
- implemented Progressive LazyLoad property, lazyLoad is now ondemand/progressive
- implemented lazyloadError event
- implemented useTransform property
- implemented pauseOnFocus property
- added resize observer to update on slider resize
- Bug Fixes
- dynamic track updates on image load
- fixed slickPause and autoPlay issues (paused slider would resume autoplay sometime)
- fixed trackStyle update on window resize
- fixed NodeList forEach problem for chrome 51 or below
- fixed bugs due to uncleared callback timers
- fixed update issues on just slider resize
## 0.21.0
**Release Changes**
- Fixed issues
- dataset undefined error in case of swipeToSlide but finite slides
- slideWidth issue by transform scale
- variableWidth + finite alignment problems
- wrapper direction rtl issues
- added onload update handler for images
- fixed breaking of animation on setState
- Mixins to Pure Functions
- getWidth, getHeight
- swipeDirection
- initialize, update
- Other Changes
- removed sass, using pure CSS instead
- enforced dir='ltr' in the slider, so dir='rtl' in wrapper doesn't break the slider
- corrected up/down direction conventions
- added more tests along with snapshots
## 0.20.0
**Release Changes**
- handled responsive breakpoint collision
- renamed autoPlayTimer to autoplayTimer and removed it from state
- changed es5 module.exports with es6 export default in src/index
- implemented slider syncing with asNavFor prop
- made all the slides untabbable
- implemented getSlick method as in slick
- implemented slickGetOption method
- implemented lazyLoaded event
- implemented reInit event
- implemented onSwipe event and documented edgeEvent
## 0.19.0
**Release Changes**
Following are the changes to be mentioned:
- fixed slideWidth calculation approximation
- fixed unusual scrolls in focusOnSelect mode
- added appendDots method for customization of dots
- modified logic for handling odd/even cases where there were unusual scrolls in opposite direction
- implemented unslick feature properly
- fixed variableWidth issues like blank spaces at edges, improper alignment
- handling focus loss in case of fade=true
- responsive lazyloading bug fixed
- increased verticalswiping resistance from 4 to 10
## 0.18.0
**Major Changes:**
- `centerPadding` prop now accepts % value as well
- Fixed dots count in certain cases, where it was wrong
- Fixed fade property mess-up on click
- Fixed invisibility issue when fade & vertical are true
- Modified logic for updating lazyLoadedList, earlier there were some whitespaces at ends, now they're gone
- Fixed getTrackLeft issue for slideCount=1
## 0.17.1
**Major Changes**
* Enforced some settings in specific configurations like:
- `slidesToScroll = 1` *when fade is true*
- `slidesToScroll = 1` *when centerMode is true*
- `slidesToShow = 1` *when fade is true*
* Changed the number of clones (preclones and postclones), that fixed couple of issues like blank spaces after last slide and/or before first slide which occurred in several cases.
**Minor Changes**
- Rich amount of tests and test-utilities added
- Additional documentation comments added
- Refactored small snippets for betterment
- Fixed several lazyload and centerMode bugs
================================================
FILE: CONTRIBUTING.md
================================================
# Contribute
## Introduction
First, thank you for considering contributing to react-slick! It's people like you that make the open source community such a great community! 😊
We welcome any type of contribution, not only code. You can help with
- **QA**: file bug reports, the more details you can give the better (e.g. screenshots with the console open)
- **Marketing**: writing blog posts, howto's, printing stickers, ...
- **Community**: presenting the project at meetups, organizing a dedicated meetup for the local community, ...
- **Code**: take a look at the [open issues](https://github.com/akiran/react-slick/issues). Even if you can't write code, commenting on them, showing that you care about a given issue matters. It helps us triage them. To get started you can also [sign up to triage react-slick issues on CodeTriage](https://www.codetriage.com/akiran/react-slick).
- **Money**: we welcome financial contributions in full transparency on our [open collective](https://opencollective.com/react-slick).
## Your First Contribution
Working on your first Pull Request? You can learn how from this *free* series, [How to Contribute to an Open Source Project on GitHub](https://app.egghead.io/playlists/how-to-contribute-to-an-open-source-project-on-github).
## Submitting code
Any code change should be submitted as a pull request. The description should explain what the code does and give steps to execute it. The pull request should also contain tests.
## Code review process
The bigger the pull request, the longer it will take to review and merge. Try to break down large pull requests in smaller chunks that are easier to review and merge.
It is also always helpful to have some context for your pull request. What was the purpose? Why does it matter to you?
## Financial contributions
We also welcome financial contributions in full transparency on our [open collective](https://opencollective.com/react-slick).
Anyone can file an expense. If the expense makes sense for the development of the community, it will be "merged" in the ledger of our open collective by the core contributors and the person who filed the expense will be reimbursed.
## Questions
If you have any questions, create an [issue](https://github.com/akiran/react-slick/issues) (protip: do a quick search first to see if someone else didn't ask the same question before!).
You can also reach us at hello@react-slick.opencollective.com.
## Credits
### Contributors
Thank you to all the people who have already contributed to react-slick!
### Backers
Thank you to all our backers! [[Become a backer](https://opencollective.com/react-slick#backer)]
### Sponsors
Thank you to all our sponsors! (please ask your company to also support this open source project by [becoming a sponsor](https://opencollective.com/react-slick#sponsor))
================================================
FILE: LICENSE
================================================
The MIT License (MIT)
Copyright (c) 2014 Kiran Abburi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
### react-slick
[](#backers) [](#sponsors)
##### Carousel component built with React. It is a react port of [slick carousel](http://kenwheeler.github.io/slick/)
## [Documentation](http://react-slick.neostack.com)
### Installation
**npm**
```bash
npm install react-slick --save
```
**yarn**
```bash
yarn add react-slick
```
**Also install slick-carousel for css and font**
```bash
npm install slick-carousel
// Import css files
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
```
or add cdn link in your html
```html
```
### [PlayGround](https://stackblitz.com/edit/vitejs-vite-ownrun?file=src%2FImageSlider.jsx)
### Example
```js
import React from "react";
import Slider from "react-slick";
export default function SimpleSlider() {
var settings = {
dots: true,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1
};
return (
1
2
3
4
5
6
);
}
```
### Props
For all available props, go [here](https://react-slick.neostack.com/docs/api/).
### Methods
For all available methods, go [here](https://react-slick.neostack.com/docs/api#methods)
### Development
Want to run demos locally
```bash
git clone https://github.com/akiran/react-slick
cd react-slick
npm install
npm start
open http://localhost:8080
```
## Community
Join our [discord channel](https://discord.gg/z7stRE4Cyb) to discuss react-slick bugs and ask for help
## Contributing
Please see the [contributing guidelines](./CONTRIBUTING.md)
================================================
FILE: __tests__/SimpleSlider.test.js
================================================
// includes tests of
// SimpleSlider, MultipleItems
import { testSlider } from "./testUtils";
describe("SimpleSlider with combinations of possibilities", function() {
// try around several possibilities
let _noOfSlides = [2, 5, 12];
let _slidesToShow = [2, 5, 10];
let _slidesToScroll = [1, 2, 3, 10];
if (true) {
// for switching real quick to lesser/easier tests for simplicity
_noOfSlides = [5];
_slidesToShow = [2];
_slidesToScroll = [1, 2];
}
for (let noOfSlides of _noOfSlides) {
for (let slidesToShow of _slidesToShow) {
for (let slidesToScroll of _slidesToScroll) {
// following restrictions may not be 100% correct, and there may be more restrictions
if (slidesToShow > noOfSlides || slidesToScroll > slidesToShow) {
continue;
}
if (noOfSlides === slidesToShow) {
// temporary, jquery slick disables arrows in this case, so the tests fail
continue;
}
if (slidesToShow === slidesToScroll) {
// temporary, active-class is not being assigned properly, so tests fail
continue;
}
const settings1 = {
infinite: true,
speed: 0,
noOfSlides,
slidesToShow,
slidesToScroll,
useCSS: false
};
test(`Test with settings => noOfSlides: ${noOfSlides}, slidesToShow: ${slidesToShow}, slidesToScroll: ${slidesToScroll}`, function() {
testSlider(settings1);
});
}
}
}
});
================================================
FILE: __tests__/TestComponents.js
================================================
import React from "react";
import Slider from "../src/index";
export function GenericSliderComponent({ slidesCount, settings }) {
const slides = [...Array(slidesCount).keys()].map(item => (
);
}
}
describe("Slider", function() {
it("should render", function() {
const { container } = render();
clickNext(container);
expect(getActiveSlide(container).textContent).toEqual("slide2");
clickNext(container);
expect(getActiveSlide(container).textContent).toEqual("slide3");
clickPrevious(container);
expect(getActiveSlide(container).textContent).toEqual("slide2");
});
});
================================================
FILE: __tests__/lazyLoad.test.js
================================================
import { render } from "@testing-library/react";
import assign from "object-assign";
import { getRequiredLazySlides } from "../src/utils/innerSliderUtils";
import {
createInnerSliderWrapper,
clickNext,
clickPrev,
tryAllConfigs
} from "./testUtils";
// const testSettings = settings => {
// let {container} = createInnerSliderWrapper(settings);
// for (let click = 0; click < settings.noOfSlides + 2; click++) {
// let lazyLoadedList = slider.state().lazyLoadedList;
// let expectedLazyLoadedList = getRequiredLazySlides(
// assign({}, slider.props(), slider.state())
// );
// expectedLazyLoadedList.forEach(slide => {
// expect(lazyLoadedList.indexOf(slide) >= 0).toEqual(true);
// });
// clickNext(slider);
// }
// slider = createInnerSliderWrapper(settings);
// for (let click = 0; click < settings.noOfSlides + 2; click++) {
// let lazyLoadedList = slider.state().lazyLoadedList;
// let expectedLazyLoadedList = getRequiredLazySlides(
// assign({}, slider.props(), slider.state())
// );
// expectedLazyLoadedList.forEach(slide => {
// expect(lazyLoadedList.indexOf(slide) >= 0).toEqual(true);
// });
// clickPrev(slider);
// }
// slider = createInnerSliderWrapper(settings);
// for (let click = 0; click < settings.noOfSlides + 2; click++) {
// let lazyLoadedList = slider.state().lazyLoadedList;
// lazyLoadedList.forEach(slideIndex => {
// expect(
// slider.find(`[data-index=${slideIndex}]`).props().children !== undefined
// ).toBe(true);
// });
// }
// };
// describe("LazyLoad test", () => {
// let settings = {
// lazyLoad: true,
// useCSS: false,
// speed: 0,
// noOfSlides: [7, 8],
// initialSlide: [0, 5],
// slidesToShow: [1, 3, 4],
// slidesToScroll: [1, 3],
// centerMode: [true, false]
// };
// let settingsList = [];
// tryAllConfigs(settings, settingsList);
// // shuffle the list
// settingsList.sort(() => 0.5 - Math.random());
// settingsList.forEach((settings, index) => {
// if (Math.random() < 0.5) {
// test(`Testing config no. ${index}`, () => testSettings(settings));
// }
// });
// });
================================================
FILE: __tests__/observations.json
================================================
{
"jQueryTest": [
{
"observation": "Clicks on arrows are not working properly",
"possibleCause": "Animation effects are taking effects somehow",
"solutions": [
{
"description": "set useCSS property to false",
"status": "did not work"
},
{
"description": "set speed property to 0",
"status": "worked, now the clicks are working as of now"
}
]
},
{
"observation": "arrows are disabled when slidesToShow are equal to noOfSlides",
"status": "causes few tests to fail"
},
{
"observation": "tests are very slow",
"possibleCause": "synchronous click event simulation and slow DOM api",
"status": "not tried yet"
}
],
"reactTest": [
{
"observation": "Clicks on arrows are not working properly",
"possibleCause": "Animation effects are taking effects somehow",
"solutions": [
{
"description": "set useCSS property to false",
"status": "worked, now the clicks are working as of now"
}
]
}
],
"misc": [
{
"observation": "In case of reverse scrolling, slick-active class is not being assigned properly.",
"example": {
"settings": {
"noOfSlides": 5,
"slidesToShow": 2,
"slidesToScroll": 2
},
"jqueryBehaviour": "after one prev click, current-slide is 5th and active-class is assigned to slide 4th and 5th while the same are displayed in frame",
"reactBehaviour": "after one prev click, current-slide is 5th and active-class is assigned to slide 5th and 1st(cloned) while 4th and 5th are displayed in frame",
"status": "several tests are failing due to this property"
}
}
]
}
================================================
FILE: __tests__/regression/fix-1813.test.js
================================================
//Test fix of #1813: In infinite mode, when slidesToShow equal to the length of slides, infinite functionality is not working.
// Reversed the fix for #1813 to match the slick carousel functionality. When slides <= slidesToShow, unslick will be activated
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import {
clickNext,
clickPrevious,
getActiveButton,
getActiveSlidesCount,
getActiveSlidesText,
getButtons,
getButtonsLength,
getClonesCount,
getCurrentSlide,
getSlidesCount,
hasArrows,
hasDots
} from "../../test-utils";
import { GenericSliderComponent } from "../TestComponents";
function MultipleItems() {
const settings = {
dots: true,
infinite: true,
speed: 500,
slidesToShow: 9,
slidesToScroll: 3
};
return ;
}
describe("Multiple Items with slidesToShow = slides count in infinite mode", function() {
it("should have 9 active slides", function() {
const { container } = render();
expect(getActiveSlidesCount(container)).toEqual(9);
});
it("should show first 9 slides", function() {
const { container } = render();
//expect(getActiveButton(container)).toEqual(["1"]);
expect(getActiveSlidesText(container)).toEqual([
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"
]);
});
it("shouldn't have any arrows", () => {
const { container } = render();
expect(hasArrows(container)).toEqual(false);
});
it("shouldn't have any dots", () => {
const { container } = render();
expect(hasDots(container)).toEqual(false);
});
// it("should have 0 dots", function() {
// const { container } = render();
// expect(getButtonsLength(container)).toEqual(0);
// });
// it("should show slides from 4 when next button is clicked", function() {
// const { container } = render();
// clickNext(container);
// expect(getActiveButton(container)).toEqual(["2"]);
// expect(getActiveSlidesText(container)).toEqual([
// "4",
// "5",
// "6",
// "7",
// "8",
// "9",
// "1",
// "2",
// "3"
// ]);
// });
// it("should show slides from 7 when previous button is clicked", function() {
// const { container } = render();
// clickPrevious(container);
// expect(getActiveButton(container)).toEqual(["3"]);
// expect(getActiveSlidesText(container)).toEqual([
// "7",
// "8",
// "9",
// "1",
// "2",
// "3",
// "4",
// "5",
// "6"
// ]);
// });
// it("should show slides first 9 slides when first dot is clicked", function() {
// const { container } = render();
// fireEvent(
// getButtons(container)[0],
// new MouseEvent("click", {
// bubbles: true,
// cancelable: true
// })
// );
// expect(getActiveButton(container)).toEqual(["1"]);
// expect(getActiveSlidesText(container)).toEqual([
// "1",
// "2",
// "3",
// "4",
// "5",
// "6",
// "7",
// "8",
// "9"
// ]);
// });
// it("should show slides from 4 when middle dot is clicked", function() {
// const { container } = render();
// fireEvent(
// getButtons(container)[1],
// new MouseEvent("click", {
// bubbles: true,
// cancelable: true
// })
// );
// expect(getActiveButton(container)).toEqual(["2"]);
// expect(getActiveSlidesText(container)).toEqual([
// "4",
// "5",
// "6",
// "7",
// "8",
// "9",
// "1",
// "2",
// "3"
// ]);
// });
// it("should show slides from 7 when last dot is clicked", function() {
// const { container } = render();
// fireEvent(
// getButtons(container)[2],
// new MouseEvent("click", {
// bubbles: true,
// cancelable: true
// })
// );
// expect(getActiveButton(container)).toEqual(["3"]);
// expect(getActiveSlidesText(container)).toEqual([
// "7",
// "8",
// "9",
// "1",
// "2",
// "3",
// "4",
// "5",
// "6"
// ]);
// });
});
================================================
FILE: __tests__/regression/fix-1874.test.js
================================================
// Test fix of#1874: "slick-current" is always on first slide despite initialSlide != 0
import React from "react";
import { render } from "@testing-library/react";
import { getCurrentSlideContent, clickNext } from "../../test-utils";
import { GenericSliderComponent } from "../TestComponents";
describe("currentSlide test with different initialSlide values", () => {
it("currentSlide is 0 when initialSlide is 0", function() {
const { container } = render();
expect(getCurrentSlideContent(container)).toEqual("1");
clickNext(container);
expect(getCurrentSlideContent(container)).toEqual("2");
});
it("currentSlide is 2 when initialSlide is 2", function() {
const { container } = render(
);
expect(getCurrentSlideContent(container)).toEqual("3");
clickNext(container);
expect(getCurrentSlideContent(container)).toEqual("4");
});
});
================================================
FILE: __tests__/regression/fix-2315.test.js
================================================
// Test fix of #2315: Slider crashing after a state change in parent component
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import { getCurrentSlideContent, getSlidesCount } from "../../test-utils";
import { GenericSliderComponent } from "../TestComponents";
function TestSlider() {
const [count, setCount] = React.useState();
return (
);
}
describe("State change in parent component of slider", () => {
it("Slider shoud work afer clicking on Increment button", function() {
const { container } = render();
fireEvent(
container.getElementsByClassName("increment-button")[0],
new MouseEvent("click", {
bubbles: true,
cancelable: true
})
);
// Throws an error "Maximum update depth exceeded." if the bug exists
expect(getCurrentSlideContent(container)).toEqual("1");
expect(getSlidesCount(container)).toEqual(8);
});
});
================================================
FILE: __tests__/regression/fix-2414.test.js
================================================
// Test fix of #2414: Extra clones in infinite mode when there is only one slide or unslick is true
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import {
getCurrentSlideContent,
getSlidesCount,
getActiveSlidesCount,
getClonesCount,
hasArrows,
hasDots
} from "../../test-utils";
import { GenericSliderComponent } from "../TestComponents";
function SliderWithOneSlide() {
const settings = {
dots: true,
infinite: true
};
return ;
}
function SliderWithUnslick() {
const settings = {
dots: true,
infinite: true,
unslick: true
};
return ;
}
describe("Slider with one slide", function() {
it("should have 1 active slide", function() {
const { container } = render();
expect(getActiveSlidesCount(container)).toEqual(1);
});
it("should not have any clones", function() {
const { container } = render();
expect(getClonesCount(container)).toEqual(0);
});
it("should no have dots and arrows", function() {
const { container } = render();
expect(hasArrows(container)).toEqual(false);
expect(hasDots(container)).toEqual(false);
});
});
describe("Slider with unslick=true", function() {
it("should have one active slide", function() {
const { container } = render();
expect(getActiveSlidesCount(container)).toEqual(1);
});
it("should not have any clones", function() {
const { container } = render();
expect(getClonesCount(container)).toEqual(0);
});
it("should no have dots and arrows", function() {
const { container } = render();
expect(hasArrows(container)).toEqual(false);
expect(hasDots(container)).toEqual(false);
});
});
================================================
FILE: __tests__/sliderStyles.test.js
================================================
import assign from "object-assign";
import { getRequiredLazySlides } from "../src/utils/innerSliderUtils";
import {
createInnerSliderWrapper,
clickNext,
clickPrev,
tryAllConfigs,
actualTrackLeft,
testTrackLeft
} from "./testUtils";
import { getTrackLeft } from "../src/utils/innerSliderUtils";
const testSettings = settings => {
let slider = createInnerSliderWrapper(settings);
for (let click = 0; click < settings.noOfSlides + 2; click++) {
testTrackLeft(slider);
clickNext(slider);
}
slider = createInnerSliderWrapper(settings);
for (let click = 0; click < settings.noOfSlides + 2; click++) {
testTrackLeft(slider);
clickPrev(slider);
}
};
describe("Slider Styles Tests", () => {
let settings = {
useCSS: false,
speed: 0,
centerMode: [true, false],
noOfSlides: [7, 8],
initialSlide: [0, 5],
slidesToShow: [1, 3, 4]
};
let settingsList = [];
tryAllConfigs(settings, settingsList);
// shuffle the list
settingsList.sort(() => 0.5 - Math.random());
settingsList.forEach((settings, index) => {
// if (Math.random() < 0.5) {
// test(`Testing config no. ${index}`, () => testSettings(settings));
// }
});
});
================================================
FILE: __tests__/testUtils.js
================================================
import React from "react";
import $ from "jquery";
import assign from "object-assign";
import { render } from "@testing-library/react";
import Slider from "../src/slider";
import { InnerSlider } from "../src/inner-slider";
import defaultProps from "../src/default-props";
import * as slickCarousel from "slick-carousel"; // defining slick in global environment
import { getTrackLeft } from "../src/utils/innerSliderUtils";
import {
getActiveSlides,
getActiveSlidesCount,
clickNext,
clickPrevious
} from "../test-utils";
// finds active slide number in the last transition in the forward direction
export function activeSlideInLastTransition(
noOfSlides,
slidesToShow,
slidesToScroll
) {
let currentSlide = 0;
while (currentSlide < noOfSlides) {
currentSlide += slidesToScroll;
}
return currentSlide - slidesToScroll;
}
// create jsx-form children for react slider
export function createReactSliderChildren(noOfSlides) {
return Array.from(Array(noOfSlides).keys()).map(i => (
{i + 1}
));
}
// create a react-slider with given noOfSlides and other props
// variable widths are ignored for now for simplicity
export function createReactSlider({ noOfSlides, ...props }) {
return {createReactSliderChildren(noOfSlides)};
}
// create a react inner-slider with given noOfSlides and other props
// performs most operations like the ones when mounted inside Slider component
export function createInnerSlider({ noOfSlides, ...settings }) {
if (settings.centerMode) {
settings.slidesToScroll = 1; // always scroll by one when centerMode is enabled
}
settings = assign({}, defaultProps, settings);
const children = React.Children.toArray(
createReactSliderChildren(noOfSlides)
);
return {children};
}
export function createInnerSliderWrapper(settings) {
return render(createInnerSlider(settings)).container;
}
// creates a dom string, containing children of slick children
export function createJQuerySliderChildren(noOfSlides) {
let children = [];
for (let i = 0; i < noOfSlides; i++) {
children.push(`
${i + 1}
`);
}
return children.join("");
}
// performs the very basic tests while clicking next or prev
export function testSliderScroll({ direction, ...settings }) {
const { noOfSlides, slidesToShow, slidesToScroll, initialSlide } = settings;
// initialize react slider
const { container } = render(createReactSlider(settings));
// initialize jquery slider
document.body.innerHTML = `
${createJQuerySliderChildren(noOfSlides)}
`;
$(".regular.slider").slick({
...settings
});
// console.log('setings:', settings)
let expectedSlideIndex = initialSlide || 0;
for (let click = 0; click < 2 * noOfSlides + 2; click++) {
let activeslides = getActiveSlides(container);
let $activeSlides = $(".regular.slider").find("div.slick-active");
expect(getActiveSlidesCount(container)).toEqual(slidesToShow || 1);
expect($activeSlides.length).toEqual(slidesToShow || 1);
let firstActiveSlide = activeslides[0];
let $firstActiveSlide = $activeSlides.first();
// console.log('classes', $firstActiveSlide.attr('data-slick-index'))
// console.warn('currentSlide:', firstActiveSlide.prop('data-index'), 'expected slide', expectedSlideIndex)
expect(parseInt(firstActiveSlide.getAttribute("data-index"))).toEqual(
expectedSlideIndex % noOfSlides
);
expect(parseInt($firstActiveSlide.attr("data-slick-index"))).toEqual(
expectedSlideIndex % noOfSlides
);
if (direction === "next") {
// click the next arrow button
clickNext(container);
$("button.slick-next").click();
expectedSlideIndex += slidesToScroll || 1;
if (expectedSlideIndex >= noOfSlides) {
expectedSlideIndex = 0;
}
} else {
// click on the prev arrow button
clickPrevious(container);
$("button.slick-prev").click();
expectedSlideIndex -= slidesToScroll || 1;
if (expectedSlideIndex < 0) {
expectedSlideIndex = activeSlideInLastTransition(
noOfSlides,
slidesToShow,
slidesToScroll
);
}
}
}
}
// function to run tests on a slider,
// scrolls slider to the right by clicking right arrow several times
// scrolls slider to the left by clicking left arrow several times
export function testSlider(settings) {
const settings1 = { direction: "next", ...settings };
const settings2 = { direction: "prev", ...settings };
testSliderScroll(settings1);
testSliderScroll(settings2);
}
export const tryAllConfigs = (settings, settingsList) => {
let leaf = true;
for (let key of Object.keys(settings)) {
if (Array.isArray(settings[key])) {
leaf = false;
for (let val of settings[key]) {
tryAllConfigs({ ...settings, [key]: val }, settingsList);
}
}
}
if (leaf) {
if (
settingsList
.map(setting => JSON.stringify(setting))
.indexOf(JSON.stringify(settings)) < 0
) {
settingsList.push(settings);
}
}
};
export const actualTrackLeft = container =>
container
.querySelector(".slick-track")
.style.transform.match(/translate3d\((\d+)px/i)[1];
export const testTrackLeft = container => {
let trackLeft = parseInt(actualTrackLeft(container));
let spec = assign({}, wrapper.props(), wrapper.state(), {
slideIndex: wrapper.state().currentSlide,
trackRef: null
});
let expectedTrackLeft = getTrackLeft(spec);
expect(trackLeft).toEqual(parseInt(expectedTrackLeft));
};
test("fake test", () => {
expect(1).toBe(1);
});
================================================
FILE: __tests__/utils/filterSettings.test.js
================================================
import { filterSettings } from "../../src/utils/innerSliderUtils";
describe("filterSettings", () => {
it("returns empty object if there are no valid settings", () => {
expect(filterSettings({})).toEqual({});
expect(filterSettings({ age: 10 })).toEqual({});
});
it("return an object with valid settings and omits extra properties", () => {
expect(filterSettings({ arrows: true, dots: true })).toEqual({
arrows: true,
dots: true
});
expect(filterSettings({ arrows: true, dots: true, age: 10 })).toEqual({
arrows: true,
dots: true
});
});
});
================================================
FILE: docs/api.md
================================================
### Methods
| Name | Arguments | Description |
| ------------ | ------------------ | --------------------------- |
| `slickPrev` | None | go to previous slide |
| `slickNext` | None | go to next slide |
| `slickGoTo` | index, dontAnimate | go to the given slide index |
| `slickPause` | None | pause the autoplay |
| `slickPlay` | None | start the autoplay |
#### Followings are not going to be implemented
| Name | type | Reason |
| ------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `unslick` | method | same functionality can be achieved with `unslick` prop |
| `slickSetOption` | method | same functionality can be achieved via props and managing state for them in wrapper |
| `slickFilter` | method | same functionality can be achieved as with dynamic slides, look at dynamic slides [example](https://github.com/akiran/react-slick/blob/master/examples/DynamicSlides.js) |
| `slickUnfilter` | method | same functionality can be achieved as with dynamic slides, look at dynamic slides [example](https://github.com/akiran/react-slick/blob/master/examples/DynamicSlides.js) |
| `slickAdd` | method | same functionality can be achieved as with dynamic slides, look at dynamic slides [example](https://github.com/akiran/react-slick/blob/master/examples/DynamicSlides.js) |
| `slickRemove` | method | same functionality can be achieved as with dynamic slides, look at dynamic slides [example](https://github.com/akiran/react-slick/blob/master/examples/DynamicSlides.js) |
| `slickCurrentSlide` | method | same functionality can be achieved with `beforeChange hook` |
| `slickGetOption` | method | manage wrapper state for desired options |
| `getSlick` | method | a simple ref will do |
================================================
FILE: docs/common.md
================================================
#### `responsive` property
Array of objects in the form of `{ breakpoint: int, settings: { ... } }` The breakpoint _int_ is the `maxWidth` so the settings will be applied when resolution is below this value. Breakpoints in the array should be ordered from smallest to greatest. Use 'unslick' in place of the settings object to disable rendering the carousel at that breakpoint. Example: `[ { breakpoint: 768, settings: { slidesToShow: 3 } }, { breakpoint: 1024, settings: { slidesToShow: 5 } }, { breakpoint: 100000, settings: 'unslick' } ]`
### Custom next/prev arrows
To customize the next/prev arrow elements, simply create new React components and set them
as the values of nextArrow and prevArrow.
```js
class LeftNavButton extends React.Component {
render() {
return ;
}
}
```
Important: be sure that you pass your component's props to your clickable element
like the example above. If you don't, your custom component won't trigger the click handler.
You can also set `onClick={this.props.onClick}` if you only want to set the click handler.
### Flexbox support
If you have flex property on container div of slider, add below css
```css
* {
min-height: 0;
min-width: 0;
}
```
### Test Setup
If you try to run tests with jest in a project that uses react-slick, you may run into this error
```
matchMedia not present, legacy browsers require a polyfill
```
To fix this issue add below snippet in test-setup.js
```js
window.matchMedia =
window.matchMedia ||
function() {
return {
matches: false,
addListener: function() {},
removeListener: function() {}
};
};
```
and add below jest config in package.json
```json
"jest": {
"setupFiles": ["test-setup.js"]
}
```
### Polyfills for old IE support
`matchMedia` support from [media-match](https://github.com/weblinc/media-match)
================================================
FILE: docs/demos.js
================================================
"use strict";
import React from "react";
import Slider from "../src/slider";
import SimpleSlider from "../examples/SimpleSlider";
import SlideChangeHooks from "../examples/SlideChangeHooks";
import MultipleItems from "../examples/MultipleItems";
import MultipleRows from "../examples/MultipleRows";
import Responsive from "../examples/Responsive";
import Resizable from "../examples/Resizable";
import UnevenSetsInfinite from "../examples/UnevenSetsInfinite";
import UnevenSetsFinite from "../examples/UnevenSetsFinite";
import CenterMode from "../examples/CenterMode";
import FocusOnSelect from "../examples/FocusOnSelect";
import AutoPlay from "../examples/AutoPlay";
import AutoPlayMethods from "../examples/AutoPlayMethods";
import PauseOnHover from "../examples/PauseOnHover";
import Rtl from "../examples/Rtl";
import VariableWidth from "../examples/VariableWidth";
import AdaptiveHeight from "../examples/AdaptiveHeight";
import LazyLoad from "../examples/LazyLoad";
import Fade from "../examples/Fade";
import SlickGoTo from "../examples/SlickGoTo";
import CustomArrows from "../examples/CustomArrows";
import PreviousNextMethods from "../examples/PreviousNextMethods";
import DynamicSlides from "../examples/DynamicSlides";
import VerticalMode from "../examples/VerticalMode";
import SwipeToSlide from "../examples/SwipeToSlide";
import VerticalSwipeToSlide from "../examples/VerticalSwipeToSlide";
import CustomPaging from "../examples/CustomPaging";
import CustomSlides from "../examples/CustomSlides";
import AsNavFor from "../examples/AsNavFor";
import AppendDots from "../examples/AppendDots";
export default class App extends React.Component {
render() {
return (
);
}
================================================
FILE: playwright-tests/sample/sample.spec.tsx
================================================
//Imports the test and expect functions from the Playwright ct-react module
import { test, expect } from "@playwright/experimental-ct-react";
//Imports the App component to test from the relative ../App path
import App from "./sample.story";
//Configures the viewport to a 500x500 size
test.use({ viewport: { width: 500, height: 500 } });
//Starts a test case named "should work" which will run asynchronously,
//mount function binding is destructured from test parameter
test("Sample playwright test", async ({ mount }) => {
//Uses mount() to instantiate the component in isolation
const component = await mount();
//Asserts that component contains expected "Learn React" text on it verifying basic render.
await expect(component).toContainText("Learn React");
});
================================================
FILE: playwright-tests/sample/sample.story.tsx
================================================
import React from "react";
export default function App() {
return
);
};
}
================================================
FILE: src/slider.js
================================================
"use strict";
import React from "react";
import { InnerSlider } from "./inner-slider";
import json2mq from "json2mq";
import defaultProps from "./default-props";
import { canUseDOM, filterSettings } from "./utils/innerSliderUtils";
export default class Slider extends React.Component {
constructor(props) {
super(props);
this.state = {
breakpoint: null
};
this._responsiveMediaHandlers = [];
}
innerSliderRefHandler = ref => (this.innerSlider = ref);
media(query, handler) {
// javascript handler for css media query
const mql = window.matchMedia(query);
const listener = ({ matches }) => {
if (matches) {
handler();
}
};
mql.addListener(listener);
this._responsiveMediaHandlers.push({ mql, query, listener });
}
// handles responsive breakpoints
componentDidMount() {
// performance monitoring
//if (process.env.NODE_ENV !== 'production') {
//const { whyDidYouUpdate } = require('why-did-you-update')
//whyDidYouUpdate(React)
//}
if (this.props.responsive) {
let breakpoints = this.props.responsive.map(
breakpt => breakpt.breakpoint
);
// sort them in increasing order of their numerical value
breakpoints.sort((x, y) => x - y);
breakpoints.forEach((breakpoint, index) => {
// media query for each breakpoint
let bQuery;
if (index === 0) {
bQuery = json2mq({ minWidth: 0, maxWidth: breakpoint });
} else {
bQuery = json2mq({
minWidth: breakpoints[index - 1] + 1,
maxWidth: breakpoint
});
}
// when not using server side rendering
canUseDOM() &&
this.media(bQuery, () => {
this.setState({ breakpoint: breakpoint });
});
});
// Register media query for full screen. Need to support resize from small to large
// convert javascript object to media query string
let query = json2mq({ minWidth: breakpoints.slice(-1)[0] });
canUseDOM() &&
this.media(query, () => {
this.setState({ breakpoint: null });
});
}
}
componentWillUnmount() {
this._responsiveMediaHandlers.forEach(function(obj) {
obj.mql.removeListener(obj.listener);
});
}
slickPrev = () => this.innerSlider.slickPrev();
slickNext = () => this.innerSlider.slickNext();
slickGoTo = (slide, dontAnimate = false) =>
this.innerSlider.slickGoTo(slide, dontAnimate);
slickPause = () => this.innerSlider.pause("paused");
slickPlay = () => this.innerSlider.autoPlay("play");
render() {
var settings;
var newProps;
if (this.state.breakpoint) {
newProps = this.props.responsive.filter(
resp => resp.breakpoint === this.state.breakpoint
);
settings =
newProps[0].settings === "unslick"
? "unslick"
: { ...defaultProps, ...this.props, ...newProps[0].settings };
} else {
settings = { ...defaultProps, ...this.props };
}
// force scrolling by one if centerMode is on
if (settings.centerMode) {
if (
settings.slidesToScroll > 1 &&
process.env.NODE_ENV !== "production"
) {
console.warn(
`slidesToScroll should be equal to 1 in centerMode, you are using ${settings.slidesToScroll}`
);
}
settings.slidesToScroll = 1;
}
// force showing one slide and scrolling by one if the fade mode is on
if (settings.fade) {
if (settings.slidesToShow > 1 && process.env.NODE_ENV !== "production") {
console.warn(
`slidesToShow should be equal to 1 when fade is true, you're using ${settings.slidesToShow}`
);
}
if (
settings.slidesToScroll > 1 &&
process.env.NODE_ENV !== "production"
) {
console.warn(
`slidesToScroll should be equal to 1 when fade is true, you're using ${settings.slidesToScroll}`
);
}
settings.slidesToShow = 1;
settings.slidesToScroll = 1;
}
// makes sure that children is an array, even when there is only 1 child
let children = React.Children.toArray(this.props.children);
// Children may contain false or null, so we should filter them
// children may also contain string filled with spaces (in certain cases where we use jsx strings)
children = children.filter(child => {
if (typeof child === "string") {
return !!child.trim();
}
return !!child;
});
// rows and slidesPerRow logic is handled here
if (
settings.variableWidth &&
(settings.rows > 1 || settings.slidesPerRow > 1)
) {
console.warn(
`variableWidth is not supported in case of rows > 1 or slidesPerRow > 1`
);
settings.variableWidth = false;
}
let newChildren = [];
let currentWidth = null;
for (
let i = 0;
i < children.length;
i += settings.rows * settings.slidesPerRow
) {
let newSlide = [];
for (
let j = i;
j < i + settings.rows * settings.slidesPerRow;
j += settings.slidesPerRow
) {
let row = [];
for (let k = j; k < j + settings.slidesPerRow; k += 1) {
if (settings.variableWidth && children[k].props.style) {
currentWidth = children[k].props.style.width;
}
if (k >= children.length) break;
row.push(
React.cloneElement(children[k], {
key: 100 * i + 10 * j + k,
tabIndex: -1,
style: {
width: `${100 / settings.slidesPerRow}%`,
display: "inline-block"
}
})
);
}
newSlide.push(
{row}
);
}
if (settings.variableWidth) {
newChildren.push(