use-scroller 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,22 +1,15 @@
1
- ![npm](https://img.shields.io/npm/v/scroll-js)
2
- ![node](https://img.shields.io/node/v/scroll-js)
3
- ![license](https://img.shields.io/npm/l/scroll-js)
1
+ # use-scroller
4
2
 
5
- ## use-scroll
3
+ A lightweight library that enables smooth scrolling for HTML elements using simple React hooks provided by the package. This library offers additional scrolling features and provides several callback functions for customizable scroll operations.
6
4
 
7
- A light-weight library that will allow you to scroll any html element using simple react hooks provided by the package.
8
- In addition to providing extra scrolling features, this library also return a number of callback that can triger scroll operation depending on the user needs.
9
-
10
- <p align="middle">
5
+ <div align="center">
11
6
  <img src="https://s9.gifyu.com/images/ezgif.com-gif-maker3092e8916a41884d.gif" width="400"/>
12
7
  <img src="https://s3.gifyu.com/images/ezgif.com-gif-maker-1c4ee7c66dcd4dd01.gif" width="400"/>
13
- </p>
8
+ </div>
14
9
 
15
10
  ## Installation
16
11
 
17
- ### React
18
-
19
- ```javascript
12
+ ```bash
20
13
  #YARN
21
14
  yarn add use-scroller
22
15
 
@@ -30,23 +23,42 @@ pnpm install use-scroller
30
23
 
31
24
  ## Usage
32
25
 
26
+ ### Scroll state
27
+
28
+ ```typescript
29
+ import { useScroll } from 'use-scroller'
30
+
31
+ export default function ScrollStateExample() {
32
+ const { ref, state } = useScroll<HTMLDivElement>()
33
+
34
+ return (
35
+ <>
36
+ <p>Scroll State:</p>
37
+ <pre>{JSON.stringify(state, null, 2)}</pre>
38
+ <div ref={ref} style={{ height: '200px', overflowY: 'scroll' }}>
39
+ {/* Your content */}
40
+ </div>
41
+ </>
42
+ )
43
+ }
44
+ ```
45
+
33
46
  ### Carousel
34
47
 
35
- ```javascript
48
+ ```typescript
36
49
  import { useScroll } from 'use-scroller'
37
50
 
38
- export default function Carousel() {
51
+ export default function CarouselExample() {
39
52
  const { ref, scrollLeft, scrollRight } = useScroll<HTMLDivElement>()
53
+
40
54
  return (
41
55
  <>
42
- <button onClick={() => scrollLeft()} >
43
- Top
44
- </button>
45
- <button onClick={() => scrollRight()}>
46
- Next
47
- </button>
48
-
49
- <Box ref={ref} />
56
+ <button onClick={() => scrollLeft()}>Scroll Left</button>
57
+ <button onClick={() => scrollRight()}>Scroll Right</button>
58
+
59
+ <div ref={ref} style={{ width: '400px', overflowX: 'scroll' }}>
60
+ {/* Your carousel content */}
61
+ </div>
50
62
  </>
51
63
  )
52
64
  }
@@ -54,30 +66,56 @@ export default function Carousel() {
54
66
 
55
67
  ### Box
56
68
 
57
- ```javascript
69
+ ```typescript
58
70
  import { useScroll } from 'use-scroller'
59
71
 
60
- export default function ScrollBox() {
61
- const { ref, scrollLeft, scrollRight, scrollTop, scrollBotom, scrollCenter } = useScroll<HTMLDivElement>()
72
+ export default function BoxExample() {
73
+ const { ref, scrollLeft, scrollRight, scrollTop, scrollBottom, scrollToCenter } =
74
+ useScroll<HTMLDivElement>()
75
+
62
76
  return (
63
77
  <>
64
- <button onClick={() => scrollCenter()} >
65
- Center
66
- </button>
67
- <button onClick={() => scrollLeft()} >
68
- Left
69
- </button>
70
- <button onClick={() => scrollRight()}>
71
- Right
72
- </button>
73
- <button onClick={() => scrollTop()}>
74
- Top
75
- </button>
76
- <button onClick={() => scrollBotom()}>
77
- Bottom
78
- </button>
79
- # Box component
80
- <Box ref={ref} />
78
+ <button onClick={() => scrollToCenter()}>Scroll to Center</button>
79
+ <button onClick={() => scrollLeft()}>Scroll Left</button>
80
+ <button onClick={() => scrollRight()}>Scroll Right</button>
81
+ <button onClick={() => scrollTop()}>Scroll to Top</button>
82
+ <button onClick={() => scrollBottom()}>Scroll to Bottom</button>
83
+ <div ref={ref} style={{ width: '300px', height: '300px', overflow: 'scroll' }}>
84
+ {/* Your content */}
85
+ </div>
86
+ </>
87
+ )
88
+ }
89
+ ```
90
+
91
+ ### Scroll to target
92
+
93
+ ```typescript
94
+ import { useWindowScroll } from 'use-scroller'
95
+
96
+ export default function ScrollToTargetExample() {
97
+ const { scrollToTarget } = useWindowScroll()
98
+ const ref1 = useRef(null)
99
+ const ref2 = useRef(null)
100
+ const ref3 = useRef(null)
101
+
102
+ return (
103
+ <>
104
+ <button onClick={() => scrollToTarget(ref1)}>Scroll to Target 1</button>
105
+ <button onClick={() => scrollToTarget(ref2)}>Scroll to Target 2</button>
106
+ <button onClick={() => scrollToTarget(ref3)}>Scroll to Target 3</button>
107
+
108
+ <div>
109
+ <div ref={ref1} style={{ height: '400px', backgroundColor: 'lightcoral' }}>
110
+ Target 1
111
+ </div>
112
+ <div ref={ref2} style={{ height: '400px', backgroundColor: 'lightseagreen' }}>
113
+ Target 2
114
+ </div>
115
+ <div ref={ref3} style={{ height: '400px', backgroundColor: 'lightsalmon' }}>
116
+ Target 3
117
+ </div>
118
+ </div>
81
119
  </>
82
120
  )
83
121
  }
@@ -85,24 +123,28 @@ export default function ScrollBox() {
85
123
 
86
124
  ## API Documentation
87
125
 
88
- #### Available hooks
126
+ ### Available Hooks
127
+
128
+ #### `useScroll`
129
+
130
+ This hook is designed to handle element scroll events, enabling smooth scrolling for specified HTML elements.
131
+
132
+ ##### Options
133
+
134
+ - `direction` (Type: `horizontal` or `vertical`, Default: `vertical`): Specifies the desired scroll direction. You can set it to `horizontal` or `vertical` based on your needs.
135
+
136
+ - `duration` (Type: `number`, Default: `300`): Sets the animation duration for scrolling. Adjust this value to control the speed of the scroll animation.
137
+
138
+ - `easingOption` (Type: `EasingOptions`, Default: `ease-in-out`): Determines the type of animation to use for scrolling. You can choose from various easing options such as `ease-in`, `ease-out`, `ease-in-out`, etc., to customize the scroll animation's behavior.
139
+
140
+ #### `useWindowScroll`
89
141
 
90
- | Hook | Description |
91
- | ----------------- | ----------------------------- |
92
- | `useScroll` | Handles element scroll events |
93
- | `useWindowScroll` | Handles window scroll events |
142
+ This hook is tailored to handle window scroll events, making it easy to manage scrolling operations within the entire window.
94
143
 
95
- #### useScroll(options)
144
+ ##### Options
96
145
 
97
- | Option | Type | Description |
98
- | -------------- | ------------------------ | ------------------------------------ |
99
- | `direction` | `horizontal or vertical` | Set the desired scroll direction |
100
- | `duration` | `number` | Set the animation duration to scroll |
101
- | `easingOption` | `EasingOptions` | Set the type of animation to scroll |
146
+ - `duration` (Type: `number`, Default: `300`): Specifies the animation duration for scrolling operations. Adjust this value to control the speed of the scroll animation.
102
147
 
103
- #### useWindowScroll(options)
148
+ - `easingOption` (Type: `EasingOptions`, Default: `ease-in-out`): Defines the type of animation to use for scrolling within the window. You can choose from various easing options to customize the scroll animation's behavior.
104
149
 
105
- | Option | Type | Description |
106
- | -------------- | --------------- | ------------------------------------ |
107
- | `duration` | `number` | Set the animation duration to scroll |
108
- | `easingOption` | `EasingOptions` | Set the type of animation to scroll |
150
+ Feel free to update and expand this documentation further based on your library's features and usage patterns.
@@ -10,10 +10,15 @@ export declare function useScroll<T extends HTMLElement>(props?: Partial<ScrollO
10
10
  isScrolledRight: boolean;
11
11
  isScrolledTop: boolean;
12
12
  isScrolledBottom: boolean;
13
+ maxScrollLeft: number;
14
+ maxScrollTop: number;
15
+ scrollPercentageX: number;
16
+ scrollPercentageY: number;
13
17
  };
14
- scrollLeft: (offset?: number) => void;
15
- scrollRight: (offset?: number) => void;
16
- scrollBottom: (offset?: number) => void;
17
- scrollTop: (offset?: number) => void;
18
- scrollCenter: () => void;
18
+ scrollLeft: ((offset?: number | undefined) => void) | undefined;
19
+ scrollRight: ((offset?: number | undefined) => void) | undefined;
20
+ scrollBottom: ((offset?: number | undefined) => void) | undefined;
21
+ scrollTop: ((offset?: number | undefined) => void) | undefined;
22
+ scrollCenter: (() => void) | undefined;
23
+ scrollTo: ((to: number) => void) | undefined;
19
24
  };
@@ -16,69 +16,65 @@ var react_1 = require("react");
16
16
  var scroller_1 = require("../scroller");
17
17
  var utils_1 = require("../utils");
18
18
  var use_iso_morphic_effect_1 = require("./use-iso-morphic-effect");
19
+ var initialState = {
20
+ left: 0,
21
+ top: 0,
22
+ isScrollable: false,
23
+ isScrolledLeft: true,
24
+ isScrolledRight: false,
25
+ isScrolledTop: true,
26
+ isScrolledBottom: false,
27
+ maxScrollLeft: 0,
28
+ maxScrollTop: 0,
29
+ scrollPercentageX: 0,
30
+ scrollPercentageY: 0, // Percentage of vertical scroll covered
31
+ };
19
32
  function useScroll(props) {
33
+ var _a, _b, _c, _d, _e, _f;
20
34
  var ref = (0, react_1.useRef)(null);
21
- var _a = (0, react_1.useState)(function () { return ({
22
- left: 0,
23
- top: 0,
24
- isScrollable: false,
25
- isScrolledLeft: false,
26
- isScrolledRight: false,
27
- isScrolledTop: false,
28
- isScrolledBottom: false,
29
- }); }), state = _a[0], setState = _a[1];
30
35
  var scrollOpt = __assign(__assign({}, utils_1.defaultScrollOptions), props);
31
- var scrollCenter = (0, react_1.useCallback)(function () {
32
- var container = (0, utils_1.getElement)(ref);
33
- var scrollContainer = (0, scroller_1.scroller)({ container: container, options: scrollOpt });
34
- scrollContainer.scrollToCenter();
35
- }, []);
36
- var scrollTop = (0, react_1.useCallback)(function (offset) {
37
- var container = (0, utils_1.getElement)(ref);
38
- var scrollContainer = (0, scroller_1.scroller)({ container: container, options: scrollOpt });
39
- scrollContainer.scrollToTop(offset);
40
- }, []);
41
- var scrollBottom = (0, react_1.useCallback)(function (offset) {
42
- var container = (0, utils_1.getElement)(ref);
43
- var scrollContainer = (0, scroller_1.scroller)({ container: container, options: scrollOpt });
44
- scrollContainer.scrollToBottom(offset);
45
- }, []);
46
- var scrollRight = (0, react_1.useCallback)(function (offset) {
47
- var container = (0, utils_1.getElement)(ref);
48
- var scrollContainer = (0, scroller_1.scroller)({ container: container, options: scrollOpt });
49
- scrollContainer.scrollToRight(offset);
50
- }, []);
51
- var scrollLeft = (0, react_1.useCallback)(function (offset) {
52
- var container = (0, utils_1.getElement)(ref);
53
- var scrollContainer = (0, scroller_1.scroller)({ container: container, options: scrollOpt });
54
- scrollContainer.scrollToLeft(offset);
55
- }, []);
36
+ var scrollerRef = (0, react_1.useRef)(null);
37
+ var _g = (0, react_1.useState)(function () { return initialState; }), state = _g[0], setState = _g[1];
56
38
  (0, use_iso_morphic_effect_1.useIsoMorphicEffect)(function () {
57
39
  var scrollContainer = ref.current;
58
40
  if (!scrollContainer)
59
41
  return;
42
+ // Create the scroller instance only once inside the effect
43
+ if (!scrollerRef.current) {
44
+ var container = (0, utils_1.getElement)(ref);
45
+ scrollerRef.current = (0, scroller_1.createScroller)(container, scrollOpt);
46
+ }
60
47
  var scrollWidth = scrollContainer.scrollWidth, clientWidth = scrollContainer.clientWidth, clientHeight = scrollContainer.clientHeight, scrollHeight = scrollContainer.scrollHeight;
61
48
  var isScrollable = clientWidth !== scrollWidth || clientHeight !== scrollHeight;
62
49
  setState(function (prev) { return (__assign(__assign({}, prev), { isScrollable: isScrollable })); });
63
50
  var handleScroll = function () {
64
51
  if (scrollContainer) {
65
- var scrollLeft_1 = scrollContainer.scrollLeft, scrollTop_1 = scrollContainer.scrollTop;
66
- var scrollState = __assign(__assign({}, state), { isScrolledLeft: scrollLeft_1 === 0, isScrolledRight: scrollLeft_1 >= scrollWidth - clientWidth, isScrolledTop: scrollTop_1 === 0, isScrolledBottom: scrollTop_1 >= scrollHeight - clientHeight, left: scrollLeft_1, top: scrollTop_1 });
52
+ var scrollLeft = scrollContainer.scrollLeft, scrollTop = scrollContainer.scrollTop;
53
+ var isAtLeftBoundary = scrollLeft === 0;
54
+ var isAtRightBoundary = scrollLeft >= scrollWidth - clientWidth;
55
+ var isAtTopBoundary = scrollTop === 0;
56
+ var isAtBottomBoundary = scrollTop >= scrollHeight - clientHeight;
57
+ var scrollState = __assign(__assign({}, state), { isScrolledLeft: isAtLeftBoundary, isScrolledRight: isAtRightBoundary, isScrolledTop: isAtTopBoundary, isScrolledBottom: isAtBottomBoundary, left: scrollLeft, top: scrollTop, maxScrollLeft: scrollWidth - clientWidth, maxScrollTop: scrollHeight - clientHeight, scrollPercentageX: isAtLeftBoundary
58
+ ? 0
59
+ : (scrollLeft / (scrollWidth - clientWidth)) * 100, scrollPercentageY: isAtTopBoundary
60
+ ? 0
61
+ : (scrollTop / (scrollHeight - clientHeight)) * 100 });
67
62
  setState(scrollState);
68
63
  }
69
64
  };
70
65
  handleScroll();
71
66
  scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer.addEventListener('scroll', handleScroll);
72
67
  return function () { return scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer.removeEventListener('scroll', handleScroll); };
73
- }, []);
68
+ }, [state]);
74
69
  return {
75
70
  ref: ref,
76
71
  state: state,
77
- scrollLeft: scrollLeft,
78
- scrollRight: scrollRight,
79
- scrollBottom: scrollBottom,
80
- scrollTop: scrollTop,
81
- scrollCenter: scrollCenter,
72
+ scrollLeft: (_a = scrollerRef.current) === null || _a === void 0 ? void 0 : _a.scrollToLeft,
73
+ scrollRight: (_b = scrollerRef.current) === null || _b === void 0 ? void 0 : _b.scrollToRight,
74
+ scrollBottom: (_c = scrollerRef.current) === null || _c === void 0 ? void 0 : _c.scrollToBottom,
75
+ scrollTop: (_d = scrollerRef.current) === null || _d === void 0 ? void 0 : _d.scrollToTop,
76
+ scrollCenter: (_e = scrollerRef.current) === null || _e === void 0 ? void 0 : _e.scrollToCenter,
77
+ scrollTo: (_f = scrollerRef.current) === null || _f === void 0 ? void 0 : _f.scrollTo,
82
78
  };
83
79
  }
84
80
  exports.useScroll = useScroll;
@@ -9,4 +9,5 @@ export declare function useWindowScroll(props?: Partial<Omit<ScrollOptions, 'dir
9
9
  scrollTop: (offset?: number | undefined) => void;
10
10
  scrollBottom: (offset?: number | undefined) => void;
11
11
  scrollToTarget: (currentTarget: HTMLElement) => void;
12
+ scrollTo: (position: number) => void;
12
13
  };
@@ -23,34 +23,40 @@ function useWindowScroll(props) {
23
23
  isScrolledTop: false,
24
24
  isScrolledBottom: false,
25
25
  }); }), state = _a[0], setState = _a[1];
26
- var scrollArgs = {
27
- container: window,
28
- options: __assign(__assign({}, utils_1.defaultScrollOptions), props),
29
- };
26
+ var container = window;
27
+ var scrollOptions = __assign(__assign({}, utils_1.defaultScrollOptions), props);
28
+ var scrollContainer = (0, scroller_1.createScroller)(container, scrollOptions);
29
+ var handleScroll = (0, react_1.useCallback)(function () {
30
+ var _a = (0, utils_1.resolveScrollValues)(container), scrollLeft = _a.scrollLeft, scrollTop = _a.scrollTop, clientHeight = _a.clientHeight, scrollHeight = _a.scrollHeight;
31
+ var isAtTopBoundary = scrollTop === 0;
32
+ var isAtBottomBoundary = scrollTop >= scrollHeight - clientHeight;
33
+ var scrollState = {
34
+ left: scrollLeft,
35
+ top: scrollTop,
36
+ isScrolledTop: isAtTopBoundary,
37
+ isScrolledBottom: isAtBottomBoundary,
38
+ };
39
+ setState(scrollState);
40
+ }, [container]);
41
+ (0, use_iso_morphic_effect_1.useIsoMorphicEffect)(function () {
42
+ handleScroll();
43
+ container.addEventListener('scroll', handleScroll);
44
+ return function () {
45
+ container.removeEventListener('scroll', handleScroll);
46
+ };
47
+ }, [handleScroll]);
48
+ var scrollTo = (0, react_1.useCallback)(function (position) {
49
+ scrollContainer.scrollTo(position);
50
+ }, [scrollContainer]);
30
51
  var scrollToTarget = (0, react_1.useCallback)(function (currentTarget) {
31
- var scrollContainer = (0, scroller_1.scroller)(scrollArgs);
32
52
  scrollContainer.scrollToTarget(currentTarget);
33
- }, []);
53
+ }, [scrollContainer]);
34
54
  var scrollTop = (0, react_1.useCallback)(function (offset) {
35
- var scrollContainer = (0, scroller_1.scroller)(scrollArgs);
36
55
  scrollContainer.scrollToTop(offset);
37
- }, []);
56
+ }, [scrollContainer]);
38
57
  var scrollBottom = (0, react_1.useCallback)(function (offset) {
39
- var scrollContainer = (0, scroller_1.scroller)(scrollArgs);
40
58
  scrollContainer.scrollToBottom(offset);
41
- }, []);
42
- (0, use_iso_morphic_effect_1.useIsoMorphicEffect)(function () {
43
- var handleScroll = function () {
44
- if (window) {
45
- var _a = (0, utils_1.resolveScrollValues)(window), scrollLeft = _a.scrollLeft, scrollTop_1 = _a.scrollTop, scrollHeight = _a.scrollHeight, clientHeight = _a.clientHeight;
46
- var scrollState = __assign(__assign({}, state), { isScrolledTop: scrollTop_1 === 0, isScrolledBottom: scrollTop_1 >= scrollHeight - clientHeight, left: scrollLeft, top: scrollTop_1 });
47
- setState(scrollState);
48
- }
49
- };
50
- handleScroll();
51
- window === null || window === void 0 ? void 0 : window.addEventListener('scroll', handleScroll);
52
- return function () { return window === null || window === void 0 ? void 0 : window.removeEventListener('scroll', handleScroll); };
53
- }, []);
54
- return { state: state, scrollTop: scrollTop, scrollBottom: scrollBottom, scrollToTarget: scrollToTarget };
59
+ }, [scrollContainer]);
60
+ return { state: state, scrollTop: scrollTop, scrollBottom: scrollBottom, scrollToTarget: scrollToTarget, scrollTo: scrollTo };
55
61
  }
