Skip to content

Commit

Permalink
added onBeforeScroll callback, updated types, fixes #40, #61
Browse files Browse the repository at this point in the history
  • Loading branch information
VikLiegostaiev committed Oct 22, 2020
1 parent d29fe88 commit 810b248
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export interface ReactPageScrollerProps {
* Callback fired when someone tries to scroll over last or first child component.
*/
handleScrollUnavailable?: () => void;
/**
* Callback fired before new page started scrolling into view.
*
* @param {number} page The number of next page.
*/
onBeforePageScroll?: (nextPage: number) => void;
/**
* Callback fired when the selected page changes.
*
Expand Down
36 changes: 24 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const DEFAULT_COMPONENTS_TO_RENDER_LENGTH = 0;
const DEFAULT_ANIMATION_TIMER_BUFFER = 200;
const KEY_UP = 38;
const KEY_DOWN = 40;
const MINIMAL_DELTA_Y_DIFFERENCE = 1;
const DISABLED_CLASS_NAME = "rps-scroll--disabled";

let previousTouchMove = null;
Expand All @@ -38,6 +39,7 @@ const ReactPageScroller = ({
containerWidth,
customPageNumber,
handleScrollUnavailable,
onBeforePageScroll,
pageOnChange,
renderAllPagesOnFirstRender,
transitionTimingFunction,
Expand All @@ -50,6 +52,18 @@ const ReactPageScroller = ({
const pageContainer = useRef(null);
const lastScrolledElement = useRef(null);

const scrollPage = useCallback(
nextComponentIndex => {
if (onBeforePageScroll) {
onBeforePageScroll(nextComponentIndex);
}

pageContainer.current.style.transform = `translate3d(0, ${nextComponentIndex *
-100}%, 0)`;
},
[onBeforePageScroll],
);

const addNextComponent = useCallback(
componentsToRenderOnMountLength => {
let tempComponentsToRenderLength = 0;
Expand Down Expand Up @@ -126,9 +140,7 @@ const ReactPageScroller = ({
if (!isNil(containers[componentIndex + 1])) {
disableScroll();
isScrolling = true;
pageContainer.current.style.transform = `translate3d(0, ${(componentIndex +
1) *
-100}%, 0)`;
scrollPage(componentIndex + 1);

setTimeout(() => {
if (isMounted) {
Expand All @@ -150,16 +162,15 @@ const ReactPageScroller = ({
disableScroll,
enableDocumentScroll,
handleScrollUnavailable,
scrollPage,
]);

const scrollWindowUp = useCallback(() => {
if (!isScrolling && !blockScrollUp) {
if (!isNil(containers[componentIndex - 1])) {
disableScroll();
isScrolling = true;
pageContainer.current.style.transform = `translate3d(0, ${(componentIndex -
1) *
-100}%, 0)`;
scrollPage(componentIndex - 1);

setTimeout(() => {
if (isMounted) {
Expand All @@ -181,6 +192,7 @@ const ReactPageScroller = ({
disableScroll,
enableDocumentScroll,
handleScrollUnavailable,
scrollPage,
]);

const touchMove = useCallback(
Expand All @@ -200,7 +212,7 @@ const ReactPageScroller = ({

const wheelScroll = useCallback(
event => {
if (Math.abs(event.deltaY) > 1) {
if (Math.abs(event.deltaY) > MINIMAL_DELTA_Y_DIFFERENCE) {
if (isPositiveNumber(event.deltaY)) {
lastScrolledElement.current = event.target;
scrollWindowDown();
Expand Down Expand Up @@ -265,8 +277,7 @@ const ReactPageScroller = ({
if (!isNil(containers[customPageNumber]) && !isScrolling) {
isScrolling = true;
// eslint-disable-next-line max-len
pageContainer.current.style.transform = `translate3d(0, ${customPageNumber *
-100}%, 0)`;
scrollPage(customPageNumber);

if (
isNil(containers[customPageNumber]) &&
Expand Down Expand Up @@ -294,14 +305,13 @@ const ReactPageScroller = ({
}
}
}
}, [customPageNumber]);
}, [customPageNumber, scrollPage]);

useEffect(() => {
if (isTransitionAfterComponentsToRenderChanged) {
isTransitionAfterComponentsToRenderChanged = false;

pageContainer.current.style.transform = `translate3d(0, ${customPageNumber *
-100}%, 0)`;
scrollPage(customPageNumber);

setTimeout(() => {
setComponentIndex(customPageNumber);
Expand All @@ -312,6 +322,7 @@ const ReactPageScroller = ({
animationTimerBuffer,
componentsToRenderLength,
customPageNumber,
scrollPage,
]);

return (
Expand Down Expand Up @@ -349,6 +360,7 @@ ReactPageScroller.propTypes = {
containerWidth: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
customPageNumber: PropTypes.number,
handleScrollUnavailable: PropTypes.func,
onBeforePageScroll: PropTypes.func,
pageOnChange: PropTypes.func,
renderAllPagesOnFirstRender: PropTypes.bool,
transitionTimingFunction: PropTypes.string,
Expand Down

0 comments on commit 810b248

Please sign in to comment.