@marianmeres/stuic 3.170.0 → 3.171.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.
@@ -47,7 +47,7 @@
47
47
  search?: boolean | ContextMenuSearchConfig;
48
48
  /** Show backdrop in fallback mode (default: true) */
49
49
  showBackdrop?: boolean;
50
- /** Reserve scrollbar space to prevent layout shift on open (see DropdownMenu) */
50
+ /** Reserve scrollbar space; auto-detected once the menu scrolls (see DropdownMenu) */
51
51
  scrollbarGutter?: boolean;
52
52
  /** Skip the body scroll lock in fallback mode */
53
53
  noScrollLock?: boolean;
@@ -39,7 +39,7 @@ export interface Props extends Omit<HTMLAttributes<HTMLDivElement>, "children">
39
39
  search?: boolean | ContextMenuSearchConfig;
40
40
  /** Show backdrop in fallback mode (default: true) */
41
41
  showBackdrop?: boolean;
42
- /** Reserve scrollbar space to prevent layout shift on open (see DropdownMenu) */
42
+ /** Reserve scrollbar space; auto-detected once the menu scrolls (see DropdownMenu) */
43
43
  scrollbarGutter?: boolean;
44
44
  /** Skip the body scroll lock in fallback mode */
45
45
  noScrollLock?: boolean;
@@ -29,7 +29,7 @@ trigger semantics and the cursor anchoring.
29
29
  | `closeOnEscape` | `boolean` | `true` | Close on Escape |
30
30
  | `search` | `boolean \| ContextMenuSearchConfig` | - | Search/filter input inside the menu (see DropdownMenu) |
31
31
  | `showBackdrop` | `boolean` | `true` | Backdrop in fallback (centered modal) mode |
32
- | `scrollbarGutter` | `boolean` | auto | Reserve scrollbar space (auto-enables at 7 items) |
32
+ | `scrollbarGutter` | `boolean` | auto | Reserve scrollbar space (auto: once the menu actually scrolls, kept until close) |
33
33
  | `noScrollLock` | `boolean` | - | Skip the body scroll lock in fallback mode |
34
34
  | `forceFallback` | `boolean` | `false` | Force the centered-modal fallback (testing) |
35
35
  | `onOpen` / `onClose` | `() => void` | - | Open/close callbacks |
@@ -194,8 +194,10 @@
194
194
  onClose?: () => void;
195
195
  /** Called when any action item is selected (fallback if item has no onSelect) */
196
196
  onSelect?: (item: DropdownMenuActionItem) => void | boolean | Promise<void | boolean>;
197
- /** Reserve scrollbar space to prevent layout shift on open (useful for long lists).
198
- * When undefined, auto-enables if items count >= 7. */
197
+ /** Reserve scrollbar space (`scrollbar-gutter: stable`) so the menu width does not
198
+ * jump when the scrollbar comes and goes while open (search filtering, expandable
199
+ * sections). `true`/`false` force it. When undefined, the gutter is reserved only
200
+ * once the content actually scrolls, and kept until the menu closes. */
199
201
  scrollbarGutter?: boolean;
200
202
  /** Reference to trigger element */
201
203
  triggerEl?: HTMLButtonElement;
@@ -684,9 +686,58 @@
684
686
  // Computed transition duration
685
687
  let transitionDuration = $derived(reducedMotion.current ? 0 : 100);
686
688
 
