@andrew_l/dom 0.3.21 → 0.4.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/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  interface AnimationInstance {
2
- isCancelled: boolean;
2
+ isCancelled: boolean;
3
3
  }
4
4
  /**
5
5
  * Animates a single tick of an animation, running repeatedly until cancelled or the tick function returns `false`.
@@ -54,30 +54,30 @@ declare function animate(tick: Function): void;
54
54
  declare function animateInstantly(tick: Function): void;
55
55
  type TimingFn = (t: number) => number;
56
56
  type AnimateNumberProps = {
57
- to: number | number[];
58
- from: number | number[];
59
- duration: number;
60
- onUpdate: (value: any) => void;
61
- timing?: TimingFn;
62
- onEnd?: () => void;
57
+ to: number | number[];
58
+ from: number | number[];
59
+ duration: number;
60
+ onUpdate: (value: any) => void;
61
+ timing?: TimingFn;
62
+ onEnd?: () => void;
63
63
  };
64
64
  declare const timingFunctions: {
65
- linear: (t: number) => number;
66
- easeIn: (t: number) => number;
67
- easeOut: (t: number) => number;
68
- easeInOut: (t: number) => number;
69
- easeInQuad: (t: number) => number;
70
- easeOutQuad: (t: number) => number;
71
- easeInOutQuad: (t: number) => number;
72
- easeInCubic: (t: number) => number;
73
- easeOutCubic: (t: number) => number;
74
- easeInOutCubic: (t: number) => number;
75
- easeInQuart: (t: number) => number;
76
- easeOutQuart: (t: number) => number;
77
- easeInOutQuart: (t: number) => number;
78
- easeInQuint: (t: number) => number;
79
- easeOutQuint: (t: number) => number;
80
- easeInOutQuint: (t: number) => number;
65
+ linear: (t: number) => number;
66
+ easeIn: (t: number) => number;
67
+ easeOut: (t: number) => number;
68
+ easeInOut: (t: number) => number;
69
+ easeInQuad: (t: number) => number;
70
+ easeOutQuad: (t: number) => number;
71
+ easeInOutQuad: (t: number) => number;
72
+ easeInCubic: (t: number) => number;
73
+ easeOutCubic: (t: number) => number;
74
+ easeInOutCubic: (t: number) => number;
75
+ easeInQuart: (t: number) => number;
76
+ easeOutQuart: (t: number) => number;
77
+ easeInOutQuart: (t: number) => number;
78
+ easeInQuint: (t: number) => number;
79
+ easeOutQuint: (t: number) => number;
80
+ easeInOutQuint: (t: number) => number;
81
81
  };
82
82
  /**
83
83
  * Animates a numeric value from `from` to `to` over a specified duration, applying an optional timing function.
@@ -109,8 +109,14 @@ declare const timingFunctions: {
109
109
  *
110
110
  * @group Animation
111
111
  */
112
- declare function animateNumber({ timing, onUpdate, duration, onEnd, from, to, }: AnimateNumberProps): () => void;
113
-
112
+ declare function animateNumber({
113
+ timing,
114
+ onUpdate,
115
+ duration,
116
+ onEnd,
117
+ from,
118
+ to
119
+ }: AnimateNumberProps): () => void;
114
120
  /**
115
121
  * Copies the provided text to the system clipboard.
116
122
  *
@@ -134,16 +140,15 @@ declare function animateNumber({ timing, onUpdate, duration, onEnd, from, to, }:
134
140
  * @group Clipboard
135
141
  */
136
142
  declare function copyTextToClipboard(text: string): Promise<boolean>;
137
-
138
143
  declare const FAST_SMOOTH_MAX_DISTANCE = 1500;
139
144
  declare const FAST_SMOOTH_MIN_DURATION = 250;
140
145
  declare const FAST_SMOOTH_MAX_DURATION = 600;
141
146
  declare const FAST_SMOOTH_SHORT_TRANSITION_MAX_DISTANCE = 500;
142
147
  type ScrollCompleteCallback = (target: HTMLElement) => void;
