Skip to content

Commit c2a230f

Browse files
Added functionality to disable auto scroll to top.
1 parent e0bd798 commit c2a230f

6 files changed

Lines changed: 27 additions & 10 deletions

File tree

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"react/no-array-index-key": "off",
2121
"jsx-a11y/click-events-have-key-events": "off",
2222
"react/function-component-definition": "off",
23-
"jsx-a11y/no-static-element-interactions": "off"
23+
"jsx-a11y/no-static-element-interactions": "off",
24+
"react-hooks/exhaustive-deps": "warn"
2425
}
2526
}

example/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"@vitejs/plugin-react": "file:../node_modules/@vitejs/plugin-react",
1212
"react": "file:../node_modules/react",
1313
"react-dom": "file:../node_modules/react-dom",
14-
"react-swipeable-wrapper": "file:..",
1514
"vite": "file:../node_modules/vite"
1615
}
1716
}

example/src/App.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable import/no-unresolved */
12
import React, { useCallback, useRef, useState } from "react";
23

34
import SwipeableWrapper from "react-swipeable-wrapper";
@@ -98,6 +99,7 @@ const App = () => {
9899
initialIndex={initialIndex}
99100
onSlideChange={handleSlideChange}
100101
bottomBarRef={bottomBarRef}
102+
disableAutoScroll
101103
>
102104
<div style={{ ...styles.slide, ...styles.slide1 }}>1st slide</div>
103105
<div style={{ ...styles.slide, ...styles.slide2 }}>2nd slide</div>

example/vite.config.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
import { defineConfig } from "vite";
22
import react from "@vitejs/plugin-react";
3+
const path = require("path");
34

45
export default defineConfig({
56
plugins: [react()],
6-
base: "react-swipeable-wrapper",
7+
base: "/react-swipeable-wrapper/",
8+
resolve: {
9+
alias: {
10+
"react-swipeable-wrapper": path.resolve(__dirname, "../lib"),
11+
},
12+
},
13+
server: {
14+
fs: {
15+
// Allow serving files from one level up to the project root
16+
allow: [".."],
17+
},
18+
},
719
});

example/yarn.lock

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,6 @@ react-refresh@^0.11.0:
603603
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.11.0.tgz#77198b944733f0f1f1a90e791de4541f9f074046"
604604
integrity sha512-F27qZr8uUqwhWZboondsPx8tnC3Ct3SxZA3V5WyEvujRyyNv0VYPhoBg1gZ8/MV5tubQp76Trw8lTv9hzRBa+A==
605605

606-
"react-swipeable-wrapper@file:..":
607-
version "1.1.0"
608-
609606
"react@file:../node_modules/react":
610607
version "17.0.2"
611608
dependencies:

lib/SwipeableWrapper/index.jsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const SwipeableWrapper = forwardRef(
4343
transitionDuration,
4444
transitionTimingFunction,
4545
containerStyles,
46+
disableAutoScroll,
4647
},
4748
ref,
4849
) => {
@@ -58,7 +59,7 @@ const SwipeableWrapper = forwardRef(
5859
const currIndex = index.current;
5960
rAF(() => {
6061
onSlideChange(index.current);
61-
scrollToTop();
62+
if (!disableAutoScroll) scrollToTop();
6263
const { current: el = null } = elementRef;
6364
if (el) {
6465
const height = window.innerHeight - el.clientTop;
@@ -68,7 +69,7 @@ const SwipeableWrapper = forwardRef(
6869
});
6970
previousIndex.current = index.current;
7071
}
71-
}, [onSlideChange, rAF]);
72+
}, [disableAutoScroll, onSlideChange, rAF]);
7273

7374
const swipeToIndex = useCallback(
7475
(slideToIndex, avoidAnimation = false) => {
@@ -223,7 +224,10 @@ const SwipeableWrapper = forwardRef(
223224

224225
SwipeableWrapper.propTypes = {
225226
bottomBarRef: PropTypes.shape({
226-
current: PropTypes.oneOf([PropTypes.node, PropTypes.object]),
227+
current: PropTypes.oneOfType([
228+
PropTypes.node,
229+
PropTypes.objectOf(PropTypes.object),
230+
]),
227231
}),
228232
initialIndex: PropTypes.number,
229233
onSlideChange: PropTypes.func,
@@ -232,16 +236,18 @@ SwipeableWrapper.propTypes = {
232236
transitionDuration: PropTypes.number,
233237
transitionTimingFunction: PropTypes.string,
234238
containerStyles: PropTypes.shape({}),
239+
disableAutoScroll: PropTypes.bool,
235240
};
236241

237242
SwipeableWrapper.defaultProps = {
238-
bottomBarRef: null,
243+
bottomBarRef: { current: null },
239244
onSlideChange: () => {},
240245
initialIndex: 0,
241246
filterNodes: [],
242247
transitionDuration: 300,
243248
transitionTimingFunction: "ease-out",
244249
containerStyles: {},
250+
disableAutoScroll: false,
245251
};
246252

247253
export default memo(SwipeableWrapper);

0 commit comments

Comments
 (0)