├── .editorconfig ├── .eslintignore ├── .eslintrc.cjs ├── .github └── FUNDING.yml ├── .gitignore ├── CODEOWNERS ├── LICENSE ├── README.md ├── demo ├── App.tsx ├── FreeScrollSliderDemo │ └── index.tsx ├── MarqueeSliderDemo │ └── index.tsx ├── ResponsiveSlider │ └── index.tsx ├── ScrollSnapSliderDemo │ └── index.tsx ├── ThumbnailSliderDemo │ └── index.tsx ├── index.html └── index.tsx ├── jest.config.js ├── package.json ├── src ├── DotNav │ └── index.tsx ├── Slide │ ├── index.tsx │ └── useIntersection.ts ├── SliderButton │ └── index.tsx ├── SliderNav │ └── index.tsx ├── SliderProgress │ └── index.tsx ├── SliderProvider │ ├── context.tsx │ ├── index.tsx │ ├── reducer.ts │ ├── useAutoplay.ts │ ├── useBreakpoints.ts │ ├── useDragScroll.ts │ ├── useMarquee.ts │ └── useScrollToIndex.ts ├── SliderTrack │ ├── getGhostSlideWidth.ts │ └── index.tsx ├── index.test.js ├── index.ts ├── types.ts ├── useSlider │ └── index.tsx └── withSlider │ └── index.tsx ├── tsconfig.build.json ├── tsconfig.json ├── webpack.build-demo.config.js ├── webpack.dev.config.js └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | end_of_line = lf 10 | max_line_length = null -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | dist 2 | dist-demo 3 | -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | extends: [ 4 | 'eslint:recommended', 5 | 'plugin:react/recommended', 6 | 'plugin:react-hooks/recommended', 7 | 'plugin:@typescript-eslint/recommended', 8 | ], 9 | env: { 10 | node: true, 11 | browser: true, 12 | jest: true, 13 | }, 14 | plugins: [ 15 | 'react', 16 | "react-hooks", 17 | 'jest', 18 | 'jest-dom', 19 | ], 20 | rules: { 21 | 'react/prop-types': 'off', 22 | 'react/jsx-max-props-per-line': [ 23 | 1, 24 | { 25 | maximum: 1, 26 | }, 27 | ], 28 | 'react/jsx-first-prop-new-line': [ 29 | 1, 30 | 'multiline', 31 | ], 32 | 'react/jsx-closing-bracket-location': [ 33 | 1, 34 | 'tag-aligned', 35 | ], 36 | 'object-curly-newline': [ 37 | 1, 38 | { 39 | multiline: true, 40 | consistent: true, 41 | }, 42 | ], 43 | 'object-property-newline': [ 44 | 1, 45 | { 46 | allowAllPropertiesOnSameLine: false, 47 | }, 48 | ], 49 | 'no-unused-vars': [ 50 | 1, 51 | ], 52 | '@typescript-eslint/ban-ts-comment': 'off', 53 | }, 54 | settings: { 55 | react: { 56 | version: 'detect', 57 | }, 58 | 'import/resolver': { 59 | node: { 60 | extensions: ['.js', '.jsx', '.ts', '.tsx'], 61 | }, 62 | }, 63 | }, 64 | }; 65 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [jacobsfletch] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 2 | patreon: # Replace with a single Patreon username 3 | open_collective: # Replace with a single Open Collective username 4 | ko_fi: # Replace with a single Ko-fi username 5 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 6 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 7 | liberapay: # Replace with a single Liberapay username 8 | issuehunt: # Replace with a single IssueHunt username 9 | otechie: # Replace with a single Otechie username 10 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | package-lock.json 4 | dist 5 | dist-demo 6 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @jacobsfletch 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Faceless UI 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![NPM](https://img.shields.io/npm/v/@faceless-ui/slider)](https://www.npmjs.com/@faceless-ui/slider) 2 | ![Bundle Size](https://img.shields.io/bundlephobia/minzip/@faceless-ui/slider?label=zipped) 3 | 4 | # React Slider 5 | 6 | Read the full documentation [here](https://facelessui.com/docs/slider). 7 | 8 | ## Installation 9 | 10 | ```bash 11 | $ npm i @faceless-ui/slider 12 | $ # or 13 | $ yarn add @faceless-ui/slider 14 | ``` 15 | 16 | ## Development 17 | 18 | To develop this module locally, spin up the [demo app](./demo/App.demo.js): 19 | 20 | ```bash 21 | $ git clone git@github.com:faceless-ui/slider.git 22 | $ yarn 23 | $ yarn dev 24 | $ open http://localhost:3000 25 | ``` 26 | 27 | ## License 28 | 29 | [MIT](https://github.com/faceless-ui/slider/blob/master/LICENSE) Copyright (c) Faceless UI 30 | -------------------------------------------------------------------------------- /demo/App.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ResponsiveSlider from './ResponsiveSlider/index.js'; 3 | // import MarqueeSliderDemo from './MarqueeSliderDemo/index.js'; 4 | // import FreeScrollSliderDemo from './FreeScrollSliderDemo/index.js'; 5 | // import ThumbnailSliderDemo from './ThumbnailSliderDemo/index.js'; 6 | // import ScrollSnapSliderDemo from './ScrollSnapSliderDemo/index.js'; 7 | 8 | const App: React.FC = () => ( 9 |
15 | {/*

16 | Free Scroll Slider: 17 |

*/} 18 | {/* */} 19 |

