blob: 2ed0c3eff28ea1f9ed8339f75071f819cd90c0f6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
// packages/intersect/src/index.js
function src_default(Alpine) {
Alpine.directive("intersect", (el, {value, expression, modifiers}, {evaluateLater, cleanup}) => {
let evaluate = evaluateLater(expression);
let options = {
rootMargin: getRootMargin(modifiers),
threshold: getThreshhold(modifiers)
};
let observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting === (value === "leave"))
return;
evaluate();
modifiers.includes("once") && observer.disconnect();
});
}, options);
observer.observe(el);
cleanup(() => {
observer.disconnect();
});
});
}
function getThreshhold(modifiers) {
if (modifiers.includes("full"))
return 0.99;
if (modifiers.includes("half"))
return 0.5;
if (!modifiers.includes("threshold"))
return 0;
let threshold = modifiers[modifiers.indexOf("threshold") + 1];
if (threshold === "100")
return 1;
if (threshold === "0")
return 0;
return Number(`.${threshold}`);
}
function getLengthValue(rawValue) {
let match = rawValue.match(/^(-?[0-9]+)(px|%)?$/);
return match ? match[1] + (match[2] || "px") : void 0;
}
function getRootMargin(modifiers) {
const key = "margin";
const fallback = "0px 0px 0px 0px";
const index = modifiers.indexOf(key);
if (index === -1)
return fallback;
let values = [];
for (let i = 1; i < 5; i++) {
values.push(getLengthValue(modifiers[index + i] || ""));
}
values = values.filter((v) => v !== void 0);
return values.length ? values.join(" ").trim() : fallback;
}
// packages/intersect/builds/module.js
var module_default = src_default;
export {
module_default as default
};
|