scroll-direction-manager 1.0.1 → 1.0.2

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,2 @@
1
+ export { ScrollDirectionManager } from "./ScrollDirectionManager";
2
+ export type { ScrollDirection } from "./ScrollDirectionManager";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scroll-direction-manager",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "A utility package that simplifies tracking the scroll direction (up, down, or initial) of a web page",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -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";