56
62
  exports.useWindowScroll = useWindowScroll;
@@ -1,9 +1,10 @@
1
- import { ScrollerProps } from './types';
2
- export declare function scroller({ container, options }: ScrollerProps): {
1
+ import { ScrollOptions } from './types';
2
+ export declare function createScroller(container: HTMLElement | Window, options?: ScrollOptions): {
3
+ scrollTo: (to: number) => void;
4
+ scrollToCenter: () => void;
5
+ scrollToTarget: (target: HTMLElement, offset?: number) => void;
3
6
  scrollToLeft: (offset?: number) => void;
4
7
  scrollToRight: (offset?: number) => void;
5
8
  scrollToTop: (offset?: number) => void;
6
9
  scrollToBottom: (offset?: number) => void;
7
- scrollToTarget: (target: HTMLElement) => void;
8
- scrollToCenter: () => void;
9
10
  };
@@ -1,41 +1,46 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.scroller = void 0;
3
+ exports.createScroller = void 0;
4
4
  var utils_1 = require("./utils");
5
- function scroller(_a) {
6
- var container = _a.container, _b = _a.options, options = _b === void 0 ? utils_1.defaultScrollOptions : _b;
5
+ function createScroller(container, options) {
6
+ if (options === void 0) { options = utils_1.defaultScrollOptions; }
7
7
  (0, utils_1.validateElement)(container);
8
8
  var duration = options.duration, easingOption = options.easingOption, direction = options.direction;
9
- var _c = (0, utils_1.resolveScrollValues)(container), scrollLeft = _c.scrollLeft, scrollTop = _c.scrollTop, clientWidth = _c.clientWidth, clientHeight = _c.clientHeight, scrollWidth = _c.scrollWidth, scrollHeight = _c.scrollHeight;
10
- /**
11
- * Scroll the Element or Window to the given
12
- * position.
13
- *
14
- * @param {number} from the current scroll position.
15
- * @param {number} to the new position to scroll.
16
- * @param {string} layout the scroll direction.
17
- */
18
- function scrollTo(from, to, layout) {
9
+ var _a = (0, utils_1.resolveScrollValues)(container), scrollLeft = _a.scrollLeft, scrollTop = _a.scrollTop, clientWidth = _a.clientWidth, clientHeight = _a.clientHeight, scrollWidth = _a.scrollWidth, scrollHeight = _a.scrollHeight;
10
+ function scroll(from, to, layout) {
11
+ if (layout === void 0) { layout = direction; }
19
12
  var startTime = null;
20
13
  var requestId = 0;
21
- function loop(currentTime) {
22
- if (!startTime)
23
- startTime = currentTime;
24
- var time = Math.min((currentTime - startTime) / duration, 1);
14
+ var distance = Math.abs(to - from);
15
+ function calculateCurrentPosition(timeFraction) {
25
16
  var easingFunc = (0, utils_1.getEasing)(easingOption);
26
- var currentPositionInTime = easingFunc(time) * (to - from) + from;
17
+ return easingFunc(timeFraction) * distance + from;
18
+ }
19
+ function updateScrollPosition(currentPosition) {
27
20
  container.scrollTo({
28
- left: layout === 'horizontal' ? currentPositionInTime : undefined,
29
- top: layout === 'vertical' ? currentPositionInTime : undefined,
21
+ left: layout === 'horizontal' ? currentPosition : undefined,
22
+ top: layout === 'vertical' ? currentPosition : undefined,
30
23
  });
31
- if (time < 1) {
32
- requestId = requestAnimationFrame(loop);
24
+ }
25
+ function animateScroll(currentTime) {
26
+ if (!startTime)
27
+ startTime = currentTime;
28
+ var elapsedTime = currentTime - startTime;
29
+ var timeFraction = elapsedTime / duration;
30
+ var currentPosition = calculateCurrentPosition(timeFraction);
31
+ updateScrollPosition(currentPosition);
32
+ if (timeFraction < 1) {
33
+ requestId = requestAnimationFrame(animateScroll);
33
34
  }
34
35
  else {
35
36
  cancelAnimationFrame(requestId);
36
37
  }
37
38
  }
38
- requestId = requestAnimationFrame(loop);
39
+ requestId = requestAnimationFrame(animateScroll);
40
+ }
41
+ function scrollTo(to) {
42
+ var from = direction === 'vertical' ? scrollTop : scrollLeft;
43
+ scroll(from, to);
39
44
  }
40
45
  function scrollToCenter() {
41
46
  var from = 0;
@@ -50,46 +55,40 @@ function scroller(_a) {
50
55
  from = scrollLeft;
51
56
  to = scrollXCenterPosition;
52
57
  }
53
- scrollTo(from, to, direction);
58
+ scroll(from, to, direction);
54
59
  }
55
- function scrollToTarget(target) {
60
+ function scrollToTarget(target, offset) {
61
+ if (offset === void 0) { offset = 0; }
56
62
  (0, utils_1.validateElement)(target);
57
63
  var offsetTop = target.offsetTop, offsetLeft = target.offsetLeft;
58
64
  var from = 0;
59
65
  var to = 0;
60
66
  if (direction === 'vertical') {
61
67
  from = scrollTop;
62
- to = offsetTop;
68
+ to = offsetTop - offset;
63
69
  }
64
70
  if (direction === 'horizontal') {
65
71
  from = scrollLeft;
66
- to = offsetLeft;
72
+ to = offsetLeft - offset;
67
73
  }
68
- scrollTo(from, to, direction);
74
+ scroll(from, to, direction);
69
75
  }
70
- var scrollToLeft = function (offset) {
71
- var scrollPosition = offset || scrollLeft - clientWidth;
72
- scrollTo(scrollLeft, scrollPosition, 'horizontal');
73
- };
74
- var scrollToRight = function (offset) {
75
- var scrollPosition = offset || scrollLeft + clientWidth;
76
- scrollTo(scrollLeft, scrollPosition, 'horizontal');
77
- };
78
- var scrollToTop = function (offset) {
79
- var scrollPosition = offset || scrollTop - clientHeight;
80
- scrollTo(scrollTop, scrollPosition, 'vertical');
81
- };
82
- var scrollToBottom = function (offset) {
83
- var scrollPosition = offset || scrollTop + clientHeight;
84
- scrollTo(scrollTop, scrollPosition, 'vertical');
85
- };
86
76
  return {
87
- scrollToLeft: scrollToLeft,
88
- scrollToRight: scrollToRight,
89
- scrollToTop: scrollToTop,
90
- scrollToBottom: scrollToBottom,
91
- scrollToTarget: scrollToTarget,
77
+ scrollTo: scrollTo,
92
78
  scrollToCenter: scrollToCenter,
79
+ scrollToTarget: scrollToTarget,
80
+ scrollToLeft: function (offset) {
81
+ scroll(scrollLeft, offset || scrollLeft - clientWidth, 'horizontal');
82
+ },
83
+ scrollToRight: function (offset) {
84
+ scroll(scrollLeft, offset || scrollLeft + clientWidth, 'horizontal');
85
+ },
86
+ scrollToTop: function (offset) {
87
+ scroll(scrollTop, offset || scrollTop - clientHeight, 'vertical');
88
+ },
89
+ scrollToBottom: function (offset) {
90
+ scroll(scrollTop, offset || scrollTop + clientHeight, 'vertical');
91
+ },
93
92
  };
94
93
  }
95
- exports.scroller = scroller;
94
+ exports.createScroller = createScroller;
@@ -3,8 +3,9 @@ import { EasingFunction, EasingFunctions, EasingOptions, ScrollOptions } from '.
3
3
  export declare const isServer: boolean;
4
4
  export declare const defaultScrollOptions: ScrollOptions;
5
5
  /**
6
- * Resolves scroll values based on the type of param.
7
- * @param {HTMLElement | Window} element
6
+ * Resolves scroll values based on the type of element.
7
+ * @param {HTMLElement | Window} element - The element to retrieve scroll values from.
8
+ * @returns {object} - An object containing scroll-related properties.
8
9
  */
9
10
  export declare const resolveScrollValues: (element: HTMLElement | Window) => {
10
11
  scrollLeft: number;
@@ -13,12 +14,24 @@ export declare const resolveScrollValues: (element: HTMLElement | Window) => {
13
14
  clientWidth: number;
14
15
  clientHeight: number;
15
16
  scrollHeight: number;
17
+ scrollLeftMax: number;
18
+ scrollTopMax: number;
16
19
  };
17
20
  /**
18
- * Validate if element passed to the function is valid.
19
- * @param {HTMLElement | Window} element
21
+ * Validate if the element passed to the function is valid.
22
+ * @param {HTMLElement | Window} element - The element to validate.
20
23
  */
21
24
  export declare function validateElement(element?: HTMLElement | Window): void;
25
+ /**
26
+ * Get the DOM element from a React ref object.
27
+ * @param {RefObject<HTMLElement>} ref - The React ref object.
28
+ * @returns {HTMLElement} - The DOM element.
29
+ */
22
30
  export declare const getElement: (ref: RefObject<HTMLElement>) => HTMLElement;
23
31
  export declare const easingMap: EasingFunctions;
32
+ /**
33
+ * Get an easing function based on the provided easing option or the default option.
34
+ * @param {EasingOptions} easing - The easing option.
35
+ * @returns {EasingFunction} - The easing function.
36
+ */
24
37
  export declare const getEasing: (easing?: EasingOptions) => EasingFunction;