scroll-direction-manager 1.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 -0
- package/README.md +52 -0
- package/dist/ScrollDirectionManager.js +56 -0
- package/dist/index.js +5 -0
- package/jest.config.js +8 -0
- package/package.json +40 -0
- package/tsconfig.json +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +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.
|
package/README.md
ADDED
|
@@ -0,0 +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
|
+
```
|
|
@@ -0,0 +1,56 @@
|
|
|
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.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
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
ADDED
|
@@ -0,0 +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
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "scroll-direction-manager",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A utility package that simplifies tracking the scroll direction (up, down, or initial) of a web page",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"clean": "rm -rf dist",
|
|
9
|
+
"build": "npm run clean && tsc",
|
|
10
|
+
"test": "jest",
|
|
11
|
+
"test:watch": "jest --watch",
|
|
12
|
+
"test:coverage": "jest --coverage"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/masaroli/scroll-direction-manager.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"scroll",
|
|
20
|
+
"scroll-direction"
|
|
21
|
+
],
|
|
22
|
+
"author": "Martin Sazzaroli",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/masaroli/scroll-direction-manager/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/masaroli/scroll-direction-manager#readme",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/jest": "^29.5.12",
|
|
30
|
+
"jest": "^29.7.0",
|
|
31
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
32
|
+
"jsdom": "^24.0.0",
|
|
33
|
+
"ts-jest": "^29.1.3",
|
|
34
|
+
"typescript": "^5.4.5"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"seng-disposable-event-listener": "^2.0.1",
|
|
38
|
+
"seng-disposable-manager": "^2.0.0"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES5",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"skipLibCheck": true
|
|
10
|
+
},
|
|
11
|
+
"include": ["src/**/*"],
|
|
12
|
+
"exclude": ["node_modules", "**/__tests__/*"]
|
|
13
|
+
}
|