143
148
  declare enum FocusDirection {
144
- Up = 0,
145
- Down = 1,
146
- Static = 2
149
+ Up = 0,
150
+ Down = 1,
151
+ Static = 2
147
152
  }
148
153
  /**
149
154
  * Smoothly scrolls the specified container to bring the given element into view,
@@ -169,7 +174,6 @@ declare enum FocusDirection {
169
174
  * @group Scrolling
170
175
  */
171
176
  declare function fastSmoothScroll(container: HTMLElement, element: HTMLElement, position: ScrollLogicalPosition | 'centerOrTop', margin?: number, maxDistance?: number, forceDirection?: FocusDirection, forceDuration?: number, forceNormalContainerHeight?: boolean, onComplete?: ScrollCompleteCallback): void;
172
-
173
177
  /**
174
178
  * Determines if the specified element or the document/window is scrolled near the bottom.
175
179
  *
@@ -196,7 +200,6 @@ declare function fastSmoothScroll(container: HTMLElement, element: HTMLElement,
196
200
  * @group Scrolling
197
201
  */
198
202
  declare function isScrolledToDown(target: HTMLElement | Document | Window, threshold?: number): boolean;
199
-
200
203
  /**
201
204
  * Resets the scroll position of the specified container element.
202
205
  *
@@ -215,5 +218,5 @@ declare function isScrolledToDown(target: HTMLElement | Document | Window, thres
215
218
  * @group Scrolling
216
219
  */
217
220
  declare function resetScroll(container: HTMLDivElement, scrollTop?: number): void;
