@inertiaui/vanilla 1.3.0 → 1.3.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/index.js +456 -219
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1232,23 +1232,187 @@ function focusFirstEnabledElement(elements) {
1232
1232
  return false;
1233
1233
  }
1234
1234
  //#endregion
1235
- //#region src/position.ts
1235
+ //#region src/position/style.ts
1236
+ function setStyleProperty(style, property, value) {
1237
+ if (style.getPropertyValue(property) !== value) style.setProperty(property, value);
1238
+ }
1239
+ function removeStyleProperty(style, property) {
1240
+ if (style.getPropertyValue(property) !== "") style.removeProperty(property);
1241
+ }
1242
+ function captureStyleProperty(style, property) {
1243
+ return {
1244
+ value: style.getPropertyValue(property),
1245
+ priority: style.getPropertyPriority(property)
1246
+ };
1247
+ }
1248
+ function restoreStyleProperty(style, property, stored) {
1249
+ if (stored.value === "") {
1250
+ removeStyleProperty(style, property);
1251
+ return;
1252
+ }
1253
+ style.setProperty(property, stored.value, stored.priority);
1254
+ }
1255
+ //#endregion
1256
+ //#region src/position/anchor.ts
1236
1257
  var ANCHOR_CLASS_PREFIX = "iui-anchor-";
1237
1258
  var _supportsAnchor = null;
1259
+ var _supportsAnchorFlip = null;
1238
1260
  var _supportsTopLayerAnchor = null;
1239
1261
  var _supportsTopLayerAnchorSizing = null;
1262
+ var _isWebKit = null;
1240
1263
  function supportsAnchorPositioning() {
1241
1264
  if (_supportsAnchor === null) _supportsAnchor = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("anchor-name", "--a") && CSS.supports("position-anchor", "--a") && CSS.supports("top", "anchor(bottom)");
1242
1265
  return _supportsAnchor;
1243
1266
  }
1244
1267
  function supportsTopLayerAnchorPositioning() {
1245
- if (_supportsTopLayerAnchor === null) _supportsTopLayerAnchor = supportsAnchorPositioning() && CSS.supports("width", "anchor-size(width)");
1268
+ if (_supportsTopLayerAnchor === null) _supportsTopLayerAnchor = supportsAnchorPositioning() && CSS.supports("width", "anchor-size(width)") && !isWebKit();
1246
1269
  return _supportsTopLayerAnchor;
1247
1270
  }
1271
+ function isWebKit() {
1272
+ if (_isWebKit === null) _isWebKit = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("-webkit-backdrop-filter", "none");
1273
+ return _isWebKit;
1274
+ }
1275
+ function supportsAnchorFlip() {
1276
+ if (_supportsAnchorFlip === null) _supportsAnchorFlip = supportsAnchorPositioning() && (CSS.supports("position-try-fallbacks", "flip-block") || CSS.supports("position-try", "flip-block"));
1277
+ return _supportsAnchorFlip;
1278
+ }
1248
1279
  function supportsTopLayerAnchorSizing() {
1249
1280
  if (_supportsTopLayerAnchorSizing === null) _supportsTopLayerAnchorSizing = supportsTopLayerAnchorPositioning() && CSS.supports("height", "max-content") && CSS.supports("max-height", "stretch");
1250
1281
  return _supportsTopLayerAnchorSizing;
1251
1282
  }
