scroll-direction-manager 1.0.5 → 2.1.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/LICENSE +21 -21
- package/README.md +132 -52
- package/dist/cjs/ScrollDirectionManager.d.ts +4 -14
- package/dist/cjs/ScrollDirectionManager.js +18 -17
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/react.d.ts +3 -0
- package/dist/cjs/react.js +6 -0
- package/dist/cjs/useScrollDirection.d.ts +17 -0
- package/dist/cjs/useScrollDirection.js +44 -0
- package/package.json +73 -41
- package/dist/ScrollDirectionManager.d.ts +0 -27
- package/dist/ScrollDirectionManager.js +0 -56
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -5
- package/jest.config.js +0 -8
- package/src/ScrollDirectionManager.ts +0 -64
- package/src/__tests__/ScrollDirectionManager.test.ts +0 -49
- package/src/index.ts +0 -2
- package/tsconfig.json +0 -13
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 masaroli
|
|
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.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 masaroli
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,52 +1,132 @@
|
|
|
1
|
-
# ScrollDirectionManager
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
## Key Features
|
|
8
|
-
|
|
9
|
-
- **
|
|
10
|
-
- **
|
|
11
|
-
- **
|
|
12
|
-
- **
|
|
13
|
-
|
|
14
|
-
## Installation
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
```
|
|
1
|
+
# ScrollDirectionManager
|
|
2
|
+
|
|
3
|
+
`scroll-direction-manager` is a lightweight, zero-dependency utility package that simplifies tracking the scroll direction (`"up"`, `"down"`, or `"initial"`) of a web page. It ships with both a framework-agnostic `ScrollDirectionManager` class and a dedicated React hook entry point (`scroll-direction-manager/react`).
|
|
4
|
+
|
|
5
|
+
It is particularly useful for responsive navigation bars and sticky headers that hide on scroll down, reappear with a scrolled state on scroll up, and reset when near the top of the page.
|
|
6
|
+
|
|
7
|
+
## Key Features
|
|
8
|
+
|
|
9
|
+
- **React Hook (`useScrollDirection`)**: Exposed via `scroll-direction-manager/react` so vanilla projects stay 100% React-free. Returns `{ direction, isUp, isDown, isInitial }` and automatically manages listener lifecycle.
|
|
10
|
+
- **Scroll Direction Detection**: Accurately determines the current scroll direction (`"up"`, `"down"`, or `"initial"`) based on `window.scrollY` and an optional `initialOffset` threshold.
|
|
11
|
+
- **Callback Support**: Pass an optional `onDirectionChange` callback that fires only when the scroll direction state transitions.
|
|
12
|
+
- **Zero External Runtime Dependencies**: Uses native browser event listeners with SSR safety (`typeof window !== "undefined"`).
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npm install scroll-direction-manager
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## React Usage
|
|
21
|
+
|
|
22
|
+
Import the `useScrollDirection` hook from `scroll-direction-manager/react` to declaratively track scroll direction in your React components:
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { useScrollDirection } from "scroll-direction-manager/react";
|
|
26
|
+
|
|
27
|
+
export function Navbar() {
|
|
28
|
+
const { direction, isUp, isDown, isInitial } = useScrollDirection({
|
|
29
|
+
initialOffset: 120,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<header
|
|
34
|
+
className={[
|
|
35
|
+
"navbar",
|
|
36
|
+
isDown ? "navbar--hidden" : "",
|
|
37
|
+
isUp ? "navbar--scrolled" : "",
|
|
38
|
+
isInitial ? "navbar--top" : "",
|
|
39
|
+
]
|
|
40
|
+
.filter(Boolean)
|
|
41
|
+
.join(" ")}
|
|
42
|
+
data-scroll-direction={direction}
|
|
43
|
+
>
|
|
44
|
+
<nav>My Navigation</nav>
|
|
45
|
+
</header>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Vanilla TypeScript / JavaScript Usage
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import {
|
|
54
|
+
ScrollDirectionManager,
|
|
55
|
+
type ScrollDirection,
|
|
56
|
+
} from "scroll-direction-manager";
|
|
57
|
+
|
|
58
|
+
const element = document.querySelector('[data-element="some-element"]');
|
|
59
|
+
|
|
60
|
+
const navOnScroll = (direction: ScrollDirection) => {
|
|
61
|
+
if (!element) return;
|
|
62
|
+
|
|
63
|
+
if (direction === "initial" || direction === "up") {
|
|
64
|
+
element.classList.remove("hide");
|
|
65
|
+
|
|
66
|
+
if (direction === "up") {
|
|
67
|
+
element.classList.add("scrolled");
|
|
68
|
+
} else {
|
|
69
|
+
element.classList.remove("scrolled");
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
element.classList.add("hide");
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const scrollDirectionInstance = new ScrollDirectionManager({
|
|
77
|
+
initialOffset: 145,
|
|
78
|
+
onDirectionChange: navOnScroll,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Clean up when no longer needed:
|
|
82
|
+
// scrollDirectionInstance.dispose();
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## API Reference
|
|
86
|
+
|
|
87
|
+
### `useScrollDirection(options?: UseScrollDirectionOptions): UseScrollDirectionResult`
|
|
88
|
+
|
|
89
|
+
Imported from `"scroll-direction-manager/react"`.
|
|
90
|
+
|
|
91
|
+
#### Options (`UseScrollDirectionOptions` / `ScrollDirectionManagerOptions`)
|
|
92
|
+
|
|
93
|
+
| Option | Type | Default | Description |
|
|
94
|
+
| --- | --- | --- | --- |
|
|
95
|
+
| `initialOffset` | `number` | `120` | Scroll threshold in pixels (`window.scrollY`). At or below this offset, direction is always `"initial"`. |
|
|
96
|
+
| `onDirectionChange` | `(direction: ScrollDirection) => void` | `undefined` | Optional callback invoked whenever the scroll direction changes. |
|
|
97
|
+
|
|
98
|
+
#### Return Value (`UseScrollDirectionResult`)
|
|
99
|
+
|
|
100
|
+
| Property | Type | Description |
|
|
101
|
+
| --- | --- | --- |
|
|
102
|
+
| `direction` | `"initial" \| "up" \| "down"` | Current scroll direction state. |
|
|
103
|
+
| `isInitial` | `boolean` | `true` when `direction === "initial"` (at or above `initialOffset`). |
|
|
104
|
+
| `isUp` | `boolean` | `true` when `direction === "up"` (scrolling up below `initialOffset`). |
|
|
105
|
+
| `isDown` | `boolean` | `true` when `direction === "down"` (scrolling down below `initialOffset`). |
|
|
106
|
+
|
|
107
|
+
### `ScrollDirectionManager`
|
|
108
|
+
|
|
109
|
+
Imported from `"scroll-direction-manager"`.
|
|
110
|
+
|
|
111
|
+
- `new ScrollDirectionManager(options?: ScrollDirectionManagerOptions)`: Starts listening to `window` scroll events.
|
|
112
|
+
- `getDirection(): ScrollDirection`: Returns the current scroll direction (`"initial" | "up" | "down"`).
|
|
113
|
+
- `dispose(): void`: Removes the `window` scroll event listener.
|
|
114
|
+
|
|
115
|
+
## Notes for AI Agents / LLMs
|
|
116
|
+
|
|
117
|
+
When integrating or modifying `scroll-direction-manager`, keep these rules in mind:
|
|
118
|
+
|
|
119
|
+
1. **Entry Points & Named Exports**:
|
|
120
|
+
- React: `import { useScrollDirection } from "scroll-direction-manager/react";`
|
|
121
|
+
- Vanilla JS/TS: `import { ScrollDirectionManager, type ScrollDirection } from "scroll-direction-manager";`
|
|
122
|
+
- Keep React imports isolated to the `/react` subpath so non-React consumers do not need `react` installed.
|
|
123
|
+
2. **State Semantics (`"initial"` vs `"up"` vs `"down"`)**:
|
|
124
|
+
- While `window.scrollY <= initialOffset` (default `120`px), the direction is strictly `"initial"`, even if the user scrolls slightly down or up within that top region.
|
|
125
|
+
- `"down"` and `"up"` are only emitted when `window.scrollY > initialOffset`.
|
|
126
|
+
- For sticky headers/navbars: show the header in its default/transparent style when `isInitial` (`direction === "initial"`), hide it when `isDown` (`direction === "down"`), and show it with an elevated/compact backdrop when `isUp` (`direction === "up"`).
|
|
127
|
+
3. **SSR & Next.js Compatibility**:
|
|
128
|
+
- `ScrollDirectionManager` guards all `window` access with `typeof window !== "undefined"`, making it safe to import on the server.
|
|
129
|
+
- `scroll-direction-manager/react` includes the `"use client";` directive; in Next.js App Router, add `"use client";` at the top of any component file that calls `useScrollDirection`.
|
|
130
|
+
4. **Cleanup & Re-renders**:
|
|
131
|
+
- In React, `useScrollDirection` automatically calls `manager.dispose()` on unmount and holds `onDirectionChange` in a ref so inline callbacks do not reset scroll tracking across renders.
|
|
132
|
+
- In Vanilla JS or non-React frameworks, always call `instance.dispose()` during component teardown to prevent memory leaks.
|
|
@@ -1,27 +1,17 @@
|
|
|
1
1
|
export type ScrollDirection = "initial" | "up" | "down";
|
|
2
|
-
type ScrollDirectionManagerOptions = {
|
|
2
|
+
export type ScrollDirectionManagerOptions = {
|
|
3
3
|
initialOffset?: number;
|
|
4
4
|
onDirectionChange?: (direction: ScrollDirection) => void;
|
|
5
5
|
};
|
|
6
|
-
/**
|
|
7
|
-
* @example
|
|
8
|
-
* const scrollDirection = new ScrollDirection({
|
|
9
|
-
* onDirectionChange: (direction) => {
|
|
10
|
-
* if (direction === "up") // do something if its up
|
|
11
|
-
* else if (direction === "initial") // do if its initial
|
|
12
|
-
* },
|
|
13
|
-
* treshold: 30
|
|
14
|
-
* })
|
|
15
|
-
*/
|
|
16
6
|
export declare class ScrollDirectionManager {
|
|
17
7
|
private readonly onDirectionChange;
|
|
18
8
|
private scrollPosition;
|
|
19
9
|
private scrollDirection;
|
|
20
10
|
private readonly initialOffset;
|
|
21
|
-
private
|
|
22
|
-
constructor({ initialOffset, onDirectionChange }
|
|
11
|
+
private scrollListener;
|
|
12
|
+
constructor({ initialOffset, onDirectionChange, }?: ScrollDirectionManagerOptions);
|
|
23
13
|
private readonly setupSubscriptions;
|
|
24
14
|
private readonly handleScrollDirection;
|
|
15
|
+
getDirection(): ScrollDirection;
|
|
25
16
|
dispose(): void;
|
|
26
17
|
}
|
|
27
|
-
export {};
|
|
@@ -1,31 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ScrollDirectionManager = void 0;
|
|
4
|
-
var seng_disposable_event_listener_1 = require("seng-disposable-event-listener");
|
|
5
|
-
var seng_disposable_manager_1 = require("seng-disposable-manager");
|
|
6
|
-
/**
|
|
7
|
-
* @example
|
|
8
|
-
* const scrollDirection = new ScrollDirection({
|
|
9
|
-
* onDirectionChange: (direction) => {
|
|
10
|
-
* if (direction === "up") // do something if its up
|
|
11
|
-
* else if (direction === "initial") // do if its initial
|
|
12
|
-
* },
|
|
13
|
-
* treshold: 30
|
|
14
|
-
* })
|
|
15
|
-
*/
|
|
16
4
|
var ScrollDirectionManager = /** @class */ (function () {
|
|
17
5
|
function ScrollDirectionManager(_a) {
|
|
18
|
-
var initialOffset =
|
|
6
|
+
var _b = _a === void 0 ? {} : _a, initialOffset = _b.initialOffset, onDirectionChange = _b.onDirectionChange;
|
|
19
7
|
var _this = this;
|
|
20
8
|
this.onDirectionChange = undefined;
|
|
21
9
|
this.scrollDirection = "initial";
|
|
22
10
|
this.initialOffset = 120;
|
|
23
|
-
this.
|
|
11
|
+
this.scrollListener = null;
|
|
24
12
|
this.setupSubscriptions = function () {
|
|
25
|
-
|
|
13
|
+
if (typeof window === "undefined")
|
|
14
|
+
return;
|
|
15
|
+
// Setting up the scroll event listener
|
|
16
|
+
_this.scrollListener = _this.handleScrollDirection.bind(_this);
|
|
17
|
+
window.addEventListener("scroll", _this.scrollListener);
|
|
26
18
|
};
|
|
27
19
|
this.handleScrollDirection = function () {
|
|
28
20
|
var _a;
|
|
21
|
+
if (typeof window === "undefined")
|
|
22
|
+
return;
|
|
29
23
|
var position = window.scrollY;
|
|
30
24
|
var direction = "initial";
|
|
31
25
|
if (position > _this.initialOffset) {
|
|
@@ -43,13 +37,20 @@ var ScrollDirectionManager = /** @class */ (function () {
|
|
|
43
37
|
_this.scrollDirection = direction;
|
|
44
38
|
};
|
|
45
39
|
this.scrollPosition = 0;
|
|
46
|
-
if (initialOffset)
|
|
40
|
+
if (initialOffset !== undefined)
|
|
47
41
|
this.initialOffset = initialOffset;
|
|
48
42
|
this.onDirectionChange = onDirectionChange;
|
|
49
43
|
this.setupSubscriptions();
|
|
50
44
|
}
|
|
45
|
+
ScrollDirectionManager.prototype.getDirection = function () {
|
|
46
|
+
return this.scrollDirection;
|
|
47
|
+
};
|
|
51
48
|
ScrollDirectionManager.prototype.dispose = function () {
|
|
52
|
-
|
|
49
|
+
// Remove the event listener when disposing
|
|
50
|
+
if (typeof window !== "undefined" && this.scrollListener) {
|
|
51
|
+
window.removeEventListener("scroll", this.scrollListener);
|
|
52
|
+
this.scrollListener = null;
|
|
53
|
+
}
|
|
53
54
|
};
|
|
54
55
|
return ScrollDirectionManager;
|
|
55
56
|
}());
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { ScrollDirectionManager } from "./ScrollDirectionManager";
|
|
2
|
-
export type { ScrollDirection } from "./ScrollDirectionManager";
|
|
2
|
+
export type { ScrollDirection, ScrollDirectionManagerOptions, } from "./ScrollDirectionManager";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useScrollDirection = void 0;
|
|
5
|
+
var useScrollDirection_1 = require("./useScrollDirection");
|
|
6
|
+
Object.defineProperty(exports, "useScrollDirection", { enumerable: true, get: function () { return useScrollDirection_1.useScrollDirection; } });
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ScrollDirection, ScrollDirectionManagerOptions } from "./ScrollDirectionManager";
|
|
2
|
+
export type UseScrollDirectionOptions = ScrollDirectionManagerOptions;
|
|
3
|
+
export type UseScrollDirectionResult = {
|
|
4
|
+
direction: ScrollDirection;
|
|
5
|
+
isUp: boolean;
|
|
6
|
+
isDown: boolean;
|
|
7
|
+
isInitial: boolean;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* React hook for tracking window scroll direction.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const { direction, isUp, isDown, isInitial } = useScrollDirection({
|
|
14
|
+
* initialOffset: 120,
|
|
15
|
+
* });
|
|
16
|
+
*/
|
|
17
|
+
export declare function useScrollDirection(options?: UseScrollDirectionOptions): UseScrollDirectionResult;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useScrollDirection = void 0;
|
|
5
|
+
var react_1 = require("react");
|
|
6
|
+
var ScrollDirectionManager_1 = require("./ScrollDirectionManager");
|
|
7
|
+
/**
|
|
8
|
+
* React hook for tracking window scroll direction.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const { direction, isUp, isDown, isInitial } = useScrollDirection({
|
|
12
|
+
* initialOffset: 120,
|
|
13
|
+
* });
|
|
14
|
+
*/
|
|
15
|
+
function useScrollDirection(options) {
|
|
16
|
+
if (options === void 0) { options = {}; }
|
|
17
|
+
var initialOffset = options.initialOffset, onDirectionChange = options.onDirectionChange;
|
|
18
|
+
var _a = (0, react_1.useState)("initial"), direction = _a[0], setDirection = _a[1];
|
|
19
|
+
var onDirectionChangeRef = (0, react_1.useRef)(onDirectionChange);
|
|
20
|
+
(0, react_1.useEffect)(function () {
|
|
21
|
+
onDirectionChangeRef.current = onDirectionChange;
|
|
22
|
+
}, [onDirectionChange]);
|
|
23
|
+
(0, react_1.useEffect)(function () {
|
|
24
|
+
var manager = new ScrollDirectionManager_1.ScrollDirectionManager({
|
|
25
|
+
initialOffset: initialOffset,
|
|
26
|
+
onDirectionChange: function (newDirection) {
|
|
27
|
+
var _a;
|
|
28
|
+
setDirection(newDirection);
|
|
29
|
+
(_a = onDirectionChangeRef.current) === null || _a === void 0 ? void 0 : _a.call(onDirectionChangeRef, newDirection);
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
setDirection(manager.getDirection());
|
|
33
|
+
return function () {
|
|
34
|
+
manager.dispose();
|
|
35
|
+
};
|
|
36
|
+
}, [initialOffset]);
|
|
37
|
+
return {
|
|
38
|
+
direction: direction,
|
|
39
|
+
isUp: direction === "up",
|
|
40
|
+
isDown: direction === "down",
|
|
41
|
+
isInitial: direction === "initial",
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
exports.useScrollDirection = useScrollDirection;
|
package/package.json
CHANGED
|
@@ -1,41 +1,73 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "scroll-direction-manager",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "A utility package that simplifies tracking the scroll direction (up, down, or initial) of a web page",
|
|
5
|
-
"main": "dist/cjs/index.js",
|
|
6
|
-
"types": "dist/cjs/index.d.ts",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "scroll-direction-manager",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "A utility package that simplifies tracking the scroll direction (up, down, or initial) of a web page, with React hook support",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"types": "dist/cjs/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/cjs/index.d.ts",
|
|
10
|
+
"default": "./dist/cjs/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./react": {
|
|
13
|
+
"types": "./dist/cjs/react.d.ts",
|
|
14
|
+
"default": "./dist/cjs/react.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"typesVersions": {
|
|
18
|
+
"*": {
|
|
19
|
+
"react": [
|
|
20
|
+
"dist/cjs/react.d.ts"
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"clean": "rm -rf dist",
|
|
29
|
+
"build:cjs": "tsc --project tsconfig.json",
|
|
30
|
+
"build": "npm run build:cjs",
|
|
31
|
+
"test": "jest",
|
|
32
|
+
"test:watch": "jest --watch",
|
|
33
|
+
"test:coverage": "jest --coverage"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/masaroli/scroll-direction-manager.git"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"scroll",
|
|
41
|
+
"scroll-direction",
|
|
42
|
+
"react",
|
|
43
|
+
"react-hooks",
|
|
44
|
+
"use-scroll-direction"
|
|
45
|
+
],
|
|
46
|
+
"author": "Martin Sazzaroli",
|
|
47
|
+
"license": "MIT",
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/masaroli/scroll-direction-manager/issues"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/masaroli/scroll-direction-manager#readme",
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"react": ">=16.8.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependenciesMeta": {
|
|
56
|
+
"react": {
|
|
57
|
+
"optional": true
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@testing-library/react": "^16.3.2",
|
|
62
|
+
"@types/jest": "^29.5.12",
|
|
63
|
+
"@types/react": "^19.0.0",
|
|
64
|
+
"jest": "^29.7.0",
|
|
65
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
66
|
+
"jsdom": "^24.0.0",
|
|
67
|
+
"react": "^19.0.0",
|
|
68
|
+
"react-dom": "^19.0.0",
|
|
69
|
+
"ts-jest": "^29.1.3",
|
|
70
|
+
"typescript": "^5.4.5"
|
|
71
|
+
},
|
|
72
|
+
"dependencies": {}
|
|
73
|
+
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export type ScrollDirection = "initial" | "up" | "down";
|
|
2
|
-
type ScrollDirectionManagerOptions = {
|
|
3
|
-
initialOffset?: number;
|
|
4
|
-
onDirectionChange?: (direction: ScrollDirection) => void;
|
|
5
|
-
};
|
|
6
|
-
/**
|
|
7
|
-
* @example
|
|
8
|
-
* const scrollDirection = new ScrollDirection({
|
|
9
|
-
* onDirectionChange: (direction) => {
|
|
10
|
-
* if (direction === "up") // do something if its up
|
|
11
|
-
* else if (direction === "initial") // do if its initial
|
|
12
|
-
* },
|
|
13
|
-
* treshold: 30
|
|
14
|
-
* })
|
|
15
|
-
*/
|
|
16
|
-
export declare class ScrollDirectionManager {
|
|
17
|
-
private readonly onDirectionChange;
|
|
18
|
-
private scrollPosition;
|
|
19
|
-
private scrollDirection;
|
|
20
|
-
private readonly initialOffset;
|
|
21
|
-
private readonly disposables;
|
|
22
|
-
constructor({ initialOffset, onDirectionChange }: ScrollDirectionManagerOptions);
|
|
23
|
-
private readonly setupSubscriptions;
|
|
24
|
-
private readonly handleScrollDirection;
|
|
25
|
-
dispose(): void;
|
|
26
|
-
}
|
|
27
|
-
export {};
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ScrollDirectionManager = void 0;
|
|
4
|
-
var seng_disposable_event_listener_1 = require("seng-disposable-event-listener");
|
|
5
|
-
var seng_disposable_manager_1 = require("seng-disposable-manager");
|
|
6
|
-
/**
|
|
7
|
-
* @example
|
|
8
|
-
* const scrollDirection = new ScrollDirection({
|
|
9
|
-
* onDirectionChange: (direction) => {
|
|
10
|
-
* if (direction === "up") // do something if its up
|
|
11
|
-
* else if (direction === "initial") // do if its initial
|
|
12
|
-
* },
|
|
13
|
-
* treshold: 30
|
|
14
|
-
* })
|
|
15
|
-
*/
|
|
16
|
-
var ScrollDirectionManager = /** @class */ (function () {
|
|
17
|
-
function ScrollDirectionManager(_a) {
|
|
18
|
-
var initialOffset = _a.initialOffset, onDirectionChange = _a.onDirectionChange;
|
|
19
|
-
var _this = this;
|
|
20
|
-
this.onDirectionChange = undefined;
|
|
21
|
-
this.scrollDirection = "initial";
|
|
22
|
-
this.initialOffset = 120;
|
|
23
|
-
this.disposables = new seng_disposable_manager_1.DisposableManager();
|
|
24
|
-
this.setupSubscriptions = function () {
|
|
25
|
-
_this.disposables.add((0, seng_disposable_event_listener_1.addEventListener)(window, "scroll", _this.handleScrollDirection));
|
|
26
|
-
};
|
|
27
|
-
this.handleScrollDirection = function () {
|
|
28
|
-
var _a;
|
|
29
|
-
var position = window.scrollY;
|
|
30
|
-
var direction = "initial";
|
|
31
|
-
if (position > _this.initialOffset) {
|
|
32
|
-
if (position < _this.scrollPosition) {
|
|
33
|
-
direction = "up";
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
direction = "down";
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
if (direction !== _this.scrollDirection) {
|
|
40
|
-
(_a = _this.onDirectionChange) === null || _a === void 0 ? void 0 : _a.call(_this, direction);
|
|
41
|
-
}
|
|
42
|
-
_this.scrollPosition = position;
|
|
43
|
-
_this.scrollDirection = direction;
|
|
44
|
-
};
|
|
45
|
-
this.scrollPosition = 0;
|
|
46
|
-
if (initialOffset)
|
|
47
|
-
this.initialOffset = initialOffset;
|
|
48
|
-
this.onDirectionChange = onDirectionChange;
|
|
49
|
-
this.setupSubscriptions();
|
|
50
|
-
}
|
|
51
|
-
ScrollDirectionManager.prototype.dispose = function () {
|
|
52
|
-
this.disposables.dispose();
|
|
53
|
-
};
|
|
54
|
-
return ScrollDirectionManager;
|
|
55
|
-
}());
|
|
56
|
-
exports.ScrollDirectionManager = ScrollDirectionManager;
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ScrollDirectionManager = void 0;
|
|
4
|
-
var ScrollDirectionManager_1 = require("./ScrollDirectionManager");
|
|
5
|
-
Object.defineProperty(exports, "ScrollDirectionManager", { enumerable: true, get: function () { return ScrollDirectionManager_1.ScrollDirectionManager; } });
|
package/jest.config.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
preset: "ts-jest",
|
|
3
|
-
testEnvironment: "jest-environment-jsdom", // <-- Change this line
|
|
4
|
-
testMatch: ["**/__tests__/**/*.ts?(x)", "**/?(*.)+(spec|test).ts?(x)"],
|
|
5
|
-
collectCoverageFrom: ["src/**/*.{js,ts}"],
|
|
6
|
-
coverageDirectory: "coverage",
|
|
7
|
-
coverageReporters: ["text", "lcov"],
|
|
8
|
-
};
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { addEventListener } from "seng-disposable-event-listener";
|
|
2
|
-
import { DisposableManager } from "seng-disposable-manager";
|
|
3
|
-
|
|
4
|
-
export type ScrollDirection = "initial" | "up" | "down";
|
|
5
|
-
|
|
6
|
-
type ScrollDirectionManagerOptions = {
|
|
7
|
-
initialOffset?: number;
|
|
8
|
-
onDirectionChange?: (direction: ScrollDirection) => void;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @example
|
|
13
|
-
* const scrollDirection = new ScrollDirection({
|
|
14
|
-
* onDirectionChange: (direction) => {
|
|
15
|
-
* if (direction === "up") // do something if its up
|
|
16
|
-
* else if (direction === "initial") // do if its initial
|
|
17
|
-
* },
|
|
18
|
-
* treshold: 30
|
|
19
|
-
* })
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
export class ScrollDirectionManager {
|
|
23
|
-
private readonly onDirectionChange: ScrollDirectionManagerOptions["onDirectionChange"] =
|
|
24
|
-
undefined;
|
|
25
|
-
private scrollPosition: number;
|
|
26
|
-
private scrollDirection: ScrollDirection = "initial";
|
|
27
|
-
private readonly initialOffset: number = 120;
|
|
28
|
-
private readonly disposables = new DisposableManager();
|
|
29
|
-
|
|
30
|
-
public constructor({ initialOffset, onDirectionChange }: ScrollDirectionManagerOptions) {
|
|
31
|
-
this.scrollPosition = 0;
|
|
32
|
-
if (initialOffset) this.initialOffset = initialOffset;
|
|
33
|
-
this.onDirectionChange = onDirectionChange;
|
|
34
|
-
this.setupSubscriptions();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
private readonly setupSubscriptions = (): void => {
|
|
38
|
-
this.disposables.add(addEventListener(window, "scroll", this.handleScrollDirection));
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
private readonly handleScrollDirection = (): void => {
|
|
42
|
-
const position = window.scrollY;
|
|
43
|
-
let direction: ScrollDirection = "initial";
|
|
44
|
-
|
|
45
|
-
if (position > this.initialOffset) {
|
|
46
|
-
if (position < this.scrollPosition) {
|
|
47
|
-
direction = "up";
|
|
48
|
-
} else {
|
|
49
|
-
direction = "down";
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (direction !== this.scrollDirection) {
|
|
54
|
-
this.onDirectionChange?.(direction);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
this.scrollPosition = position;
|
|
58
|
-
this.scrollDirection = direction;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
public dispose(): void {
|
|
62
|
-
this.disposables.dispose();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { ScrollDirectionManager } from "../ScrollDirectionManager";
|
|
2
|
-
|
|
3
|
-
describe("ScrollDirectionManager", () => {
|
|
4
|
-
let manager: ScrollDirectionManager;
|
|
5
|
-
const scrollEvents: number[] = [0, 200, 100, 300];
|
|
6
|
-
let onDirectionChangeMock: jest.Mock;
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
jest.resetModules();
|
|
10
|
-
Object.defineProperty(global, "window", {
|
|
11
|
-
value: {
|
|
12
|
-
addEventListener: jest.fn(),
|
|
13
|
-
scrollY: 0,
|
|
14
|
-
},
|
|
15
|
-
writable: true,
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
onDirectionChangeMock = jest.fn();
|
|
19
|
-
manager = new ScrollDirectionManager({
|
|
20
|
-
onDirectionChange: onDirectionChangeMock,
|
|
21
|
-
initialOffset: 10,
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test("should initialize with the correct initial offset", () => {
|
|
26
|
-
expect(manager["initialOffset"]).toBe(10);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test("should call onDirectionChange when scroll direction changes", () => {
|
|
30
|
-
scrollEvents.forEach((scrollY, index) => {
|
|
31
|
-
Object.defineProperty(global.window, "scrollY", {
|
|
32
|
-
value: scrollY,
|
|
33
|
-
writable: true,
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
manager["handleScrollDirection"]();
|
|
37
|
-
|
|
38
|
-
if (index === 0) {
|
|
39
|
-
expect(onDirectionChangeMock).not.toHaveBeenCalled();
|
|
40
|
-
} else if (scrollY > scrollEvents[index - 1]) {
|
|
41
|
-
expect(onDirectionChangeMock).toHaveBeenCalledWith("down");
|
|
42
|
-
} else {
|
|
43
|
-
expect(onDirectionChangeMock).toHaveBeenCalledWith("up");
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
onDirectionChangeMock.mockClear();
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
package/src/index.ts
DELETED
package/tsconfig.json
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES5",
|
|
4
|
-
"module": "CommonJS",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"outDir": "./dist/cjs",
|
|
7
|
-
"strict": true,
|
|
8
|
-
"esModuleInterop": true,
|
|
9
|
-
"skipLibCheck": true
|
|
10
|
-
},
|
|
11
|
-
"include": ["src/**/*"],
|
|
12
|
-
"exclude": ["node_modules", "**/__tests__/*"]
|
|
13
|
-
}
|