scroll-direction-manager 1.0.1 → 1.0.3

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.
@@ -0,0 +1,27 @@
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 {};
@@ -0,0 +1,27 @@
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 {};
@@ -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;
@@ -0,0 +1,2 @@
1
+ export { ScrollDirectionManager } from "./ScrollDirectionManager";
2
+ export type { ScrollDirection } from "./ScrollDirectionManager";
@@ -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; } });
@@ -0,0 +1,27 @@
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 {};
@@ -0,0 +1,53 @@
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 };
@@ -0,0 +1,2 @@
1
+ export { ScrollDirectionManager } from "./ScrollDirectionManager";
2
+ export type { ScrollDirection } from "./ScrollDirectionManager";
@@ -0,0 +1 @@
1
+ export { ScrollDirectionManager } from "./ScrollDirectionManager";
@@ -0,0 +1,2 @@
1
+ export { ScrollDirectionManager } from "./ScrollDirectionManager";
2
+ export type { ScrollDirection } from "./ScrollDirectionManager";
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "scroll-direction-manager",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
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
- "types": "dist/index.d.ts",
5
+ "main": "dist/cjs/index.js",
6
+ "module": "dist/esm/index.mjs",
7
+ "types": "dist/cjs/index.d.ts",
8
8
  "scripts": {
9
- "clean": "rm -rf dist",
10
- "build": "npm run clean && tsc",
9
+ "build:cjs": "tsc --project tsconfig.json",
10
+ "build:esm": "tsc --project tsconfig.build.json",
11
+ "build": "npm run build:cjs && npm run build:esm",
11
12
  "test": "jest",
12
13
  "test:watch": "jest --watch",
13
14
  "test:coverage": "jest --coverage"
@@ -0,0 +1,64 @@
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
+ }
@@ -0,0 +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
+ });
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { ScrollDirectionManager } from "./ScrollDirectionManager";
2
+ export type { ScrollDirection } from "./ScrollDirectionManager";
@@ -0,0 +1,14 @@
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
+ }
package/tsconfig.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "target": "ES5",
4
4
  "module": "CommonJS",
5
5
  "declaration": true,
6
- "outDir": "./dist",
6
+ "outDir": "./dist/cjs",
7
7
  "strict": true,
8
8
  "esModuleInterop": true,
9
9
  "skipLibCheck": true