easing-scroll 1.0.2 → 1.0.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 ☃︎
3
+ Copyright (c) 2022-2026 ☃︎
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,8 +1,23 @@
1
1
  # easing-scroll
2
2
 
3
- [![npm-version](https://img.shields.io/npm/v/easing-scroll.svg)](https://npmjs.org/package/easing-scroll)
3
+ [![npm version](https://img.shields.io/npm/v/easing-scroll.svg)](https://npmjs.org/package/easing-scroll)
4
+ [![npm bundle size](https://img.shields.io/bundlephobia/minzip/easing-scroll)](https://bundlephobia.com/package/easing-scroll)
5
+ [![license](https://img.shields.io/npm/l/easing-scroll.svg)](LICENSE)
6
+ [![TypeScript](https://img.shields.io/badge/TypeScript-ready-blue.svg)](https://www.typescriptlang.org)
4
7
 
5
- ♿️ Smooth scrolling. [Demo](https://easing-scroll.vercel.app).
8
+ Programmatic smooth scrolling with custom easing, abort support, and promise-based completion tracking.
9
+
10
+ [Demo](https://easing-scroll-liard.vercel.app)
11
+
12
+ ## Highlights
13
+
14
+ - **Zero dependencies** — ~450 bytes min+gzip
15
+ - **TypeScript-first** — written in TypeScript, ships type declarations
16
+ - **Dual package** — ESM and CJS builds
17
+ - **Customizable** — bring your own [easing function](https://easings.net)
18
+ - **Cancellable** — abort with [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
19
+ - **Promise-based** — `await` completion or track partial progress
20
+ - **Universal** — works with any scrollable `Element`
6
21
 
7
22
  ## Install
8
23
 
@@ -10,89 +25,125 @@
10
25
  npm install easing-scroll
11
26
  ```
12
27
 
13
- ## Features
14
-
15
- - 📦 Zero dependencies
16
- - 📈 Customize [easing function](https://easings.net)
17
- - 🚫 Abort scrolling ([AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal))
18
- - 🔄 Waiting for animation to end
19
- - ☸️ Supports vertical and horizontal scroll
28
+ ```sh
29
+ pnpm add easing-scroll
30
+ ```
20
31
 
21
- ## Usage
32
+ ## Quick Start
22
33
 
23
34
  ```ts
24
35
  import { easingScroll } from "easing-scroll";
25
36
 
26
- const controller = new AbortController();
27
- // Abort scrolling
28
- // controller.abort(); ❌
29
-
30
- const target = document.querySelector(".container");
37
+ const container = document.querySelector(".container");
31
38
 
32
- const progress = await easingScroll(target, {
33
- left: 0, // px
34
- top: 300, // px
35
- duration: 400, // ms
36
- signal: controller.signal,
37
- // 👀 https://easings.net/#easeOutCubic
38
- easing: (x) => 1 - Math.pow(1 - x, 3),
39
+ await easingScroll(container, {
40
+ top: 300,
41
+ duration: 400,
42
+ easing: (x) => 1 - Math.pow(1 - x, 3), // easeOutCubic
39
43
  });
40
-
41
- if (progress === 1) {
42
- console.log("Completed");
43
- } else {
44
- console.log("Aborted");
45
- }
46
44
  ```
47
45
 
48
- ### Animation
46
+ ## API
47
+
48
+ ### `easingScroll(target, options): Promise<number>`
49
+
50
+ Smoothly scrolls `target` to the given position.
51
+
52
+ #### `target`
53
+
54
+ Type: `Element`
55
+
56
+ Any scrollable DOM element.
49
57
 
50
- Linear function `(t) => t` is used by default. Pass [easing](https://easings.net), if you want to change easing function.
51
- `duration` is animation duration in milliseconds.
58
+ #### `options`
59
+
60
+ | Option | Type | Default | Description |
61
+ | ---------- | ----------------------- | ---------- | ---------------------------------------------------------------------------- |
62
+ | `top` | `number` | — | Target vertical scroll position in pixels |
63
+ | `left` | `number` | — | Target horizontal scroll position in pixels |
64
+ | `duration` | `number` | `0` | Animation duration in milliseconds |
65
+ | `easing` | `(t: number) => number` | `(t) => t` | [Easing function](https://easings.net) mapping progress (0–1) to eased value |
66
+ | `signal` | `AbortSignal` | — | Signal to cancel the animation |
67
+
68
+ #### Return value
69
+
70
+ Resolves with a `number` between `0` and `1` representing animation progress:
71
+
72
+ | Value | Meaning |
73
+ | ----------- | ---------------------------------------------------- |
74
+ | `1` | Animation completed fully |
75
+ | `0 < x < 1` | Animation was aborted at _x_ progress |
76
+ | `0` | Animation never started (signal was already aborted) |
77
+
78
+ ### Behavior
79
+
80
+ - **Instant scroll** — when `duration` is `0` or negative, the element scrolls instantly and resolves `1`.
81
+ - **No-op** — when both `top` and `left` are omitted, resolves `1` immediately.
82
+ - **Clamping** — scroll values are clamped to the element's scrollable range. No visual flash occurs.
83
+ - **Already-aborted signal** — resolves `0` without scrolling.
84
+
85
+ ## Examples
86
+
87
+ ### Custom Easing
88
+
89
+ The default easing is linear `(t) => t`. Pass any function from [easings.net](https://easings.net):
52
90
 
53
91
  ```ts
54
- easingScroll(target, {
55
- duration: 400, // ms
56
- // 👀 https://easings.net/#easeOutCubic
92
+ await easingScroll(element, {
93
+ top: 500,
94
+ duration: 600,
95
+ // https://easings.net/#easeOutCubic
57
96
  easing: (x) => 1 - Math.pow(1 - x, 3),
58
97
  });
59
98
  ```
60
99
 
61
- ### Abort scrolling
100
+ ### Abort Scrolling
62
101
 
63
- Pass `signal` ([AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)),
64
- if you want to abort scrolling.
102
+ Use an `AbortController` to cancel an in-flight animation:
65
103
 
66
104
  ```ts
67
105
  const controller = new AbortController();
68
- setTimeout(() => {
69
- controller.abort();
70
- }, 100);
71
106
 
72
- const progress = await easingScroll(target, {
73
- ...,
107
+ setTimeout(() => controller.abort(), 100);
108
+
109
+ const progress = await easingScroll(element, {
110
+ top: 1000,
111
+ duration: 400,
74
112
  signal: controller.signal,
75
113
  });
76
114
 
77
- if (progress !== 1) {
78
- console.log('Scrolling has been aborted.');
115
+ if (progress < 1) {
116
+ console.log(`Aborted at ${Math.round(progress * 100)}%`);
79
117
  }
80
118
  ```
81
119
 
82
- `progress` is a number from _0_ to _1_.
120
+ ### React Hook
83
121
 
84
- `1` - Scrolling is completed _100%_.
122
+ A reusable hook that cancels the previous scroll when dependencies change or the component unmounts:
85
123
 
86
- `<1` - Scrolling has been aborted and completed _x%_.
124
+ ```tsx
125
+ import { useEffect, RefObject } from "react";
126
+ import { easingScroll } from "easing-scroll";
87
127
 
88
- ```ts
89
- const progress = await easingScroll(target, {
90
- ...,
91
- });
128
+ function useEasingScroll(ref: RefObject<HTMLElement | null>, top: number) {
129
+ useEffect(() => {
130
+ const target = ref.current;
131
+ if (!target) return;
92
132
 
93
- if (progress !== 1) {
94
- console.log('Scrolling has been aborted.');
95
- } else {
96
- console.log('Completed.');
133
+ const controller = new AbortController();
134
+
135
+ easingScroll(target, {
136
+ top,
137
+ duration: 400,
138
+ signal: controller.signal,
139
+ easing: (x) => 1 - Math.pow(1 - x, 3),
140
+ });
141
+
142
+ return () => controller.abort();
143
+ }, [top]);
97
144
  }
98
145
  ```
146
+
147
+ ## License
148
+
149
+ [MIT](LICENSE)
@@ -0,0 +1 @@
1
+ "use strict";var f=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var y=(e,n)=>{for(var s in n)f(e,s,{get:n[s],enumerable:!0})},A=(e,n,s,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of M(n))!v.call(e,o)&&o!==s&&f(e,o,{get:()=>n[o],enumerable:!(r=x(n,o))||r.enumerable});return e};var O=e=>A(f({},"__esModule",{value:!0}),e);var h={};y(h,{easingScroll:()=>F});module.exports=O(h);var k=e=>e,l=(e,n,s)=>{e.scrollTop=n??e.scrollTop,e.scrollLeft=s??e.scrollLeft},F=(e,{top:n,left:s,signal:r,duration:o=0,easing:p=k})=>r?.aborted?Promise.resolve(0):n===void 0&&s===void 0?Promise.resolve(1):o<=0?(l(e,n,s),Promise.resolve(1)):new Promise(P=>{let d=e.scrollTop,u=e.scrollLeft;l(e,n,s),n=e.scrollTop,s=e.scrollLeft,e.scrollTop=d,e.scrollLeft=u;let c,m,a=t=>c===void 0?0:(t-c)/o,b=()=>{cancelAnimationFrame(m);let t=Math.max(0,Math.min(a(performance.now()),1));P(t)};r?.addEventListener("abort",b,{once:!0});let L=t=>{c===void 0&&(c=t);let i=a(t),T=n===void 0?void 0:d+(n-d)*p(i),E=s===void 0?void 0:u+(s-u)*p(i);if(i<1)l(e,T,E),m=requestAnimationFrame(L);else{l(e,n,s),r?.removeEventListener("abort",b),P(1);return}};m=requestAnimationFrame(L)});0&&(module.exports={easingScroll});
@@ -0,0 +1,60 @@
1
+ /** Pixel value */
2
+ type Px = number;
3
+ /** Milliseconds value */
4
+ type Ms = number;
5
+ /**
6
+ * Progress percentage as a number between 0 and 1.
7
+ *
8
+ * - `0` — 0% (animation not started or aborted immediately)
9
+ * - `1` — 100% (animation completed)
10
+ * - `0 < value < 1` — partial progress (animation was aborted mid-way)
11
+ */
12
+ type Pct = number;
13
+ type Options = {
14
+ /** Target vertical scroll position in pixels */
15
+ top?: Px;
16
+ /** Target horizontal scroll position in pixels */
17
+ left?: Px;
18
+ /**
19
+ * Animation duration in milliseconds.
20
+ * If `0` or negative, scrolls instantly without animation.
21
+ * @default 0
22
+ */
23
+ duration?: Ms;
24
+ /**
25
+ * An `AbortSignal` to cancel the scroll animation.
26
+ * When aborted, the promise resolves with the current progress (0–1).
27
+ */
28
+ signal?: AbortSignal;
29
+ /**
30
+ * Easing function that maps animation progress `t` (0–1) to eased value.
31
+ * @default linear
32
+ * @see Easing functions https://easings.net
33
+ */
34
+ easing?: (t: Pct) => Pct;
35
+ };
36
+ /**
37
+ * Smoothly scroll an element to the given position using a custom easing function.
38
+ *
39
+ * @param target - The scrollable HTML element
40
+ * @param options - Scroll options (top, left, duration, easing, signal)
41
+ * @returns A promise that resolves with the animation progress:
42
+ * - `1` if the animation completed fully
43
+ * - `0` if the signal was already aborted before starting
44
+ * - `0 < value < 1` if the animation was aborted mid-way
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * const controller = new AbortController();
49
+ *
50
+ * const progress = await easingScroll(element, {
51
+ * top: 500,
52
+ * duration: 400,
53
+ * easing: (t) => 1 - Math.pow(1 - t, 3), // easeOutCubic
54
+ * signal: controller.signal,
55
+ * });
56
+ * ```
57
+ */
58
+ declare const easingScroll: <E extends Element>(target: E, { top, left, signal, duration, easing }: Options) => Promise<Pct>;
59
+
60
+ export { easingScroll };
@@ -1,20 +1,60 @@
1
- type Px = number;
2
- type Ms = number;
3
- /**
4
- * Percent is number 0 - 1
5
- *
6
- * 0 = 0%, 1 = 100%
7
- */
8
- type Pct = number;
9
- type Options = {
10
- top: Px;
11
- left: Px;
12
- duration: Ms;
13
- signal: AbortSignal;
14
- /**
15
- * @see Easing functions https://easings.net
16
- */
17
- easing: (t: Pct) => Pct;
18
- };
19
- export declare const easingScroll: <E extends Element>(target: E, { top, left, signal, duration, easing }: Partial<Options>) => Promise<Pct>;
20
- export {};
1
+ /** Pixel value */
2
+ type Px = number;
3
+ /** Milliseconds value */
4
+ type Ms = number;
5
+ /**
6
+ * Progress percentage as a number between 0 and 1.
7
+ *
8
+ * - `0` — 0% (animation not started or aborted immediately)
9
+ * - `1` — 100% (animation completed)
10
+ * - `0 < value < 1` — partial progress (animation was aborted mid-way)
11
+ */
12
+ type Pct = number;
13
+ type Options = {
14
+ /** Target vertical scroll position in pixels */
15
+ top?: Px;
16
+ /** Target horizontal scroll position in pixels */
17
+ left?: Px;
18
+ /**
19
+ * Animation duration in milliseconds.
20
+ * If `0` or negative, scrolls instantly without animation.
21
+ * @default 0
22
+ */
23
+ duration?: Ms;
24
+ /**
25
+ * An `AbortSignal` to cancel the scroll animation.
26
+ * When aborted, the promise resolves with the current progress (0–1).
27
+ */
28
+ signal?: AbortSignal;
29
+ /**
30
+ * Easing function that maps animation progress `t` (0–1) to eased value.
31
+ * @default linear
32
+ * @see Easing functions https://easings.net
33
+ */
34
+ easing?: (t: Pct) => Pct;
35
+ };
36
+ /**
37
+ * Smoothly scroll an element to the given position using a custom easing function.
38
+ *
39
+ * @param target - The scrollable HTML element
40
+ * @param options - Scroll options (top, left, duration, easing, signal)
41
+ * @returns A promise that resolves with the animation progress:
42
+ * - `1` if the animation completed fully
43
+ * - `0` if the signal was already aborted before starting
44
+ * - `0 < value < 1` if the animation was aborted mid-way
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * const controller = new AbortController();
49
+ *
50
+ * const progress = await easingScroll(element, {
51
+ * top: 500,
52
+ * duration: 400,
53
+ * easing: (t) => 1 - Math.pow(1 - t, 3), // easeOutCubic
54
+ * signal: controller.signal,
55
+ * });
56
+ * ```
57
+ */
58
+ declare const easingScroll: <E extends Element>(target: E, { top, left, signal, duration, easing }: Options) => Promise<Pct>;
59
+
60
+ export { easingScroll };
@@ -1 +1 @@
1
- "use strict";const b=o=>o,g=(o,{top:l,left:t,signal:e,duration:s=0,easing:i=b})=>e!=null&&e.aborted?Promise.resolve(0):s?new Promise(c=>{const a=o.scrollTop,m=o.scrollLeft,u=performance.now();let n;const v=()=>(performance.now()-u)/s,f=()=>{cancelAnimationFrame(n);const r=v();c(r)};e==null||e.addEventListener("abort",f);const p=()=>{const r=v(),d=l===void 0?void 0:a+(l-a)*i(r),T=t===void 0?void 0:m+(t-m)*i(r);r<1?(o.scrollTo({top:d,left:T}),n=requestAnimationFrame(p)):(o.scrollTo({top:l,left:t}),e==null||e.removeEventListener("abort",f),c(1))};n=requestAnimationFrame(p)}):(o.scrollTo({top:l,left:t}),Promise.resolve(1));exports.easingScroll=g;
1
+ var E=e=>e,c=(e,n,s)=>{e.scrollTop=n??e.scrollTop,e.scrollLeft=s??e.scrollLeft},x=(e,{top:n,left:s,signal:i,duration:m=0,easing:f=E})=>i?.aborted?Promise.resolve(0):n===void 0&&s===void 0?Promise.resolve(1):m<=0?(c(e,n,s),Promise.resolve(1)):new Promise(p=>{let l=e.scrollTop,d=e.scrollLeft;c(e,n,s),n=e.scrollTop,s=e.scrollLeft,e.scrollTop=l,e.scrollLeft=d;let r,u,P=o=>r===void 0?0:(o-r)/m,a=()=>{cancelAnimationFrame(u);let o=Math.max(0,Math.min(P(performance.now()),1));p(o)};i?.addEventListener("abort",a,{once:!0});let b=o=>{r===void 0&&(r=o);let t=P(o),L=n===void 0?void 0:l+(n-l)*f(t),T=s===void 0?void 0:d+(s-d)*f(t);if(t<1)c(e,L,T),u=requestAnimationFrame(b);else{c(e,n,s),i?.removeEventListener("abort",a),p(1);return}};u=requestAnimationFrame(b)});export{x as easingScroll};
package/package.json CHANGED
@@ -1,29 +1,31 @@
1
1
  {
2
2
  "name": "easing-scroll",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "♿️ Smooth scrolling",
5
5
  "type": "module",
6
- "source": "src/index.ts",
7
- "main": "dist/index.js",
8
- "module": "dist/index.mjs",
9
- "types": "dist/index.d.ts",
6
+ "types": "dist/easing-scroll.d.ts",
10
7
  "exports": {
11
8
  ".": {
12
- "require": "./dist/index.js",
13
- "import": "./dist/index.mjs",
14
- "default": "./dist/index.mjs"
15
- },
16
- "./easing-scroll": {
17
- "require": "./dist/easing-scroll.js",
18
- "import": "./dist/easing-scroll.mjs",
19
- "default": "./dist/easing-scroll.mjs"
9
+ "types": "./dist/easing-scroll.d.ts",
10
+ "require": {
11
+ "types": "./dist/easing-scroll.d.cts",
12
+ "default": "./dist/easing-scroll.cjs"
13
+ },
14
+ "import": "./dist/easing-scroll.js"
20
15
  }
21
16
  },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "sideEffects": false,
22
21
  "scripts": {
22
+ "test": "vitest run",
23
23
  "check-types": "tsc",
24
- "clean": "rm -r dist/*",
25
- "build": "npm run clean && rollup -c",
26
- "release": "npm run check-types && npm publish"
24
+ "build": "tsup src/easing-scroll.ts --format cjs,esm --dts --minify --clean",
25
+ "release:patch": "npm version patch",
26
+ "preversion": "npm run check-types && npm run build",
27
+ "postversion": "git add . && git push && git push --tags && npm publish",
28
+ "prepare": "husky"
27
29
  },
28
30
  "repository": {
29
31
  "type": "git",
@@ -41,12 +43,13 @@
41
43
  "url": "https://github.com/faustienf/easing-scroll/issues"
42
44
  },
43
45
  "homepage": "https://github.com/faustienf/easing-scroll#readme",
44
- "prettier": {},
45
46
  "devDependencies": {
46
- "esbuild": "^0.16.12",
47
- "rollup": "^3.9.0",
48
- "rollup-plugin-esbuild": "^5.0.0",
49
- "rollup-plugin-typescript2": "^0.34.1",
50
- "typescript": "^4.9.4"
47
+ "@vitest/coverage-v8": "^4.0.18",
48
+ "esbuild": "^0.27.3",
49
+ "husky": "^9.1.7",
50
+ "jsdom": "^28.1.0",
51
+ "tsup": "^8.5.1",
52
+ "typescript": "^5.9.3",
53
+ "vitest": "^4.0.18"
51
54
  }
52
55
  }
@@ -1 +0,0 @@
1
- const u=o=>o,L=(o,{top:t,left:r,signal:n,duration:s=0,easing:i=u})=>n?.aborted?Promise.resolve(0):s?new Promise(a=>{const c=o.scrollTop,m=o.scrollLeft,f=performance.now();let l;const d=()=>(performance.now()-f)/s,p=()=>{cancelAnimationFrame(l);const e=d();a(e)};n?.addEventListener("abort",p);const v=()=>{const e=d(),T=t===void 0?void 0:c+(t-c)*i(e),b=r===void 0?void 0:m+(r-m)*i(e);e<1?(o.scrollTo({top:T,left:b}),l=requestAnimationFrame(v)):(o.scrollTo({top:t,left:r}),n?.removeEventListener("abort",p),a(1))};l=requestAnimationFrame(v)}):(o.scrollTo({top:t,left:r}),Promise.resolve(1));export{L as easingScroll};
package/dist/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export { easingScroll } from "./easing-scroll";
package/dist/index.js DELETED
@@ -1 +0,0 @@
1
- "use strict";const b=o=>o,g=(o,{top:l,left:t,signal:e,duration:s=0,easing:i=b})=>e!=null&&e.aborted?Promise.resolve(0):s?new Promise(c=>{const a=o.scrollTop,m=o.scrollLeft,u=performance.now();let n;const v=()=>(performance.now()-u)/s,f=()=>{cancelAnimationFrame(n);const r=v();c(r)};e==null||e.addEventListener("abort",f);const p=()=>{const r=v(),d=l===void 0?void 0:a+(l-a)*i(r),T=t===void 0?void 0:m+(t-m)*i(r);r<1?(o.scrollTo({top:d,left:T}),n=requestAnimationFrame(p)):(o.scrollTo({top:l,left:t}),e==null||e.removeEventListener("abort",f),c(1))};n=requestAnimationFrame(p)}):(o.scrollTo({top:l,left:t}),Promise.resolve(1));exports.easingScroll=g;
package/dist/index.mjs DELETED
@@ -1 +0,0 @@
1
- const u=o=>o,L=(o,{top:t,left:r,signal:n,duration:s=0,easing:i=u})=>n?.aborted?Promise.resolve(0):s?new Promise(a=>{const c=o.scrollTop,m=o.scrollLeft,f=performance.now();let l;const d=()=>(performance.now()-f)/s,p=()=>{cancelAnimationFrame(l);const e=d();a(e)};n?.addEventListener("abort",p);const v=()=>{const e=d(),T=t===void 0?void 0:c+(t-c)*i(e),b=r===void 0?void 0:m+(r-m)*i(e);e<1?(o.scrollTo({top:T,left:b}),l=requestAnimationFrame(v)):(o.scrollTo({top:t,left:r}),n?.removeEventListener("abort",p),a(1))};l=requestAnimationFrame(v)}):(o.scrollTo({top:t,left:r}),Promise.resolve(1));export{L as easingScroll};
@@ -1,86 +0,0 @@
1
- type Px = number;
2
- type Ms = number;
3
- /**
4
- * Percent is number 0 - 1
5
- *
6
- * 0 = 0%, 1 = 100%
7
- */
8
- type Pct = number;
9
-
10
- type Options = {
11
- top?: Px;
12
- left?: Px;
13
- duration?: Ms;
14
- signal?: AbortSignal;
15
- /**
16
- * @see Easing functions https://easings.net
17
- */
18
- easing?: (t: Pct) => Pct;
19
- };
20
-
21
- const linear = (t: number): number => t;
22
-
23
- const scrollTo = <E extends Element>(
24
- target: E,
25
- { top, left }: Pick<Options, "top" | "left">
26
- ) => {
27
- target.scrollTop = top ?? target.scrollTop;
28
- target.scrollLeft = left ?? target.scrollLeft;
29
- };
30
-
31
- export const easingScroll = <E extends Element>(
32
- target: E,
33
- { top, left, signal, duration = 0, easing = linear }: Options
34
- ): Promise<Pct> => {
35
- if (signal?.aborted) {
36
- return Promise.resolve(0);
37
- }
38
-
39
- if (!duration) {
40
- scrollTo(target, { top, left });
41
- return Promise.resolve(1);
42
- }
43
-
44
- return new Promise<Pct>((resolve) => {
45
- const startTop = target.scrollTop;
46
- const startLeft = target.scrollLeft;
47
- const startTimestamp = performance.now();
48
- let ramID: number;
49
-
50
- const getProgress = (): Pct => {
51
- const elapsed = performance.now() - startTimestamp;
52
- return elapsed / duration;
53
- };
54
-
55
- const abortHandler = () => {
56
- cancelAnimationFrame(ramID);
57
- const progress = getProgress();
58
- resolve(progress);
59
- };
60
-
61
- signal?.addEventListener("abort", abortHandler);
62
-
63
- const tick = () => {
64
- const progress = getProgress();
65
- const tickTop =
66
- top === undefined
67
- ? undefined
68
- : startTop + (top - startTop) * easing(progress);
69
- const tickLeft =
70
- left === undefined
71
- ? undefined
72
- : startLeft + (left - startLeft) * easing(progress);
73
-
74
- if (progress < 1) {
75
- scrollTo(target, { top: tickTop, left: tickLeft });
76
- ramID = requestAnimationFrame(tick);
77
- } else {
78
- scrollTo(target, { top, left });
79
- signal?.removeEventListener("abort", abortHandler);
80
- resolve(1);
81
- }
82
- };
83
-
84
- ramID = requestAnimationFrame(tick);
85
- });
86
- };
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export { easingScroll } from "./easing-scroll";