20 | Responsive Slider: 21 |

22 | 23 | {/*

24 | Scroll Snap Slider: 25 |

26 | */} 27 | {/*

28 | Thumbnail Slider Demo: 29 |

30 | */} 31 | {/*

32 | Marquee Slider Demo 33 |

34 | */} 35 |
36 | ); 37 | 38 | export default App; 39 | -------------------------------------------------------------------------------- /demo/FreeScrollSliderDemo/index.tsx: -------------------------------------------------------------------------------- 1 | import React, { Fragment } from 'react'; 2 | import { 3 | SliderProvider, 4 | Slide, 5 | SliderNav, 6 | SliderProgress, 7 | SliderTrack, 8 | DotNav 9 | } from '@faceless-ui/slider'; 10 | 11 | const FreeScrollSliderDemo: React.FC = () => ( 12 | 13 | 14 |
 15 |         slidesToShow: 2
 16 |         
17 | scrollable: true 18 |
19 |
20 | 32 |
41 | prev
), 45 | }} 46 | nextButtonProps={{ 47 | children: (
next
), 48 | }} 49 | /> 50 | 56 | 63 |
64 | Slide 1 65 |
66 |
67 | 74 |
75 | Slide 2 76 | 85 |
86 |
87 | 94 |
95 | Slide 3 96 |
97 |
98 | 105 |
106 | Slide 4 107 |
108 |
109 |
110 | 122 | 123 | 132 |
133 |
134 | ); 135 | 136 | export default FreeScrollSliderDemo; 137 | -------------------------------------------------------------------------------- /demo/MarqueeSliderDemo/index.tsx: -------------------------------------------------------------------------------- 1 | import React, { Fragment } from 'react'; 2 | import { 3 | SliderProvider, 4 | Slide, 5 | SliderTrack, 6 | } from '@faceless-ui/slider'; 7 | 8 | const MarqueeSliderDemo: React.FC = () => ( 9 | 10 | 11 |
 12 |         slidesToShow: 2
 13 |         
14 | scrollable: true 15 |
16 |
17 | 22 |
31 | 37 | 44 |
45 | Slide 1 46 |
47 |
48 | 55 |
56 | Slide 2 57 | 66 |
67 |
68 | 75 |
76 | Slide 3 77 |
78 |
79 | 86 |
87 | Slide 4 88 |
89 |
90 | 97 |
98 | Slide 5 99 |
100 |
101 | 108 |
109 | Slide 6 110 |
111 |
112 |
113 |
114 |
115 |
116 | ); 117 | 118 | export default MarqueeSliderDemo; 119 | -------------------------------------------------------------------------------- /demo/ResponsiveSlider/index.tsx: -------------------------------------------------------------------------------- 1 | import React, { Fragment, useCallback, useEffect, useState } from 'react'; 2 | import { 3 | SliderProvider, 4 | Slide, 5 | SliderTrack, 6 | } from '@faceless-ui/slider'; 7 | 8 | // NOTE: this components test both internal breakpoints and external breakpoints 9 | 10 | const slides = Array.from(Array(6).keys()); // NOTE: create array from number 11 | 12 | const desktopSlidesToShow = 3; 13 | 14 | const ResponsiveSlider: React.FC = () => { 15 | const [slidesToShow, setSlidesToShow] = useState(desktopSlidesToShow); 16 | 17 | const handleResize = useCallback(() => { 18 | const windowWidth = window.innerWidth; 19 | if (windowWidth < 768) setSlidesToShow(1) 20 | else setSlidesToShow(desktopSlidesToShow) 21 | }, []); 22 | 23 | useEffect(() => { 24 | handleResize(); 25 | 26 | window.addEventListener('resize', handleResize) 27 | 28 | return () => { 29 | window.removeEventListener('resize', handleResize) 30 | } 31 | }, [handleResize]) 32 | 33 | return ( 34 | 35 | 44 |
53 | 54 | {slides.map((slide, index) => ( 55 | 59 |
66 | {`Slide ${index + 1}`} 67 |
68 |
69 | ))} 70 |
71 |
72 |
73 |
74 | ); 75 | }; 76 | 77 | export default ResponsiveSlider; 78 | -------------------------------------------------------------------------------- /demo/ScrollSnapSliderDemo/index.tsx: -------------------------------------------------------------------------------- 1 | import React, { Fragment, useState } from 'react'; 2 | import { 3 | SliderProvider, 4 | Slide, 5 | SliderNav, 6 | SliderProgress, 7 | SliderTrack, 8 | } from '@faceless-ui/slider'; 9 | 10 | const ScrollSnapSliderDemo: React.FC = () => { 11 | const [autoPlay, setAutoplay] = useState(true); 12 | return ( 13 | 14 | 15 |
 16 |           slidesToShow: 1
 17 |           
18 | {`autoPlay: ${autoPlay}`} 19 |
20 |
21 | 29 | 34 |
43 | prev
), 47 | }} 48 | nextButtonProps={{ 49 | children: (
next
), 50 | }} 51 | /> 52 | 58 | 65 |
66 | Slide 1 67 |
68 |
69 | 76 |
77 | Slide 2 78 |
79 |
80 | 87 |
88 | Slide 3 89 |
90 |
91 |
92 | 104 | 105 |
106 |
107 | ); 108 | }; 109 | 110 | export default ScrollSnapSliderDemo; 111 | -------------------------------------------------------------------------------- /demo/ThumbnailSliderDemo/index.tsx: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react'; 2 | import { 3 | SliderProvider, 4 | Slide, 5 | SliderNav, 6 | SliderProgress, 7 | SliderTrack, 8 | } from '@faceless-ui/slider'; 9 | 10 | const ThumbnailSliderDemo: React.FC = () => { 11 | const [index, setIndex] = useState(0); 12 | 13 | return ( 14 |
15 | 19 | 28 | 37 |
38 | Slide 1 39 |
40 |
41 | 50 |
51 | Slide 2 52 |
53 |
54 | 63 |
64 | Slide 3 65 |
66 |
67 | 76 |
77 | Slide 4 78 |
79 |
80 | 89 |
90 | Slide 5 91 |
92 |
93 |
94 |
95 | { 97 | setIndex(incomingIndex); 98 | }} 99 | slidesToShow={3} 100 | slideOnSelect 101 | > 102 | prev
), 105 | }} 106 | nextButtonProps={{ 107 | children: (
next
), 108 | }} 109 | style={{ 110 | 111 | display: 'flex', 112 | }} 113 | /> 114 | 123 | 132 |
133 | Slide 1 134 |
135 |
136 | 145 |
146 | Slide 2 147 |
148 |
149 | 158 |
159 | Slide 3 160 |
161 |
162 | 171 |
172 | Slide 4 173 |
174 |
175 | 184 |
185 | Slide 5 186 |
187 |
188 |
189 | 204 | 205 | 206 | ); 207 | } 208 | 209 | export default ThumbnailSliderDemo; 210 | -------------------------------------------------------------------------------- /demo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | React Slider 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | -------------------------------------------------------------------------------- /demo/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import App from './App.js'; 3 | import { createRoot } from 'react-dom/client'; 4 | const container = document.getElementById('root'); 5 | if (container) { 6 | const root = createRoot(container); 7 | root.render(); 8 | } 9 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | verbose: true, 3 | setupFilesAfterEnv: ['./src/index.test.js'], 4 | testPathIgnorePatterns: ['./src/index.test.js'], 5 | }; 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@faceless-ui/slider", 3 | "version": "2.0.0-beta.0", 4 | "type": "module", 5 | "main": "dist/index.js", 6 | "types": "dist/index.d.ts", 7 | "homepage:": "https://facelessui.com/docs/slider", 8 | "repository": "git@github.com:faceless-ui/slider.git", 9 | "description": "A React library for building every kind of slider", 10 | "author": { 11 | "email": "dev@facelessui.com", 12 | "name": "Faceless UI", 13 | "url": "https://facelessui.com" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/faceless-ui/slider/issues", 17 | "email": "dev@facelessui.com" 18 | }, 19 | "funding": [ 20 | { 21 | "type": "individual", 22 | "url": "https://github.com/sponsors/jacobsfletch" 23 | } 24 | ], 25 | "contributors": [ 26 | { 27 | "email": "jacobsfletch@gmail.com", 28 | "name": "Jacob Fletcher", 29 | "url": "https://jacobsfletch.com" 30 | } 31 | ], 32 | "license": "MIT", 33 | "keywords": [ 34 | "react", 35 | "react-component", 36 | "slider", 37 | "scroll", 38 | "slide", 39 | "react-slick", 40 | "slick-slider" 41 | ], 42 | "scripts": { 43 | "build": "yarn lint:src && tsc --project tsconfig.build.json", 44 | "build:demo": "webpack --config webpack.build-demo.config.js", 45 | "dev": "webpack serve --config webpack.dev.config.js", 46 | "lint:src": "eslint ./src", 47 | "lint:demo": "eslint ./demo", 48 | "test": "jest" 49 | }, 50 | "peerDependencies": { 51 | "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0-rc.0", 52 | "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0-rc.0" 53 | }, 54 | "dependencies": { 55 | "smoothscroll-polyfill": "^0.4.4" 56 | }, 57 | "devDependencies": { 58 | "@pmmmwh/react-refresh-webpack-plugin": "^0.5.5", 59 | "@types/node": "^14.14.22", 60 | "@types/react": "^18.0.0", 61 | "@types/react-dom": "^18.0.0", 62 | "@types/smoothscroll-polyfill": "^0.3.1", 63 | "@typescript-eslint/eslint-plugin": "^7.3.1", 64 | "@typescript-eslint/parser": "^7.3.1", 65 | "@types/webpack": "^5.28.5", 66 | "eslint": "^8.56.0", 67 | "eslint-plugin-import": "^2.26.0", 68 | "eslint-plugin-jest": "^26.1.5", 69 | "eslint-plugin-jest-dom": "^4.0.1", 70 | "eslint-plugin-jsx-a11y": "^6.5.1", 71 | "eslint-plugin-node": "^11.1.0", 72 | "eslint-plugin-react": "^7.29.4", 73 | "eslint-plugin-react-hooks": "^4.5.0", 74 | "eslint-webpack-plugin": "^3.1.1", 75 | "html-webpack-plugin": "^5.5.0", 76 | "jest": "^28.0.2", 77 | "react": "^19.0.0-rc.0", 78 | "react-dom": "^19.0.0-rc.0", 79 | "react-refresh": "^0.13.0", 80 | "react-refresh-typescript": "^2.0.4", 81 | "ts-loader": "^9.5.1", 82 | "typescript": "^5.4.5", 83 | "webpack": "^5.91.0", 84 | "webpack-cli": "^5.1.4", 85 | "webpack-dev-server": "^5.0.4" 86 | }, 87 | "files": [ 88 | "dist" 89 | ] 90 | } 91 | -------------------------------------------------------------------------------- /src/DotNav/index.tsx: -------------------------------------------------------------------------------- 1 | import React, { HTMLProps } from 'react'; 2 | import { useSlider } from '../useSlider/index.js'; 3 | 4 | export interface DotNavProps extends HTMLProps { 5 | htmlElement?: React.ElementType 6 | dotClassName?: string 7 | activeDotClassName?: string 8 | buttonProps?: HTMLProps 9 | } 10 | 11 | export const DotNav: React.FC = (props) => { 12 | const { 13 | htmlElement: Tag = 'div', 14 | dotClassName, 15 | activeDotClassName, 16 | buttonProps = {}, 17 | ...rest 18 | } = props; 19 | 20 | const { 21 | currentSlideIndex, 22 | slides, 23 | setIsPaused, 24 | pauseOnHover, 25 | goToSlideIndex, 26 | id: idFromContext 27 | } = useSlider(); 28 | 29 | const dotsArray = Array.from(Array(slides.length || 0).keys()); 30 | 31 | return ( 32 | { 35 | if (pauseOnHover) setIsPaused(true); 36 | }} 37 | onMouseLeave={() => { 38 | if (pauseOnHover) setIsPaused(false); 39 | }} 40 | > 41 | {dotsArray.map((dot, index) => { 42 | const isActive = currentSlideIndex === index; 43 | 44 | return ( 45 |