@jsenv/dom 0.14.0 → 0.14.1

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/dist/jsenv_dom.js +36 -3
  2. package/package.json +1 -1
package/dist/jsenv_dom.js CHANGED
@@ -11055,12 +11055,17 @@ const MIN_CONTENT_VISIBILITY_RATIO = 0.6;
11055
11055
  const visibleRectEffect = (
11056
11056
  element,
11057
11057
  update,
11058
- { event: initialEvent = new CustomEvent("initialization") } = {},
11058
+ {
11059
+ event: initialEvent = new CustomEvent("initialization"),
11060
+ skipElementResize,
11061
+ } = {},
11059
11062
  ) => {
11060
11063
  const [teardown, addTeardown] = createPubSub();
11061
11064
  const scrollContainer = getScrollContainer(element);
11062
11065
  const scrollContainerIsDocument =
11063
11066
  scrollContainer === document.documentElement;
11067
+ let lastMeasuredWidth;
11068
+ let lastMeasuredHeight;
11064
11069
  let ancestorClosedCount = 0;
11065
11070
  const check = (event) => {
11066
11071
 
@@ -11107,6 +11112,8 @@ const visibleRectEffect = (
11107
11112
 
11108
11113
  // 2. Calculate element visible width/height
11109
11114
  const { width, height } = element.getBoundingClientRect();
11115
+ lastMeasuredWidth = width;
11116
+ lastMeasuredHeight = height;
11110
11117
  const visibleAreaWidth = scrollContainer.clientWidth;
11111
11118
  const visibleAreaHeight = scrollContainer.clientHeight;
11112
11119
  const visibleAreaRight = visibleAreaLeft + visibleAreaWidth;
@@ -11293,11 +11300,37 @@ const visibleRectEffect = (
11293
11300
  });
11294
11301
  }
11295
11302
  }
11296
- {
11303
+ on_element_resize: {
11304
+ if (skipElementResize) {
11305
+ break on_element_resize;
11306
+ }
11307
+
11308
+ let isFirst = true;
11309
+ let handlingResize = false;
11297
11310
  const resizeObserver = new ResizeObserver(() => {
11298
- {
11311
+ if (isFirst) {
11312
+ isFirst = false;
11299
11313
  return;
11300
11314
  }
11315
+ if (handlingResize) {
11316
+ return;
11317
+ }
11318
+ // we use directly the result of getBoundingClientRect() instead of the resizeEntry.contentRect or resizeEntry.borderBoxSize
11319
+ // so that:
11320
+ // - We can compare the dimensions measure in the last check and the current one
11321
+ // - We don't have to check element boz-sizing to know what to compare
11322
+ // - resizeEntry.borderBoxSize browser support is not that great
11323
+ const { width, height } = element.getBoundingClientRect();
11324
+ const widthDiff = Math.abs(width - lastMeasuredWidth);
11325
+ const heightDiff = Math.abs(height - lastMeasuredHeight);
11326
+ if (widthDiff === 0 && heightDiff === 0) {
11327
+ return;
11328
+ }
11329
+ handlingResize = true;
11330
+ autoCheck(
11331
+ new CustomEvent("element_size_change", { detail: { width, height } }),
11332
+ );
11333
+ handlingResize = false;
11301
11334
  });
11302
11335
  resizeObserver.observe(element);
11303
11336
  // Temporarily disconnect ResizeObserver to prevent feedback loops eventually caused by update function
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/dom",
3
- "version": "0.14.0",
3
+ "version": "0.14.1",
4
4
  "type": "module",
5
5
  "description": "DOM utilities for writing frontend code",
6
6
  "repository": {