@surdeddd/bottom-sheet 0.3.0 → 0.4.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 (45) hide show
  1. package/dist/{BottomSheetEngine-CAymrH-P.d.cts → BottomSheetEngine-BJrH7qY3.d.cts} +39 -3
  2. package/dist/{BottomSheetEngine-DcqcuudN.d.ts → BottomSheetEngine-L05jGZvU.d.ts} +39 -3
  3. package/dist/element.cjs +490 -91
  4. package/dist/element.d.cts +23 -4
  5. package/dist/element.d.ts +23 -4
  6. package/dist/element.iife.global.js +2 -2
  7. package/dist/element.js +490 -91
  8. package/dist/index.cjs +347 -80
  9. package/dist/index.d.cts +3 -2
  10. package/dist/index.d.ts +3 -2
  11. package/dist/index.js +347 -80
  12. package/dist/integrations/formik.d.cts +1 -1
  13. package/dist/integrations/formik.d.ts +1 -1
  14. package/dist/integrations/react-hook-form.d.cts +1 -1
  15. package/dist/integrations/react-hook-form.d.ts +1 -1
  16. package/dist/overlay.cjs +13 -2
  17. package/dist/overlay.js +13 -2
  18. package/dist/preact.d.cts +1 -1
  19. package/dist/preact.d.ts +1 -1
  20. package/dist/qwik.cjs +358 -81
  21. package/dist/qwik.d.cts +4 -2
  22. package/dist/qwik.d.ts +4 -2
  23. package/dist/qwik.js +358 -81
  24. package/dist/react.cjs +451 -89
  25. package/dist/react.d.cts +29 -4
  26. package/dist/react.d.ts +29 -4
  27. package/dist/react.js +451 -89
  28. package/dist/solid.cjs +347 -80
  29. package/dist/solid.d.cts +38 -2
  30. package/dist/solid.d.ts +38 -2
  31. package/dist/solid.js +347 -80
  32. package/dist/solid.source.jsx +347 -80
  33. package/dist/styles.css +68 -55
  34. package/dist/svelte-core.cjs +352 -80
  35. package/dist/svelte-core.js +352 -80
  36. package/dist/svelte-src/BottomSheet.svelte +113 -15
  37. package/dist/svelte.cjs +474 -133
  38. package/dist/svelte.d.cts +21 -0
  39. package/dist/svelte.d.ts +21 -0
  40. package/dist/svelte.js +474 -133
  41. package/dist/vue.cjs +610 -109
  42. package/dist/vue.d.cts +51 -2
  43. package/dist/vue.d.ts +51 -2
  44. package/dist/vue.js +611 -110
  45. package/package.json +8 -8
