@m3ui-vue/m3ui-vue 0.9.3 → 1.0.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 (97) hide show
  1. package/README.md +24 -29
  2. package/dist/API.md +179 -19
  3. package/dist/{MTextField-DKtgPymd.js → MTextField-BPbFeI9M.js} +305 -177
  4. package/dist/MTextField-BPbFeI9M.js.map +1 -0
  5. package/dist/api.json +1 -1
  6. package/dist/code-editor.js +1 -1
  7. package/dist/components/MAppBarWithSearch.vue.d.ts +70 -0
  8. package/dist/components/MBottomAppBar.vue.d.ts +37 -0
  9. package/dist/components/MBottomSheetScaffold.vue.d.ts +40 -0
  10. package/dist/components/MChatBubble.vue.d.ts +1 -1
  11. package/dist/components/MChip.vue.d.ts +19 -2
  12. package/dist/components/MContainer.vue.d.ts +1 -1
  13. package/dist/components/MDataTable.vue.d.ts +1 -1
  14. package/dist/components/MDialog.vue.d.ts +1 -1
  15. package/dist/components/MFab.vue.d.ts +13 -4
  16. package/dist/components/MListItem.vue.d.ts +2 -2
  17. package/dist/components/MLoadingOverlay.vue.d.ts +1 -1
  18. package/dist/components/MNavigationDrawer.vue.d.ts +9 -6
  19. package/dist/components/MNavigationRail.vue.d.ts +6 -0
  20. package/dist/components/MSearchBar.vue.d.ts +5 -3
  21. package/dist/components/MShortNavigationBar.vue.d.ts +1 -1
  22. package/dist/components/MTable.vue.d.ts +1 -1
  23. package/dist/components/MTopAppBar.vue.d.ts +33 -2
  24. package/dist/components/MTransition.vue.d.ts +0 -9
  25. package/dist/composables/_searchAppBar.d.ts +6 -0
  26. package/dist/composables/_springAnimation.d.ts +17 -0
  27. package/dist/composables/_themeTransition.d.ts +2 -0
  28. package/dist/composables/_useSpring.d.ts +3 -5
  29. package/dist/composables/motion.d.ts +101 -0
  30. package/dist/composables/useColorPalette.d.ts +10 -0
  31. package/dist/composables/useLocale.d.ts +3 -0
  32. package/dist/index.d.ts +8 -1
  33. package/dist/locales/index.js +21 -0
  34. package/dist/locales/index.js.map +1 -1
  35. package/dist/m3ui-vue.css +1 -1
  36. package/dist/m3ui.js +7345 -6317
  37. package/dist/m3ui.js.map +1 -1
  38. package/dist/plugin.d.ts +7 -0
  39. package/dist/rich-text-editor.js +2 -2
  40. package/dist/styles/palettes-contrast.css +7764 -0
  41. package/dist/styles/theme.css +46 -2
  42. package/dist/styles.css +1 -1
  43. package/dist/{useLocale-Cn9MVIdw.js → useLocale-CuvWRZ1h.js} +4 -1
  44. package/dist/useLocale-CuvWRZ1h.js.map +1 -0
  45. package/package.json +3 -1
  46. package/src/__tests__/MAppBarWithSearch.test.ts +108 -0
  47. package/src/__tests__/MBottomAppBar.test.ts +71 -0
  48. package/src/__tests__/MBottomSheetScaffold.test.ts +122 -0
  49. package/src/__tests__/MChip.test.ts +63 -0
  50. package/src/__tests__/MFab.test.ts +36 -0
  51. package/src/__tests__/MNavigationDrawer.test.ts +185 -0
  52. package/src/__tests__/MNavigationRail.test.ts +93 -0
  53. package/src/__tests__/MPullToRefresh.test.ts +18 -1
  54. package/src/__tests__/MTopAppBar.test.ts +99 -0
  55. package/src/__tests__/MTransition.test.ts +12 -0
  56. package/src/__tests__/contrast.test.ts +64 -0
  57. package/src/__tests__/dynamicColor.test.ts +17 -0
  58. package/src/__tests__/motion.test.ts +37 -0
  59. package/src/__tests__/sheets.test.ts +19 -15
  60. package/src/__tests__/themeTransition.test.ts +119 -0
  61. package/src/components/MAppBarWithSearch.vue +236 -0
  62. package/src/components/MBottomAppBar.vue +181 -0
  63. package/src/components/MBottomSheetScaffold.vue +317 -0
  64. package/src/components/MBreadcrumbs.vue +1 -1
  65. package/src/components/MChip.vue +137 -5
  66. package/src/components/MContextMenu.vue +34 -33
  67. package/src/components/MFab.vue +73 -12
  68. package/src/components/MMenu.vue +34 -33
  69. package/src/components/MNavigationDrawer.vue +298 -38
  70. package/src/components/MNavigationRail.vue +189 -8
  71. package/src/components/MPullToRefresh.vue +18 -0
  72. package/src/components/MSearchBar.vue +29 -12
  73. package/src/components/MTopAppBar.vue +252 -28
  74. package/src/components/MTransition.vue +36 -41
  75. package/src/composables/_searchAppBar.ts +9 -0
  76. package/src/composables/_springAnimation.ts +74 -0
  77. package/src/composables/_themeColor.ts +11 -4
  78. package/src/composables/_themeTransition.ts +71 -0
  79. package/src/composables/_useSheetMotion.ts +101 -88
  80. package/src/composables/_useSpring.ts +11 -21
  81. package/src/composables/motion.ts +73 -0
  82. package/src/composables/useColorPalette.ts +103 -20
  83. package/src/composables/useLocale.ts +6 -0
  84. package/src/composables/useTheme.ts +4 -12
  85. package/src/index.ts +8 -1
  86. package/src/locales/de.ts +3 -0
  87. package/src/locales/es.ts +3 -0
  88. package/src/locales/fr.ts +3 -0
  89. package/src/locales/ja.ts +3 -0
  90. package/src/locales/ko.ts +3 -0
  91. package/src/locales/pt.ts +3 -0
  92. package/src/locales/zh.ts +3 -0
  93. package/src/plugin.ts +9 -1
  94. package/src/styles/palettes-contrast.css +7764 -0
  95. package/src/styles/theme.css +46 -2
  96. package/dist/MTextField-DKtgPymd.js.map +0 -1
  97. package/dist/useLocale-Cn9MVIdw.js.map +0 -1
