@cocreate/events 1.25.0 → 1.26.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/CHANGELOG.md +13 -0
- package/package.json +1 -1
- package/src/index.js +72 -23
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
# [1.26.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.25.0...v1.26.0) (2025-01-18)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* removed ovserver from local storage ([b07e5f7](https://github.com/CoCreate-app/CoCreate-events/commit/b07e5f7ef2d6f8a39dc77a56a63f078d0576f85a))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* added focus ([0b81f3e](https://github.com/CoCreate-app/CoCreate-events/commit/0b81f3edff2c254281cb5a1028d2396b3b86e9e8))
|
|
12
|
+
* intersection handling ([dea8872](https://github.com/CoCreate-app/CoCreate-events/commit/dea887246eb0489022450fd135418dc42767fd59))
|
|
13
|
+
|
|
1
14
|
# [1.25.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.24.0...v1.25.0) (2024-12-14)
|
|
2
15
|
|
|
3
16
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -40,8 +40,10 @@ const CoCreateEvents = {
|
|
|
40
40
|
this.initPrefix("selected", ["click"]);
|
|
41
41
|
this.initPrefix("onload", ["onload"]);
|
|
42
42
|
this.initPrefix("observe", ["observer"]);
|
|
43
|
+
this.initPrefix("intersection", ["intersection"]);
|
|
43
44
|
this.initPrefix("resize", ["onload", "resize"]);
|
|
44
|
-
this.initPrefix("localstorage", ["onload"
|
|
45
|
+
this.initPrefix("localstorage", ["onload"]);
|
|
46
|
+
this.initPrefix("focus", ["focus"]);
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
let customEventEls = document.querySelectorAll("[event-name]");
|
|
@@ -92,33 +94,22 @@ const CoCreateEvents = {
|
|
|
92
94
|
observer.init({
|
|
93
95
|
name: "CoCreateEventattributes",
|
|
94
96
|
observe: ["attributes", "addedNodes"],
|
|
95
|
-
attributeName: [
|
|
96
|
-
|
|
97
|
+
attributeName: [
|
|
98
|
+
`${prefix}-key`,
|
|
99
|
+
`${prefix}-value`,
|
|
100
|
+
`${prefix}-selector`,
|
|
101
|
+
`${prefix}-closest`,
|
|
102
|
+
`${prefix}-parent`,
|
|
103
|
+
`${prefix}-next`,
|
|
104
|
+
`${prefix}-previous`,
|
|
105
|
+
`${prefix}-events`
|
|
106
|
+
],
|
|
107
|
+
selector,
|
|
97
108
|
callback: function (mutation) {
|
|
98
109
|
self.initElements([mutation.target], prefix, events);
|
|
99
110
|
}
|
|
100
111
|
});
|
|
101
112
|
|
|
102
|
-
if (events && events.includes("observer")) {
|
|
103
|
-
observer.init({
|
|
104
|
-
name: "observerAttributes",
|
|
105
|
-
observe: ["attributes"],
|
|
106
|
-
attributeName: [
|
|
107
|
-
`${prefix}-key`,
|
|
108
|
-
`${prefix}-value`,
|
|
109
|
-
`${prefix}-selector`,
|
|
110
|
-
`${prefix}-closest`,
|
|
111
|
-
`${prefix}-parent`,
|
|
112
|
-
`${prefix}-next`,
|
|
113
|
-
`${prefix}-previous`
|
|
114
|
-
],
|
|
115
|
-
callback: function (mutation) {
|
|
116
|
-
// remove previous observer
|
|
117
|
-
self.initElements([mutation.target], prefix, events);
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
113
|
let elements = document.querySelectorAll(selector);
|
|
123
114
|
this.initElements(elements, prefix, events);
|
|
124
115
|
},
|
|
@@ -221,10 +212,68 @@ const CoCreateEvents = {
|
|
|
221
212
|
resizeObserver.observe(el);
|
|
222
213
|
}
|
|
223
214
|
|
|
215
|
+
if (events.includes("intersection")) {
|
|
216
|
+
const rootSelector = el.getAttribute(`${prefix}-root`);
|
|
217
|
+
const rootMargin =
|
|
218
|
+
el.getAttribute(`${prefix}-root-margin`) || "0px";
|
|
219
|
+
const threshold = el.getAttribute(`${prefix}-threshold`)
|
|
220
|
+
? JSON.parse(el.getAttribute(`${prefix}-threshold`))
|
|
221
|
+
: [0];
|
|
222
|
+
const trigger = el.getAttribute(`${prefix}-trigger`) || "both"; // Default to "both"
|
|
223
|
+
|
|
224
|
+
// Resolve root from the selector or use null for viewport
|
|
225
|
+
const root = rootSelector
|
|
226
|
+
? document.querySelector(rootSelector)
|
|
227
|
+
: null;
|
|
228
|
+
|
|
229
|
+
// Validate and filter threshold values
|
|
230
|
+
const validThreshold = Array.isArray(threshold)
|
|
231
|
+
? threshold.filter((value) => value >= 0 && value <= 1)
|
|
232
|
+
: [0];
|
|
233
|
+
|
|
234
|
+
// Create IntersectionObserver
|
|
235
|
+
const intersectionObserver = new IntersectionObserver(
|
|
236
|
+
(entries) => {
|
|
237
|
+
entries.forEach((entry) => {
|
|
238
|
+
const isVisible = entry.isIntersecting;
|
|
239
|
+
|
|
240
|
+
// Determine if __updateElements should be called based on trigger
|
|
241
|
+
// if (
|
|
242
|
+
// (trigger === "visible" && isVisible) ||
|
|
243
|
+
// (trigger === "not-visible" && !isVisible) ||
|
|
244
|
+
// trigger === "both"
|
|
245
|
+
// ) {
|
|
246
|
+
if (isVisible) {
|
|
247
|
+
// Set an attribute to reflect visibility state
|
|
248
|
+
el.setAttribute(
|
|
249
|
+
`${prefix}-is-visible`,
|
|
250
|
+
isVisible ? "true" : "false"
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
// Call __updateElements
|
|
254
|
+
self.__updateElements(
|
|
255
|
+
el,
|
|
256
|
+
prefix
|
|
257
|
+
// {
|
|
258
|
+
// isVisible,
|
|
259
|
+
// intersectionRatio:
|
|
260
|
+
// entry.intersectionRatio
|
|
261
|
+
// }
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
},
|
|
266
|
+
{ root, rootMargin, threshold: validThreshold }
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
intersectionObserver.observe(el);
|
|
270
|
+
}
|
|
271
|
+
|
|
224
272
|
for (let i = 0; i < events.length; i++) {
|
|
225
273
|
if (
|
|
226
274
|
events[i] !== "onload" &&
|
|
227
275
|
events[i] !== "observer" &&
|
|
276
|
+
events[i] !== "intersection" &&
|
|
228
277
|
events[i] !== "resize"
|
|
229
278
|
) {
|
|
230
279
|
el.removeEventListener(events[i], eventFunction);
|