element-vir 23.1.2 → 23.2.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.
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { MaybePromise } from '@augment-vir/common';
|
|
2
|
+
import { PartInfo } from '../../lit-exports/all-lit-exports.js';
|
|
3
|
+
/**
|
|
4
|
+
* Callback called by the {@link onIntersect} directive.
|
|
5
|
+
*
|
|
6
|
+
* @category Internal
|
|
7
|
+
*/
|
|
8
|
+
export type OnIntersectCallback = (params: {
|
|
9
|
+
entry: IntersectionObserverEntry;
|
|
10
|
+
allEntries: IntersectionObserverEntry[];
|
|
11
|
+
observer: IntersectionObserver;
|
|
12
|
+
element: Element;
|
|
13
|
+
}) => MaybePromise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Options used for the {@link onIntersect} directive.
|
|
16
|
+
*
|
|
17
|
+
* @category Internal
|
|
18
|
+
*/
|
|
19
|
+
export type OnIntersectOptions = IntersectionObserverInit;
|
|
20
|
+
/**
|
|
21
|
+
* A directive that fires its listener any time the element's configured "intersection" is crossed.
|
|
22
|
+
* This is commonly use for detecting when an element has scrolled into view. This uses the
|
|
23
|
+
* [built-in `IntersectionObserver`
|
|
24
|
+
* API](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/IntersectionObserver), so it
|
|
25
|
+
* is very efficient.
|
|
26
|
+
*
|
|
27
|
+
* @category Directives
|
|
28
|
+
* @example
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* import {html, defineElementNoInputs, onIntersect} from 'element-vir';
|
|
32
|
+
*
|
|
33
|
+
* const MyElement = defineElementNoInputs({
|
|
34
|
+
* tagName: 'my-element',
|
|
35
|
+
* render() {
|
|
36
|
+
* return html`
|
|
37
|
+
* <div
|
|
38
|
+
* ${onIntersect({threshold: 1}, ({element, entry}) => {
|
|
39
|
+
* if (entry.isIntersecting) {
|
|
40
|
+
* console.log('is intersecting!');
|
|
41
|
+
* } else {
|
|
42
|
+
* console.log('is not intersecting');
|
|
43
|
+
* }
|
|
44
|
+
* })}
|
|
45
|
+
* >
|
|
46
|
+
* Some div
|
|
47
|
+
* </div>
|
|
48
|
+
* `;
|
|
49
|
+
* },
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare const onIntersect: (options: IntersectionObserverInit, callback: OnIntersectCallback) => import("lit-html/directive.js").DirectiveResult<{
|
|
54
|
+
new (partInfo: PartInfo): {
|
|
55
|
+
element: Element | undefined;
|
|
56
|
+
options: OnIntersectOptions | undefined;
|
|
57
|
+
intersectionObserver: undefined | IntersectionObserver;
|
|
58
|
+
callback: OnIntersectCallback | undefined;
|
|
59
|
+
fireCallback(entries: IntersectionObserverEntry[], observer: IntersectionObserver): void;
|
|
60
|
+
update(partInfo: PartInfo, [options, callback,]: [OnIntersectOptions, OnIntersectCallback]): undefined;
|
|
61
|
+
render(options: OnIntersectOptions, callback: OnIntersectCallback): undefined;
|
|
62
|
+
readonly _$isConnected: boolean;
|
|
63
|
+
};
|
|
64
|
+
}>;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { assert, assertWrap, check } from '@augment-vir/assert';
|
|
2
|
+
import { directive, Directive } from '../../lit-exports/all-lit-exports.js';
|
|
3
|
+
import { assertIsElementPartInfo } from './directive-helpers.js';
|
|
4
|
+
const directiveName = 'onIntersect';
|
|
5
|
+
/**
|
|
6
|
+
* A directive that fires its listener any time the element's configured "intersection" is crossed.
|
|
7
|
+
* This is commonly use for detecting when an element has scrolled into view. This uses the
|
|
8
|
+
* [built-in `IntersectionObserver`
|
|
9
|
+
* API](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/IntersectionObserver), so it
|
|
10
|
+
* is very efficient.
|
|
11
|
+
*
|
|
12
|
+
* @category Directives
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import {html, defineElementNoInputs, onIntersect} from 'element-vir';
|
|
17
|
+
*
|
|
18
|
+
* const MyElement = defineElementNoInputs({
|
|
19
|
+
* tagName: 'my-element',
|
|
20
|
+
* render() {
|
|
21
|
+
* return html`
|
|
22
|
+
* <div
|
|
23
|
+
* ${onIntersect({threshold: 1}, ({element, entry}) => {
|
|
24
|
+
* if (entry.isIntersecting) {
|
|
25
|
+
* console.log('is intersecting!');
|
|
26
|
+
* } else {
|
|
27
|
+
* console.log('is not intersecting');
|
|
28
|
+
* }
|
|
29
|
+
* })}
|
|
30
|
+
* >
|
|
31
|
+
* Some div
|
|
32
|
+
* </div>
|
|
33
|
+
* `;
|
|
34
|
+
* },
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export const onIntersect = directive(class extends Directive {
|
|
39
|
+
element;
|
|
40
|
+
options;
|
|
41
|
+
intersectionObserver;
|
|
42
|
+
callback;
|
|
43
|
+
constructor(partInfo) {
|
|
44
|
+
super(partInfo);
|
|
45
|
+
assertIsElementPartInfo(partInfo, directiveName);
|
|
46
|
+
}
|
|
47
|
+
fireCallback(entries, observer) {
|
|
48
|
+
assert.isLengthAtLeast(entries, 1);
|
|
49
|
+
void this.callback?.({
|
|
50
|
+
element: assertWrap.isDefined(this.element),
|
|
51
|
+
allEntries: entries,
|
|
52
|
+
observer,
|
|
53
|
+
entry: entries[0],
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
update(partInfo, [options, callback,]) {
|
|
57
|
+
assertIsElementPartInfo(partInfo, directiveName);
|
|
58
|
+
this.callback = callback;
|
|
59
|
+
let needsObserving = false;
|
|
60
|
+
const newOptions = options;
|
|
61
|
+
const oldOptions = this.options;
|
|
62
|
+
if (!this.intersectionObserver ||
|
|
63
|
+
!oldOptions ||
|
|
64
|
+
!check.entriesEqual(newOptions, oldOptions)) {
|
|
65
|
+
this.options = options;
|
|
66
|
+
this.intersectionObserver?.disconnect();
|
|
67
|
+
this.intersectionObserver = new IntersectionObserver((entries, observer) => this.fireCallback(entries, observer), options);
|
|
68
|
+
needsObserving = true;
|
|
69
|
+
}
|
|
70
|
+
const newElement = partInfo.element;
|
|
71
|
+
const oldElement = this.element;
|
|
72
|
+
// if the element changes we need to observe the new one
|
|
73
|
+
if (newElement !== oldElement) {
|
|
74
|
+
this.element = newElement;
|
|
75
|
+
if (oldElement) {
|
|
76
|
+
this.intersectionObserver.unobserve(oldElement);
|
|
77
|
+
}
|
|
78
|
+
needsObserving = true;
|
|
79
|
+
}
|
|
80
|
+
if (needsObserving) {
|
|
81
|
+
this.intersectionObserver.observe(newElement);
|
|
82
|
+
}
|
|
83
|
+
return this.render(options, callback);
|
|
84
|
+
}
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
86
|
+
render(options, callback) {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export * from './declarative-element/directives/is-resolved.directive.js';
|
|
|
12
12
|
export * from './declarative-element/directives/listen.directive.js';
|
|
13
13
|
export * from './declarative-element/directives/on-dom-created.directive.js';
|
|
14
14
|
export * from './declarative-element/directives/on-dom-rendered.directive.js';
|
|
15
|
+
export * from './declarative-element/directives/on-intersect.directive.js';
|
|
15
16
|
export * from './declarative-element/directives/on-resize.directive.js';
|
|
16
17
|
export * from './declarative-element/directives/render-async.directive.js';
|
|
17
18
|
export * from './declarative-element/directives/render-if.directive.js';
|
package/dist/index.js
CHANGED
|
@@ -12,6 +12,7 @@ export * from './declarative-element/directives/is-resolved.directive.js';
|
|
|
12
12
|
export * from './declarative-element/directives/listen.directive.js';
|
|
13
13
|
export * from './declarative-element/directives/on-dom-created.directive.js';
|
|
14
14
|
export * from './declarative-element/directives/on-dom-rendered.directive.js';
|
|
15
|
+
export * from './declarative-element/directives/on-intersect.directive.js';
|
|
15
16
|
export * from './declarative-element/directives/on-resize.directive.js';
|
|
16
17
|
export * from './declarative-element/directives/render-async.directive.js';
|
|
17
18
|
export * from './declarative-element/directives/render-if.directive.js';
|