1283
+ var anchorStyleId = "inertiaui-anchor-styles";
1284
+ function ensureAnchorStylesheet() {
1285
+ let style = document.getElementById(anchorStyleId);
1286
+ if (!style) {
1287
+ style = document.createElement("style");
1288
+ style.id = anchorStyleId;
1289
+ document.head.appendChild(style);
1290
+ }
1291
+ return style.sheet;
1292
+ }
1293
+ function findAnchorClass(element) {
1294
+ return Array.from(element.classList).find((c) => c.startsWith(ANCHOR_CLASS_PREFIX));
1295
+ }
1296
+ function removeAnchorRule(className) {
1297
+ const style = document.getElementById(anchorStyleId);
1298
+ if (!style?.sheet) return;
1299
+ for (let i = 0; i < style.sheet.cssRules.length; i++) if (style.sheet.cssRules[i].selectorText === `.${className}`) {
1300
+ style.sheet.deleteRule(i);
1301
+ return;
1302
+ }
1303
+ }
1304
+ function setupAnchorPositioning(reference, floating, placement, offset, flip) {
1305
+ const id = generateId(ANCHOR_CLASS_PREFIX);
1306
+ const anchorName = `--${id}`;
1307
+ reference.style.anchorName = anchorName;
1308
+ floating.style.positionAnchor = anchorName;
1309
+ floating.style.position = "fixed";
1310
+ const sheet = ensureAnchorStylesheet();
1311
+ const className = id;
1312
+ const positionRules = getAnchorPositionRules(placement, offset);
1313
+ let tryFallback = "";
1314
+ if (flip) tryFallback = `position-try-fallbacks: flip-block, flip-inline; position-try: flip-block, flip-inline;`;
1315
+ const rule = `.${className} { ${positionRules} ${tryFallback} }`;
1316
+ sheet.insertRule(rule, sheet.cssRules.length);
1317
+ floating.classList.add(className);
1318
+ return className;
1319
+ }
1320
+ function cleanupAnchorPositioning(reference, floating, className) {
1321
+ reference.style.anchorName = "";
1322
+ floating.style.positionAnchor = "";
1323
+ floating.style.position = "";
1324
+ floating.classList.remove(className);
1325
+ removeAnchorRule(className);
1326
+ }
1327
+ function detachAnchorPositioning(reference, floating) {
1328
+ reference.style.removeProperty("anchor-name");
1329
+ floating.style.removeProperty("position-anchor");
1330
+ for (const className of Array.from(floating.classList)) if (className.startsWith(ANCHOR_CLASS_PREFIX)) {
1331
+ floating.classList.remove(className);
1332
+ removeAnchorRule(className);
1333
+ }
1334
+ }
1335
+ function getAnchorPositionRules(placement, offset) {
1336
+ const [side, alignment] = placement.split("-");
1337
+ let positionRules = "";
1338
+ switch (side) {
1339
+ case "bottom":
1340
+ positionRules += `top: anchor(bottom);`;
1341
+ positionRules += `margin-top: ${offset}px;`;
1342
+ break;
1343
+ case "top":
1344
+ positionRules += `bottom: anchor(top);`;
1345
+ positionRules += `margin-bottom: ${offset}px;`;
1346
+ break;
1347
+ case "left":
1348
+ positionRules += `right: anchor(left);`;
1349
+ positionRules += `margin-right: ${offset}px;`;
1350
+ break;
1351
+ case "right":
1352
+ positionRules += `left: anchor(right);`;
1353
+ positionRules += `margin-left: ${offset}px;`;
1354
+ break;
1355
+ }
1356
+ if (side === "bottom" || side === "top") switch (alignment) {
1357
+ case "start":
1358
+ positionRules += `left: anchor(left);`;
1359
+ break;
1360
+ case "end":
1361
+ positionRules += `right: anchor(right);`;
1362
+ break;
1363
+ default:
1364
+ positionRules += `left: anchor(center); translate: -50% 0;`;
1365
+ break;
1366
+ }
1367
+ else switch (alignment) {
1368
+ case "start":
1369
+ positionRules += `top: anchor(top);`;
1370
+ break;
1371
+ case "end":
1372
+ positionRules += `bottom: anchor(bottom);`;
1373
+ break;
1374
+ default:
1375
+ positionRules += `top: anchor(center); translate: 0 -50%;`;
1376
+ break;
1377
+ }
1378
+ return positionRules;
1379
+ }
1380
+ function getTopLayerAnchorSizingRules(placement, viewportMargin) {
1381
+ const [side] = placement.split("-");
1382
+ if (side === "bottom") return `bottom: ${viewportMargin}px; height: max-content; max-height: stretch;`;
1383
+ if (side === "top") return `top: ${viewportMargin}px; height: max-content; max-height: stretch; align-self: end;`;
1384
+ return "";
1385
+ }
1386
+ function setupTopLayerAnchorPositioning(reference, floating, placement, offset, flip, matchReferenceWidth, maxWidth, viewportMargin, stretchToAvailableHeight) {
1387
+ const existingClass = findAnchorClass(floating);
1388
+ const existingAnchorName = existingClass ? `--${existingClass}` : "";
1389
+ const canReuseAnchor = !!existingClass && reference.style.getPropertyValue("anchor-name") === existingAnchorName && floating.style.getPropertyValue("position-anchor") === existingAnchorName;
1390
+ const className = canReuseAnchor ? existingClass : generateId(ANCHOR_CLASS_PREFIX);
1391
+ const anchorName = `--${className}`;
1392
+ const sheet = ensureAnchorStylesheet();
1393
+ if (canReuseAnchor) removeAnchorRule(className);
1394
+ else detachAnchorPositioning(reference, floating);
1395
+ reference.style.anchorName = anchorName;
1396
+ floating.style.positionAnchor = anchorName;
1397
+ removeStyleProperty(floating.style, "inset");
1398
+ removeStyleProperty(floating.style, "top");
1399
+ removeStyleProperty(floating.style, "right");
1400
+ removeStyleProperty(floating.style, "bottom");
1401
+ removeStyleProperty(floating.style, "left");
1402
+ removeStyleProperty(floating.style, "margin");
1403
+ setStyleProperty(floating.style, "position", "fixed");
1404
+ if (stretchToAvailableHeight) {
1405
+ setStyleProperty(floating.style, "height", "max-content");
1406
+ removeStyleProperty(floating.style, "max-height");
1407
+ } else removeStyleProperty(floating.style, "height");
1408
+ if (matchReferenceWidth) setStyleProperty(floating.style, "width", `min(anchor-size(width), ${maxWidth}px)`);
1409
+ const rule = `.${className} { position: fixed; inset: auto; margin: 0; ${getAnchorPositionRules(placement, offset)} ${flip ? "position-try-fallbacks: flip-block, flip-inline; position-try: flip-block, flip-inline;" : ""} ${matchReferenceWidth ? `width: min(anchor-size(width), ${maxWidth}px);` : ""} ${stretchToAvailableHeight ? getTopLayerAnchorSizingRules(placement, viewportMargin) : ""} }`;
1410
+ sheet.insertRule(rule, sheet.cssRules.length);
1411
+ floating.classList.add(className);
1412
+ return className;
1413
+ }
1414
+ //#endregion
1415
+ //#region src/position/geometry.ts
1252
1416
  function isRtl$1() {
1253
1417
  return typeof document !== "undefined" && document.documentElement.dir === "rtl";
1254
1418
  }
@@ -1314,6 +1478,11 @@ function topLayerRectOverflowsViewport(rect, viewport) {
1314
1478
  const tolerance = .5;
1315
1479
  return rect.top < viewport.top - tolerance || rect.left < viewport.left - tolerance || rect.right > viewport.right + tolerance || rect.bottom > viewport.bottom + tolerance;
1316
1480
  }
