svelte-intersection-observer 0.10.2 → 1.1.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/lib/index.js DELETED
@@ -1,720 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
- typeof define === 'function' && define.amd ? define(factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global["svelte-intersection-observer"] = factory());
5
- })(this, (function () { 'use strict';
6
-
7
- /** @returns {void} */
8
- function noop() {}
9
-
10
- /**
11
- * @template T
12
- * @template S
13
- * @param {T} tar
14
- * @param {S} src
15
- * @returns {T & S}
16
- */
17
- function assign(tar, src) {
18
- // @ts-ignore
19
- for (const k in src) tar[k] = src[k];
20
- return /** @type {T & S} */ (tar);
21
- }
22
-
23
- function run(fn) {
24
- return fn();
25
- }
26
-
27
- function blank_object() {
28
- return Object.create(null);
29
- }
30
-
31
- /**
32
- * @param {Function[]} fns
33
- * @returns {void}
34
- */
35
- function run_all(fns) {
36
- fns.forEach(run);
37
- }
38
-
39
- /**
40
- * @param {any} thing
41
- * @returns {thing is Function}
42
- */
43
- function is_function(thing) {
44
- return typeof thing === 'function';
45
- }
46
-
47
- /** @returns {boolean} */
48
- function safe_not_equal(a, b) {
49
- return a != a ? b == b : a !== b || (a && typeof a === 'object') || typeof a === 'function';
50
- }
51
-
52
- /** @returns {boolean} */
53
- function is_empty(obj) {
54
- return Object.keys(obj).length === 0;
55
- }
56
-
57
- function create_slot(definition, ctx, $$scope, fn) {
58
- if (definition) {
59
- const slot_ctx = get_slot_context(definition, ctx, $$scope, fn);
60
- return definition[0](slot_ctx);
61
- }
62
- }
63
-
64
- function get_slot_context(definition, ctx, $$scope, fn) {
65
- return definition[1] && fn ? assign($$scope.ctx.slice(), definition[1](fn(ctx))) : $$scope.ctx;
66
- }
67
-
68
- function get_slot_changes(definition, $$scope, dirty, fn) {
69
- if (definition[2] && fn) {
70
- const lets = definition[2](fn(dirty));
71
- if ($$scope.dirty === undefined) {
72
- return lets;
73
- }
74
- if (typeof lets === 'object') {
75
- const merged = [];
76
- const len = Math.max($$scope.dirty.length, lets.length);
77
- for (let i = 0; i < len; i += 1) {
78
- merged[i] = $$scope.dirty[i] | lets[i];
79
- }
80
- return merged;
81
- }
82
- return $$scope.dirty | lets;
83
- }
84
- return $$scope.dirty;
85
- }
86
-
87
- /** @returns {void} */
88
- function update_slot_base(
89
- slot,
90
- slot_definition,
91
- ctx,
92
- $$scope,
93
- slot_changes,
94
- get_slot_context_fn
95
- ) {
96
- if (slot_changes) {
97
- const slot_context = get_slot_context(slot_definition, ctx, $$scope, get_slot_context_fn);
98
- slot.p(slot_context, slot_changes);
99
- }
100
- }
101
-
102
- /** @returns {any[] | -1} */
103
- function get_all_dirty_from_scope($$scope) {
104
- if ($$scope.ctx.length > 32) {
105
- const dirty = [];
106
- const length = $$scope.ctx.length / 32;
107
- for (let i = 0; i < length; i++) {
108
- dirty[i] = -1;
109
- }
110
- return dirty;
111
- }
112
- return -1;
113
- }
114
-
115
- /**
116
- * @param {Node} node
117
- * @returns {void}
118
- */
119
- function detach(node) {
120
- if (node.parentNode) {
121
- node.parentNode.removeChild(node);
122
- }
123
- }
124
-
125
- /**
126
- * @param {Element} element
127
- * @returns {ChildNode[]}
128
- */
129
- function children(element) {
130
- return Array.from(element.childNodes);
131
- }
132
-
133
- /**
134
- * @template T
135
- * @param {string} type
136
- * @param {T} [detail]
137
- * @param {{ bubbles?: boolean, cancelable?: boolean }} [options]
138
- * @returns {CustomEvent<T>}
139
- */
140
- function custom_event(type, detail, { bubbles = false, cancelable = false } = {}) {
141
- return new CustomEvent(type, { detail, bubbles, cancelable });
142
- }
143
-
144
- /**
145
- * @typedef {Node & {
146
- * claim_order?: number;
147
- * hydrate_init?: true;
148
- * actual_end_child?: NodeEx;
149
- * childNodes: NodeListOf<NodeEx>;
150
- * }} NodeEx
151
- */
152
-
153
- /** @typedef {ChildNode & NodeEx} ChildNodeEx */
154
-
155
- /** @typedef {NodeEx & { claim_order: number }} NodeEx2 */
156
-
157
- /**
158
- * @typedef {ChildNodeEx[] & {
159
- * claim_info?: {
160
- * last_index: number;
161
- * total_claimed: number;
162
- * };
163
- * }} ChildNodeArray
164
- */
165
-
166
- let current_component;
167
-
168
- /** @returns {void} */
169
- function set_current_component(component) {
170
- current_component = component;
171
- }
172
-
173
- function get_current_component() {
174
- if (!current_component) throw new Error('Function called outside component initialization');
175
- return current_component;
176
- }
177
-
178
- /**
179
- * Creates an event dispatcher that can be used to dispatch [component events](/docs#template-syntax-component-directives-on-eventname).
180
- * Event dispatchers are functions that can take two arguments: `name` and `detail`.
181
- *
182
- * Component events created with `createEventDispatcher` create a
183
- * [CustomEvent](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent).
184
- * These events do not [bubble](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_bubbling_and_capture).
185
- * The `detail` argument corresponds to the [CustomEvent.detail](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/detail)
186
- * property and can contain any type of data.
187
- *
188
- * The event dispatcher can be typed to narrow the allowed event names and the type of the `detail` argument:
189
- * ```ts
190
- * const dispatch = createEventDispatcher<{
191
- * loaded: never; // does not take a detail argument
192
- * change: string; // takes a detail argument of type string, which is required
193
- * optional: number | null; // takes an optional detail argument of type number
194
- * }>();
195
- * ```
196
- *
197
- * https://svelte.dev/docs/svelte#createeventdispatcher
198
- * @template {Record<string, any>} [EventMap=any]
199
- * @returns {import('./public.js').EventDispatcher<EventMap>}
200
- */
201
- function createEventDispatcher() {
202
- const component = get_current_component();
203
- return (type, detail, { cancelable = false } = {}) => {
204
- const callbacks = component.$$.callbacks[type];
205
- if (callbacks) {
206
- // TODO are there situations where events could be dispatched
207
- // in a server (non-DOM) environment?
208
- const event = custom_event(/** @type {string} */ (type), detail, { cancelable });
209
- callbacks.slice().forEach((fn) => {
210
- fn.call(component, event);
211
- });
212
- return !event.defaultPrevented;
213
- }
214
- return true;
215
- };
216
- }
217
-
218
- const dirty_components = [];
219
- const binding_callbacks = [];
220
-
221
- let render_callbacks = [];
222
-
223
- const flush_callbacks = [];
224
-
225
- const resolved_promise = /* @__PURE__ */ Promise.resolve();
226
-
227
- let update_scheduled = false;
228
-
229
- /** @returns {void} */
230
- function schedule_update() {
231
- if (!update_scheduled) {
232
- update_scheduled = true;
233
- resolved_promise.then(flush);
234
- }
235
- }
236
-
237
- /** @returns {void} */
238
- function add_render_callback(fn) {
239
- render_callbacks.push(fn);
240
- }
241
-
242
- // flush() calls callbacks in this order:
243
- // 1. All beforeUpdate callbacks, in order: parents before children
244
- // 2. All bind:this callbacks, in reverse order: children before parents.
245
- // 3. All afterUpdate callbacks, in order: parents before children. EXCEPT
246
- // for afterUpdates called during the initial onMount, which are called in
247
- // reverse order: children before parents.
248
- // Since callbacks might update component values, which could trigger another
249
- // call to flush(), the following steps guard against this:
250
- // 1. During beforeUpdate, any updated components will be added to the
251
- // dirty_components array and will cause a reentrant call to flush(). Because
252
- // the flush index is kept outside the function, the reentrant call will pick
253
- // up where the earlier call left off and go through all dirty components. The
254
- // current_component value is saved and restored so that the reentrant call will
255
- // not interfere with the "parent" flush() call.
256
- // 2. bind:this callbacks cannot trigger new flush() calls.
257
- // 3. During afterUpdate, any updated components will NOT have their afterUpdate
258
- // callback called a second time; the seen_callbacks set, outside the flush()
259
- // function, guarantees this behavior.
260
- const seen_callbacks = new Set();
261
-
262
- let flushidx = 0; // Do *not* move this inside the flush() function
263
-
264
- /** @returns {void} */
265
- function flush() {
266
- // Do not reenter flush while dirty components are updated, as this can
267
- // result in an infinite loop. Instead, let the inner flush handle it.
268
- // Reentrancy is ok afterwards for bindings etc.
269
- if (flushidx !== 0) {
270
- return;
271
- }
272
- const saved_component = current_component;
273
- do {
274
- // first, call beforeUpdate functions
275
- // and update components
276
- try {
277
- while (flushidx < dirty_components.length) {
278
- const component = dirty_components[flushidx];
279
- flushidx++;
280
- set_current_component(component);
281
- update(component.$$);
282
- }
283
- } catch (e) {
284
- // reset dirty state to not end up in a deadlocked state and then rethrow
285
- dirty_components.length = 0;
286
- flushidx = 0;
287
- throw e;
288
- }
289
- set_current_component(null);
290
- dirty_components.length = 0;
291
- flushidx = 0;
292
- while (binding_callbacks.length) binding_callbacks.pop()();
293
- // then, once components are updated, call
294
- // afterUpdate functions. This may cause
295
- // subsequent updates...
296
- for (let i = 0; i < render_callbacks.length; i += 1) {
297
- const callback = render_callbacks[i];
298
- if (!seen_callbacks.has(callback)) {
299
- // ...so guard against infinite loops
300
- seen_callbacks.add(callback);
301
- callback();
302
- }
303
- }
304
- render_callbacks.length = 0;
305
- } while (dirty_components.length);
306
- while (flush_callbacks.length) {
307
- flush_callbacks.pop()();
308
- }
309
- update_scheduled = false;
310
- seen_callbacks.clear();
311
- set_current_component(saved_component);
312
- }
313
-
314
- /** @returns {void} */
315
- function update($$) {
316
- if ($$.fragment !== null) {
317
- $$.update();
318
- run_all($$.before_update);
319
- const dirty = $$.dirty;
320
- $$.dirty = [-1];
321
- $$.fragment && $$.fragment.p($$.ctx, dirty);
322
- $$.after_update.forEach(add_render_callback);
323
- }
324
- }
325
-
326
- /**
327
- * Useful for example to execute remaining `afterUpdate` callbacks before executing `destroy`.
328
- * @param {Function[]} fns
329
- * @returns {void}
330
- */
331
- function flush_render_callbacks(fns) {
332
- const filtered = [];
333
- const targets = [];
334
- render_callbacks.forEach((c) => (fns.indexOf(c) === -1 ? filtered.push(c) : targets.push(c)));
335
- targets.forEach((c) => c());
336
- render_callbacks = filtered;
337
- }
338
-
339
- const outroing = new Set();
340
-
341
- /**
342
- * @type {Outro}
343
- */
344
- let outros;
345
-
346
- /**
347
- * @param {import('./private.js').Fragment} block
348
- * @param {0 | 1} [local]
349
- * @returns {void}
350
- */
351
- function transition_in(block, local) {
352
- if (block && block.i) {
353
- outroing.delete(block);
354
- block.i(local);
355
- }
356
- }
357
-
358
- /**
359
- * @param {import('./private.js').Fragment} block
360
- * @param {0 | 1} local
361
- * @param {0 | 1} [detach]
362
- * @param {() => void} [callback]
363
- * @returns {void}
364
- */
365
- function transition_out(block, local, detach, callback) {
366
- if (block && block.o) {
367
- if (outroing.has(block)) return;
368
- outroing.add(block);
369
- outros.c.push(() => {
370
- outroing.delete(block);
371
- if (callback) {
372
- if (detach) block.d(1);
373
- callback();
374
- }
375
- });
376
- block.o(local);
377
- } else if (callback) {
378
- callback();
379
- }
380
- }
381
-
382
- /** @typedef {1} INTRO */
383
- /** @typedef {0} OUTRO */
384
- /** @typedef {{ direction: 'in' | 'out' | 'both' }} TransitionOptions */
385
- /** @typedef {(node: Element, params: any, options: TransitionOptions) => import('../transition/public.js').TransitionConfig} TransitionFn */
386
-
387
- /**
388
- * @typedef {Object} Outro
389
- * @property {number} r
390
- * @property {Function[]} c
391
- * @property {Object} p
392
- */
393
-
394
- /**
395
- * @typedef {Object} PendingProgram
396
- * @property {number} start
397
- * @property {INTRO|OUTRO} b
398
- * @property {Outro} [group]
399
- */
400
-
401
- /**
402
- * @typedef {Object} Program
403
- * @property {number} a
404
- * @property {INTRO|OUTRO} b
405
- * @property {1|-1} d
406
- * @property {number} duration
407
- * @property {number} start
408
- * @property {number} end
409
- * @property {Outro} [group]
410
- */
411
-
412
- /** @returns {void} */
413
- function mount_component(component, target, anchor) {
414
- const { fragment, after_update } = component.$$;
415
- fragment && fragment.m(target, anchor);
416
- // onMount happens before the initial afterUpdate
417
- add_render_callback(() => {
418
- const new_on_destroy = component.$$.on_mount.map(run).filter(is_function);
419
- // if the component was destroyed immediately
420
- // it will update the `$$.on_destroy` reference to `null`.
421
- // the destructured on_destroy may still reference to the old array
422
- if (component.$$.on_destroy) {
423
- component.$$.on_destroy.push(...new_on_destroy);
424
- } else {
425
- // Edge case - component was destroyed immediately,
426
- // most likely as a result of a binding initialising
427
- run_all(new_on_destroy);
428
- }
429
- component.$$.on_mount = [];
430
- });
431
- after_update.forEach(add_render_callback);
432
- }
433
-
434
- /** @returns {void} */
435
- function destroy_component(component, detaching) {
436
- const $$ = component.$$;
437
- if ($$.fragment !== null) {
438
- flush_render_callbacks($$.after_update);
439
- run_all($$.on_destroy);
440
- $$.fragment && $$.fragment.d(detaching);
441
- // TODO null out other refs, including component.$$ (but need to
442
- // preserve final state?)
443
- $$.on_destroy = $$.fragment = null;
444
- $$.ctx = [];
445
- }
446
- }
447
-
448
- /** @returns {void} */
449
- function make_dirty(component, i) {
450
- if (component.$$.dirty[0] === -1) {
451
- dirty_components.push(component);
452
- schedule_update();
453
- component.$$.dirty.fill(0);
454
- }
455
- component.$$.dirty[(i / 31) | 0] |= 1 << i % 31;
456
- }
457
-
458
- /** @returns {void} */
459
- function init(
460
- component,
461
- options,
462
- instance,
463
- create_fragment,
464
- not_equal,
465
- props,
466
- append_styles,
467
- dirty = [-1]
468
- ) {
469
- const parent_component = current_component;
470
- set_current_component(component);
471
- /** @type {import('./private.js').T$$} */
472
- const $$ = (component.$$ = {
473
- fragment: null,
474
- ctx: [],
475
- // state
476
- props,
477
- update: noop,
478
- not_equal,
479
- bound: blank_object(),
480
- // lifecycle
481
- on_mount: [],
482
- on_destroy: [],
483
- on_disconnect: [],
484
- before_update: [],
485
- after_update: [],
486
- context: new Map(options.context || (parent_component ? parent_component.$$.context : [])),
487
- // everything else
488
- callbacks: blank_object(),
489
- dirty,
490
- skip_bound: false,
491
- root: options.target || parent_component.$$.root
492
- });
493
- append_styles && append_styles($$.root);
494
- let ready = false;
495
- $$.ctx = instance
496
- ? instance(component, options.props || {}, (i, ret, ...rest) => {
497
- const value = rest.length ? rest[0] : ret;
498
- if ($$.ctx && not_equal($$.ctx[i], ($$.ctx[i] = value))) {
499
- if (!$$.skip_bound && $$.bound[i]) $$.bound[i](value);
500
- if (ready) make_dirty(component, i);
501
- }
502
- return ret;
503
- })
504
- : [];
505
- $$.update();
506
- ready = true;
507
- run_all($$.before_update);
508
- // `false` as a special case of no DOM component
509
- $$.fragment = create_fragment ? create_fragment($$.ctx) : false;
510
- if (options.target) {
511
- if (options.hydrate) {
512
- const nodes = children(options.target);
513
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
514
- $$.fragment && $$.fragment.l(nodes);
515
- nodes.forEach(detach);
516
- } else {
517
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
518
- $$.fragment && $$.fragment.c();
519
- }
520
- if (options.intro) transition_in(component.$$.fragment);
521
- mount_component(component, options.target, options.anchor);
522
- flush();
523
- }
524
- set_current_component(parent_component);
525
- }
526
-
527
- /**
528
- * Base class for Svelte components. Used when dev=false.
529
- *
530
- * @template {Record<string, any>} [Props=any]
531
- * @template {Record<string, any>} [Events=any]
532
- */
533
- class SvelteComponent {
534
- /**
535
- * ### PRIVATE API
536
- *
537
- * Do not use, may change at any time
538
- *
539
- * @type {any}
540
- */
541
- $$ = undefined;
542
- /**
543
- * ### PRIVATE API
544
- *
545
- * Do not use, may change at any time
546
- *
547
- * @type {any}
548
- */
549
- $$set = undefined;
550
-
551
- /** @returns {void} */
552
- $destroy() {
553
- destroy_component(this, 1);
554
- this.$destroy = noop;
555
- }
556
-
557
- /**
558
- * @template {Extract<keyof Events, string>} K
559
- * @param {K} type
560
- * @param {((e: Events[K]) => void) | null | undefined} callback
561
- * @returns {() => void}
562
- */
563
- $on(type, callback) {
564
- if (!is_function(callback)) {
565
- return noop;
566
- }
567
- const callbacks = this.$$.callbacks[type] || (this.$$.callbacks[type] = []);
568
- callbacks.push(callback);
569
- return () => {
570
- const index = callbacks.indexOf(callback);
571
- if (index !== -1) callbacks.splice(index, 1);
572
- };
573
- }
574
-
575
- /**
576
- * @param {Partial<Props>} props
577
- * @returns {void}
578
- */
579
- $set(props) {
580
- if (this.$$set && !is_empty(props)) {
581
- this.$$.skip_bound = true;
582
- this.$$set(props);
583
- this.$$.skip_bound = false;
584
- }
585
- }
586
- }
587
-
588
- /**
589
- * @typedef {Object} CustomElementPropDefinition
590
- * @property {string} [attribute]
591
- * @property {boolean} [reflect]
592
- * @property {'String'|'Boolean'|'Number'|'Array'|'Object'} [type]
593
- */
594
-
595
- // generated during release, do not modify
596
- const PUBLIC_VERSION = '4';
597
-
598
- if (typeof window !== 'undefined')
599
- // @ts-ignore
600
- (window.__svelte || (window.__svelte = { v: new Set() })).v.add(PUBLIC_VERSION);
601
-
602
- /* src/IntersectionObserver.svelte generated by Svelte v4.1.0 */
603
-
604
- const get_default_slot_changes = dirty => ({
605
- intersecting: dirty & /*intersecting*/ 1,
606
- entry: dirty & /*entry*/ 2,
607
- observer: dirty & /*observer*/ 4
608
- });
609
-
610
- const get_default_slot_context = ctx => ({
611
- intersecting: /*intersecting*/ ctx[0],
612
- entry: /*entry*/ ctx[1],
613
- observer: /*observer*/ ctx[2]
614
- });
615
-
616
- function create_fragment(ctx) {
617
- let current;
618
- const default_slot_template = /*#slots*/ ctx[9].default;
619
- const default_slot = create_slot(default_slot_template, ctx, /*$$scope*/ ctx[8], get_default_slot_context);
620
-
621
- return {
622
- c() {
623
- if (default_slot) default_slot.c();
624
- },
625
- m(target, anchor) {
626
- if (default_slot) {
627
- default_slot.m(target, anchor);
628
- }
629
-
630
- current = true;
631
- },
632
- p(ctx, [dirty]) {
633
- if (default_slot) {
634
- if (default_slot.p && (!current || dirty & /*$$scope, intersecting, entry, observer*/ 263)) {
635
- update_slot_base(
636
- default_slot,
637
- default_slot_template,
638
- ctx,
639
- /*$$scope*/ ctx[8],
640
- !current
641
- ? get_all_dirty_from_scope(/*$$scope*/ ctx[8])
642
- : get_slot_changes(default_slot_template, /*$$scope*/ ctx[8], dirty, get_default_slot_changes),
643
- get_default_slot_context
644
- );
645
- }
646
- }
647
- },
648
- i(local) {
649
- if (current) return;
650
- transition_in(default_slot, local);
651
- current = true;
652
- },
653
- o(local) {
654
- transition_out(default_slot, local);
655
- current = false;
656
- },
657
- d(detaching) {
658
- if (default_slot) default_slot.d(detaching);
659
- }
660
- };
661
- }
662
-
663
- function instance($$self, $$props, $$invalidate) {
664
- let { $$slots: slots = {}, $$scope } = $$props;
665
- let { element = null } = $$props;
666
- let { once = false } = $$props;
667
- let { intersecting = false } = $$props;
668
- let { root = null } = $$props;
669
- let { rootMargin = "0px" } = $$props;
670
- let { threshold = 0 } = $$props;
671
- let { entry = null } = $$props;
672
- let { observer = null } = $$props;
673
- createEventDispatcher();
674
-
675
- $$self.$$set = $$props => {
676
- if ('element' in $$props) $$invalidate(3, element = $$props.element);
677
- if ('once' in $$props) $$invalidate(4, once = $$props.once);
678
- if ('intersecting' in $$props) $$invalidate(0, intersecting = $$props.intersecting);
679
- if ('root' in $$props) $$invalidate(5, root = $$props.root);
680
- if ('rootMargin' in $$props) $$invalidate(6, rootMargin = $$props.rootMargin);
681
- if ('threshold' in $$props) $$invalidate(7, threshold = $$props.threshold);
682
- if ('entry' in $$props) $$invalidate(1, entry = $$props.entry);
683
- if ('observer' in $$props) $$invalidate(2, observer = $$props.observer);
684
- if ('$$scope' in $$props) $$invalidate(8, $$scope = $$props.$$scope);
685
- };
686
-
687
- return [
688
- intersecting,
689
- entry,
690
- observer,
691
- element,
692
- once,
693
- root,
694
- rootMargin,
695
- threshold,
696
- $$scope,
697
- slots
698
- ];
699
- }
700
-
701
- class IntersectionObserver_1 extends SvelteComponent {
702
- constructor(options) {
703
- super();
704
-
705
- init(this, options, instance, create_fragment, safe_not_equal, {
706
- element: 3,
707
- once: 4,
708
- intersecting: 0,
709
- root: 5,
710
- rootMargin: 6,
711
- threshold: 7,
712
- entry: 1,
713
- observer: 2
714
- });
715
- }
716
- }
717
-
718
- return IntersectionObserver_1;
719
-
720
- }));