218
-
219
- export { type AnimateNumberProps, FAST_SMOOTH_MAX_DISTANCE, FAST_SMOOTH_MAX_DURATION, FAST_SMOOTH_MIN_DURATION, FAST_SMOOTH_SHORT_TRANSITION_MAX_DISTANCE, FocusDirection, type ScrollCompleteCallback, type TimingFn, animate, animateInstantly, animateNumber, animateSingle, copyTextToClipboard, fastSmoothScroll, isScrolledToDown, resetScroll, timingFunctions };
221
+ export { AnimateNumberProps, FAST_SMOOTH_MAX_DISTANCE, FAST_SMOOTH_MAX_DURATION, FAST_SMOOTH_MIN_DURATION, FAST_SMOOTH_SHORT_TRANSITION_MAX_DISTANCE, FocusDirection, ScrollCompleteCallback, TimingFn, animate, animateInstantly, animateNumber, animateSingle, copyTextToClipboard, fastSmoothScroll, isScrolledToDown, resetScroll, timingFunctions };
222
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":["isCancelled","Function","tick","AnimationInstance","instance","t","to","from","duration","onUpdate","value","timing","TimingFn","onEnd","linear","easeIn","easeOut","easeInOut","easeInQuad","easeOutQuad","easeInOutQuad","easeInCubic","easeOutCubic","easeInOutCubic","easeInQuart","easeOutQuart","easeInOutQuart","easeInQuint","easeOutQuint","easeInOutQuint","AnimateNumberProps","text","Promise","HTMLElement","target","Up","Down","Static","container","element","ScrollLogicalPosition","position","margin","maxDistance","FocusDirection","forceDirection","forceDuration","forceNormalContainerHeight","ScrollCompleteCallback","onComplete","HTMLElement","Document","Window","target","threshold","HTMLDivElement","container","scrollTop"],"sources":["../src/animation.d.ts","../src/copyTextToClipboard.d.ts","../src/fastSmoothScroll.d.ts","../src/isScrolledToDown.d.ts","../src/resetScroll.d.ts"],"mappings":"UAAU,iBAAA;EACNA,WAAW;AAAA;;AAAA;AAoBf;;;;;;;;;AAAkF;AAgBlF;;;;AAA8C;AAgB9C;iBAhCwB,aAAA,CAAcE,IAAAA,EAAM,QAAA,EAAUE,QAAAA,GAAW,iBAAiB;;;AAgC3B;AACvD;;;;AAAiC;AACjC;;;;;;;iBAlBwB,OAAA,CAAQF,IAAc,EAAR,QAAQ;;;;;;AAwBrC;AAET;;;;;;;;;iBAVwB,gBAAA,CAAiBA,IAAc,EAAR,QAAQ;AAAA,KAC3C,QAAA,IAAYG,CAAS;AAAA,KACrB,kBAAA;EACRC,EAAAA;EACAC,IAAAA;EACAC,QAAAA;EACAC,QAAAA,GAAWC,KAAAA;EACXC,MAAAA,GAAS,QAAQ;EACjBE,KAAAA;AAAAA;AAAAA,cAEiB,eAAA;EACjBC,MAAAA,GAAST,CAAAA;EACTU,MAAAA,GAASV,CAAAA;EACTW,OAAAA,GAAUX,CAAAA;EACVY,SAAAA,GAAYZ,CAAAA;EACZa,UAAAA,GAAab,CAAAA;EACbc,WAAAA,GAAcd,CAAAA;EACde,aAAAA,GAAgBf,CAAAA;EAChBgB,WAAAA,GAAchB,CAAAA;EACdiB,YAAAA,GAAejB,CAAAA;EACfkB,cAAAA,GAAiBlB,CAAAA;EACjBmB,WAAAA,GAAcnB,CAAAA;EACdoB,YAAAA,GAAepB,CAAAA;EACfqB,cAAAA,GAAiBrB,CAAAA;EACjBsB,WAAAA,GAActB,CAAAA;EACduB,YAAAA,GAAevB,CAAAA;EACfwB,cAAAA,GAAiBxB,CAAAA;AAAAA;AAAS;AAgC9B;;;;;;;;;;;;;;;;;;;;;;;;;;AAA0G;;ACzF1G;ADyD8B,iBAgCN,aAAA;EAAgBM,MAAAA;EAAQF,QAAAA;EAAUD,QAAAA;EAAUK,KAAAA;EAAON,IAAAA;EAAMD;AAAAA,GAAO,kBAAA;;;;;AA9GzE;AAoBf;;;;;;;;;AAAkF;AAgBlF;;;;AAA8C;AAgB9C;;iBC/BwB,mBAAA,CAAoByB,IAAAA,WAAe,OAAO;AAAA,cCtB7C,wBAAA;AAAA,cACA,wBAAA;AAAA,cACA,wBAAA;AAAA,cACA,yCAAA;AAAA,KACT,sBAAA,IAA0BG,MAAmB,EAAX,WAAW;AAAA,aACrC,cAAA;EAChBC,EAAAA;EACAC,IAAAA;EACAC,MAAAA;AAAAA;;;;;AFa8E;AAgBlF;;;;AAA8C;AAgB9C;;;;AAAuD;AACvD;;;;AAAiC;AACjC;;;iBEtBwB,gBAAA,CAAiBC,SAAAA,EAAW,WAAA,EAAaC,OAAAA,EAAS,WAAA,EAAaE,QAAAA,EAAU,qBAAA,kBAAuCC,MAAAA,WAAiBC,WAAAA,WAAsBE,cAAAA,GAAiB,cAAA,EAAgBC,aAAAA,WAAwBC,0BAAAA,YAAsCE,UAAAA,GAAa,sBAAA;;;;;AFhC5Q;AAoBf;;;;;;;;;AAAkF;AAgBlF;;;;AAA8C;AAgB9C;;;;AAAuD;iBG5B/B,gBAAA,CAAiBI,MAAAA,EAAQ,WAAA,GAAc,QAAA,GAAW,MAAA,EAAQC,SAAAA;;;;;AHxBnE;AAoBf;;;;;;;;;AAAkF;AAgBlF;;iBIpBwB,WAAA,CAAYE,SAAAA,EAAW,cAAc,EAAEC,SAAAA"}
package/dist/index.mjs CHANGED
@@ -1,266 +1,200 @@
1
- import { fastRaf } from '@andrew_l/toolkit';
2
-
1
+ import { fastRaf } from "@andrew_l/toolkit";
3
2
  let currentInstance;