1481
+ function referenceIntersectsViewport(reference) {
1482
+ const rect = reference.getBoundingClientRect();
1483
+ const viewport = getViewportRect();
1484
+ return rect.width > 0 && rect.height > 0 && rect.bottom > viewport.top && rect.top < viewport.bottom && rect.right > viewport.left && rect.left < viewport.right;
1485
+ }
1317
1486
  function getAvailableSpace(referenceRect, viewport, side, offset, margin) {
1318
1487
  if (side === "top") return referenceRect.top - viewport.top - offset - margin;
1319
1488
  if (side === "bottom") return viewport.bottom - referenceRect.bottom - offset - margin;
@@ -1401,9 +1570,9 @@ function clampToViewport(x, y, floatingRect) {
1401
1570
  y: Math.max(margin, Math.min(y, vh - floatingRect.height - margin))
1402
1571
  };
1403
1572
  }
1404
- function computeWithFallback(reference, floating, options = {}) {
1573
+ function computeWithFallback(reference, floating, options = {}, referenceRect) {
1405
1574
  const { placement = "bottom-start", offset = 0, flip = true } = options;
1406
- const refRect = reference.getBoundingClientRect();
1575
+ const refRect = referenceRect ?? reference.getBoundingClientRect();
1407
1576
  const floatingRect = floating.getBoundingClientRect();
1408
1577
  let { x, y } = computePositionFromRect(refRect, floatingRect, placement, offset);
1409
1578
  let actualPlacement = placement;
@@ -1423,178 +1592,8 @@ function computeWithFallback(reference, floating, options = {}) {
1423
1592
  placement: actualPlacement
1424
1593
  };
1425
1594
  }
1426
- var anchorStyleId = "inertiaui-anchor-styles";
1427
- function ensureAnchorStylesheet() {
1428
- let style = document.getElementById(anchorStyleId);
1429
- if (!style) {
1430
- style = document.createElement("style");
1431
- style.id = anchorStyleId;
1432
- document.head.appendChild(style);
1433
- }
1434
- return style.sheet;
1435
- }
1436
- function findAnchorClass(element) {
1437
- return Array.from(element.classList).find((c) => c.startsWith(ANCHOR_CLASS_PREFIX));
1438
- }
1439
- function removeAnchorRule(className) {
1440
- const style = document.getElementById(anchorStyleId);
1441
- if (!style?.sheet) return;
1442
- for (let i = 0; i < style.sheet.cssRules.length; i++) if (style.sheet.cssRules[i].selectorText === `.${className}`) {
1443
- style.sheet.deleteRule(i);
1444
- return;
1445
- }
1446
- }
1447
- function setupAnchorPositioning(reference, floating, placement, offset, flip) {
1448
- const id = generateId(ANCHOR_CLASS_PREFIX);
1449
- const anchorName = `--${id}`;
1450
- reference.style.anchorName = anchorName;
1451
- floating.style.positionAnchor = anchorName;
1452
- floating.style.position = "fixed";
1453
- const [side, alignment] = placement.split("-");
1454
- const sheet = ensureAnchorStylesheet();
1455
- const className = id;
1456
- let positionRules = "";
1457
- let tryFallback = "";
1458
- switch (side) {
1459
- case "bottom":
1460
- positionRules += `top: anchor(bottom);`;
1461
- positionRules += `margin-top: ${offset}px;`;
1462
- break;
1463
- case "top":
1464
- positionRules += `bottom: anchor(top);`;
1465
- positionRules += `margin-bottom: ${offset}px;`;
1466
- break;
1467
- case "left":
1468
- positionRules += `right: anchor(left);`;
1469
- positionRules += `margin-right: ${offset}px;`;
1470
- break;
1471
- case "right":
1472
- positionRules += `left: anchor(right);`;
1473
- positionRules += `margin-left: ${offset}px;`;
1474
- break;
1475
- }
1476
- if (side === "bottom" || side === "top") switch (alignment) {
1477
- case "start":
1478
- positionRules += `left: anchor(left);`;
1479
- break;
1480
- case "end":
1481
- positionRules += `right: anchor(right);`;
1482
- break;
1483
- default:
1484
- positionRules += `left: anchor(center); translate: -50% 0;`;
1485
- break;
1486
- }
1487
- else switch (alignment) {
1488
- case "start":
1489
- positionRules += `top: anchor(top);`;
1490
- break;
1491
- case "end":
1492
- positionRules += `bottom: anchor(bottom);`;
1493
- break;
1494
- default:
1495
- positionRules += `top: anchor(center); translate: 0 -50%;`;
1496
- break;
1497
- }
1498
- if (flip) tryFallback = `position-try-fallbacks: flip-block, flip-inline;`;
1499
- const rule = `.${className} { ${positionRules} ${tryFallback} }`;
1500
- sheet.insertRule(rule, sheet.cssRules.length);
1501
- floating.classList.add(className);
1502
- return className;
1503
- }
1504
- function cleanupAnchorPositioning(reference, floating, className) {
1505
- reference.style.anchorName = "";
1506
- floating.style.positionAnchor = "";
1507
- floating.style.position = "";
1508
- floating.classList.remove(className);
1509
- removeAnchorRule(className);
1510
- }
1511
- function detachAnchorPositioning(reference, floating) {
1512
- reference.style.removeProperty("anchor-name");
1513
- floating.style.removeProperty("position-anchor");
1514
- for (const className of Array.from(floating.classList)) if (className.startsWith(ANCHOR_CLASS_PREFIX)) {
1515
- floating.classList.remove(className);
1516
- removeAnchorRule(className);
1517
- }
1518
- }
1519
- function getAnchorPositionRules(placement, offset) {
1520
- const [side, alignment] = placement.split("-");
1521
- let positionRules = "";
1522
- switch (side) {
1523
- case "bottom":
1524
- positionRules += `top: anchor(bottom);`;
1525
- positionRules += `margin-top: ${offset}px;`;
1526
- break;
1527
- case "top":
1528
- positionRules += `bottom: anchor(top);`;
1529
- positionRules += `margin-bottom: ${offset}px;`;
1530
- break;
1531
- case "left":
1532
- positionRules += `right: anchor(left);`;
1533
- positionRules += `margin-right: ${offset}px;`;
1534
- break;
1535
- case "right":
1536
- positionRules += `left: anchor(right);`;
1537
- positionRules += `margin-left: ${offset}px;`;
1538
- break;
1539
- }
1540
- if (side === "bottom" || side === "top") switch (alignment) {
1541
- case "start":
1542
- positionRules += `left: anchor(left);`;
1543
- break;
1544
- case "end":
1545
- positionRules += `right: anchor(right);`;
1546
- break;
1547
- default:
1548
- positionRules += `left: anchor(center); translate: -50% 0;`;
1549
- break;
1550
- }
1551
- else switch (alignment) {
1552
- case "start":
1553
- positionRules += `top: anchor(top);`;
1554
- break;
1555
- case "end":
1556
- positionRules += `bottom: anchor(bottom);`;
1557
- break;
1558
- default:
1559
- positionRules += `top: anchor(center); translate: 0 -50%;`;
1560
- break;
1561
- }
1562
- return positionRules;
1563
- }
1564
- function getTopLayerAnchorSizingRules(placement, viewportMargin) {
1565
- const [side] = placement.split("-");
1566
- if (side === "bottom") return `bottom: ${viewportMargin}px; height: max-content; max-height: stretch;`;
1567
- if (side === "top") return `top: ${viewportMargin}px; height: max-content; max-height: stretch; align-self: end;`;
1568
- return "";
1569
- }
1570
- function setupTopLayerAnchorPositioning(reference, floating, placement, offset, matchReferenceWidth, maxWidth, viewportMargin, stretchToAvailableHeight) {
1571
- const existingClass = findAnchorClass(floating);
1572
- const existingAnchorName = existingClass ? `--${existingClass}` : "";
1573
- const canReuseAnchor = !!existingClass && reference.style.getPropertyValue("anchor-name") === existingAnchorName && floating.style.getPropertyValue("position-anchor") === existingAnchorName;
1574
- const className = canReuseAnchor ? existingClass : generateId(ANCHOR_CLASS_PREFIX);
1575
- const anchorName = `--${className}`;
1576
- const sheet = ensureAnchorStylesheet();
1577
- if (canReuseAnchor) removeAnchorRule(className);
1578
- else detachAnchorPositioning(reference, floating);
1579
- reference.style.anchorName = anchorName;
1580
- floating.style.positionAnchor = anchorName;
1581
- floating.style.removeProperty("inset");
1582
- floating.style.removeProperty("top");
1583
- floating.style.removeProperty("right");
1584
- floating.style.removeProperty("bottom");
1585
- floating.style.removeProperty("left");
1586
- floating.style.removeProperty("margin");
1587
- floating.style.position = "fixed";
1588
- if (stretchToAvailableHeight) {
1589
- floating.style.height = "max-content";
1590
- floating.style.removeProperty("max-height");
1591
- } else floating.style.removeProperty("height");
1592
- if (matchReferenceWidth) floating.style.width = `min(anchor-size(width), ${maxWidth}px)`;
1593
- const rule = `.${className} { position: fixed; inset: auto; margin: 0; ${getAnchorPositionRules(placement, offset)} ${matchReferenceWidth ? `width: min(anchor-size(width), ${maxWidth}px);` : ""} ${stretchToAvailableHeight ? getTopLayerAnchorSizingRules(placement, viewportMargin) : ""} }`;
1594
- sheet.insertRule(rule, sheet.cssRules.length);
1595
- floating.classList.add(className);
1596
- return className;
1597
- }
1595
+ //#endregion
1596
+ //#region src/position/floating.ts
1598
1597
  var SIZE_MARGIN = 8;
1599
1598
  /**
1600
1599
  * Cap the floating element's height to the space available between its anchored
@@ -1607,8 +1606,8 @@ var SIZE_MARGIN = 8;
1607
1606
  * direction is derived from the rendered rects: a menu whose top sits above the
1608
1607
  * reference opens upward (its bottom edge is anchored); otherwise it opens downward.
1609
1608
  */
1610
- function applySize(reference, floating, offset, side) {
1611
- const refRect = reference.getBoundingClientRect();
1609
+ function applySize(reference, floating, offset, side, refRect) {
1610
+ refRect ??= reference.getBoundingClientRect();
1612
1611
  const vh = window.innerHeight;
1613
1612
  let available;
1614
1613
  if (side === "top") available = refRect.top - offset - SIZE_MARGIN;
@@ -1623,7 +1622,7 @@ function applySize(reference, floating, offset, side) {
1623
1622
  }
1624
1623
  function computePosition(reference, floating, options = {}) {
1625
1624
  const { placement = "bottom-start", offset = 0, flip = true, autoSize = false } = options;
1626
- if (supportsAnchorPositioning()) {
1625
+ if (supportsAnchorPositioning() && (!flip || supportsAnchorFlip())) {
1627
1626
  if (!findAnchorClass(floating)) setupAnchorPositioning(reference, floating, placement, offset, flip);
1628
1627
  if (autoSize) applySize(reference, floating, offset);
1629
1628
  const rect = floating.getBoundingClientRect();
@@ -1633,18 +1632,47 @@ function computePosition(reference, floating, options = {}) {
1633
1632
  placement
1634
1633
  };
1635
1634
  }
1636
- if (autoSize) applySize(reference, floating, offset, placement.split("-")[0]);
1635
+ let referenceRect;
1636
+ if (autoSize) {
1637
+ referenceRect = reference.getBoundingClientRect();
1638
+ applySize(reference, floating, offset, placement.split("-")[0], referenceRect);
1639
+ }
1637
1640
  const result = computeWithFallback(reference, floating, {
1638
1641
  placement,
1639
1642
  offset,
1640
1643
  flip
1641
- });
1644
+ }, referenceRect);
1642
1645
  floating.style.position = "fixed";
1643
1646
  floating.style.top = `${result.y}px`;
1644
1647
  floating.style.left = `${result.x}px`;
1645
- if (autoSize) applySize(reference, floating, offset, result.placement.split("-")[0]);
1648
+ if (autoSize) applySize(reference, floating, offset, result.placement.split("-")[0], referenceRect);
1646
1649
  return result;
1647
1650
  }
1651
+ function observeElementResize(reference, floating, scheduleUpdate) {
1652
+ if (typeof ResizeObserver !== "function") return () => {};
1653
+ let reobserveFrame;
1654
+ const resizeObserver = new ResizeObserver(([firstEntry]) => {
1655
+ const referenceResized = firstEntry?.target === reference;
1656
+ if (referenceResized) resizeObserver.unobserve(floating);
1657
+ scheduleUpdate();
1658
+ if (referenceResized) {
1659
+ if (reobserveFrame !== void 0) cancelAnimationFrame(reobserveFrame);
1660
+ reobserveFrame = requestAnimationFrame(() => {
1661
+ reobserveFrame = void 0;
1662
+ resizeObserver.observe(floating);
1663
+ });
1664
+ }
1665
+ });
1666
+ resizeObserver.observe(reference);
1667
+ resizeObserver.observe(floating);
1668
+ return () => {
1669
+ if (reobserveFrame !== void 0) {
1670
+ cancelAnimationFrame(reobserveFrame);
1671
+ reobserveFrame = void 0;
1672
+ }
1673
+ resizeObserver.disconnect();
1674
+ };
1675
+ }
1648
1676
  function autoUpdate(reference, floating, update) {
1649
1677
  let rafId;
1650
1678
  function scheduleUpdate() {
@@ -1655,10 +1683,11 @@ function autoUpdate(reference, floating, update) {
1655
1683
  });
1656
1684
  }
1657
1685
  window.addEventListener("resize", scheduleUpdate);
1658
- window.addEventListener("scroll", scheduleUpdate, true);
1659
- const resizeObserver = new ResizeObserver(scheduleUpdate);
1660
- resizeObserver.observe(reference);
1661
- resizeObserver.observe(floating);
1686
+ window.addEventListener("scroll", scheduleUpdate, {
1687
+ passive: true,
1688
+ capture: true
1689
+ });
1690
+ const cleanupResizeObserver = observeElementResize(reference, floating, scheduleUpdate);
1662
1691
  let cleaned = false;
1663
1692
  return function cleanup() {
1664
1693
  if (cleaned) return;
@@ -1666,25 +1695,85 @@ function autoUpdate(reference, floating, update) {
1666
1695
  if (rafId !== void 0) cancelAnimationFrame(rafId);
1667
1696
  window.removeEventListener("resize", scheduleUpdate);
1668
1697
  window.removeEventListener("scroll", scheduleUpdate, true);
1669
- resizeObserver.disconnect();
1698
+ cleanupResizeObserver();
1670
1699
  const anchorClass = findAnchorClass(floating);
1671
1700
  if (anchorClass) cleanupAnchorPositioning(reference, floating, anchorClass);
1672
1701
  };
1673
1702
  }
1703
+ //#endregion
1704
+ //#region src/position/topLayer.ts
1705
+ function observeTopLayerElementResize(reference, floating, scheduleUpdate, shouldObserveFloating) {
1706
+ if (typeof ResizeObserver !== "function") return {
1707
+ cleanup: () => {},
1708
+ syncFloatingObserver: () => {}
1709
+ };
1710
+ let floatingObserved = false;
1711
+ let floatingObserverDeferred = false;
1712
+ let reobserveFrame;
1713
+ const resizeObserver = new ResizeObserver((entries) => {
1714
+ const referenceResized = entries.some((entry) => entry.target === reference);
1715
+ if (entries.some((entry) => entry.target === floating) && !referenceResized && !shouldObserveFloating()) {
1716
+ syncFloatingObserver();
1717
+ return;
1718
+ }
1719
+ if (referenceResized && floatingObserved) {
1720
+ resizeObserver.unobserve(floating);
1721
+ floatingObserved = false;
1722
+ floatingObserverDeferred = true;
1723
+ }
1724
+ scheduleUpdate();
1725
+ if (referenceResized) {
1726
+ if (reobserveFrame !== void 0) cancelAnimationFrame(reobserveFrame);
1727
+ reobserveFrame = requestAnimationFrame(() => {
1728
+ reobserveFrame = void 0;
1729
+ floatingObserverDeferred = false;
1730
+ syncFloatingObserver();
1731
+ });
1732
+ }
1733
+ });
1734
+ const syncFloatingObserver = () => {
1735
+ if (floatingObserverDeferred) return;
1736
+ if (shouldObserveFloating()) {
1737
+ if (!floatingObserved) {
1738
+ resizeObserver.observe(floating);
1739
+ floatingObserved = true;
1740
+ }
1741
+ return;
1742
+ }
1743
+ if (floatingObserved) {
1744
+ resizeObserver.unobserve(floating);
1745
+ floatingObserved = false;
1746
+ }
1747
+ };
1748
+ resizeObserver.observe(reference);
1749
+ syncFloatingObserver();
1750
+ return {
1751
+ cleanup: () => {
1752
+ if (reobserveFrame !== void 0) {
1753
+ cancelAnimationFrame(reobserveFrame);
1754
+ reobserveFrame = void 0;
1755
+ }
1756
+ resizeObserver.disconnect();
1757
+ floatingObserved = false;
1758
+ floatingObserverDeferred = false;
1759
+ },
1760
+ syncFloatingObserver
1761
+ };
1762
+ }
1674
1763
  function positionTopLayerPopover(reference, floating, options = {}) {
1675
1764
  const { placement = "bottom-start", offset = 4, flip = true, matchReferenceWidth = false, viewportMargin = 8, anchorPositioning = true } = options;
1676
1765
  const viewport = getViewportRect();
1677
1766
  const maxWidth = Math.max(0, Math.floor(viewport.width - viewportMargin * 2));
1678
- floating.style.position = "fixed";
1679
- floating.style.boxSizing = "border-box";
1680
- floating.style.maxWidth = `${maxWidth}px`;
1681
- floating.style.overflowX = "auto";
1682
- floating.style.overflowY = "auto";
1683
- floating.style.overscrollBehavior = "contain";
1767
+ setStyleProperty(floating.style, "position", "fixed");
1768
+ setStyleProperty(floating.style, "box-sizing", "border-box");
1769
+ setStyleProperty(floating.style, "max-width", `${maxWidth}px`);
1770
+ setStyleProperty(floating.style, "overflow-x", "auto");
1771
+ setStyleProperty(floating.style, "overflow-y", "auto");
1772
+ setStyleProperty(floating.style, "overscroll-behavior", "contain");
1684
1773
  const referenceRect = reference.getBoundingClientRect();
1685
1774
  const viewportHeight = Math.max(0, Math.floor(viewport.height - viewportMargin * 2));
1686
- if (anchorPositioning && supportsTopLayerAnchorPositioning()) {
1687
- if (matchReferenceWidth) floating.style.width = `${Math.min(referenceRect.width, maxWidth)}px`;
1775
+ if (anchorPositioning && supportsTopLayerAnchorPositioning() && (!flip || supportsAnchorFlip())) {
1776
+ if (matchReferenceWidth) setStyleProperty(floating.style, "width", `${Math.min(referenceRect.width, maxWidth)}px`);
1688
1777
  const measuredRect = floating.getBoundingClientRect();
1689
1778
  const resolvedPlacement = resolveTopLayerPlacement(placement, referenceRect, {
1690
1779
  width: Math.max(measuredRect.width, floating.scrollWidth),
@@ -1692,12 +1781,12 @@ function positionTopLayerPopover(reference, floating, options = {}) {
1692
1781
  }, viewport, offset, viewportMargin, flip);
1693
1782
  const [resolvedSide] = resolvedPlacement.split("-");
1694
1783
  const stretchToAvailableHeight = (resolvedSide === "top" || resolvedSide === "bottom") && supportsTopLayerAnchorSizing();
1695
- if (stretchToAvailableHeight) floating.style.removeProperty("max-height");
1784
+ if (stretchToAvailableHeight) removeStyleProperty(floating.style, "max-height");
1696
1785
  else {
1697
1786
  const availableHeight = resolvedSide === "top" || resolvedSide === "bottom" ? Math.min(viewportHeight, Math.max(0, Math.floor(getAvailableSpace(referenceRect, viewport, resolvedSide, offset, viewportMargin)))) : viewportHeight;
1698
- floating.style.maxHeight = `${availableHeight}px`;
1787
+ setStyleProperty(floating.style, "max-height", `${availableHeight}px`);
1699
1788
  }
1700
- setupTopLayerAnchorPositioning(reference, floating, resolvedPlacement, offset, matchReferenceWidth, maxWidth, viewportMargin, stretchToAvailableHeight);
1789
+ setupTopLayerAnchorPositioning(reference, floating, resolvedPlacement, offset, flip, matchReferenceWidth, maxWidth, viewportMargin, stretchToAvailableHeight);
1701
1790
  const finalRect = floating.getBoundingClientRect();
1702
1791
  if (!topLayerRectOverflowsViewport(finalRect, viewport)) return {
1703
1792
  x: finalRect.left,
@@ -1706,12 +1795,12 @@ function positionTopLayerPopover(reference, floating, options = {}) {
1706
1795
  };
1707
1796
  }
1708
1797
  detachAnchorPositioning(reference, floating);
1709
- floating.style.inset = "auto";
1710
- floating.style.right = "auto";
1711
- floating.style.bottom = "auto";
1712
- floating.style.margin = "0";
1713
- floating.style.removeProperty("height");
1714
- if (matchReferenceWidth) floating.style.width = `${Math.min(referenceRect.width, maxWidth)}px`;
1798
+ setStyleProperty(floating.style, "inset", "auto");
1799
+ setStyleProperty(floating.style, "right", "auto");
1800
+ setStyleProperty(floating.style, "bottom", "auto");
1801
+ setStyleProperty(floating.style, "margin", "0");
1802
+ removeStyleProperty(floating.style, "height");
1803
+ if (matchReferenceWidth) setStyleProperty(floating.style, "width", `${Math.min(referenceRect.width, maxWidth)}px`);
1715
1804
  const measuredRect = floating.getBoundingClientRect();
1716
1805
  const resolvedPlacement = resolveTopLayerPlacement(placement, referenceRect, {
1717
1806
  width: Math.max(measuredRect.width, floating.scrollWidth),
@@ -1719,13 +1808,13 @@ function positionTopLayerPopover(reference, floating, options = {}) {
1719
1808
  }, viewport, offset, viewportMargin, flip);
1720
1809
  const [resolvedSide] = resolvedPlacement.split("-");
1721
1810
  const availableHeight = Math.min(viewportHeight, Math.max(0, Math.floor(getAvailableSpace(referenceRect, viewport, resolvedSide, offset, viewportMargin))));
1722
- if (resolvedSide === "top" || resolvedSide === "bottom") floating.style.maxHeight = `${availableHeight}px`;
1723
- else floating.style.maxHeight = `${viewportHeight}px`;
1811
+ if (resolvedSide === "top" || resolvedSide === "bottom") setStyleProperty(floating.style, "max-height", `${availableHeight}px`);
1812
+ else setStyleProperty(floating.style, "max-height", `${viewportHeight}px`);
1724
1813
  const floatingRect = floating.getBoundingClientRect();
1725
1814
  const coordinates = computePositionFromRect(referenceRect, floatingRect, resolvedPlacement, offset);
1726
1815
  const { x, y } = clampTopLayerCoordinates(coordinates.x, coordinates.y, floatingRect, resolvedPlacement, viewport, viewportMargin);
1727
- floating.style.left = `${Math.round(x)}px`;
1728
- floating.style.top = `${Math.round(y)}px`;
1816
+ setStyleProperty(floating.style, "left", `${Math.round(x)}px`);
1817
+ setStyleProperty(floating.style, "top", `${Math.round(y)}px`);
1729
1818
  return {
1730
1819
  x,
1731
1820
  y,
@@ -1733,30 +1822,168 @@ function positionTopLayerPopover(reference, floating, options = {}) {
1733
1822
  };
1734
1823
  }
1735
1824
  function autoUpdateTopLayerPopover(reference, floating, update) {
1736
- const cleanupAutoUpdate = autoUpdate(reference, floating, update);
1737
1825
  const visualViewport = window.visualViewport;
1738
- let rafId = null;
1826
+ const browserAnchorsPopover = () => supportsTopLayerAnchorPositioning() && findAnchorClass(floating) !== void 0;
1827
+ let rafId;
1828
+ let restoreMotionFrame;
1829
+ let scrollListenersAttached = false;
1830
+ let syncFloatingResizeObserver = () => {};
1831
+ let referenceHidden = !referenceIntersectsViewport(reference);
1832
+ let anchorParked = false;
1833
+ let parkedStyles;
1834
+ let restoreVisibilityAfterUpdate = false;
1835
+ const hasReferenceVisibilityObserver = typeof IntersectionObserver === "function";
1739
1836
  const scheduleUpdate = () => {
1740
- if (rafId !== null) return;
1837
+ if (rafId !== void 0) return;
1741
1838
  rafId = requestAnimationFrame(() => {
1742
- rafId = null;
1743
- update();
1839
+ rafId = void 0;
1840
+ syncReferenceVisibility();
1841
+ if (!anchorParked) update();
1842
+ syncScrollListeners();
1843
+ syncFloatingResizeObserver();
1844
+ if (restoreVisibilityAfterUpdate && !anchorParked) restoreParkedVisibility();
1845
+ });
1846
+ };
1847
+ const cancelMotionRestore = () => {
1848
+ if (restoreMotionFrame === void 0) return;
1849
+ cancelAnimationFrame(restoreMotionFrame);
1850
+ restoreMotionFrame = void 0;
1851
+ };
1852
+ const restoreParkedMotionStyles = () => {
1853
+ if (!parkedStyles) {
1854
+ floating.removeAttribute("data-inertiaui-top-layer-parked");
1855
+ return;
1856
+ }
1857
+ restoreStyleProperty(floating.style, "transition", parkedStyles.transition);
1858
+ restoreStyleProperty(floating.style, "animation", parkedStyles.animation);
1859
+ floating.removeAttribute("data-inertiaui-top-layer-parked");
1860
+ };
1861
+ const scheduleMotionRestore = () => {
1862
+ cancelMotionRestore();
1863
+ restoreMotionFrame = requestAnimationFrame(() => {
1864
+ restoreMotionFrame = void 0;
1865
+ restoreParkedMotionStyles();
1866
+ parkedStyles = void 0;
1867
+ });
1868
+ };
1869
+ const handleScroll = () => {
1870
+ syncReferenceVisibility();
1871
+ if (anchorParked || browserAnchorsPopover()) return;
1872
+ scheduleUpdate();
1873
+ };
1874
+ const addScrollListeners = () => {
1875
+ if (scrollListenersAttached) return;
1876
+ scrollListenersAttached = true;
1877
+ window.addEventListener("scroll", handleScroll, {
1878
+ passive: true,
1879
+ capture: true
1744
1880
  });
1881
+ visualViewport?.addEventListener("scroll", handleScroll, { passive: true });
1882
+ };
1883
+ const removeScrollListeners = () => {
1884
+ if (!scrollListenersAttached) return;
1885
+ scrollListenersAttached = false;
1886
+ window.removeEventListener("scroll", handleScroll, true);
1887
+ visualViewport?.removeEventListener("scroll", handleScroll);
1888
+ };
1889
+ const syncScrollListeners = () => {
1890
+ if (anchorParked && hasReferenceVisibilityObserver) {
1891
+ removeScrollListeners();
1892
+ return;
1893
+ }
1894
+ addScrollListeners();
1895
+ };
1896
+ const parkPopover = () => {
1897
+ if (anchorParked) return;
1898
+ if (restoreVisibilityAfterUpdate) restoreVisibilityAfterUpdate = false;
1899
+ else if (!parkedStyles) parkedStyles = {
1900
+ visibility: captureStyleProperty(floating.style, "visibility"),
1901
+ transition: captureStyleProperty(floating.style, "transition"),
1902
+ animation: captureStyleProperty(floating.style, "animation")
1903
+ };
1904
+ cancelMotionRestore();
1905
+ floating.setAttribute("data-inertiaui-top-layer-parked", "");
1906
+ floating.style.setProperty("transition", "none", "important");
1907
+ floating.style.setProperty("animation", "none", "important");
1908
+ floating.style.setProperty("visibility", "hidden");
1909
+ anchorParked = true;
1910
+ syncScrollListeners();
1911
+ syncFloatingResizeObserver();
1912
+ };
1913
+ const restoreParkedVisibility = () => {
1914
+ if (!parkedStyles) {
1915
+ restoreVisibilityAfterUpdate = false;
1916
+ return;
1917
+ }
1918
+ restoreStyleProperty(floating.style, "visibility", parkedStyles.visibility);
1919
+ restoreVisibilityAfterUpdate = false;
1920
+ scheduleMotionRestore();
1921
+ };
1922
+ const restoreParkedPopover = (schedule = true) => {
1923
+ if (!anchorParked) return;
1924
+ anchorParked = false;
1925
+ if (schedule) {
1926
+ restoreVisibilityAfterUpdate = true;
1927
+ scheduleUpdate();
1928
+ return;
1929
+ }
1930
+ restoreParkedVisibility();
1931
+ };
1932
+ const syncReferenceVisibility = (visible = referenceIntersectsViewport(reference)) => {
1933
+ referenceHidden = !visible;
1934
+ if (visible) {
1935
+ restoreParkedPopover();
1936
+ return;
1937
+ }
1938
+ parkPopover();
1745
1939
  };
1940
+ let cleanupReferenceVisibilityObserver = () => {};
1941
+ if (typeof IntersectionObserver === "function") {
1942
+ const intersectionObserver = new IntersectionObserver(([entry]) => {
1943
+ syncReferenceVisibility(Boolean(entry?.isIntersecting || (entry?.intersectionRatio ?? 0) > 0));
1944
+ });
1945
+ intersectionObserver.observe(reference);
1946
+ cleanupReferenceVisibilityObserver = () => {
1947
+ intersectionObserver.disconnect();
1948
+ };
1949
+ }
1950
+ window.addEventListener("resize", scheduleUpdate);
1746
1951
  visualViewport?.addEventListener("resize", scheduleUpdate);
1747
- visualViewport?.addEventListener("scroll", scheduleUpdate);
1952
+ syncScrollListeners();
1953
+ syncReferenceVisibility(!referenceHidden);
1954
+ const elementResizeObserver = observeTopLayerElementResize(reference, floating, scheduleUpdate, () => !anchorParked && !browserAnchorsPopover());
1955
+ syncFloatingResizeObserver = elementResizeObserver.syncFloatingObserver;
1956
+ let cleaned = false;
1748
1957
  return () => {
1749
- cleanupAutoUpdate();
1750
- if (rafId !== null) {
1958
+ if (cleaned) return;
1959
+ cleaned = true;
1960
+ if (rafId !== void 0) {
1751
1961
  cancelAnimationFrame(rafId);
1752
- rafId = null;
1962
+ rafId = void 0;
1753
1963
  }
1964
+ cancelMotionRestore();
1965
+ window.removeEventListener("resize", scheduleUpdate);
1754
1966
  visualViewport?.removeEventListener("resize", scheduleUpdate);
1755
- visualViewport?.removeEventListener("scroll", scheduleUpdate);
1967
+ cleanupReferenceVisibilityObserver();
1968
+ elementResizeObserver.cleanup();
1969
+ removeScrollListeners();
1970
+ restoreParkedPopover(false);
1971
+ cancelMotionRestore();
1972
+ restoreParkedMotionStyles();
1973
+ parkedStyles = void 0;
1974
+ const anchorClass = findAnchorClass(floating);
1975
+ if (anchorClass) cleanupAnchorPositioning(reference, floating, anchorClass);
1756
1976
  };
1757
1977
  }
1758
1978
  //#endregion
1759
1979
  //#region src/nativePopover.ts
1980
+ function restoreVisibility(popover, visibility) {
1981
+ if (visibility === void 0) {
1982
+ popover.style.removeProperty("visibility");
1983
+ return;
1984
+ }
1985
+ popover.style.setProperty("visibility", visibility);
1986
+ }
1760
1987
  function createNativePopoverDisclosure(options) {
1761
1988
  let isOpen = false;
1762
1989
  let autoUpdateCleanup = null;
@@ -1792,7 +2019,17 @@ function createNativePopoverDisclosure(options) {
1792
2019
  autoUpdateCleanup = autoUpdateTopLayerPopover(elements.reference, elements.popover, updatePosition);
1793
2020
  };
1794
2021
  const showPopover = () => {
1795
- options.popover()?.showPopover();
2022
+ const elements = getElements();
2023
+ if (!elements) return;
2024
+ const { reference, popover } = elements;
2025
+ const previousVisibility = popover.style.getPropertyValue("visibility") || void 0;
2026
+ popover.style.setProperty("visibility", "hidden");
2027
+ try {
2028
+ popover.showPopover();
2029
+ positionTopLayerPopover(reference, popover, options.position);
2030
+ } finally {
2031
+ restoreVisibility(popover, previousVisibility);
2032
+ }
1796
2033
  };
1797
2034
  const hidePopover = () => {
1798
2035
  options.popover()?.hidePopover();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inertiaui/vanilla",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "author": "Pascal Baljet <pascal@protone.media>",
5
5
  "private": false,
6
6
  "license": "MIT",