689
+ // Scrollbar gutter.
690
+ // `scrollbar-gutter: stable` keeps the dropdown width from jumping when the vertical
691
+ // scrollbar comes and goes while the menu is open (search filtering, expanding or
692
+ // collapsing sections). Reserving it up front for a menu that never scrolls leaves a
693
+ // dead strip along the right edge (with classic, space-taking scrollbars), so unless
694
+ // the `scrollbarGutter` prop decides, the gutter is reserved only once a space-taking
695
+ // scrollbar is actually present — and then kept until the menu closes.
696
+ let hasScrollbar = $state(false);
697
+
698
+ /** Whether `el` currently renders a vertical scrollbar that takes up layout space.
699
+ * Overlay scrollbars take none (and `scrollbar-gutter` reserves nothing for them).
700
+ * Both halves are needed: an already reserved gutter takes the same space as a
701
+ * scrollbar (so width alone is fooled by a stale gutter from the previous session),
702
+ * and a mid-transition frame (`overflow: hidden`, reduced height) "overflows"
703
+ * without any scrollbar (so height alone is fooled by the slide animation). */
704
+ function hasVerticalScrollbar(el: HTMLElement): boolean {
705
+ const cs = getComputedStyle(el);
706
+ const borders =
707
+ (parseFloat(cs.borderLeftWidth) || 0) + (parseFloat(cs.borderRightWidth) || 0);
708
+ // offsetWidth - clientWidth = horizontal borders + vertical scrollbar (or gutter)
709
+ const takesSpace = el.offsetWidth - el.clientWidth - borders > 1;
710
+ return takesSpace && el.scrollHeight > el.clientHeight;
711
+ }
712
+
713
+ function measureScrollbar() {
714
+ if (hasScrollbar || !dropdownEl) return;
715
+ if (hasVerticalScrollbar(dropdownEl)) hasScrollbar = true;
716
+ }
717
+
718
+ // Each open session measures afresh. Reset on open rather than on close, so the
719
+ // gutter survives the closing slide-out (no width jump mid-outro).
720
+ $effect(() => {
721
+ if (isOpen) hasScrollbar = false;
722
+ });
723
+
724
+ // Measure on open and whenever the rendered content can grow (search results,
725
+ // expanded sections, items, positioning mode). Effects run before the slide intro
726
+ // applies its first zero-height frame, so this reads the settled layout; the
727
+ // `onintrostart` / `onintroend` handlers in the template re-check around transitions
728
+ // (a re-open that interrupts the closing slide-out is measured mid-animation here,
729
+ // and only settles by `introend`).
730
+ $effect(() => {
731
+ if (!isOpen || !dropdownEl) return;
732
+ void filteredItems;
733
+ void expandedSections;
734
+ void isSupported;
735
+ untrack(measureScrollbar);
736
+ });
737
+
687
738
  // Position styles for CSS Anchor Positioning
688
739
  let dropdownStyle = $derived.by(() => {
689
- const useGutter = scrollbarGutter ?? items.length >= 7;
740
+ const useGutter = scrollbarGutter ?? hasScrollbar;
690
741
  const gutterStyle = useGutter ? "scrollbar-gutter: stable;" : "";
691
742
  if (isSupported) {
692
743
  // Use fixed height when search is enabled AND position is a "top" variant
@@ -878,6 +929,8 @@
878
929
  )}
879
930
  style={dropdownStyle}
880
931
  transition:slide={{ duration: transitionDuration }}
932
+ onintrostart={measureScrollbar}
933
+ onintroend={measureScrollbar}
881
934
  >
882
935
  <!-- Close button (fallback mode only) -->
883
936
  {#if !isSupported}
@@ -1017,6 +1070,7 @@
1017
1070
  classExpandableContent
1018
1071
  )}
1019
1072
  transition:slide={{ duration: transitionDuration }}
1073
+ onintroend={measureScrollbar}
1020
1074
  >
1021
1075
  {#each item.items as childItem}
1022
1076
  {#if childItem.type === "action"}
@@ -163,8 +163,10 @@ export interface Props extends Omit<HTMLButtonAttributes, "children"> {
163
163
  onClose?: () => void;
164
164
  /** Called when any action item is selected (fallback if item has no onSelect) */
165
165
  onSelect?: (item: DropdownMenuActionItem) => void | boolean | Promise<void | boolean>;
166
- /** Reserve scrollbar space to prevent layout shift on open (useful for long lists).
167
- * When undefined, auto-enables if items count >= 7. */
166
+ /** Reserve scrollbar space (`scrollbar-gutter: stable`) so the menu width does not
167
+ * jump when the scrollbar comes and goes while open (search filtering, expandable
168
+ * sections). `true`/`false` force it. When undefined, the gutter is reserved only
169
+ * once the content actually scrolls, and kept until the menu closes. */
168
170
  scrollbarGutter?: boolean;
169
171
  /** Reference to trigger element */
170
172
  triggerEl?: HTMLButtonElement;
@@ -11,6 +11,7 @@ A feature-rich dropdown menu component with CSS Anchor Positioning (with fallbac
11
11
  | `position` | `DropdownMenuPosition` | `"bottom-span-right"` | Popover position relative to trigger |
12
12
  | `offset` | `string` | `"0.25rem"` | Offset from trigger element (CSS value) |
13
13
  | `maxHeight` | `string` | `"300px"` | Max height of dropdown |
14
+ | `scrollbarGutter` | `boolean` | auto | Reserve scrollbar space (auto: once it scrolls) |
14
15
  | `closeOnSelect` | `boolean` | `true` | Close menu when action item is selected |
15
16
  | `closeOnClickOutside` | `boolean` | `true` | Close on click outside |
16
17
  | `closeOnEscape` | `boolean` | `true` | Close on Escape key |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marianmeres/stuic",
3
- "version": "3.170.0",
3
+ "version": "3.171.0",
4
4
  "packageManager": "pnpm@11.5.0",
5
5
  "scripts": {
6
6
  "dev": "vite dev",