scroll-direction-manager 1.0.2 → 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,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";
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "scroll-direction-manager",
3
- "version": "1.0.2",
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,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