@phun-ky/speccer 9.0.5 → 9.0.7

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 (2) hide show
  1. package/README.md +34 -8
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -143,23 +143,49 @@ If no attribute is applied, it will default to `data-dom`, as in, it will initia
143
143
 
144
144
  ### Lazy
145
145
 
146
- If you're importing speccer instead of with a script tag, you can use the following approach to apply lazy loading:
146
+ If you're importing speccer instead of with a script tag, [you can use the following approach](https://codepen.io/phun-ky/pen/VwRRLyY) to apply lazy loading:
147
147
 
148
148
  ```javascript
149
- import { dissect } from '@phun-ky/speccer';
149
+ import { dissect, ElementDissectionResult } from "https://esm.sh/@phun-ky/speccer";
150
+
151
+ /**
152
+ * Function to dissect an HTML element
153
+ * @param {Element} target - The element to be dissected
154
+ * @returns {Promise<ElementDissectionResult>} Promise that resolves with the dissection result
155
+ */
156
+ const dissectElement = (target: Element): Promise<ElementDissectionResult> => {
157
+ return dissect.element(target);
158
+ };
150
159
 
151
- let dissectElementObserver = new IntersectionObserver((entries, observer) => {
152
- entries.forEach((entry) => {
153
- const targets = entry.target.querySelectorAll('[data-anatomy]');
160
+ /**
161
+ * Callback function for IntersectionObserver
162
+ * @param {IntersectionObserverEntry[]} entries - Array of entries being observed
163
+ * @param {IntersectionObserver} observer - The IntersectionObserver instance
164
+ * @returns {Promise<void>} Promise that resolves when element dissection is complete
165
+ */
166
+ const intersectionCallback: IntersectionObserverCallback = async (entries, observer) => {
167
+ entries.forEach(async (entry) => {
154
168
  if (entry.intersectionRatio > 0) {
155
- targets.forEach(dissect.element);
169
+ await dissectElement(entry.target);
156
170
  observer.unobserve(entry.target);
157
171
  }
158
172
  });
159
- });
173
+ };
160
174
 
161
- document.querySelectorAll('[data-anatomy-section]').forEach((el) => {
175
+ // Creating IntersectionObserver instance with the callback
176
+ const dissectElementObserver = new IntersectionObserver(intersectionCallback);
177
+
178
+ /**
179
+ * Function to observe elements using IntersectionObserver
180
+ * @param {Element} el - The element to be observed
181
+ */
182
+ const observeElement = (el: Element): void => {
162
183
  dissectElementObserver.observe(el);
184
+ };
185
+
186
+ // Observing elements with the specified data attribute
187
+ document.querySelectorAll('[data-anatomy-section]').forEach((el) => {
188
+ observeElement(el);
163
189
  });
164
190
  ```
165
191
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phun-ky/speccer",
3
- "version": "9.0.5",
3
+ "version": "9.0.7",
4
4
  "description": "A script to annotate, show spacing specs and to display typography information in documentation/website on HTML elements",
5
5
  "main": "dist/speccer.js",
6
6
  "module": "dist/speccer.esm.js",