vevet 2.2.0 → 2.5.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.
Files changed (47) hide show
  1. package/build/cdn/index.js +1 -1
  2. package/build/cjs/app/events/Viewport.js +17 -6
  3. package/build/cjs/components/scroll/section/ScrollSectionProgress.js +261 -0
  4. package/build/cjs/index.js +5 -1
  5. package/build/cjs/utils/scroll/getScrollValues.js +20 -0
  6. package/build/cjs/utils/scroll/index.js +12 -0
  7. package/build/cjs/utils/scroll/scrollTo.js +41 -0
  8. package/build/cjs/utils/scroll/scrollToElement.js +39 -0
  9. package/build/cjs/utils/scroll/to.js +41 -0
  10. package/build/es/app/events/Viewport.js +16 -6
  11. package/build/es/components/scroll/section/ScrollSectionProgress.js +181 -0
  12. package/build/es/index.js +4 -1
  13. package/build/es/utils/scroll/getScrollValues.js +16 -0
  14. package/build/es/utils/scroll/index.js +4 -0
  15. package/build/es/utils/scroll/scrollTo.js +34 -0
  16. package/build/es/utils/scroll/scrollToElement.js +32 -0
  17. package/build/es/utils/scroll/to.js +34 -0
  18. package/build/types/app/events/Viewport.d.ts +9 -1
  19. package/build/types/app/events/Viewport.d.ts.map +1 -1
  20. package/build/types/components/scroll/section/ScrollSectionProgress.d.ts +131 -0
  21. package/build/types/components/scroll/section/ScrollSectionProgress.d.ts.map +1 -0
  22. package/build/types/components/scroll/types.d.ts +2 -5
  23. package/build/types/components/scroll/types.d.ts.map +1 -1
  24. package/build/types/index.d.ts +4 -1
  25. package/build/types/index.d.ts.map +1 -1
  26. package/build/types/utils/scroll/getScrollValues.d.ts +9 -0
  27. package/build/types/utils/scroll/getScrollValues.d.ts.map +1 -0
  28. package/build/types/utils/scroll/index.d.ts +5 -0
  29. package/build/types/utils/scroll/index.d.ts.map +1 -0
  30. package/build/types/utils/scroll/scrollTo.d.ts +28 -0
  31. package/build/types/utils/scroll/scrollTo.d.ts.map +1 -0
  32. package/build/types/utils/scroll/scrollToElement.d.ts +32 -0
  33. package/build/types/utils/scroll/scrollToElement.d.ts.map +1 -0
  34. package/build/types/utils/scroll/to.d.ts +28 -0
  35. package/build/types/utils/scroll/to.d.ts.map +1 -0
  36. package/build/types/utils/types/general.d.ts +6 -0
  37. package/build/types/utils/types/general.d.ts.map +1 -1
  38. package/package.json +1 -1
  39. package/src/ts/app/events/Viewport.ts +18 -6
  40. package/src/ts/components/scroll/section/ScrollSectionProgress.ts +344 -0
  41. package/src/ts/components/scroll/types.ts +3 -5
  42. package/src/ts/index.ts +4 -0
  43. package/src/ts/utils/scroll/getScrollValues.ts +20 -0
  44. package/src/ts/utils/scroll/index.ts +9 -0
  45. package/src/ts/utils/scroll/scrollTo.ts +65 -0
  46. package/src/ts/utils/scroll/scrollToElement.ts +74 -0
  47. package/src/ts/utils/types/general.ts +7 -0
