scroll-direction-manager 1.0.4 → 2.0.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 +52 -52
- package/dist/cjs/ScrollDirectionManager.d.ts +2 -12
- package/dist/cjs/ScrollDirectionManager.js +9 -15
- package/jest.config.js +8 -8
- package/package.json +38 -43
- package/src/ScrollDirectionManager.ts +60 -64
- package/src/__tests__/ScrollDirectionManager.test.ts +49 -49
- package/src/index.ts +2 -2
- package/tsconfig.json +13 -13
- package/dist/ScrollDirectionManager.d.ts +0 -27
- package/dist/ScrollDirectionManager.js +0 -56
- package/dist/esm/ScrollDirectionManager.d.ts +0 -27
- package/dist/esm/ScrollDirectionManager.js +0 -53
- package/dist/esm/index.d.ts +0 -2
- package/dist/esm/index.js +0 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -5
- package/tsconfig.build.json +0 -14
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,52 @@
|
|
|
1
|
-
# ScrollDirectionManager
|
|
2
|
-
|
|
3
|
-
The ScrollDirectionManager is a utility package that simplifies tracking the scroll direction (up, down, or initial) of a web page. It provides an easy-to-use class that manages the scroll event listener and calculates the current scroll direction based on the scroll position and an optional initial offset.
|
|
4
|
-
|
|
5
|
-
This package is particularly useful when building responsive user interfaces that need to adapt or perform specific actions based on the user's scroll behavior. For example, you could use it to show or hide a sticky header, control the visibility of scroll-to-top buttons, or trigger animations based on the scroll direction.
|
|
6
|
-
|
|
7
|
-
## Key Features
|
|
8
|
-
|
|
9
|
-
- **Scroll Direction Detection**: Accurately determines the current scroll direction (up, down, or initial) based on the scroll position and an optional initial offset.
|
|
10
|
-
- **Callback Function**: Allows you to pass a callback function that gets called whenever the scroll direction changes, enabling you to update your UI or perform custom logic accordingly.
|
|
11
|
-
- **Customizable Initial Offset**: Allows you to set a custom initial offset value to adjust the sensitivity of the scroll direction detection.
|
|
12
|
-
- **Lightweight and Easy to Use**: Designed with a simple and intuitive API, making it easy to integrate into your projects.
|
|
13
|
-
|
|
14
|
-
## Installation
|
|
15
|
-
|
|
16
|
-
Install the package via npm:
|
|
17
|
-
|
|
18
|
-
```sh
|
|
19
|
-
npm install scroll-direction-manager
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Usage
|
|
23
|
-
|
|
24
|
-
Basic example of use:
|
|
25
|
-
|
|
26
|
-
```typescript
|
|
27
|
-
import ScrollDirectionManager from 'scroll-direction-manager';
|
|
28
|
-
import type ScrollDirection from 'scroll-direction-manager';
|
|
29
|
-
|
|
30
|
-
let element = document.querySelector('[data-element="some-element"]');
|
|
31
|
-
|
|
32
|
-
let scrollDirectionInstance = new ScrollDirectionManager({
|
|
33
|
-
initialOffset: 145,
|
|
34
|
-
onDirectionChange: navOnScroll,
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
function navOnScroll(direction: ScrollDirection) => {
|
|
38
|
-
if (element) {
|
|
39
|
-
if (direction === "initial" || direction === "up") {
|
|
40
|
-
element.classList.remove("hide");
|
|
41
|
-
|
|
42
|
-
if (direction === "up") {
|
|
43
|
-
element.classList.add("scrolled");
|
|
44
|
-
} else {
|
|
45
|
-
element.classList.remove("scrolled");
|
|
46
|
-
}
|
|
47
|
-
} else {
|
|
48
|
-
element.classList.add("hide");
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
```
|
|
1
|
+
# ScrollDirectionManager
|
|
2
|
+
|
|
3
|
+
The ScrollDirectionManager is a utility package that simplifies tracking the scroll direction (up, down, or initial) of a web page. It provides an easy-to-use class that manages the scroll event listener and calculates the current scroll direction based on the scroll position and an optional initial offset.
|
|
4
|
+
|
|
5
|
+
This package is particularly useful when building responsive user interfaces that need to adapt or perform specific actions based on the user's scroll behavior. For example, you could use it to show or hide a sticky header, control the visibility of scroll-to-top buttons, or trigger animations based on the scroll direction.
|
|
6
|
+
|
|
7
|
+
## Key Features
|
|
8
|
+
|
|
9
|
+
- **Scroll Direction Detection**: Accurately determines the current scroll direction (up, down, or initial) based on the scroll position and an optional initial offset.
|
|
10
|
+
- **Callback Function**: Allows you to pass a callback function that gets called whenever the scroll direction changes, enabling you to update your UI or perform custom logic accordingly.
|
|
11
|
+
- **Customizable Initial Offset**: Allows you to set a custom initial offset value to adjust the sensitivity of the scroll direction detection.
|
|
12
|
+
- **Lightweight and Easy to Use**: Designed with a simple and intuitive API, making it easy to integrate into your projects.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Install the package via npm:
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install scroll-direction-manager
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Basic example of use:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import ScrollDirectionManager from 'scroll-direction-manager';
|
|
28
|
+
import type ScrollDirection from 'scroll-direction-manager';
|
|
29
|
+
|
|
30
|
+
let element = document.querySelector('[data-element="some-element"]');
|
|
31
|
+
|
|
32
|
+
let scrollDirectionInstance = new ScrollDirectionManager({
|
|
33
|
+
initialOffset: 145,
|
|
34
|
+
onDirectionChange: navOnScroll,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
function navOnScroll(direction: ScrollDirection) => {
|
|
38
|
+
if (element) {
|
|
39
|
+
if (direction === "initial" || direction === "up") {
|
|
40
|
+
element.classList.remove("hide");
|
|
41
|
+
|
|
42
|
+
if (direction === "up") {
|
|
43
|
+
element.classList.add("scrolled");
|
|
44
|
+
} else {
|
|
45
|
+
element.classList.remove("scrolled");
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
element.classList.add("hide");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
```
|
|
@@ -3,23 +3,13 @@ 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 }: ScrollDirectionManagerOptions);
|
|
11
|
+
private scrollListener;
|
|
12
|
+
constructor({ initialOffset, onDirectionChange, }: ScrollDirectionManagerOptions);
|
|
23
13
|
private readonly setupSubscriptions;
|
|
24
14
|
private readonly handleScrollDirection;
|
|
25
15
|
dispose(): void;
|
|
@@ -1,18 +1,6 @@
|
|
|
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
6
|
var initialOffset = _a.initialOffset, onDirectionChange = _a.onDirectionChange;
|
|
@@ -20,9 +8,11 @@ var ScrollDirectionManager = /** @class */ (function () {
|
|
|
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
|
+
// Setting up the scroll event listener
|
|
14
|
+
_this.scrollListener = _this.handleScrollDirection.bind(_this);
|
|
15
|
+
window.addEventListener("scroll", _this.scrollListener);
|
|
26
16
|
};
|
|
27
17
|
this.handleScrollDirection = function () {
|
|
28
18
|
var _a;
|
|
@@ -49,7 +39,11 @@ var ScrollDirectionManager = /** @class */ (function () {
|
|
|
49
39
|
this.setupSubscriptions();
|
|
50
40
|
}
|
|
51
41
|
ScrollDirectionManager.prototype.dispose = function () {
|
|
52
|
-
|
|
42
|
+
// Remove the event listener when disposing
|
|
43
|
+
if (this.scrollListener) {
|
|
44
|
+
window.removeEventListener("scroll", this.scrollListener);
|
|
45
|
+
this.scrollListener = null;
|
|
46
|
+
}
|
|
53
47
|
};
|
|
54
48
|
return ScrollDirectionManager;
|
|
55
49
|
}());
|
package/jest.config.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
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
|
+
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
|
+
};
|
package/package.json
CHANGED
|
@@ -1,43 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "scroll-direction-manager",
|
|
3
|
-
"version": "
|
|
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
|
-
"
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
"build
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"test": "jest"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"jest
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"dependencies": {
|
|
40
|
-
"seng-disposable-event-listener": "^2.0.1",
|
|
41
|
-
"seng-disposable-manager": "^2.0.0"
|
|
42
|
-
}
|
|
43
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "scroll-direction-manager",
|
|
3
|
+
"version": "2.0.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
|
+
"scripts": {
|
|
8
|
+
"clean": "rm -rf dist/esm",
|
|
9
|
+
"build:cjs": "tsc --project tsconfig.json",
|
|
10
|
+
"build": "npm run build:cjs",
|
|
11
|
+
"test": "jest",
|
|
12
|
+
"test:watch": "jest --watch",
|
|
13
|
+
"test:coverage": "jest --coverage"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/masaroli/scroll-direction-manager.git"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"scroll",
|
|
21
|
+
"scroll-direction"
|
|
22
|
+
],
|
|
23
|
+
"author": "Martin Sazzaroli",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/masaroli/scroll-direction-manager/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/masaroli/scroll-direction-manager#readme",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/jest": "^29.5.12",
|
|
31
|
+
"jest": "^29.7.0",
|
|
32
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
33
|
+
"jsdom": "^24.0.0",
|
|
34
|
+
"ts-jest": "^29.1.3",
|
|
35
|
+
"typescript": "^5.4.5"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {}
|
|
38
|
+
}
|
|
@@ -1,64 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
private readonly
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
public dispose(): void {
|
|
62
|
-
this.disposables.dispose();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
1
|
+
export type ScrollDirection = "initial" | "up" | "down";
|
|
2
|
+
|
|
3
|
+
type ScrollDirectionManagerOptions = {
|
|
4
|
+
initialOffset?: number;
|
|
5
|
+
onDirectionChange?: (direction: ScrollDirection) => void;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export class ScrollDirectionManager {
|
|
9
|
+
private readonly onDirectionChange: ScrollDirectionManagerOptions["onDirectionChange"] =
|
|
10
|
+
undefined;
|
|
11
|
+
private scrollPosition: number;
|
|
12
|
+
private scrollDirection: ScrollDirection = "initial";
|
|
13
|
+
private readonly initialOffset: number = 120;
|
|
14
|
+
|
|
15
|
+
private scrollListener: EventListener | null = null;
|
|
16
|
+
|
|
17
|
+
public constructor({
|
|
18
|
+
initialOffset,
|
|
19
|
+
onDirectionChange,
|
|
20
|
+
}: ScrollDirectionManagerOptions) {
|
|
21
|
+
this.scrollPosition = 0;
|
|
22
|
+
if (initialOffset) this.initialOffset = initialOffset;
|
|
23
|
+
this.onDirectionChange = onDirectionChange;
|
|
24
|
+
this.setupSubscriptions();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private readonly setupSubscriptions = (): void => {
|
|
28
|
+
// Setting up the scroll event listener
|
|
29
|
+
this.scrollListener = this.handleScrollDirection.bind(this);
|
|
30
|
+
window.addEventListener("scroll", this.scrollListener);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
private readonly handleScrollDirection = (): void => {
|
|
34
|
+
const position = window.scrollY;
|
|
35
|
+
let direction: ScrollDirection = "initial";
|
|
36
|
+
|
|
37
|
+
if (position > this.initialOffset) {
|
|
38
|
+
if (position < this.scrollPosition) {
|
|
39
|
+
direction = "up";
|
|
40
|
+
} else {
|
|
41
|
+
direction = "down";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (direction !== this.scrollDirection) {
|
|
46
|
+
this.onDirectionChange?.(direction);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.scrollPosition = position;
|
|
50
|
+
this.scrollDirection = direction;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
public dispose(): void {
|
|
54
|
+
// Remove the event listener when disposing
|
|
55
|
+
if (this.scrollListener) {
|
|
56
|
+
window.removeEventListener("scroll", this.scrollListener);
|
|
57
|
+
this.scrollListener = null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -1,49 +1,49 @@
|
|
|
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
|
-
});
|
|
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
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { ScrollDirectionManager } from "./ScrollDirectionManager";
|
|
2
|
-
export type { ScrollDirection } from "./ScrollDirectionManager";
|
|
1
|
+
export { ScrollDirectionManager } from "./ScrollDirectionManager";
|
|
2
|
+
export type { ScrollDirection } from "./ScrollDirectionManager";
|
package/tsconfig.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
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
|
-
}
|
|
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
|
+
}
|
|
@@ -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;
|
|
@@ -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,53 +0,0 @@
|
|
|
1
|
-
import { addEventListener } from "seng-disposable-event-listener";
|
|
2
|
-
import { DisposableManager } from "seng-disposable-manager";
|
|
3
|
-
/**
|
|
4
|
-
* @example
|
|
5
|
-
* const scrollDirection = new ScrollDirection({
|
|
6
|
-
* onDirectionChange: (direction) => {
|
|
7
|
-
* if (direction === "up") // do something if its up
|
|
8
|
-
* else if (direction === "initial") // do if its initial
|
|
9
|
-
* },
|
|
10
|
-
* treshold: 30
|
|
11
|
-
* })
|
|
12
|
-
*/
|
|
13
|
-
var ScrollDirectionManager = /** @class */ (function () {
|
|
14
|
-
function ScrollDirectionManager(_a) {
|
|
15
|
-
var initialOffset = _a.initialOffset, onDirectionChange = _a.onDirectionChange;
|
|
16
|
-
var _this = this;
|
|
17
|
-
this.onDirectionChange = undefined;
|
|
18
|
-
this.scrollDirection = "initial";
|
|
19
|
-
this.initialOffset = 120;
|
|
20
|
-
this.disposables = new DisposableManager();
|
|
21
|
-
this.setupSubscriptions = function () {
|
|
22
|
-
_this.disposables.add(addEventListener(window, "scroll", _this.handleScrollDirection));
|
|
23
|
-
};
|
|
24
|
-
this.handleScrollDirection = function () {
|
|
25
|
-
var _a;
|
|
26
|
-
var position = window.scrollY;
|
|
27
|
-
var direction = "initial";
|
|
28
|
-
if (position > _this.initialOffset) {
|
|
29
|
-
if (position < _this.scrollPosition) {
|
|
30
|
-
direction = "up";
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
direction = "down";
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if (direction !== _this.scrollDirection) {
|
|
37
|
-
(_a = _this.onDirectionChange) === null || _a === void 0 ? void 0 : _a.call(_this, direction);
|
|
38
|
-
}
|
|
39
|
-
_this.scrollPosition = position;
|
|
40
|
-
_this.scrollDirection = direction;
|
|
41
|
-
};
|
|
42
|
-
this.scrollPosition = 0;
|
|
43
|
-
if (initialOffset)
|
|
44
|
-
this.initialOffset = initialOffset;
|
|
45
|
-
this.onDirectionChange = onDirectionChange;
|
|
46
|
-
this.setupSubscriptions();
|
|
47
|
-
}
|
|
48
|
-
ScrollDirectionManager.prototype.dispose = function () {
|
|
49
|
-
this.disposables.dispose();
|
|
50
|
-
};
|
|
51
|
-
return ScrollDirectionManager;
|
|
52
|
-
}());
|
|
53
|
-
export { ScrollDirectionManager };
|
package/dist/esm/index.d.ts
DELETED
package/dist/esm/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { ScrollDirectionManager } from "./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/tsconfig.build.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES5",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"moduleResolution": "node",
|
|
7
|
-
"outDir": "./dist/esm",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"skipLibCheck": true
|
|
11
|
-
},
|
|
12
|
-
"include": ["src/**/*"],
|
|
13
|
-
"exclude": ["node_modules", "**/__tests__/*"]
|
|
14
|
-
}
|