4
3
  function animateSingle(tick, instance) {
5
- if (!instance) {
6
- if (currentInstance && !currentInstance.isCancelled) {
7
- currentInstance.isCancelled = true;
8
- }
9
- instance = { isCancelled: false };
10
- currentInstance = instance;
11
- }
12
- if (!instance.isCancelled && tick()) {
13
- fastRaf(() => {
14
- animateSingle(tick, instance);
15
- });
16
- }
4
+ if (!instance) {
5
+ if (currentInstance && !currentInstance.isCancelled) currentInstance.isCancelled = true;
6
+ instance = { isCancelled: false };
7
+ currentInstance = instance;
8
+ }
9
+ if (!instance.isCancelled && tick()) fastRaf(() => {
10
+ animateSingle(tick, instance);
11
+ });
17
12
  }
18
13
  function animate(tick) {
19
- fastRaf(() => {
20
- if (tick()) {
21
- animate(tick);
22
- }
23
- });
14
+ fastRaf(() => {
15
+ if (tick()) animate(tick);
16
+ });
24
17
  }
25
18
  function animateInstantly(tick) {
26
- if (tick()) {
27
- fastRaf(() => {
28
- animateInstantly(tick);
29
- });
30
- }
19
+ if (tick()) fastRaf(() => {
20
+ animateInstantly(tick);
21
+ });
31
22
  }
32
23
  const timingFunctions = {
33
- linear: (t) => t,
34
- easeIn: (t) => t ** 1.675,
35
- easeOut: (t) => -1 * t ** 1.675,
36
- easeInOut: (t) => 0.5 * (Math.sin((t - 0.5) * Math.PI) + 1),
37
- easeInQuad: (t) => t * t,
38
- easeOutQuad: (t) => t * (2 - t),
39
- easeInOutQuad: (t) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t,
40
- easeInCubic: (t) => t ** 3,
41
- easeOutCubic: (t) => --t * t * t + 1,
42
- easeInOutCubic: (t) => t < 0.5 ? 4 * t ** 3 : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
43
- easeInQuart: (t) => t ** 4,
44
- easeOutQuart: (t) => 1 - --t * t ** 3,
45
- easeInOutQuart: (t) => t < 0.5 ? 8 * t ** 4 : 1 - 8 * --t * t ** 3,
46
- easeInQuint: (t) => t ** 5,
47
- easeOutQuint: (t) => 1 + --t * t ** 4,
48
- easeInOutQuint: (t) => t < 0.5 ? 16 * t ** 5 : 1 + 16 * --t * t ** 4
24
+ linear: (t) => t,
25
+ easeIn: (t) => t ** 1.675,
26
+ easeOut: (t) => -1 * t ** 1.675,
27
+ easeInOut: (t) => .5 * (Math.sin((t - .5) * Math.PI) + 1),
28
+ easeInQuad: (t) => t * t,
29
+ easeOutQuad: (t) => t * (2 - t),
30
+ easeInOutQuad: (t) => t < .5 ? 2 * t * t : -1 + (4 - 2 * t) * t,
31
+ easeInCubic: (t) => t ** 3,
32
+ easeOutCubic: (t) => --t * t * t + 1,
33
+ easeInOutCubic: (t) => t < .5 ? 4 * t ** 3 : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
34
+ easeInQuart: (t) => t ** 4,
35
+ easeOutQuart: (t) => 1 - --t * t ** 3,
36
+ easeInOutQuart: (t) => t < .5 ? 8 * t ** 4 : 1 - 8 * --t * t ** 3,
37
+ easeInQuint: (t) => t ** 5,
38
+ easeOutQuint: (t) => 1 + --t * t ** 4,
39
+ easeInOutQuint: (t) => t < .5 ? 16 * t ** 5 : 1 + 16 * --t * t ** 4
49
40
  };