@@ -0,0 +1,28 @@
1
+ import { ScrollLike } from '../types/general';
2
+ interface Props {
3
+ /**
4
+ * @default window
5
+ */
6
+ container?: Window | Element | ScrollLike;
7
+ /**
8
+ * top padding
9
+ * @default 0
10
+ */
11
+ top?: number;
12
+ /**
13
+ * left padding
14
+ * @default 0
15
+ */
16
+ left?: number;
17
+ /**
18
+ * scroll duration
19
+ * @default 250
20
+ */
21
+ duration?: number;
22
+ }
23
+ /**
24
+ * Scroll to coordinates
25
+ */
26
+ export default function scrollTo({ container, top, left, duration, }: Props): Promise<void>;
27
+ export {};
28
+ //# sourceMappingURL=to.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to.d.ts","sourceRoot":"","sources":["../../../../src/ts/utils/scroll/to.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAG9C,UAAU,KAAK;IACX;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,GAAG,UAAU,CAAC;IAC1C;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAAE,EAC9B,SAAkB,EAClB,GAAO,EACP,IAAQ,EACR,QAAc,GACjB,EAAE,KAAK,iBA8BP"}
@@ -4,4 +4,10 @@ export interface IRemovable {
4
4
  export interface IDestroyable {
5
5
  destroy: () => void;
6
6
  }
7
+ export interface ScrollLike {
8
+ scrollTop: number;
9
+ scrollLeft: number;
10
+ scrollTo(options: ScrollToOptions): void;
11
+ scrollTo(x: number, y: number): void;
12
+ }
7
13
  //# sourceMappingURL=general.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"general.d.ts","sourceRoot":"","sources":["../../../../src/ts/utils/types/general.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,UAAU;IACvB,MAAM,EAAE,MAAM,IAAI,CAAC;CACtB;AAED,MAAM,WAAW,YAAY;IACzB,OAAO,EAAE,MAAM,IAAI,CAAC;CACvB"}
1
+ {"version":3,"file":"general.d.ts","sourceRoot":"","sources":["../../../../src/ts/utils/types/general.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,UAAU;IACvB,MAAM,EAAE,MAAM,IAAI,CAAC;CACtB;AAED,MAAM,WAAW,YAAY;IACzB,OAAO,EAAE,MAAM,IAAI,CAAC;CACvB;AAED,MAAM,WAAW,UAAU;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI,CAAC;IACzC,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACxC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vevet",
3
- "version": "2.2.0",
3
+ "version": "2.5.0",
4
4
  "description": "VEVET - A JavaScript library",
5
5
  "browserslist": [
6
6
  "since 2015"
@@ -323,7 +323,12 @@ export class Viewport extends Callbacks<
323
323
  /**
324
324
  * Launch callbacks on resize.
325
325
  */
326
- protected _onResize () {
326
+ protected _onResize (
327
+ /**
328
+ * force all callbacks
329
+ */
330
+ force = false,
331
+ ) {
327
332
  // copy previous values
328
333
  const prevWidth = this._prevSize.w;
329
334
  const prevHeight = this._prevSize.h;
@@ -342,29 +347,36 @@ export class Viewport extends Callbacks<
342
347
  };
343
348
 
344
349
  // only when width is changed
345
- if (width !== prevWidth && height === prevHeight) {
350
+ if (force || (width !== prevWidth && height === prevHeight)) {
346
351
  this.tbt('w_', changes);
347
352
  }
348
353
  // only when height is changed
349
- if (height !== prevHeight && width === prevWidth) {
354
+ if (force || (height !== prevHeight && width === prevWidth)) {
350
355
  this.tbt('h_', changes);
351
356
  }
352
357
  // when height & width are changed
353
- if (width !== prevWidth && height !== prevHeight) {
358
+ if (force || (width !== prevWidth && height !== prevHeight)) {
354
359
  this.tbt('wh', changes);
355
360
  this.tbt('hw', changes);
356
361
  }
357
362
  // when width is changed
358
- if (width !== prevWidth) {
363
+ if (force || width !== prevWidth) {
359
364
  this.tbt('w', changes);
360
365
  }
361
366
  // when height changed
362
- if (height !== prevHeight) {
367
+ if (force || height !== prevHeight) {
363
368
  this.tbt('h', changes);
364
369
  }
365
370
 
366
371
  // on any change
367
372
  this.tbt('', changes);
368
373
  }
374
+
375
+ /**
376
+ * Force launching all callbacks
377
+ */
378
+ public forceResize () {
379
+ this._onResize(true);
380
+ }
369
381
  }
370
382
 
@@ -0,0 +1,344 @@
1
+ import { selectOne } from 'vevet-dom';
2
+ import PCancelable from 'p-cancelable';
3
+ import { IRemovable } from '../../../utils/types/general';
4
+ import { RequiredModuleProp } from '../../../utils/types/utility';
5
+ import onScroll from '../../../utils/listeners/onScroll';
6
+ import { Component, NComponent } from '../../../base/Component';
7
+ import { NViewport } from '../../../app/events/Viewport';
8
+ import { SmoothScroll } from '../smooth-scroll/SmoothScroll';
9
+ import clampScope from '../../../utils/math/clampScope';
10
+ import getScrollValues from '../../../utils/scroll/getScrollValues';
11
+
12
+
13
+
14
+ export namespace NScrollSectionProgress {
15
+
16
+ /**
17
+ * Static properties
18
+ */
19
+ export interface StaticProp extends NComponent.StaticProp {
20
+ /**
21
+ * The scrollable container
22
+ * @default window
23
+ */
24
+ container?: string | Element | SmoothScroll | Window;
25
+ /**
26
+ * The scrollable element
27
+ */
28
+ section: string | Element;
29
+ /**
30
+ * Update sizes on resize
31
+ * @default false
32
+ */
33
+ viewportTarget?: keyof NViewport.CallbacksTypes;
34
+ /**
35
+ * @default 0
36
+ */
37
+ resizeTimeout?: number;
38
+ }
39
+
40
+ /**
41
+ * Changeable properties
42
+ */
43
+ export interface ChangeableProp extends NComponent.ChangeableProp { }
44
+
45
+ /**
46
+ * Available callbacks
47
+ */
48
+ export interface CallbacksTypes extends NComponent.CallbacksTypes {
49
+ 'render': false;
50
+ 'resize': false;
51
+ }
52
+
53
+ }
54
+
55
+
56
+
57
+ /**
58
+ * Elements into viewport
59
+ */
60
+ export class ScrollSectionProgress <
61
+ StaticProp extends NScrollSectionProgress.StaticProp
62
+ = NScrollSectionProgress.StaticProp,
63
+ ChangeableProp extends NScrollSectionProgress.ChangeableProp
64
+ = NScrollSectionProgress.ChangeableProp,
65
+ CallbacksTypes extends NScrollSectionProgress.CallbacksTypes
66
+ = NScrollSectionProgress.CallbacksTypes,
67
+ > extends Component <
68
+ StaticProp,
69
+ ChangeableProp,
70
+ CallbacksTypes
71
+ > {
72
+ protected _getDefaultProp <
73
+ T extends RequiredModuleProp<StaticProp & ChangeableProp>
74
+ > (): T {
75
+ return {
76
+ ...super._getDefaultProp(),
77
+ container: window,
78
+ updateOnResize: '',
79
+ resizeTimeout: 0,
80
+ };
81
+ }
82
+
83
+ /**
84
+ * Scroll container
85
+ */
86
+ protected _scrollContainer: Element | SmoothScroll | Window;
87
+ get scrollContainer () {
88
+ return this._scrollContainer;
89
+ }
90
+
91
+ /**
92
+ * Section element
93
+ */
94
+ protected _section: Element;
95
+ get section () {
96
+ return this._section;
97
+ }
98
+
99
+ /**
100
+ * Scroll event
101
+ */
102
+ protected _scrollEvent?: IRemovable;
103
+ /**
104
+ * Loaded event
105
+ */
106
+ protected _loadedEvent?: PCancelable<any>;
107
+
108
+ /**
109
+ * Scrolling scope
110
+ */
111
+ protected _scopeScroll: [number, number];
112
+ get scopeScroll () {
113
+ return this._scopeScroll;
114
+ }
115
+
116
+ /**
117
+ * 'In' scope relative to the global progress
118
+ */
119
+ protected _scopeIn: [number, number];
120
+ protected get scopeIn () {
121
+ return this._scopeIn;
122
+ }
123
+
124
+ /**
125
+ * 'Move' scope relative to the global progress
126
+ */
127
+ protected _scopeMove: [number, number];
128
+ get scopeMove () {
129
+ return this._scopeMove;
130
+ }
131
+
132
+ /**
133
+ * 'Move' scope relative to the global progress
134
+ */
135
+ protected _scopeOut: [number, number];
136
+ get scopeOut () {
137
+ return this._scopeOut;
138
+ }
139
+
140
+ protected _progress: number;
141
+ /**
142
+ * Global progress
143
+ */
144
+ get progress () {
145
+ return this._progress;
146
+ }
147
+ set progress (val) {
148
+ this._progress = val;
149
+ }
150
+
151
+
152
+
153
+ constructor (
154
+ initialProp?: (StaticProp & ChangeableProp),
155
+ init = true,
156
+ ) {
157
+ super(initialProp, false);
158
+
159
+ // get scroll container
160
+ if (typeof this.prop.container === 'string') {
161
+ this._scrollContainer = selectOne(this.prop.container) as Element;
162
+ } else {
163
+ this._scrollContainer = this.prop.container;
164
+ }
165
+
166
+ // get section element
167
+ if (typeof this.prop.section === 'string') {
168
+ this._section = selectOne(this.prop.section) as Element;
169
+ } else {
170
+ this._section = this.prop.section;
171
+ }
172
+
173
+ // set defaults
174
+ this._progress = -0.001;
175
+ this._scopeScroll = [0, 0];
176
+ this._scopeIn = [0, 0];
177
+ this._scopeMove = [0, 0];
178
+ this._scopeOut = [0, 0];
179
+
180
+ // initialize the class
181
+ if (init) {
182
+ this.init();
183
+ }
184
+ }
185
+
186
+ public init () {
187
+ super.init();
188
+ }
189
+
190
+ // Set Module Events
191
+ protected _setEvents () {
192
+ super._setEvents();
193
+
194
+ // set resize events
195
+ this.addViewportCallback(this.prop.viewportTarget, () => {
196
+ this.resize();
197
+ }, {
198
+ timeout: this.prop.resizeTimeout,
199
+ });
200
+
201
+ // resize on page loaded
202
+ this._loadedEvent = this._app.onPageLoaded();
203
+ this._loadedEvent.then(() => {
204
+ this.resize();
205
+ }).catch(() => {});
206
+
207
+ // set scroll events
208
+ this._scrollEvent = onScroll({
209
+ container: this.prop.container,
210
+ callback: this._handleScroll.bind(this),
211
+ });
212
+ }
213
+
214
+ protected _onPropMutate () {
215
+ super._onPropMutate();
216
+ this.resize();
217
+ }
218
+
219
+
220
+
221
+ /**
222
+ * 'in' progress
223
+ */
224
+ get progressIn () {
225
+ return clampScope(
226
+ this.progress,
227
+ this.scopeIn,
228
+ ) || 0;
229
+ }
230
+
231
+ /**
232
+ * 'out' progress
233
+ */
234
+ get progressOut () {
235
+ return clampScope(
236
+ this.progress,
237
+ this.scopeOut,
238
+ ) || 0;
239
+ }
240
+
241
+ /**
242
+ * 'move' progress
243
+ */
244
+ get progressMove () {
245
+ return clampScope(
246
+ this.progress,
247
+ this.scopeMove,
248
+ ) || 0;
249
+ }
250
+
251
+
252
+
253
+ /**
254
+ * Handle scroll event
255
+ */
256
+ protected _handleScroll () {
257
+ // calculate scopes
258
+ const scrollData = getScrollValues(this.scrollContainer);
259
+ if (!scrollData) {
260
+ return;
261
+ }
262
+ this._render(scrollData);
263
+ }
264
+
265
+
266
+
267
+ /**
268
+ * Resize the scene
269
+ */
270
+ public resize () {
271
+ // calculate scopes
272
+ const scrollData = getScrollValues(this.scrollContainer);
273
+ if (!scrollData) {
274
+ return;
275
+ }
276
+
277
+ // get sizes
278
+ const bounding = this.section.getBoundingClientRect();
279
+ const vHeight = this._app.viewport.height;
280
+
281
+ // calculate scroll scope
282
+ const inStart = scrollData.scrollTop + bounding.top - vHeight;
283
+ const moveEnd = scrollData.scrollTop + bounding.top + bounding.height;
284
+ const scrollLine = moveEnd - inStart;
285
+ this._scopeScroll = [inStart, moveEnd];
286
+
287
+ // calculate scopes
288
+ this._scopeIn = [0, vHeight / scrollLine];
289
+ this._scopeOut = [1 - vHeight / scrollLine, 1];
290
+ this._scopeMove = [this._scopeIn[1], this._scopeOut[0]];
291
+
292
+ // launch callbacks
293
+ this.callbacks.tbt('resize', false);
294
+
295
+ // render the scene
296
+ this._render(scrollData, true);
297
+ }
298
+
299
+ /**
300
+ * Render the scene
301
+ */
302
+ protected _render (
303
+ scrollData: ReturnType<typeof getScrollValues>,
304
+ force = false,
305
+ ) {
306
+ const canRender = this._canRender(scrollData, force);
307
+ if (!canRender) {
308
+ return;
309
+ }
310
+ this.callbacks.tbt('render', false);
311
+ }
312
+
313
+ /**
314
+ * Check if the section can be rendered
315
+ */
316
+ protected _canRender (
317
+ scrollData: ReturnType<typeof getScrollValues>,
318
+ force = false,
319
+ ) {
320
+ if (!scrollData) {
321
+ return false;
322
+ }
323
+ const { scrollTop } = scrollData;
324
+ const prevProgress = this.progress;
325
+
326
+ const progress = clampScope(
327
+ scrollTop,
328
+ [this._scopeScroll[0], this._scopeScroll[1]],
329
+ ) || 0;
330
+ this.progress = progress;
331
+
332
+ return force || (progress !== prevProgress);
333
+ }
334
+
335
+
336
+ /**
337
+ * Destroy the module
338
+ */
339
+ protected _destroy () {
340
+ super._destroy();
341
+ this._scrollEvent?.remove();
342
+ this._loadedEvent?.cancel();
343
+ }
344
+ }
@@ -1,8 +1,6 @@
1
- export interface ScrollableElement {
2
- scrollTop: number;
3
- scrollTo(options: ScrollToOptions): void;
4
- scrollTo(x: number, y: number): void;
5
- scrollLeft: number;
1
+ import { ScrollLike } from '../../utils/types/general';
2
+
3
+ export interface ScrollableElement extends ScrollLike {
6
4
  scrollWidth: number;
7
5
  scrollHeight: number;
8
6
  clientWidth: number;
package/src/ts/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as common from './utils/common';
2
2
  import * as listeners from './utils/listeners';
3
3
  import * as math from './utils/math';
4
+ import * as scroll from './utils/scroll';
4
5
 
5
6
  import * as GeneralTypes from './utils/types/general';
6
7
 
@@ -39,6 +40,7 @@ import { ScrollEventsBase, NScrollEventsBase } from './components/scroll/scrolla
39
40
  import { ScrollView, NScrollView } from './components/scroll/scrollable/ScrollView';
40
41
  import { SmoothScrollKeyboardPlugin, NSmoothScrollKeyboardPlugin } from './components/scroll/plugins/SmoothScrollKeyboardPlugin';
41
42
  import { SmoothScrollDragPlugin, NSmoothScrollDragPlugin } from './components/scroll/plugins/SmoothScrollDragPlugin';
43
+ import { ScrollSectionProgress, NScrollSectionProgress } from './components/scroll/section/ScrollSectionProgress';
42
44
 
43
45
  import { SplitText, NSplitText } from './components/text/SplitText';
44
46
 
@@ -48,6 +50,7 @@ const utils = {
48
50
  common,
49
51
  listeners,
50
52
  math,
53
+ scroll,
51
54
  };
52
55
 
53
56
 
@@ -91,6 +94,7 @@ export {
91
94
  ScrollView, NScrollView,
92
95
  SmoothScrollKeyboardPlugin, NSmoothScrollKeyboardPlugin,
93
96
  SmoothScrollDragPlugin, NSmoothScrollDragPlugin,
97
+ ScrollSectionProgress, NScrollSectionProgress,
94
98
 
95
99
  SplitText, NSplitText,
96
100
 
@@ -0,0 +1,20 @@
1
+ import { ScrollLike } from '../types/general';
2
+
3
+ /**
4
+ * Get scroll values of a certain element
5
+ */
6
+ export default function getScrollValues (
7
+ selector: (Window | Element | ScrollLike | undefined) = window,
8
+ ) {
9
+ if (selector) {
10
+ const scrollTop = selector instanceof Window
11
+ ? selector.pageYOffset : selector.scrollTop;
12
+ const scrollLeft = selector instanceof Window
13
+ ? selector.pageXOffset : selector.scrollLeft;
14
+ return {
15
+ scrollTop,
16
+ scrollLeft,
17
+ };
18
+ }
19
+ return undefined;
20
+ }
@@ -0,0 +1,9 @@
1
+ import getValues from './getScrollValues';
2
+ import scrollTo from './scrollTo';
3
+ import scrollToElement from './scrollToElement';
4
+
5
+ export {
6
+ getValues,
7
+ scrollTo as to,
8
+ scrollToElement as toElement,
9
+ };
@@ -0,0 +1,65 @@
1
+ import { Timeline } from '../../components/timeline/Timeline';
2
+ import { ScrollLike } from '../types/general';
3
+ import getScrollValues from './getScrollValues';
4
+
5
+ interface Props {
6
+ /**
7
+ * @default window
8
+ */
9
+ container?: Window | Element | ScrollLike;
10
+ /**
11
+ * top padding
12
+ * @default 0
13
+ */
14
+ top?: number;
15
+ /**
16
+ * left padding
17
+ * @default 0
18
+ */
19
+ left?: number;
20
+ /**
21
+ * scroll duration
22
+ * @default 250
23
+ */
24
+ duration?: number;
25
+ }
26
+
27
+ /**
28
+ * Scroll to coordinates
29
+ */
30
+ export default function scrollTo ({
31
+ container = window,
32
+ top = 0,
33
+ left = 0,
34
+ duration = 250,
35
+ }: Props) {
36
+ return new Promise<void>((
37
+ resolve, reject,
38
+ ) => {
39
+ // save start values
40
+ const startValues = getScrollValues(container);
41
+ if (startValues) {
42
+ // create animation
43
+ const timeline = new Timeline({
44
+ duration,
45
+ });
46
+ timeline.addCallback('progress', (data) => {
47
+ if (container) {
48
+ container.scrollTo({
49
+ top: startValues.scrollTop
50
+ + (top - startValues.scrollTop) * data.easing,
51
+ left: startValues.scrollLeft
52
+ + (left - startValues.scrollLeft) * data.easing,
53
+ behavior: 'auto',
54
+ });
55
+ }
56
+ });
57
+ timeline.addCallback('end', () => {
58
+ resolve();
59
+ });
60
+ timeline.play();
61
+ return;
62
+ }
63
+ reject();
64
+ });
65
+ }
@@ -0,0 +1,74 @@
1
+ import { selectOne } from 'vevet-dom';
2
+ import { ScrollLike } from '../types/general';
3
+ import getScrollValues from './getScrollValues';
4
+ import clamp from '../math/clamp';
5
+ import scrollTo from './scrollTo';
6
+
7
+ interface Props {
8
+ /**
9
+ * @default window
10
+ */
11
+ container?: Window | Element | ScrollLike;
12
+ /**
13
+ * The target element
14
+ */
15
+ el: Element | string;
16
+ /**
17
+ * top padding
18
+ * @default 0
19
+ */
20
+ top?: number;
21
+ /**
22
+ * left padding
23
+ * @default 0
24
+ */
25
+ left?: number;
26
+ /**
27
+ * scroll duration
28
+ * @default 250
29
+ */
30
+ duration?: number;
31
+ }
32
+
33
+ /**
34
+ * Scroll to element
35
+ */
36
+ export default function scrollToElement ({
37
+ container = window,
38
+ el,
39
+ top = 0,
40
+ left = 0,
41
+ duration = 250,
42
+ }: Props) {
43
+ return new Promise<void>((
44
+ resolve, reject,
45
+ ) => {
46
+ const startValues = getScrollValues(container);
47
+ if (startValues) {
48
+ const element = selectOne(el);
49
+ if (element) {
50
+ const bounding = element.getBoundingClientRect();
51
+ const toTop = clamp(
52
+ bounding.top + startValues.scrollTop - top,
53
+ [0, Infinity],
54
+ );
55
+ const toLeft = clamp(
56
+ bounding.left + startValues.scrollLeft - left,
57
+ [0, Infinity],
58
+ );
59
+ scrollTo({
60
+ container,
61
+ top: toTop,
62
+ left: toLeft,
63
+ duration,
64
+ }).then(() => {
65
+ resolve();
66
+ }).catch(() => {
67
+ reject();
68
+ });
69
+ }
70
+ return;
71
+ }
72
+ reject();
73
+ });
74
+ }
@@ -5,3 +5,10 @@ export interface IRemovable {
5
5
  export interface IDestroyable {
6
6
  destroy: () => void;
7
7
  }
8
+
9
+ export interface ScrollLike {
10
+ scrollTop: number;
11
+ scrollLeft: number;
12
+ scrollTo(options: ScrollToOptions): void;
13
+ scrollTo(x: number, y: number): void;
14
+ }