@@ -39,6 +39,15 @@ var SheetStack = class {
39
39
  size() {
40
40
  return this.entries.length;
41
41
  }
42
+ depthOf(id) {
43
+ const open = [];
44
+ for (const entry of this.entries) {
45
+ if (!entry.isOpen || entry.isOpen()) open.push(entry);
46
+ }
47
+ const idx = open.findIndex((e) => e.id === id);
48
+ if (idx === -1) return 0;
49
+ return open.length - 1 - idx;
50
+ }
42
51
  clear() {
43
52
  this.entries = [];
44
53
  }
@@ -157,7 +166,9 @@ var PERCENT = /^(-?\d+(?:\.\d+)?)%$/;
157
166
  var resolveSnap = (point, mode, measureFit) => {
158
167
  if (typeof point === "number") return Math.max(0, point);
159
168
  if (point === "full") return viewportSize(mode);
160
- if (point === "fit") return Math.max(0, measureFit?.() ?? 0);
169
+ if (point === "fit" || point === "content") {
170
+ return Math.max(0, measureFit?.() ?? 0);
171
+ }
161
172
  const pctMatch = point.match(PERCENT);
162
173
  if (pctMatch) {
163
174
  const pct = parseFloat(pctMatch[1]) / 100;
@@ -397,6 +408,42 @@ function notifyLinkedSheets(sheets, self) {
397
408
  }
398
409
 
399
410
  // src/core/features/route.ts
411
+ var closers = [];
412
+ var handlerInstalled = false;
413
+ var suppress = 0;
414
+ var ensureHandler = () => {
415
+ if (handlerInstalled || typeof window === "undefined") return;
416
+ handlerInstalled = true;
417
+ window.addEventListener("popstate", () => {
418
+ if (suppress > 0) {
419
+ suppress -= 1;
420
+ return;
421
+ }
422
+ let target = null;
423
+ for (let i = closers.length - 1; i >= 0; i -= 1) {
424
+ const c = closers[i];
425
+ if (!c.isOpen()) continue;
426
+ if (!target) target = c;
427
+ if (c.isTop()) {
428
+ target = c;
429
+ break;
430
+ }
431
+ }
432
+ if (!target) return;
433
+ target.consumed = true;
434
+ const idx = closers.indexOf(target);
435
+ if (idx !== -1) closers.splice(idx, 1);
436
+ target.close();
437
+ });
438
+ };
439
+ var programmaticBack = () => {
440
+ suppress += 1;
441
+ try {
442
+ history.back();
443
+ } catch {
444
+ suppress = Math.max(0, suppress - 1);
445
+ }
446
+ };
400
447
  function installRoute(deps) {
401
448
  if (typeof window === "undefined") {
402
449
  return () => {
@@ -406,82 +453,81 @@ function installRoute(deps) {
406
453
  return () => {
407
454
  };
408
455
  }
409
- let routePushed = false;
410
- let closeOnBackPushed = false;
411
- let detachPopstate = null;
412
- const popOurMarker = (key, expected) => {
413
- const state = history.state;
414
- if (state && state[key] === expected) {
415
- try {
416
- history.back();
417
- } catch {
418
- }
456
+ let pushed = false;
457
+ let closer = null;
458
+ const teardownEntry = () => {
459
+ if (closer) {
460
+ const idx = closers.indexOf(closer);
461
+ if (idx !== -1) closers.splice(idx, 1);
462
+ }
463
+ const wasConsumed = closer?.consumed ?? false;
464
+ closer = null;
465
+ if (pushed && !wasConsumed) {
466
+ pushed = false;
467
+ programmaticBack();
468
+ } else {
469
+ pushed = false;
419
470
  }
420
471
  };
421
472
  const onOpen = () => {
422
- if (deps.routedTo && !detachPopstate) {
423
- const target = deps.routedTo;
424
- try {
425
- history.pushState({ __bsRouted: target }, "", target);
426
- routePushed = true;
427
- } catch {
428
- }
429
- const onPop = (event) => {
430
- if (deps.isDestroyed()) return;
431
- const state = event.state;
432
- if ((!state || state.__bsRouted !== target) && deps.getSize() > 0) {
433
- routePushed = false;
434
- void deps.close();
435
- }
436
- };
437
- window.addEventListener("popstate", onPop);
438
- detachPopstate = () => window.removeEventListener("popstate", onPop);
439
- } else if (deps.closeOnBack && !detachPopstate) {
440
- try {
473
+ if (closer) return;
474
+ closer = {
475
+ isOpen: () => !deps.isDestroyed() && deps.getSize() > 0,
476
+ isTop: () => deps.isTopSheet(),
477
+ close: () => void deps.close(),
478
+ consumed: false
479
+ };
480
+ ensureHandler();
481
+ closers.push(closer);
482
+ try {
483
+ if (deps.routedTo !== void 0) {
484
+ history.pushState({ __bsRouted: deps.sheetId }, "", deps.routedTo);
485
+ } else {
441
486
  history.pushState({ __bsSheet: deps.sheetId }, "");
442
- closeOnBackPushed = true;
443
- } catch {
444
487
  }
445
- const onPop = () => {
446
- if (deps.isDestroyed()) return;
447
- if (deps.getSize() > 0 && deps.isTopSheet()) {
448
- const state = history.state;
449
- if (state?.__bsSheet !== deps.sheetId) closeOnBackPushed = false;
450
- void deps.close();
451
- }
452
- };
453
- window.addEventListener("popstate", onPop);
454
- detachPopstate = () => window.removeEventListener("popstate", onPop);
455
- }
456
- };
457
- const onClose = () => {
458
- detachPopstate?.();
459
- detachPopstate = null;
460
- if (routePushed) {
461
- routePushed = false;
462
- if (deps.routedTo !== void 0) popOurMarker("__bsRouted", deps.routedTo);
463
- }
464
- if (closeOnBackPushed) {
465
- closeOnBackPushed = false;
466
- popOurMarker("__bsSheet", deps.sheetId);
488
+ pushed = true;
489
+ } catch {
490
+ pushed = false;
467
491
  }
468
492
  };
469
493
  const unsubscribeOpen = deps.on("open", onOpen);
470
- const unsubscribeClose = deps.on("close", onClose);
494
+ const unsubscribeClose = deps.on("close", teardownEntry);
471
495
  if (deps.getSize() > 0) onOpen();
472
496
  return () => {
473
497
  unsubscribeOpen();
474
498
  unsubscribeClose();
475
- detachPopstate?.();
476
- detachPopstate = null;
477
- if (routePushed) {
478
- routePushed = false;
479
- if (deps.routedTo !== void 0) popOurMarker("__bsRouted", deps.routedTo);
480
- }
481
- if (closeOnBackPushed) {
482
- closeOnBackPushed = false;
483
- popOurMarker("__bsSheet", deps.sheetId);
484
- }
499
+ teardownEntry();
500
+ };
501
+ }
502
+ function installRouteChange(deps) {
503
+ if (typeof window === "undefined" || typeof history === "undefined") {
504
+ return () => {
505
+ };
506
+ }
507
+ const onChange = () => {
508
+ if (deps.isDestroyed() || deps.getSize() <= 0) return;
509
+ void deps.close();
510
+ };
511
+ const wrap = (key) => {
512
+ const original = history[key];
513
+ const patched = function(...args) {
514
+ original.apply(this, args);
515
+ onChange();
516
+ };
517
+ history[key] = patched;
518
+ return () => {
519
+ if (history[key] === patched) {
520
+ history[key] = original;
521
+ }
522
+ };
523
+ };
524
+ window.addEventListener("popstate", onChange);
525
+ const restorePush = wrap("pushState");
526
+ const restoreReplace = wrap("replaceState");
527
+ return () => {
528
+ window.removeEventListener("popstate", onChange);
529
+ restorePush();
530
+ restoreReplace();
485
531
  };
486
532
  }
487
533
 
@@ -787,6 +833,7 @@ function applyOverlayPosition(ws, mode, position, inset) {
787
833
 
788
834
  // src/core/controllers/scrim-controller.ts
789
835
  var VALID_CSS_LENGTH = /^-?\d+(?:\.\d+)?(?:px|em|rem|%)$/;
836
+ var DEFAULT_SCRIM_DIM = "rgba(0, 0, 0, 0.4)";
790
837
  var ScrimController = class {
791
838
  screenComponent;
792
839
  backdrop;
@@ -829,9 +876,11 @@ var ScrimController = class {
829
876
  this.backdropRange = opts.backdropRange ?? preset?.range ?? [0, 1];
830
877
  this.screenRange = opts.screenRange ?? preset?.range ?? [0, 1];
831
878
  this.scrimTapToCloseEnabled = opts.scrimTapToClose ?? false;
879
+ const explicitColor = opts.scrimColor ?? preset?.color;
880
+ const effectiveColor = explicitColor ?? (this.scrimMode !== "off" && !this.backdrop ? DEFAULT_SCRIM_DIM : void 0);
832
881
  if (this.screenComponent) {
833
882
  this.applyScrimStyles({
834
- color: opts.scrimColor ?? preset?.color,
883
+ color: effectiveColor,
835
884
  blur: opts.scrimBlur ?? preset?.blur,
836
885
  interactive: opts.scrimInteractive ?? preset?.interactive ?? false
837
886
  });
@@ -1601,7 +1650,7 @@ var LifecycleController = class {
1601
1650
  this.releaseScrollLock = null;
1602
1651
  this.inertSiblings.remove();
1603
1652
  if (this.returnFocus) {
1604
- const target = typeof this.returnFocus === "function" ? this.returnFocus() : this.returnFocus;
1653
+ const target = typeof this.returnFocus === "function" ? this.returnFocus() : typeof this.returnFocus === "string" ? typeof document !== "undefined" ? document.querySelector(this.returnFocus) : null : this.returnFocus;
1605
1654
  target?.focus?.();
1606
1655
  }
1607
1656
  }
@@ -1767,6 +1816,7 @@ var GestureController = class {
1767
1816
  dragStartSize = 0;
1768
1817
  currentPointerKind = "touch";
1769
1818
  keyboardDismissed = false;
1819
+ dragSuppressed = false;
1770
1820
  dragPayload = { size: 0, delta: 0 };
1771
1821
  constructor(deps) {
1772
1822
  this.deps = deps;
@@ -1778,6 +1828,11 @@ var GestureController = class {
1778
1828
  if (this.detach) return this.detach;
1779
1829
  this.detach = installGestures(this.deps.handle, this.deps.mode, {
1780
1830
  onStart: (_coord, kind) => {
1831
+ if (this.deps.getDisableDrag?.()) {
1832
+ this.dragSuppressed = true;
1833
+ return;
1834
+ }
1835
+ this.dragSuppressed = false;
1781
1836
  this.deps.cancelAnimation();
1782
1837
  this.isDragging_ = true;
1783
1838
  this.dragStartSize = this.deps.getDragContext().size;
@@ -1788,6 +1843,7 @@ var GestureController = class {
1788
1843
  this.deps.emit("dragstart", { size: this.dragStartSize });
1789
1844
  },
1790
1845
  onMove: (delta) => {
1846
+ if (this.dragSuppressed) return;
1791
1847
  const ctx = this.deps.getDragContext();
1792
1848
  const { min, max } = ctx.range;
1793
1849
  const maxAxis = ctx.maxAxisSize;
@@ -1811,6 +1867,10 @@ var GestureController = class {
1811
1867
  }
1812
1868
  },
1813
1869
  onEnd: (delta, velocity, kind) => {
1870
+ if (this.dragSuppressed) {
1871
+ this.dragSuppressed = false;
1872
+ return;
1873
+ }
1814
1874
  this.isDragging_ = false;
1815
1875
  this.keyboardDismissed = false;
1816
1876
  this.deps.getRoot()?.removeAttribute("data-dragging");
@@ -1821,6 +1881,10 @@ var GestureController = class {
1821
1881
  this.deps.settleAfterDrag(delta, velocity, kind);
1822
1882
  },
1823
1883
  onCancel: () => {
1884
+ if (this.dragSuppressed) {
1885
+ this.dragSuppressed = false;
1886
+ return;
1887
+ }
1824
1888
  this.isDragging_ = false;
1825
1889
  this.keyboardDismissed = false;
1826
1890
  this.deps.getRoot()?.removeAttribute("data-dragging");
@@ -2201,6 +2265,12 @@ function resolveEngineOptions(opts, extras) {
2201
2265
  dragThreshold: opts.dragThreshold ?? DEFAULT_DRAG_THRESHOLD,
2202
2266
  rubberBandEnabled: opts.rubberBand ?? true,
2203
2267
  closeOnBack: opts.closeOnBack ?? false,
2268
+ persistent: opts.persistent ?? false,
2269
+ disableClose: opts.disableClose ?? false,
2270
+ disableDrag: opts.disableDrag ?? false,
2271
+ closeOnRouteChange: opts.closeOnRouteChange ?? false,
2272
+ radius: opts.radius,
2273
+ maxHeight: opts.maxHeight,
2204
2274
  initialAllowed,
2205
2275
  initialId,
2206
2276
  scrim: {
@@ -2227,7 +2297,8 @@ function resolveEngineOptions(opts, extras) {
2227
2297
  closeOnEscape: opts.closeOnEscape,
2228
2298
  lockBodyScroll: opts.lockBodyScroll,
2229
2299
  inertSiblings: opts.inertSiblings,
2230
- shouldApplyInertSiblings: extras?.shouldApplyInertSiblings
2300
+ shouldApplyInertSiblings: extras?.shouldApplyInertSiblings,
2301
+ returnFocus: opts.returnFocusTo
2231
2302
  }
2232
2303
  };
2233
2304
  }
@@ -2269,6 +2340,11 @@ var BottomSheetEngine = class {
2269
2340
  lifecycle;
2270
2341
  closeOnBack;
2271
2342
  routedTo;
2343
+ persistent;
2344
+ disableCloseFlag;
2345
+ disableDragFlag;
2346
+ closeOnRouteChange;
2347
+ maxHeightCap;
2272
2348
  snapPointsRaw;
2273
2349
  snaps;
2274
2350
  activeId;
@@ -2281,6 +2357,7 @@ var BottomSheetEngine = class {
2281
2357
  sizeWriteSentinel = new WriteSentinel();
2282
2358
  progressWriteSentinel = new WriteSentinel();
2283
2359
  isTopSheet = true;
2360
+ opening = false;
2284
2361
  progressPayload = {
2285
2362
  value: 0,
2286
2363
  size: 0
@@ -2338,7 +2415,8 @@ var BottomSheetEngine = class {
2338
2415
  void this.snapTo(id);
2339
2416
  },
2340
2417
  close: () => {
2341
- void this.close();
2418
+ if (!this.canDismiss()) return;
2419
+ void this.close("backdrop");
2342
2420
  }
2343
2421
  },
2344
2422
  resolved.scrim
@@ -2362,6 +2440,10 @@ var BottomSheetEngine = class {
2362
2440
  );
2363
2441
  this.closeOnBack = resolved.closeOnBack;
2364
2442
  this.routedTo = opts.routedTo;
2443
+ this.persistent = resolved.persistent;
2444
+ this.disableCloseFlag = resolved.disableClose;
2445
+ this.disableDragFlag = resolved.disableDrag;
2446
+ this.closeOnRouteChange = resolved.closeOnRouteChange;
2365
2447
  this.stackEffectEnabled = opts.stackEffect ?? false;
2366
2448
  this.snapPointsRaw = opts.snapPoints;
2367
2449
  this.persistKey = opts.persistKey;
@@ -2375,7 +2457,7 @@ var BottomSheetEngine = class {
2375
2457
  this.snapPointsRaw,
2376
2458
  resolved.initialAllowed,
2377
2459
  this.mode,
2378
- () => layoutAxis(this.mode) === "width" ? this.handle.offsetWidth : this.handle.offsetHeight,
2460
+ () => this.measureFitSize(),
2379
2461
  (maxAxisSize) => {
2380
2462
  this.element.style[layoutAxis(this.mode)] = `${maxAxisSize}px`;
2381
2463
  }
@@ -2409,6 +2491,19 @@ var BottomSheetEngine = class {
2409
2491
  io.observe(this.element);
2410
2492
  this.teardowns.add(() => io.disconnect());
2411
2493
  }
2494
+ this.installFitObserver();
2495
+ if (resolved.radius !== void 0) this.setRadius(resolved.radius);
2496
+ if (resolved.maxHeight !== void 0) this.setMaxHeight(resolved.maxHeight);
2497
+ this.applyAutoAriaLabelledBy();
2498
+ if (this.closeOnRouteChange) {
2499
+ this.teardowns.add(
2500
+ installRouteChange({
2501
+ isDestroyed: () => this.destroyed,
2502
+ getSize: () => this.size,
2503
+ close: () => this.close()
2504
+ })
2505
+ );
2506
+ }
2412
2507
  if (this.persistKey) {
2413
2508
  this.teardowns.add(installPersist(this, this.persistKey));
2414
2509
  }
@@ -2433,7 +2528,7 @@ var BottomSheetEngine = class {
2433
2528
  isTopSheet: () => this.isTopSheet,
2434
2529
  getSize: () => this.size,
2435
2530
  isDestroyed: () => this.destroyed,
2436
- close: () => this.close(),
2531
+ close: () => this.close("back"),
2437
2532
  on: (event, fn) => this.on(event, fn),
2438
2533
  sheetId: this.id
2439
2534
  })
@@ -2527,6 +2622,10 @@ var BottomSheetEngine = class {
2527
2622
  const previousSize = this.size;
2528
2623
  this.scrollCache.cache(previousId, previousSize, target.size);
2529
2624
  this.activeId = id;
2625
+ if (wasClosed && target.size > 0) {
2626
+ this.opening = true;
2627
+ sheetStack.promote(this.id);
2628
+ }
2530
2629
  if (this.animation.viewTransitionsAvailable) {
2531
2630
  this.currentViewTransition?.skipTransition?.();
2532
2631
  const vt = document.startViewTransition(() => {
@@ -2541,19 +2640,26 @@ var BottomSheetEngine = class {
2541
2640
  } else {
2542
2641
  await this.animation.animateTo(target.size, velocityPxPerMs);
2543
2642
  }
2643
+ this.opening = false;
2544
2644
  externalSignal?.removeEventListener("abort", onExternalAbort);
2545
2645
  if (signal.aborted) return;
2546
2646
  this.scrollCache.restore(id, previousSize, target.size);
2547
2647
  this.updateAriaSlider();
2548
- this.emit("snap", { id, size: target.size });
2648
+ this.emit("snap", {
2649
+ id,
2650
+ size: target.size,
2651
+ progress: this.computeProgress(target.size)
2652
+ });
2549
2653
  if (wasClosed && target.size > 0) {
2550
2654
  this.emit("open", { id });
2551
2655
  this.handleOpen();
2552
2656
  notifyLinkedSheets(this.linkedSheets, this);
2657
+ this.emit("opened", { id });
2553
2658
  }
2554
2659
  if (target.size === 0) {
2555
2660
  this.emit("close", void 0);
2556
2661
  this.handleClose();
2662
+ this.emit("closed", void 0);
2557
2663
  }
2558
2664
  }
2559
2665
  async dragTo(targetSize, velocityOrOpts = 0) {
@@ -2578,11 +2684,74 @@ var BottomSheetEngine = class {
2578
2684
  const target = id ?? this.snaps.getAllowedIds().find((a) => a !== "closed") ?? this.activeId;
2579
2685
  return this.snapTo(target);
2580
2686
  }
2581
- close() {
2687
+ close(reason = "programmatic") {
2582
2688
  if (this.destroyed) return Promise.resolve();
2689
+ if (this.disableCloseFlag) return Promise.resolve();
2690
+ if (this.size > 0 && this.emitBeforeClose(reason)) return Promise.resolve();
2583
2691
  const closedId = this.snapPointsRaw.find((p) => p.id === "closed")?.id ?? this.snaps.getAllowedIds()[0];
2584
2692
  return this.snapTo(closedId ?? this.activeId);
2585
2693
  }
2694
+ canDismiss() {
2695
+ return !this.persistent && !this.disableCloseFlag && !this.destroyed;
2696
+ }
2697
+ isTop() {
2698
+ return this.isTopSheet;
2699
+ }
2700
+ depth() {
2701
+ return sheetStack.depthOf(this.id);
2702
+ }
2703
+ expand() {
2704
+ if (this.destroyed) return Promise.resolve();
2705
+ const ids = this.allowedIdsBySize();
2706
+ const target = ids[ids.length - 1];
2707
+ if (!target) return Promise.resolve();
2708
+ return this.snapTo(target);
2709
+ }
2710
+ collapse() {
2711
+ if (this.destroyed) return Promise.resolve();
2712
+ const ids = this.allowedIdsBySize();
2713
+ const nonZero = ids.find((id) => (this.snaps.findById(id)?.size ?? 0) > 0);
2714
+ const target = nonZero ?? ids[0];
2715
+ if (!target) return Promise.resolve();
2716
+ return this.snapTo(target);
2717
+ }
2718
+ setRadius(r) {
2719
+ if (this.destroyed) return;
2720
+ this.element.style.setProperty(
2721
+ "--bs-radius",
2722
+ typeof r === "number" ? `${r}px` : r
2723
+ );
2724
+ }
2725
+ setMaxHeight(h) {
2726
+ if (this.destroyed) return;
2727
+ const value = typeof h === "number" ? `${h}px` : h;
2728
+ const axis = layoutAxis(this.mode);
2729
+ this.element.style.setProperty(
2730
+ axis === "height" ? "maxHeight" : "maxWidth",
2731
+ value
2732
+ );
2733
+ if (typeof h === "number") {
2734
+ this.maxHeightCap = h;
2735
+ this.clampToMaxHeight();
2736
+ } else if (typeof window !== "undefined") {
2737
+ const probe2 = document.createElement("div");
2738
+ probe2.style.cssText = `position:absolute;visibility:hidden;pointer-events:none;${axis === "height" ? "height" : "width"}:${value};`;
2739
+ document.body.appendChild(probe2);
2740
+ const measured = axis === "height" ? probe2.offsetHeight : probe2.offsetWidth;
2741
+ probe2.remove();
2742
+ this.maxHeightCap = measured > 0 ? measured : void 0;
2743
+ this.clampToMaxHeight();
2744
+ }
2745
+ }
2746
+ clampToMaxHeight() {
2747
+ if (this.maxHeightCap === void 0) return;
2748
+ if (this.snaps.getMaxAxisSize() > this.maxHeightCap) {
2749
+ this.snaps.setMaxAxisSize(this.maxHeightCap);
2750
+ }
2751
+ if (this.size > this.maxHeightCap && !this.gesture?.isDragging) {
2752
+ this.applySize(this.maxHeightCap);
2753
+ }
2754
+ }
2586
2755
  setAllowed(ids, snap) {
2587
2756
  if (this.destroyed) return;
2588
2757
  this.snaps.setAllowedIds(ids);
@@ -2716,8 +2885,53 @@ var BottomSheetEngine = class {
2716
2885
  }
2717
2886
  this.updateAriaSlider();
2718
2887
  }
2888
+ recompute() {
2889
+ if (this.destroyed) return;
2890
+ this.snaps.recompute();
2891
+ this.scrim.invalidateOpacityCache();
2892
+ if (this.gesture?.isDragging) return;
2893
+ const current = this.snaps.findById(this.activeId);
2894
+ if (current) {
2895
+ this.size = current.size;
2896
+ this.applySize(this.size);
2897
+ this.updateAriaSlider();
2898
+ }
2899
+ }
2900
+ measureFitSize() {
2901
+ const vertical = layoutAxis(this.mode) === "height";
2902
+ const handleSize = vertical ? this.handle.offsetHeight : this.handle.offsetWidth;
2903
+ const content = this.scrollContainer;
2904
+ const natural = content ? handleSize + (vertical ? content.scrollHeight : content.scrollWidth) : handleSize;
2905
+ if (typeof window === "undefined") return natural;
2906
+ const viewport = vertical ? window.innerHeight : window.innerWidth;
2907
+ return viewport > 0 ? Math.min(natural, viewport) : natural;
2908
+ }
2909
+ installFitObserver() {
2910
+ if (typeof ResizeObserver === "undefined") return;
2911
+ if (!this.snapPointsRaw.some((p) => p.size === "fit" || p.size === "content"))
2912
+ return;
2913
+ const targets = [this.handle];
2914
+ if (this.scrollContainer && this.scrollContainer !== this.handle) {
2915
+ targets.push(this.scrollContainer);
2916
+ }
2917
+ let raf = 0;
2918
+ const ro = new ResizeObserver(() => {
2919
+ if (raf) return;
2920
+ raf = requestAnimationFrame(() => {
2921
+ raf = 0;
2922
+ if (this.destroyed || this.gesture?.isDragging) return;
2923
+ this.recompute();
2924
+ });
2925
+ });
2926
+ for (const t of targets) ro.observe(t);
2927
+ this.teardowns.add(() => {
2928
+ ro.disconnect();
2929
+ if (raf) cancelAnimationFrame(raf);
2930
+ });
2931
+ }
2719
2932
  destroy() {
2720
2933
  this.destroyed = true;
2934
+ this.opening = false;
2721
2935
  this.currentAbort.abort();
2722
2936
  this.currentViewTransition?.skipTransition?.();
2723
2937
  this.currentViewTransition = null;
@@ -2767,6 +2981,36 @@ var BottomSheetEngine = class {
2767
2981
  frozen = true;
2768
2982
  return cancelled;
2769
2983
  }
2984
+ emitBeforeClose(reason) {
2985
+ let cancelled = false;
2986
+ let frozen = false;
2987
+ this.emit("before-close", {
2988
+ reason,
2989
+ cancel: () => {
2990
+ if (frozen) {
2991
+ console.warn(
2992
+ "[BottomSheet] before-close.cancel() called asynchronously \u2014 ignored. cancel() must be invoked synchronously inside the listener."
2993
+ );
2994
+ return;
2995
+ }
2996
+ cancelled = true;
2997
+ }
2998
+ });
2999
+ frozen = true;
3000
+ return cancelled;
3001
+ }
3002
+ applyAutoAriaLabelledBy() {
3003
+ if (typeof document === "undefined") return;
3004
+ if (!this.lifecycle.focusTrapEnabled) return;
3005
+ if (this.element.getAttribute("aria-labelledby")) return;
3006
+ const scope = this.handle ?? this.element;
3007
+ const titled = scope.querySelector("[data-bs-title]") ?? scope.querySelector("h1,h2,h3,h4,h5,h6");
3008
+ if (!titled) return;
3009
+ if (!titled.id) {
3010
+ titled.id = nextInstanceId("bs-title");
3011
+ }
3012
+ this.element.setAttribute("aria-labelledby", titled.id);
3013
+ }
2770
3014
  recomputeSnaps() {
2771
3015
  const seen = /* @__PURE__ */ new Set();
2772
3016
  for (const p of this.snapPointsRaw) {
@@ -2802,11 +3046,8 @@ var BottomSheetEngine = class {
2802
3046
  },
2803
3047
  setIsTop: (isTop) => {
2804
3048
  this.isTopSheet = isTop;
2805
- if (this.backdrop) {
2806
- this.backdrop.style.display = isTop ? "" : "none";
2807
- }
2808
3049
  },
2809
- isOpen: () => this.size > 0,
3050
+ isOpen: () => this.size > 0 || this.opening,
2810
3051
  setDepth: (depth) => this.applyStackDepth(depth)
2811
3052
  }));
2812
3053
  }
@@ -2838,6 +3079,7 @@ var BottomSheetEngine = class {
2838
3079
  buf.rubberBandEnabled = this.rubberBandEnabled;
2839
3080
  return buf;
2840
3081
  },
3082
+ getDisableDrag: () => this.disableDragFlag,
2841
3083
  cancelAnimation: () => this.animation.cancel(),
2842
3084
  applySize: (size) => this.applySize(size),
2843
3085
  animateTo: (size, velocity) => this.animation.animateTo(size, velocity),
@@ -2917,8 +3159,9 @@ var BottomSheetEngine = class {
2917
3159
  if (this.lifecycle.closeOnEscape) {
2918
3160
  const onKey = (e) => {
2919
3161
  if (this.destroyed || e.defaultPrevented) return;
3162
+ if (!this.canDismiss()) return;
2920
3163
  if (e.key === "Escape" && this.isTopSheet && this.size > 0) {
2921
- void this.close();
3164
+ void this.close("escape");
2922
3165
  }
2923
3166
  };
2924
3167
  document.addEventListener("keydown", onKey);
@@ -2981,20 +3224,31 @@ var BottomSheetEngine = class {
2981
3224
  const previousSnapSize = this.snaps.findById(previousId)?.size;
2982
3225
  this.scrollCache.cache(previousId, previousSize, target.size);
2983
3226
  this.activeId = target.id;
3227
+ if (previousSnapSize === 0 && target.size > 0) {
3228
+ this.opening = true;
3229
+ sheetStack.promote(this.id);
3230
+ }
2984
3231
  void this.animation.animateTo(target.size, velocity).then(() => {
3232
+ this.opening = false;
2985
3233
  if (signal.aborted) return;
2986
3234
  this.scrollCache.restore(target.id, previousSize, target.size);
2987
3235
  this.updateAriaSlider();
2988
- this.emit("snap", { id: target.id, size: target.size });
3236
+ this.emit("snap", {
3237
+ id: target.id,
3238
+ size: target.size,
3239
+ progress: this.computeProgress(target.size)
3240
+ });
2989
3241
  this.haptic();
2990
3242
  if (previousSnapSize === 0 && target.size > 0) {
2991
3243
  this.emit("open", { id: target.id });
2992
3244
  this.handleOpen();
2993
3245
  notifyLinkedSheets(this.linkedSheets, this);
3246
+ this.emit("opened", { id: target.id });
2994
3247
  }
2995
3248
  if (target.size === 0) {
2996
3249
  this.emit("close", void 0);
2997
3250
  this.handleClose();
3251
+ this.emit("closed", void 0);
2998
3252
  }
2999
3253
  });
3000
3254
  }
@@ -3019,6 +3273,9 @@ var BottomSheetEngine = class {
3019
3273
  if (this.anchorHost && this.anchorHost !== this.scrimParent) {
3020
3274
  this.anchorHost.style.setProperty("--bs-size", `${size}px`);
3021
3275
  }
3276
+ if (this.rootEl && this.rootEl !== this.scrimParent && this.rootEl !== this.anchorHost) {
3277
+ this.rootEl.style.setProperty("--bs-size", `${size}px`);
3278
+ }
3022
3279
  }
3023
3280
  const progress = this.computeProgress(size);
3024
3281
  const progressChanged = this.progressWriteSentinel.shouldWrite(
@@ -3030,6 +3287,9 @@ var BottomSheetEngine = class {
3030
3287
  if (this.anchorHost) {
3031
3288
  this.anchorHost.style.setProperty("--bs-progress", String(progress));
3032
3289
  }
3290
+ if (this.rootEl && this.rootEl !== this.anchorHost) {
3291
+ this.rootEl.style.setProperty("--bs-progress", String(progress));
3292
+ }
3033
3293
  }
3034
3294
  this.scrim.applyOpacity(progress, progressChanged);
3035
3295
  if (progressChanged && this.bus.listenerCount("progress") > 0) {
@@ -3063,17 +3323,24 @@ var BottomSheetEngine = class {
3063
3323
  const target = this.snaps.findById(this.activeId);
3064
3324
  if (!target) return;
3065
3325
  this.updateAriaSlider();
3066
- this.emit("snap", { id: target.id, size: target.size });
3326
+ this.emit("snap", {
3327
+ id: target.id,
3328
+ size: target.size,
3329
+ progress: this.computeProgress(target.size)
3330
+ });
3067
3331
  if (target.size > 0 && !this.lifecycle.isInstalled) {
3068
3332
  this.emit("open", { id: target.id });
3069
3333
  this.handleOpen();
3070
3334
  notifyLinkedSheets(this.linkedSheets, this);
3335
+ this.emit("opened", { id: target.id });
3071
3336
  } else if (target.size === 0 && this.lifecycle.isInstalled) {
3072
3337
  this.emit("close", void 0);
3073
3338
  this.handleClose();
3339
+ this.emit("closed", void 0);
3074
3340
  }
3075
3341
  }
3076
3342
  handleClose() {
3343
+ this.opening = false;
3077
3344
  this.lifecycle.release();
3078
3345
  sheetStack.update();
3079
3346
  }