50
- function animateNumber({
51
- timing = timingFunctions.linear,
52
- onUpdate,
53
- duration,
54
- onEnd,
55
- from,
56
- to
57
- }) {
58
- const t0 = Date.now();
59
- let canceled = false;
60
- animateInstantly(() => {
61
- if (canceled) return false;
62
- const t1 = Date.now();
63
- let t = (t1 - t0) / duration;
64
- if (t > 1) t = 1;
65
- const progress = timing(t);
66
- if (typeof from === "number" && typeof to === "number") {
67
- onUpdate(from + (to - from) * progress);
68
- } else if (Array.isArray(from) && Array.isArray(to)) {
69
- const result = from.map((f, i) => f + (to[i] - f) * progress);
70
- onUpdate(result);
71
- }
72
- if (t === 1 && onEnd) onEnd();
73
- return t < 1;
74
- });
75
- return () => {
76
- canceled = true;
77
- if (onEnd) onEnd();
78
- };
41
+ function animateNumber({ timing = timingFunctions.linear, onUpdate, duration, onEnd, from, to }) {
42
+ const t0 = Date.now();
43
+ let canceled = false;
44
+ animateInstantly(() => {
45
+ if (canceled) return false;
46
+ let t = (Date.now() - t0) / duration;
47
+ if (t > 1) t = 1;
48
+ const progress = timing(t);
49
+ if (typeof from === "number" && typeof to === "number") onUpdate(from + (to - from) * progress);
50
+ else if (Array.isArray(from) && Array.isArray(to)) onUpdate(from.map((f, i) => f + (to[i] - f) * progress));
51
+ if (t === 1 && onEnd) onEnd();
52
+ return t < 1;
53
+ });
54
+ return () => {
55
+ canceled = true;
56
+ if (onEnd) onEnd();
57
+ };
79
58
  }
80
-
81
59
  const defaultWindow = globalThis?.window;
82
60
  const defaultDocument = globalThis?.document;
83
61
  const defaultNavigator = globalThis?.navigator;
84
-
62
+ globalThis?.window?.location;
85
63
  function copyTextToClipboard(text) {
86
- if (defaultNavigator?.clipboard) {
87
- return copyWithNavigator(text);
88
- } else {
89
- return copyWithFakeElement(text);
90
- }
64
+ if (defaultNavigator?.clipboard) return copyWithNavigator(text);
65
+ else return copyWithFakeElement(text);
91
66
  }
92
67
  function copyWithNavigator(text) {
93
- if (!defaultNavigator) return Promise.resolve(false);
94
- return navigator.clipboard.writeText(text).then(() => true);
68
+ if (!defaultNavigator) return Promise.resolve(false);
69
+ return navigator.clipboard.writeText(text).then(() => true);
95
70
  }
96
71
  function copyWithFakeElement(text) {
97
- return new Promise((resolve, reject) => {
98
- if (!defaultDocument) return resolve(false);
99
- if (!defaultWindow) return resolve(false);
100
- const textareaEl = defaultDocument.createElement("textarea");
101
- const range = defaultDocument.createRange();
102
- textareaEl.value = text;
103
- textareaEl.style.position = "fixed";
104
- textareaEl.contentEditable = "true";
105
- defaultDocument.body.appendChild(textareaEl);
106
- textareaEl.focus();
107
- textareaEl.select();
108
- range.selectNodeContents(textareaEl);
109
- const selection = defaultWindow.getSelection();
110
- if (selection) {
111
- selection.removeAllRanges();
112
- selection.addRange(range);
113
- }
114
- textareaEl.setSelectionRange(0, 999999);
115
- try {
116
- const successful = defaultDocument.execCommand("copy");
117
- if (successful) {
118
- resolve(true);
119
- } else {
120
- reject(new Error("copy failed"));
121
- }
122
- } catch (error) {
123
- reject(error);
124
- }
125
- if (selection) {
126
- selection.removeAllRanges();
127
- }
128
- defaultDocument.body.removeChild(textareaEl);
129
- });
72
+ return new Promise((resolve, reject) => {
73
+ if (!defaultDocument) return resolve(false);
74
+ if (!defaultWindow) return resolve(false);
75
+ const textareaEl = defaultDocument.createElement("textarea");
76
+ const range = defaultDocument.createRange();
77
+ textareaEl.value = text;
78
+ textareaEl.style.position = "fixed";
79
+ textareaEl.contentEditable = "true";
80
+ defaultDocument.body.appendChild(textareaEl);
81
+ textareaEl.focus();
82
+ textareaEl.select();
83
+ range.selectNodeContents(textareaEl);
84
+ const selection = defaultWindow.getSelection();
85
+ if (selection) {
86
+ selection.removeAllRanges();
87
+ selection.addRange(range);
88
+ }
89
+ textareaEl.setSelectionRange(0, 999999);
90
+ try {
91
+ if (defaultDocument.execCommand("copy")) resolve(true);
92
+ else reject(/* @__PURE__ */ new Error("copy failed"));
93
+ } catch (error) {
94
+ reject(error);
95
+ }
96
+ if (selection) selection.removeAllRanges();
97
+ defaultDocument.body.removeChild(textareaEl);
98
+ });
130
99
  }
131
-
132
100
  const FAST_SMOOTH_MAX_DISTANCE = 1500;
133
101
  const FAST_SMOOTH_MIN_DURATION = 250;
134
102
  const FAST_SMOOTH_MAX_DURATION = 600;
135
103
  const FAST_SMOOTH_SHORT_TRANSITION_MAX_DISTANCE = 500;
136
- var FocusDirection = /* @__PURE__ */ ((FocusDirection2) => {
137
- FocusDirection2[FocusDirection2["Up"] = 0] = "Up";
138
- FocusDirection2[FocusDirection2["Down"] = 1] = "Down";
139
- FocusDirection2[FocusDirection2["Static"] = 2] = "Static";
140
- return FocusDirection2;
141
- })(FocusDirection || {});
104
+ let FocusDirection = /* @__PURE__ */ function(FocusDirection) {
105
+ FocusDirection[FocusDirection["Up"] = 0] = "Up";
106
+ FocusDirection[FocusDirection["Down"] = 1] = "Down";
107
+ FocusDirection[FocusDirection["Static"] = 2] = "Static";
108
+ return FocusDirection;
109
+ }({});
142
110
  function fastSmoothScroll(container, element, position, margin = 0, maxDistance = FAST_SMOOTH_MAX_DISTANCE, forceDirection, forceDuration, forceNormalContainerHeight, onComplete) {
143
- const scrollFrom = calculateScrollFrom(
144
- container,
145
- element,
146
- maxDistance,
147
- forceDirection
148
- );
149
- if (forceDirection === 2 /* Static */) {
150
- scrollWithJs(container, element, scrollFrom, position, margin, 0);
151
- return;
152
- }
153
- scrollWithJs(
154
- container,
155
- element,
156
- scrollFrom,
157
- position,
158
- margin,
159
- forceDuration,
160
- forceNormalContainerHeight,
161
- onComplete
162
- );
111
+ const scrollFrom = calculateScrollFrom(container, element, maxDistance, forceDirection);
112
+ if (forceDirection === 2) {
113
+ scrollWithJs(container, element, scrollFrom, position, margin, 0);
114
+ return;
115
+ }
116
+ scrollWithJs(container, element, scrollFrom, position, margin, forceDuration, forceNormalContainerHeight, onComplete);
163
117
  }
164
118
  function calculateScrollFrom(container, element, maxDistance = FAST_SMOOTH_MAX_DISTANCE, forceDirection) {
165
- const { offsetTop: elementTop } = element;
166
- const { scrollTop } = container;
167
- if (forceDirection === void 0) {
168
- const offset = elementTop - container.scrollTop;
169
- if (offset < -maxDistance) {
170
- return scrollTop + (offset + maxDistance);
171
- } else if (offset > maxDistance) {
172
- return scrollTop + (offset - maxDistance);
173
- }
174
- } else if (forceDirection === 0 /* Up */) {
175
- return elementTop + maxDistance;
176
- } else if (forceDirection === 1 /* Down */) {
177
- return Math.max(0, elementTop - maxDistance);
178
- }
179
- return scrollTop;
119
+ const { offsetTop: elementTop } = element;
120
+ const { scrollTop } = container;
121
+ if (forceDirection === void 0) {
122
+ const offset = elementTop - container.scrollTop;
123
+ if (offset < -maxDistance) return scrollTop + (offset + maxDistance);
124
+ else if (offset > maxDistance) return scrollTop + (offset - maxDistance);
125
+ } else if (forceDirection === 0) return elementTop + maxDistance;
126
+ else if (forceDirection === 1) return Math.max(0, elementTop - maxDistance);
127
+ return scrollTop;
180
128
  }
181
129
  function scrollWithJs(container, element, scrollFrom, position, margin = 0, forceDuration, forceNormalContainerHeight, onComplete) {
182
- const { offsetTop: elementTop, offsetHeight: elementHeight } = element;
183
- const {
184
- scrollTop: currentScrollTop,
185
- offsetHeight: containerHeight,
186
- scrollHeight
187
- } = container;
188
- const targetContainerHeight = forceNormalContainerHeight && container.dataset.normalHeight ? Number(container.dataset.normalHeight) : containerHeight;
189
- if (currentScrollTop !== scrollFrom) {
190
- container.scrollTop = scrollFrom;
191
- }
192
- let path;
193
- switch (position) {
194
- case "start":
195
- path = elementTop - margin - scrollFrom;
196
- break;
197
- case "end":
198
- path = elementTop + elementHeight + margin - (scrollFrom + targetContainerHeight);
199
- break;
200
- // 'nearest' is not supported yet
201
- case "nearest":
202
- case "center":
203
- case "centerOrTop":
204
- path = elementHeight < targetContainerHeight ? elementTop + elementHeight / 2 - (scrollFrom + targetContainerHeight / 2) : elementTop - margin - scrollFrom;
205
- break;
206
- }
207
- if (path < 0) {
208
- const remainingPath = -scrollFrom;
209
- path = Math.max(path, remainingPath);
210
- } else if (path > 0) {
211
- const remainingPath = scrollHeight - (scrollFrom + targetContainerHeight);
212
- path = Math.min(path, remainingPath);
213
- }
214
- if (path === 0) {
215
- onComplete?.(container);
216
- return;
217
- }
218
- const target = scrollFrom + path;
219
- if (forceDuration === 0) {
220
- container.scrollTop = target;
221
- onComplete?.(container);
222
- return;
223
- }
224
- const absPath = Math.abs(path);
225
- const transition = absPath < FAST_SMOOTH_SHORT_TRANSITION_MAX_DISTANCE ? shortTransition : longTransition;
226
- const duration = forceDuration || FAST_SMOOTH_MIN_DURATION + absPath / FAST_SMOOTH_MAX_DISTANCE * (FAST_SMOOTH_MAX_DURATION - FAST_SMOOTH_MIN_DURATION);
227
- const startAt = Date.now();
228
- fastRaf(() => {
229
- animateSingle(() => {
230
- const t = Math.min((Date.now() - startAt) / duration, 1);
231
- const currentPath = path * (1 - transition(t));
232
- container.scrollTop = Math.round(target - currentPath);
233
- if (t > 0) {
234
- return true;
235
- }
236
- onComplete?.(container);
237
- return false;
238
- });
239
- });
130
+ const { offsetTop: elementTop, offsetHeight: elementHeight } = element;
131
+ const { scrollTop: currentScrollTop, offsetHeight: containerHeight, scrollHeight } = container;
132
+ const targetContainerHeight = forceNormalContainerHeight && container.dataset.normalHeight ? Number(container.dataset.normalHeight) : containerHeight;
133
+ if (currentScrollTop !== scrollFrom) container.scrollTop = scrollFrom;
134
+ let path;
135
+ switch (position) {
136
+ case "start":
137
+ path = elementTop - margin - scrollFrom;
138
+ break;
139
+ case "end":
140
+ path = elementTop + elementHeight + margin - (scrollFrom + targetContainerHeight);
141
+ break;
142
+ case "nearest":
143
+ case "center":
144
+ case "centerOrTop":
145
+ path = elementHeight < targetContainerHeight ? elementTop + elementHeight / 2 - (scrollFrom + targetContainerHeight / 2) : elementTop - margin - scrollFrom;
146
+ break;
147
+ }
148
+ if (path < 0) {
149
+ const remainingPath = -scrollFrom;
150
+ path = Math.max(path, remainingPath);
151
+ } else if (path > 0) {
152
+ const remainingPath = scrollHeight - (scrollFrom + targetContainerHeight);
153
+ path = Math.min(path, remainingPath);
154
+ }
155
+ if (path === 0) {
156
+ onComplete?.(container);
157
+ return;
158
+ }
159
+ const target = scrollFrom + path;
160
+ if (forceDuration === 0) {
161
+ container.scrollTop = target;
162
+ onComplete?.(container);
163
+ return;
164
+ }
165
+ const absPath = Math.abs(path);
166
+ const transition = absPath < 500 ? shortTransition : longTransition;
167
+ const duration = forceDuration || 250 + absPath / 1500 * 350;
168
+ const startAt = Date.now();
169
+ fastRaf(() => {
170
+ animateSingle(() => {
171
+ const t = Math.min((Date.now() - startAt) / duration, 1);
172
+ const currentPath = path * (1 - transition(t));
173
+ container.scrollTop = Math.round(target - currentPath);
174
+ if (t > 0) return true;
175
+ onComplete?.(container);
176
+ return false;
177
+ });
178
+ });
240
179
  }
241
180
  function longTransition(t) {
242
- return 1 - (1 - t) ** 5;
181
+ return 1 - (1 - t) ** 5;
243
182
  }
244
183
  function shortTransition(t) {
245
- return 1 - (1 - t) ** 3.5;
184
+ return 1 - (1 - t) ** 3.5;
246
185
  }
247
-
248
186
  function isScrolledToDown(target, threshold = 10) {
249
- let scrollBottom;
250
- if (target === defaultDocument) {
251
- scrollBottom = (defaultDocument?.body?.offsetHeight || 0) - ((defaultWindow?.innerHeight || 0) + (defaultWindow?.scrollY || 0));
252
- } else {
253
- const el = target;
254
- scrollBottom = el.scrollHeight - (el.offsetHeight + el.scrollTop);
255
- }
256
- return scrollBottom <= threshold;
187
+ let scrollBottom;
188
+ if (target === defaultDocument) scrollBottom = (defaultDocument?.body?.offsetHeight || 0) - ((defaultWindow?.innerHeight || 0) + (defaultWindow?.scrollY || 0));
189
+ else {
190
+ const el = target;
191
+ scrollBottom = el.scrollHeight - (el.offsetHeight + el.scrollTop);
192
+ }
193
+ return scrollBottom <= threshold;
257
194
  }
258
-
259
195
  function resetScroll(container, scrollTop) {
260
- if (scrollTop !== void 0) {
261
- container.scrollTop = scrollTop;
262
- }
196
+ if (scrollTop !== void 0) container.scrollTop = scrollTop;
263
197
  }
264
-
265
198
  export { FAST_SMOOTH_MAX_DISTANCE, FAST_SMOOTH_MAX_DURATION, FAST_SMOOTH_MIN_DURATION, FAST_SMOOTH_SHORT_TRANSITION_MAX_DISTANCE, FocusDirection, animate, animateInstantly, animateNumber, animateSingle, copyTextToClipboard, fastSmoothScroll, isScrolledToDown, resetScroll, timingFunctions };
266
- //# sourceMappingURL=index.mjs.map
199
+
200
+ //# sourceMappingURL=index.mjs.map