@@ -7,9 +7,10 @@
7
7
  * `close()` closes it.
8
8
  * @event click Emitted when the FAB itself is clicked (not a speed-dial item)
9
9
  */
10
- import { computed, onMounted, onUnmounted, ref, useSlots } from 'vue'
10
+ import { computed, inject, onMounted, onUnmounted, ref, useSlots } from 'vue'
11
11
  import MIcon from './MIcon.vue'
12
12
  import { vRipple } from '../directives/vRipple'
13
+ import { FAST_EFFECTS, FAST_SPATIAL, useSpring } from '../composables/_useSpring'
13
14
 
14
15
  export interface SpeedDialItem {
15
16
  icon: string
@@ -26,9 +27,17 @@ const props = withDefaults(
26
27
  icon: string
27
28
  /** Extended FAB label text */
28
29
  label?: string
29
- /** Color scheme */
30
+ /**
31
+ * Extended FAB only: false collapses it to a plain FAB (the label fades out and the width shrinks to a
32
+ * square), e.g. while the content scrolls. Compose's `expanded`
33
+ */
34
+ expanded?: boolean
35
+ /** Color scheme; defaults to primary, or secondary inside an MBottomAppBar */
30
36
  color?: 'primary' | 'secondary' | 'tertiary' | 'surface'
31
- /** FAB size */
37
+ /**
38
+ * FAB size. With a `label`, small/medium/large are M3 Expressive's extended FABs (56/80/96px, title-medium,
39
+ * title-large and headline-small labels); regular is the baseline extended FAB
40
+ */
32
41
  size?: 'small' | 'regular' | 'medium' | 'large'
33
42
  /** Disables interaction */
34
43
  disabled?: boolean
@@ -63,8 +72,9 @@ const props = withDefaults(
63
72
  rippleColor?: string
64
73
  }>(),
65
74
  {
66
- color: 'primary',
75
+ color: undefined,
67
76
  size: 'regular',
77
+ expanded: true,
68
78
  disabled: false,
69
79
  direction: 'up',
70
80
  persistent: false,
@@ -87,6 +97,10 @@ const hasItems = computed(() => !!props.items?.length)
87
97
  const hasContent = computed(() => !!slots.content)
88
98
  const isExpandable = computed(() => hasItems.value || hasContent.value)
89
99
 
100
+ // Inside an MBottomAppBar: secondary and flat (bottomAppBarFabColor, bottomAppBarFabElevation)
101
+ const inBottomAppBar = inject('m-bottom-app-bar', false)
102
+ const color = computed(() => props.color ?? (inBottomAppBar ? 'secondary' : 'primary'))
103
+
90
104
  const colorMap: Record<string, string> = {
91
105
  primary: 'bg-primary-container text-on-primary-container',
92
106
  secondary: 'bg-secondary-container text-on-secondary-container',
@@ -94,10 +108,20 @@ const colorMap: Record<string, string> = {
94
108
  surface: 'bg-surface-container-high text-primary',
95
109
  }
96
110
 
111
+ // Extended FABs (androidx FloatingActionButton.kt). Baseline (`regular`): 16dp start, 12dp icon-to-label,
112
+ // 20dp end, 80dp minimum width, labelLarge. Small/Medium/Large (ExtendedFab*Tokens): leading = trailing space,
113
+ // minimum width = height, MediumExtendedFabIconPadding/LargeExtendedFabIconPadding (12/16dp, the tokens'
114
+ // IconLabelSpace is marked incorrect there).
115
+ const EXTENDED = {
116
+ regular: { px: 56, icon: 24, gap: 12, box: 'h-14 min-w-20 rounded-2xl pl-4 pr-5', text: 'text-label-large font-medium' },
117
+ small: { px: 56, icon: 24, gap: 8, box: 'h-14 min-w-14 rounded-2xl px-4', text: 'text-title-medium font-medium' },
118
+ medium: { px: 80, icon: 28, gap: 12, box: 'h-20 min-w-20 rounded-lg-increased px-[26px]', text: 'text-title-large' },
119
+ large: { px: 96, icon: 32, gap: 16, box: 'h-24 min-w-24 rounded-xl px-7', text: 'text-headline-small' },
120
+ } as const
121
+ const extended = computed(() => (props.label ? EXTENDED[props.size] : null))
122
+
97
123
  const fabSizeClasses = computed(() => {
98
- // Extended FAB (androidx FloatingActionButton.kt): 16dp start padding, 12dp
99
- // icon-to-label gap, 20dp end padding, 80dp minimum width.
100
- if (props.label) return 'h-14 min-w-20 rounded-2xl pl-4 pr-5 gap-3'
124
+ if (extended.value) return extended.value.box
101
125
  switch (props.size) {
102
126
  case 'small': return 'h-10 w-10 rounded-md'
103
127
  case 'medium': return 'h-20 w-20 rounded-lg-increased'
@@ -107,7 +131,7 @@ const fabSizeClasses = computed(() => {
107
131
  })
108
132
 
109
133
  const fabIconSize = computed(() => {
110
- if (props.label) return 24
134
+ if (extended.value) return extended.value.icon
111
135
  switch (props.size) {
112
136
  case 'small': return 24
113
137
  case 'medium': return 28
@@ -117,7 +141,7 @@ const fabIconSize = computed(() => {
117
141
  })
118
142
 
119
143
  const fabPx = computed(() => {
120
- if (props.label) return 56
144
+ if (extended.value) return extended.value.px
121
145
  switch (props.size) {
122
146
  case 'small': return 40
123
147
  case 'medium': return 80
@@ -126,6 +150,34 @@ const fabPx = computed(() => {
126
150
  }
127
151
  })
128
152
 
153
+ // Collapsing an extended FAB: the width lerps from the square (the minimum width) to the full width on
154
+ // FastSpatial, the label fades on FastEffects, and it leaves the layout once fully collapsed.
155
+ const fabButton = ref<HTMLElement | { $el: HTMLElement }>()
156
+ const labelEl = ref<HTMLElement>()
157
+ const buttonEl = () => (fabButton.value instanceof HTMLElement ? fabButton.value : fabButton.value?.$el)
158
+
159
+ useSpring(
160
+ () => (extended.value ? [props.expanded ? 1 : 0] : []),
161
+ ([p]) => {
162
+ const el = buttonEl()
163
+ if (!el || p === undefined || !extended.value) return
164
+ if (labelEl.value) labelEl.value.style.display = p <= 0 && !props.expanded ? 'none' : ''
165
+ el.style.width = ''
166
+ el.style.minWidth = ''
167
+ if (p >= 1 && props.expanded) return
168
+ const full = el.offsetWidth
169
+ const min = extended.value.px
170
+ el.style.minWidth = '0'
171
+ el.style.width = `${min + (full - min) * Math.max(0, p)}px`
172
+ },
173
+ FAST_SPATIAL,
174
+ )
175
+ useSpring(
176
+ () => (extended.value ? [props.expanded ? 1 : 0] : []),
177
+ ([alpha]) => { if (labelEl.value && alpha !== undefined) labelEl.value.style.opacity = String(Math.min(1, Math.max(0, alpha))) },
178
+ FAST_EFFECTS,
179
+ )
180
+
129
181
  const ITEM_PX = 40
130
182
  const ITEM_GAP = 8
131
183
 
@@ -289,11 +341,13 @@ onUnmounted(() => {
289
341
  <div ref="fabEl" v-bind="$attrs" class="relative inline-flex items-center justify-center">
290
342
  <component
291
343
  :is="fabTag"
344
+ ref="fabButton"
292
345
  :to="to || undefined"
293
346
  :type="to ? undefined : 'button'"
294
- class="relative inline-flex cursor-pointer items-center justify-center overflow-hidden shadow-elevation-3 transition-shadow duration-150 hover:shadow-elevation-4 active:shadow-elevation-3 disabled:cursor-not-allowed disabled:opacity-[0.38] before:content-[''] before:pointer-events-none before:absolute before:inset-0 before:bg-current before:opacity-0 before:transition-opacity before:duration-150 hover:before:opacity-[0.08] active:before:opacity-[0.12]"
295
- :class="[colorMap[color], fabSizeClasses]"
347
+ class="relative inline-flex cursor-pointer items-center overflow-hidden transition-shadow duration-150 disabled:cursor-not-allowed disabled:opacity-[0.38] before:content-[''] before:pointer-events-none before:absolute before:inset-0 before:bg-current before:opacity-0 before:transition-opacity before:duration-150 hover:before:opacity-[0.08] active:before:opacity-[0.12]"
348
+ :class="[colorMap[color], fabSizeClasses, extended ? 'justify-start' : 'justify-center', inBottomAppBar ? '' : 'shadow-elevation-3 hover:shadow-elevation-4 active:shadow-elevation-3']"
296
349
  :disabled="disabled"
350
+ :aria-label="label && !expanded ? label : undefined"
297
351
  v-ripple="{ disabled, color: rippleColor }"
298
352
  @click="handleFabClick"
299
353
  >
@@ -303,7 +357,14 @@ onUnmounted(() => {
303
357
  class="transition-transform duration-300 ease-[cubic-bezier(0.34,1.56,0.64,1)]"
304
358
  :class="isExpandable && open ? 'rotate-45' : ''"
305
359
  />
306
- <span v-if="label" class="text-label-large font-medium">{{ label }}</span>
360
+ <!-- The icon-to-label space belongs to the label, so it goes when the label does -->
361
+ <span
362
+ v-if="extended"
363
+ ref="labelEl"
364
+ class="shrink-0 whitespace-nowrap"
365
+ :class="extended.text"
366
+ :style="{ paddingLeft: `${extended.gap}px` }"
367
+ >{{ label }}</span>
307
368
  </component>
308
369
  </div>
309
370
 
@@ -170,16 +170,15 @@ function openTrajectory() {
170
170
  // hook on the same still-mounted element without waiting for `done()`. Both
171
171
  // `currentIdx` (how far along the trajectory we are, so a reversal continues
172
172
  // smoothly from wherever it currently is instead of snapping back to a fixed
173
- // start) and `cancelActive` (so the previous rAF loop actually stops instead
174
- // of running alongside the new one, fighting over the same style every
175
- // frame) have to live outside this function to survive that interruption.
173
+ // start) and `cancelActive` (so the previous animation actually stops instead
174
+ // of running alongside the new one, fighting over the same style) have to live outside this function to survive that interruption.
176
175
  let currentIdx = 0
177
176
  let cancelActive: (() => void) | null = null
178
- // Set only while a close is playing: stops the loop *and* calls its `done()`.
177
+ // Set only while a close is playing: stops the animation *and* calls its `done()`.
179
178
  // Used on unmount — a menu item that navigates away (e.g. "Log out" ->
180
179
  // router.push) unmounts this component mid-close, and Vue only removes the
181
180
  // Teleported panel from <body> inside the leave's `done()`, so merely
182
- // cancelling the loop there left the half-closed panel stranded on screen.
181
+ // cancelling the animation there left the half-closed panel stranded on screen.
183
182
  let finishLeave: (() => void) | null = null
184
183
 
185
184
  function playTrajectory(el: Element, done: () => void, opening: boolean) {
@@ -187,37 +186,39 @@ function playTrajectory(el: Element, done: () => void, opening: boolean) {
187
186
  const htmlEl = el as HTMLElement
188
187
  const samples = openTrajectory()
189
188
  const last = samples.length - 1
190
- const direction = opening ? 1 : -1
191
- const target = opening ? last : 0
192
- let lastTime = 0
193
- let rafId = 0
194
- let cancelled = false
195
-
196
- function apply(idx: number) {
197
- const sample = samples[Math.round(Math.max(0, Math.min(last, idx)))]!
198
- htmlEl.style.transform = `scale(${sample.scale})`
199
- htmlEl.style.opacity = String(Math.min(1, Math.max(0, sample.alpha)))
189
+ const from = Math.round(Math.max(0, Math.min(last, currentIdx)))
190
+ const to = opening ? last : 0
191
+ const step = opening ? 1 : -1
192
+ const frames: { scale: number, alpha: number }[] = []
193
+ for (let i = from; opening ? i <= to : i >= to; i += step) frames.push(samples[i]!)
194
+ const style = (s: { scale: number, alpha: number }) => ({
195
+ transform: `scale(${s.scale})`,
196
+ opacity: String(Math.min(1, Math.max(0, s.alpha))),
197
+ })
198
+ // Played as a Web Animation (see _springAnimation.ts), which the compositor runs, so the open isn't janked by
199
+ // the panel's content mounting at the same moment. The end state goes inline underneath it.
200
+ Object.assign(htmlEl.style, style(samples[to]!))
201
+ let ended = false
202
+ function complete() {
203
+ if (ended) return
204
+ ended = true
205
+ currentIdx = to
206
+ cancelActive = null
207
+ finishLeave = null
208
+ done()
200
209
  }
201
-
202
- function step(now: number) {
203
- if (cancelled) return
204
- const dtSteps = lastTime ? (now - lastTime) / 1000 / trajectoryStep : 1
205
- lastTime = now
206
- currentIdx = Math.max(0, Math.min(last, currentIdx + direction * dtSteps))
207
- apply(currentIdx)
208
- if ((opening && currentIdx >= target) || (!opening && currentIdx <= target)) {
209
- cancelActive = null
210
- finishLeave = null
211
- done()
212
- return
213
- }
214
- rafId = requestAnimationFrame(step)
210
+ if (frames.length < 2 || typeof htmlEl.animate !== 'function') return complete()
211
+ const anim = htmlEl.animate(frames.map(style), { duration: (frames.length - 1) * trajectoryStep * 1000, easing: 'linear' })
212
+ anim.finished.catch(() => {})
213
+ anim.onfinish = complete
214
+ cancelActive = () => {
215
+ // Remember where it got to, so the reversal continues from there
216
+ const t = Number(anim.currentTime ?? 0) / (trajectoryStep * 1000)
217
+ currentIdx = Math.max(0, Math.min(last, from + step * t))
218
+ ended = true
219
+ anim.cancel()
215
220
  }
216
-
217
- cancelActive = () => { cancelled = true; cancelAnimationFrame(rafId) }
218
221
  finishLeave = opening ? null : () => { cancelActive?.(); cancelActive = null; finishLeave = null; done() }
219
- apply(currentIdx)
220
- rafId = requestAnimationFrame(step)
221
222
  }
222
223
  function panelEnter(el: Element, done: () => void) {
223
224
  playTrajectory(el, done, true)
@@ -1,7 +1,8 @@
1
1
  <script setup lang="ts">
2
2
  /**
3
3
  * A slide-out or inline drawer of grouped, badge-able, optionally-nested navigation items — modal (Teleported,
4
- * scrim-backed) or inline (participates in layout, supports a collapsed icon-only mode).
4
+ * scrim-backed, swiped closed toward its edge) or inline (participates in layout, supports a collapsed
5
+ * icon-only mode).
5
6
  *
6
7
  * @slot header Custom header content (e.g. profile card, logo). Fully generic, no positioning constraints
7
8
  * applied — overrides the title prop.
@@ -13,9 +14,11 @@
13
14
  * @event update:modelValue Open/closed state for v-model
14
15
  * @event select Fires when an item is clicked — use to track the selected item
15
16
  */
16
- import { ref, reactive, watch, provide, computed } from "vue";
17
+ import { ref, reactive, watch, provide, computed, nextTick, onBeforeUnmount } from "vue";
17
18
  import MIcon from "./MIcon.vue";
18
19
  import MDrawerItemList from "./_MDrawerItemList.vue";
20
+ import { motionSprings, type SpringSpec } from "../composables/motion";
21
+ import { sampleSpring, sampleAt, playSamples, prefersReducedMotion } from "../composables/_springAnimation";
19
22
 
20
23
  export interface DrawerItem {
21
24
  value: string | number;
@@ -61,8 +64,10 @@ const props = withDefaults(
61
64
  * variant always renders on left or right.
62
65
  */
63
66
  side?: 'left' | 'right' | 'top' | 'bottom';
67
+ /** Lets the modal drawer be swiped closed toward its edge; with false it only closes through v-model or the scrim */
68
+ swipeEnabled?: boolean;
64
69
  }>(),
65
- { modal: true, side: 'left' },
70
+ { modal: true, side: 'left', swipeEnabled: true },
66
71
  );
67
72
 
68
73
  const emit = defineEmits<{
@@ -79,6 +84,7 @@ function isSectionOpen(section: DrawerSection, index: number) {
79
84
  }
80
85
 
81
86
  function toggleSection(section: DrawerSection, index: number) {
87
+ refreshListTouchAction();
82
88
  const key = section.title ?? `__${index}`;
83
89
  openSections.value[key] = !isSectionOpen(section, index);
84
90
  }
@@ -88,6 +94,7 @@ function isItemOpen(item: DrawerItem) {
88
94
  }
89
95
 
90
96
  function toggleItem(item: DrawerItem) {
97
+ refreshListTouchAction();
91
98
  openItems[item.value] = !openItems[item.value];
92
99
  }
93
100
 
@@ -110,6 +117,7 @@ function onChildSelect(item: DrawerItem) {
110
117
  }
111
118
 
112
119
  function onChildToggle(item: DrawerItem) {
120
+ refreshListTouchAction();
113
121
  openItems[item.value] = !openItems[item.value];
114
122
  }
115
123
 
@@ -153,12 +161,276 @@ watch(
153
161
  }
154
162
  },
155
163
  );
164
+
165
+ // ── Modal: slide in/out + swipe to close ────────────────────────────────────────────────────
166
+ // After ModalNavigationDrawer (NavigationDrawer.kt), which is left/right only; top/bottom add a bottom sheet's
167
+ // nested scroll (see moveDrag). Opens with DefaultSpatial (0.9 / 700), closes with
168
+ // FastEffects (1.0 / 3800); a released swipe settles at the nearer anchor (DrawerPositionalThreshold 50%)
169
+ // unless flung faster than DrawerVelocityThreshold (400dp/s), with DefaultSpatial. The scrim's alpha follows
170
+ // the panel's offset.
171
+ //
172
+ // Each spring plays as a Web Animation (see _springAnimation.ts): a rAF loop janked the open on phones. Only the
173
+ // drag itself writes styles directly. Offsets are fractions of the panel's size, so opening never reads layout.
174
+ const DEFAULT_SPATIAL = motionSprings.standard.defaultSpatial
175
+ const FAST_EFFECTS = motionSprings.standard.fastEffects
176
+ const FLING_VELOCITY = 400
177
+
178
+ let panelEl: HTMLElement | null = null
179
+ let scrimEl: HTMLElement | null = null
180
+ // 0 = open, `closedDir` (-1 for left/top, 1 for right/bottom) = off-screen, in fractions of the panel's size
181
+ let offset = 0
182
+ let velocity = 0
183
+ let closedDir = -1
184
+ let anim: Animation | null = null
185
+ let scrimAnim: Animation | null = null
186
+ let samples: number[] = []
187
+ let settled: (() => void) | null = null
188
+
189
+ const horizontal = () => props.side === "left" || props.side === "right";
190
+ const transformAt = (p: number) => (horizontal() ? `translateX(${p * 100}%)` : `translateY(${p * 100}%)`);
191
+ const opacityAt = (p: number) => String(Math.min(1, Math.max(0, 1 - p / closedDir)));
192
+
193
+ function bindModal(root: Element) {
194
+ panelEl = root.querySelector<HTMLElement>(".nd-panel");
195
+ scrimEl = root.querySelector<HTMLElement>(".nd-scrim");
196
+ closedDir = props.side === "left" || props.side === "top" ? -1 : 1;
197
+ }
198
+
199
+ function writeModal() {
200
+ if (panelEl) panelEl.style.transform = transformAt(offset);
201
+ if (scrimEl) scrimEl.style.opacity = opacityAt(offset);
202
+ }
203
+
204
+ // Freezes the panel where the running animation has it
205
+ function stopAnim() {
206
+ if (!anim) return;
207
+ offset = sampleAt(samples, anim.currentTime);
208
+ writeModal();
209
+ anim.cancel();
210
+ scrimAnim?.cancel();
211
+ anim = scrimAnim = null;
212
+ }
213
+
214
+ function settle() {
215
+ const cb = settled;
216
+ settled = null;
217
+ cb?.();
218
+ }
219
+
220
+ // Without `done`, a pending Transition callback (an enter interrupted by a swipe) is kept for this run
221
+ function animateTo(to: number, spring: SpringSpec, done?: () => void) {
222
+ if (done) {
223
+ settled?.();
224
+ settled = done;
225
+ }
226
+ stopAnim();
227
+ const v = velocity;
228
+ velocity = 0;
229
+ const from = offset;
230
+ // The end state goes inline underneath, so nothing jumps when the animation ends
231
+ offset = to;
232
+ writeModal();
233
+ if (prefersReducedMotion() || from === to) return settle();
234
+ samples = sampleSpring(from, to, v, spring, 0.001);
235
+ const a = playSamples(panelEl, samples, (p) => ({ transform: transformAt(p) }));
236
+ if (!a) return settle();
237
+ scrimAnim = playSamples(scrimEl, samples, (p) => ({ opacity: opacityAt(p) }));
238
+ anim = a;
239
+ a.onfinish = () => {
240
+ if (anim !== a) return;
241
+ anim = scrimAnim = null;
242
+ settle();
243
+ };
244
+ }
245
+
246
+ // Top/bottom: wherever the touch-action lets the browser pan along the drawer's swipe, Chrome treats that drag as
247
+ // a possible scroll waiting on our touchmove listener and delivers it at 60Hz on a 120Hz phone (measured on a
248
+ // device: the panel visibly moved every other frame, while the left/right swipe, crosswise to pan-y, came at
249
+ // 120Hz). So the list only allows the scroll that can actually happen: at the edge the drawer closes toward,
250
+ // only scrolling away from it (bottom: pan-down, the finger moving up; top: pan-up), elsewhere both ways. Outside
251
+ // the list the panel is touch-action: none. Browsers without directional pan values (Safari) keep pan-y.
252
+ let listEl: HTMLElement | null = null;
253
+
254
+ function updateListTouchAction() {
255
+ const list = listEl;
256
+ if (!list || horizontal() || !props.swipeEnabled) return;
257
+ const atEdge = props.side === "bottom"
258
+ ? list.scrollTop <= 0
259
+ : list.scrollTop + list.clientHeight >= list.scrollHeight - 1;
260
+ const directional = typeof CSS !== "undefined" && !!CSS.supports?.("touch-action", "pan-down");
261
+ list.style.touchAction = atEdge && directional ? (props.side === "bottom" ? "pan-down" : "pan-up") : "pan-y";
262
+ }
263
+
264
+ // Expanding a section or item changes how far the list scrolls without a scroll event: re-check once it settles
265
+ function refreshListTouchAction() {
266
+ setTimeout(updateListTouchAction, 400);
267
+ }
268
+
269
+ function watchList(root: Element | null) {
270
+ listEl?.removeEventListener("scroll", updateListTouchAction);
271
+ listEl = root?.querySelector<HTMLElement>("[data-nd-list]") ?? null;
272
+ if (!listEl) return;
273
+ listEl.addEventListener("scroll", updateListTouchAction, { passive: true });
274
+ // After the first layout, so the open doesn't force one
275
+ requestAnimationFrame(updateListTouchAction);
276
+ }
277
+
278
+ function onModalEnter(el: Element, done: () => void) {
279
+ // Reopened mid-close: the old panel goes now
280
+ settled?.();
281
+ settled = null;
282
+ anim?.cancel();
283
+ scrimAnim?.cancel();
284
+ anim = scrimAnim = null;
285
+ bindModal(el);
286
+ watchList(el);
287
+ offset = closedDir;
288
+ velocity = 0;
289
+ animateTo(0, DEFAULT_SPATIAL, done);
290
+ }
291
+
292
+ function onModalLeave(el: Element, done: () => void) {
293
+ bindModal(el);
294
+ drag = null;
295
+ watchList(null);
296
+ animateTo(closedDir, FAST_EFFECTS, done);
297
+ }
298
+
299
+ onBeforeUnmount(() => {
300
+ anim?.cancel();
301
+ scrimAnim?.cancel();
302
+ watchList(null);
303
+ });
304
+
305
+ // Positions below are along the drawer's axis: x for left/right, y for top/bottom.
306
+ //
307
+ // A left/right drawer's swipe is crosswise to the list's scroll, so pointer events do (touch-action pan-y leaves
308
+ // the horizontal axis to us). A top/bottom drawer's swipe runs along it, and touch-action is fixed when the finger
309
+ // lands, so a touch there goes through touch events instead, like MPullToRefresh: the first move decides, and from
310
+ // that very touchmove on preventDefault() keeps the browser out. Deciding a few pointermoves later (as for mouse)
311
+ // let the browser start its own pan or overscroll first, and the two fought: the drag lagged on phones.
312
+ let drag: { id: number; touch: boolean; main: number; cross: number; start: number; size: number; active: boolean; list: HTMLElement | null; samples: { t: number; p: number }[] } | null = null;
313
+ let suppressClick = false;
314
+
315
+ function beginDrag(id: number, touch: boolean, x: number, y: number, t: number, target: EventTarget | null) {
316
+ const main = horizontal() ? x : y;
317
+ const list = (target as Element | null)?.closest<HTMLElement>("[data-nd-list]") ?? null;
318
+ drag = { id, touch, main, cross: horizontal() ? y : x, start: 0, size: 1, active: false, list, samples: [{ t, p: main }] };
319
+ }
320
+
321
+ // Returns true when this move started the drag
322
+ function moveDrag(x: number, y: number, t: number, panel: HTMLElement): boolean {
323
+ if (!drag) return false;
324
+ const main = horizontal() ? x : y;
325
+ let started = false;
326
+ if (!drag.active) {
327
+ const d = main - drag.main;
328
+ const dc = (horizontal() ? y : x) - drag.cross;
329
+ if (Math.abs(dc) > 8 && Math.abs(dc) > Math.abs(d)) { drag = null; return false; }
330
+ if (Math.abs(d) < (horizontal() ? 8 : drag.touch ? 2 : 4)) return false;
331
+ if (!horizontal()) {
332
+ if (Math.abs(dc) > Math.abs(d)) { drag = null; return false; }
333
+ bindModal(panel.parentElement!);
334
+ // Like a bottom sheet's nested scroll: dragging away from the edge only scrolls, and a drag toward it
335
+ // scrolls the list first, the drawer following once the list can't scroll that way any more
336
+ const list = drag.list;
337
+ const listScrolls = !!list && (d > 0 ? list.scrollTop > 0 : list.scrollTop + list.clientHeight < list.scrollHeight - 1);
338
+ if (Math.sign(d) !== closedDir || listScrolls) { drag = null; return false; }
339
+ }
340
+ drag.active = true;
341
+ started = true;
342
+ drag.main = main;
343
+ bindModal(panel.parentElement!);
344
+ // Stop a running slide; the finger owns the panel now
345
+ stopAnim();
346
+ velocity = 0;
347
+ drag.start = offset;
348
+ drag.size = (horizontal() ? panel.offsetWidth : panel.offsetHeight) || 1;
349
+ }
350
+ const min = Math.min(0, closedDir);
351
+ const max = Math.max(0, closedDir);
352
+ offset = Math.min(max, Math.max(min, drag.start + (main - drag.main) / drag.size));
353
+ writeModal();
354
+ drag.samples.push({ t, p: main });
355
+ drag.samples = drag.samples.filter((s) => t - s.t < 100);
356
+ return started;
357
+ }
358
+
359
+ function endDrag() {
360
+ if (!drag) return;
361
+ const wasActive = drag.active;
362
+ const first = drag.samples[0]!;
363
+ const lastSample = drag.samples[drag.samples.length - 1]!;
364
+ const dt = (lastSample.t - first.t) / 1000;
365
+ const v = dt > 0 ? (lastSample.p - first.p) / dt : 0;
366
+ const size = drag.size;
367
+ drag = null;
368
+ if (!wasActive) return;
369
+ suppressClick = true;
370
+ setTimeout(() => { suppressClick = false; });
371
+ const dismiss = Math.abs(v) > FLING_VELOCITY
372
+ ? Math.sign(v) === closedDir
373
+ : Math.abs(offset) > 0.5;
374
+ velocity = v / size;
375
+ if (!dismiss) return animateTo(0, DEFAULT_SPATIAL);
376
+ close();
377
+ // The parent may not close it (no v-model bound): spring back instead of staying mid-swipe
378
+ nextTick(() => {
379
+ if (props.modelValue) animateTo(0, DEFAULT_SPATIAL);
380
+ });
381
+ }
382
+
383
+ function onPanelPointerDown(e: PointerEvent) {
384
+ if (!props.swipeEnabled || e.button !== 0) return;
385
+ if (e.pointerType === "touch" && !horizontal()) return;
386
+ beginDrag(e.pointerId, false, e.clientX, e.clientY, e.timeStamp, e.target);
387
+ }
388
+
389
+ function onPanelPointerMove(e: PointerEvent) {
390
+ if (!drag || drag.touch || e.pointerId !== drag.id) return;
391
+ const panel = e.currentTarget as HTMLElement;
392
+ if (moveDrag(e.clientX, e.clientY, e.timeStamp, panel)) panel.setPointerCapture?.(e.pointerId);
393
+ }
394
+
395
+ function onPanelPointerUp(e: PointerEvent) {
396
+ if (!drag || drag.touch || e.pointerId !== drag.id) return;
397
+ endDrag();
398
+ }
399
+
400
+ function onPanelTouchStart(e: TouchEvent) {
401
+ if (!props.swipeEnabled || horizontal() || e.touches.length !== 1) return;
402
+ const t = e.touches[0]!;
403
+ beginDrag(t.identifier, true, t.clientX, t.clientY, e.timeStamp, e.target);
404
+ }
405
+
406
+ function onPanelTouchMove(e: TouchEvent) {
407
+ if (drag?.touch) {
408
+ const id = drag.id;
409
+ const t = Array.from(e.changedTouches).find((x) => x.identifier === id);
410
+ if (t) moveDrag(t.clientX, t.clientY, e.timeStamp, e.currentTarget as HTMLElement);
411
+ }
412
+ // From the very move that started it (top/bottom), or as belt and braces (left/right): the browser must not
413
+ // scroll or navigate with the swipe
414
+ if (drag?.active && e.cancelable) e.preventDefault();
415
+ }
416
+
417
+ function onPanelTouchEnd(e: TouchEvent) {
418
+ if (!drag?.touch) return;
419
+ const id = drag.id;
420
+ if (Array.from(e.changedTouches).some((x) => x.identifier === id)) endDrag();
421
+ }
422
+
423
+ function onPanelClickCapture(e: MouseEvent) {
424
+ if (!suppressClick) return;
425
+ e.preventDefault();
426
+ e.stopPropagation();
427
+ }
156
428
  </script>
157
429
 
158
430
  <template>
159
431
  <!-- ── Modal variant ──────────────────────────────────────── -->
160
432
  <Teleport v-if="modal" to="body">
161
- <Transition name="nd" :duration="{ enter: 300, leave: 280 }" appear>
433
+ <Transition :css="false" @enter="onModalEnter" @leave="onModalLeave" appear>
162
434
  <div
163
435
  v-if="modelValue"
164
436
  class="fixed inset-0 z-100 flex"
@@ -168,20 +440,30 @@ watch(
168
440
  side === 'top' ? 'flex-col' : '',
169
441
  ]"
170
442
  >
171
- <div class="nd-scrim absolute inset-0 bg-scrim/32" @click="close" />
443
+ <!-- will-change: a drag writes their transform/opacity directly, without the layer an animation gets -->
444
+ <div class="nd-scrim absolute inset-0 bg-scrim/32 will-change-[opacity]" @click="close" />
172
445
 
173
446
  <nav
174
447
  v-bind="$attrs"
175
448
  :class="[
176
- 'nd-panel relative flex flex-col bg-surface-container-low shadow-elevation-1',
177
- `nd-panel-${side}`,
449
+ 'nd-panel relative flex flex-col bg-surface-container-low shadow-elevation-1 will-change-transform',
178
450
  side === 'top' || side === 'bottom'
179
451
  ? 'w-full max-h-[85vh]'
180
452
  : 'h-full max-w-[85vw]',
181
453
  side === 'top' || side === 'bottom' || width ? '' : 'w-[22.5rem]',
182
454
  side === 'right' ? 'rounded-l-lg' : side === 'left' ? 'rounded-r-lg' : side === 'bottom' ? 'rounded-t-lg' : '',
455
+ swipeEnabled ? (side === 'left' || side === 'right' ? 'touch-pan-y' : 'touch-none') : '',
183
456
  ]"
184
457
  :style="side !== 'top' && side !== 'bottom' && width ? { width } : undefined"
458
+ @pointerdown="onPanelPointerDown"
459
+ @pointermove="onPanelPointerMove"
460
+ @pointerup="onPanelPointerUp"
461
+ @pointercancel="onPanelPointerUp"
462
+ @touchstart.passive="onPanelTouchStart"
463
+ @touchmove="onPanelTouchMove"
464
+ @touchend="onPanelTouchEnd"
465
+ @touchcancel="onPanelTouchEnd"
466
+ @click.capture="onPanelClickCapture"
185
467
  >
186
468
  <div v-if="$slots.header" class="shrink-0">
187
469
  <slot name="header" />
@@ -192,7 +474,14 @@ watch(
192
474
  </h2>
193
475
  </div>
194
476
 
195
- <div class="flex min-h-0 flex-1 flex-col overflow-y-auto px-3 py-2">
477
+ <!-- touch-action stops at the nearest scroll container, so this one needs pan-y of its own or a
478
+ finger landing on an item lets the browser take a left/right drawer's swipe (top/bottom: the
479
+ script narrows it, see updateListTouchAction) -->
480
+ <div
481
+ data-nd-list
482
+ class="scrollbar-none flex min-h-0 flex-1 flex-col overflow-y-auto px-3 py-2"
483
+ :class="swipeEnabled ? 'touch-pan-y overscroll-y-contain' : ''"
484
+ >
196
485
  <template v-for="(section, si) in sections" :key="si">
197
486
  <div v-if="si > 0" class="my-1 border-t border-outline-variant" />
198
487
 
@@ -329,7 +618,7 @@ watch(
329
618
  </div>
330
619
 
331
620
  <div
332
- class="flex min-h-0 flex-1 flex-col gap-1 overflow-y-auto overflow-x-hidden px-3 py-2"
621
+ class="scrollbar-none flex min-h-0 flex-1 flex-col gap-1 overflow-y-auto overflow-x-hidden px-3 py-2"
333
622
  >
334
623
  <template v-for="(section, si) in sections" :key="si">
335
624
  <div v-if="si > 0" class="my-1 border-t border-outline-variant" />
@@ -444,35 +733,6 @@ watch(
444
733
  </template>
445
734
 
446
735
  <style scoped>
447
- /* ── Modal transitions ─────────────────────────────────── */
448
- .nd-scrim {
449
- transition: opacity 280ms ease;
450
- }
451
- .nd-enter-from .nd-scrim,
452
- .nd-leave-to .nd-scrim {
453
- opacity: 0;
454
- }
455
-
456
- .nd-panel {
457
- transition: transform 300ms cubic-bezier(0.2, 0, 0, 1);
458
- }
459
- .nd-enter-from .nd-panel-left,
460
- .nd-leave-to .nd-panel-left {
461
- transform: translateX(-100%);
462
- }
463
- .nd-enter-from .nd-panel-right,
464
- .nd-leave-to .nd-panel-right {
465
- transform: translateX(100%);
466
- }
467
- .nd-enter-from .nd-panel-top,
468
- .nd-leave-to .nd-panel-top {
469
- transform: translateY(-100%);
470
- }
471
- .nd-enter-from .nd-panel-bottom,
472
- .nd-leave-to .nd-panel-bottom {
473
- transform: translateY(100%);
474
- }
475
-
476
736
  /* ── Section collapse animation ────────────────────────── */
477
737
  .nd-section-grid {
478
738
  display: grid;