@m3ui-vue/m3ui-vue 1.0.3 → 1.1.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.
- package/dist/API.md +75 -11
- package/dist/MMenuItem-BOkcVtEe.js +809 -0
- package/dist/MMenuItem-BOkcVtEe.js.map +1 -0
- package/dist/{MSlider-CdNIH7jc.js → MSlider-BXsBv0ox.js} +208 -182
- package/dist/MSlider-BXsBv0ox.js.map +1 -0
- package/dist/{MTextField-B4X92vH_.js → MTextField-rML9jnXs.js} +2 -2
- package/dist/{MTextField-B4X92vH_.js.map → MTextField-rML9jnXs.js.map} +1 -1
- package/dist/api.json +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MBottomSheet.vue.d.ts +20 -1
- package/dist/components/MDialog.vue.d.ts +17 -8
- package/dist/components/MDragDropList.vue.d.ts +1 -1
- package/dist/components/MMap.vue.d.ts +4 -4
- package/dist/components/MMapControl.vue.d.ts +20 -3
- package/dist/components/MMapMarker.vue.d.ts +5 -2
- package/dist/components/MMapPopup.vue.d.ts +5 -3
- package/dist/components/MMenuItem.vue.d.ts +2 -2
- package/dist/components/MSeekBar.vue.d.ts +122 -0
- package/dist/components/MSplitButton.vue.d.ts +2 -2
- package/dist/components/_MMapAttribution.vue.d.ts +7 -0
- package/dist/composables/_mapPin.d.ts +5 -0
- package/dist/composables/_popupPosition.d.ts +12 -0
- package/dist/composables/_useCalendarMonth.d.ts +24 -0
- package/dist/composables/_useEscapeStack.d.ts +3 -0
- package/dist/composables/_useScrollLock.d.ts +8 -0
- package/dist/composables/_useSheetMotion.d.ts +16 -1
- package/dist/composables/useLocale.d.ts +8 -0
- package/dist/index.d.ts +2 -0
- package/dist/locales/index.js +56 -0
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +5269 -4714
- package/dist/m3ui.js.map +1 -1
- package/dist/map.js +777 -641
- package/dist/map.js.map +1 -1
- package/dist/rich-text-editor.js +8 -8
- package/dist/styles.css +1 -1
- package/dist/{useLocale-b5-hzsL3.js → useLocale-CqQrhWFl.js} +9 -1
- package/dist/useLocale-CqQrhWFl.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/MDialog.test.ts +30 -0
- package/src/__tests__/MMap.test.ts +132 -2
- package/src/__tests__/MSeekBar.test.ts +170 -0
- package/src/__tests__/MTooltip.test.ts +110 -0
- package/src/__tests__/_fakeMaplibre.ts +3 -0
- package/src/__tests__/calendarMonth.test.ts +59 -0
- package/src/__tests__/pickerModals.test.ts +128 -0
- package/src/__tests__/popupPosition.test.ts +224 -0
- package/src/__tests__/scrollLock.test.ts +110 -0
- package/src/__tests__/sheets.test.ts +158 -0
- package/src/components/MAutocomplete.vue +2 -4
- package/src/components/MBottomSheet.vue +211 -13
- package/src/components/MCommandPalette.vue +4 -6
- package/src/components/MContextMenu.vue +19 -5
- package/src/components/MDatePicker.vue +29 -70
- package/src/components/MDatePickerModal.vue +116 -172
- package/src/components/MDateRangePicker.vue +29 -51
- package/src/components/MDateRangePickerModal.vue +84 -162
- package/src/components/MDialog.vue +87 -63
- package/src/components/MLightbox.vue +6 -3
- package/src/components/MMap.vue +74 -28
- package/src/components/MMapControl.vue +121 -7
- package/src/components/MMapIdentify.vue +2 -1
- package/src/components/MMapMarker.vue +14 -7
- package/src/components/MMapPopup.vue +8 -4
- package/src/components/MMenu.vue +31 -12
- package/src/components/MMenuItem.vue +21 -4
- package/src/components/MMultiAutocomplete.vue +2 -4
- package/src/components/MMultiSelect.vue +2 -4
- package/src/components/MNavigationDrawer.vue +2 -8
- package/src/components/MNavigationRail.vue +2 -3
- package/src/components/MSearchBar.vue +2 -5
- package/src/components/MSeekBar.vue +593 -0
- package/src/components/MSelect.vue +2 -8
- package/src/components/MSplitButton.vue +23 -7
- package/src/components/MSpotlightSearch.vue +4 -6
- package/src/components/MTimePicker.vue +23 -6
- package/src/components/MTimePickerModal.vue +213 -226
- package/src/components/MTooltip.vue +99 -3
- package/src/components/_MMapAttribution.vue +81 -0
- package/src/composables/_mapPin.ts +25 -0
- package/src/composables/_popupPosition.ts +39 -0
- package/src/composables/_useCalendarMonth.ts +74 -0
- package/src/composables/_useEscapeStack.ts +33 -0
- package/src/composables/_useScrollLock.ts +41 -0
- package/src/composables/_useSheetMotion.ts +82 -20
- package/src/composables/useLocale.ts +16 -0
- package/src/index.ts +2 -0
- package/src/locales/de.ts +8 -0
- package/src/locales/es.ts +8 -0
- package/src/locales/fr.ts +8 -0
- package/src/locales/ja.ts +8 -0
- package/src/locales/ko.ts +8 -0
- package/src/locales/pt.ts +8 -0
- package/src/locales/zh.ts +8 -0
- package/dist/MMenuItem-DFyXflk9.js +0 -707
- package/dist/MMenuItem-DFyXflk9.js.map +0 -1
- package/dist/MSlider-CdNIH7jc.js.map +0 -1
- package/dist/useLocale-b5-hzsL3.js.map +0 -1
|
@@ -1,30 +1,208 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
2
|
/**
|
|
3
3
|
* A sheet that slides up from the bottom of the screen, capped at a 640px max width and rounded top corners.
|
|
4
|
-
* Supports drag-to-dismiss via the handle bar, or tapping the scrim.
|
|
4
|
+
* Supports drag-to-dismiss via the handle bar, or tapping the scrim. With `partial`, a sheet taller than that
|
|
5
|
+
* share of the screen opens partially expanded and is dragged (handle or content) up to its full height,
|
|
6
|
+
* after Compose's ModalBottomSheet.
|
|
5
7
|
*
|
|
6
8
|
* @slot default Sheet body content (scrollable)
|
|
7
9
|
* @slot actions Footer action buttons, shown in a bordered strip below the content
|
|
8
10
|
* @event update:modelValue Emitted when the sheet closes (scrim click, drag-to-dismiss, or the close button) —
|
|
9
11
|
* powers v-model
|
|
12
|
+
* @event update:expanded Emitted when the sheet moves between partially and fully expanded (or closes) —
|
|
13
|
+
* powers v-model:expanded
|
|
10
14
|
*/
|
|
15
|
+
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
11
16
|
import MIcon from './MIcon.vue'
|
|
17
|
+
import { useLocale } from '../composables/useLocale'
|
|
12
18
|
import { useSheetMotion } from '../composables/_useSheetMotion'
|
|
13
19
|
|
|
14
|
-
withDefaults(defineProps<{
|
|
20
|
+
const props = withDefaults(defineProps<{
|
|
15
21
|
/** Open/closed state (v-model) */
|
|
16
22
|
modelValue: boolean
|
|
17
23
|
/** Sheet header title */
|
|
18
24
|
title?: string
|
|
19
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Tallest the sheet gets, as a CSS length (`'480px'`, `'70dvh'`); past it the content scrolls. Default 60vh,
|
|
27
|
+
* or the whole screen with `partial`
|
|
28
|
+
*/
|
|
29
|
+
maxHeight?: string
|
|
30
|
+
/** @deprecated Use `max-height="92vh"` */
|
|
20
31
|
fullHeight?: boolean
|
|
21
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Opens a sheet taller than this share of the screen partially expanded (`true` = half, or a ratio
|
|
34
|
+
* like 0.4); dragging the handle or the content up expands it fully
|
|
35
|
+
*/
|
|
36
|
+
partial?: boolean | number
|
|
37
|
+
/** Fully (true) or partially (false) expanded, with `partial` (v-model:expanded) */
|
|
38
|
+
expanded?: boolean
|
|
39
|
+
/** Squares the top corners while the expanded sheet fills the screen, as Android's views sheet does */
|
|
40
|
+
squareCornersWhenFull?: boolean
|
|
41
|
+
}>(), { fullHeight: false, partial: false, expanded: false, squareCornersWhenFull: false })
|
|
22
42
|
|
|
23
|
-
const emit = defineEmits<{ 'update:modelValue': [boolean] }>()
|
|
43
|
+
const emit = defineEmits<{ 'update:modelValue': [boolean]; 'update:expanded': [boolean] }>()
|
|
24
44
|
const close = () => emit('update:modelValue', false)
|
|
45
|
+
const locale = useLocale()
|
|
25
46
|
|
|
26
|
-
//
|
|
27
|
-
const
|
|
47
|
+
// Only the safe area is kept clear at the top, like the status bar inset in Compose
|
|
48
|
+
const FULL = 'calc(100dvh - env(safe-area-inset-top, 0px))'
|
|
49
|
+
const ratio = computed(() =>
|
|
50
|
+
props.partial === true ? 0.5 : typeof props.partial === 'number' ? Math.min(1, Math.max(0, props.partial)) : 0,
|
|
51
|
+
)
|
|
52
|
+
const cap = computed(() => props.maxHeight ?? (ratio.value ? FULL : props.fullHeight ? '92vh' : '60vh'))
|
|
53
|
+
|
|
54
|
+
// The partially expanded offset, as a fraction of the panel's height: it shows `ratio` of the screen, and
|
|
55
|
+
// exists only when the sheet is taller than that (calculatePartiallyExpandedOffset / isPartiallyExpandedAnchorAvailable)
|
|
56
|
+
function partialAnchor(panel: HTMLElement): number | null {
|
|
57
|
+
if (!ratio.value) return null
|
|
58
|
+
const h = panel.offsetHeight
|
|
59
|
+
const visible = window.innerHeight * ratio.value
|
|
60
|
+
return h > visible ? (h - visible) / h : null
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const isExpanded = ref(true)
|
|
64
|
+
// The sheet is tall enough for a partially expanded state (the handle then toggles it)
|
|
65
|
+
const hasPartial = ref(false)
|
|
66
|
+
const dragging = ref(false)
|
|
67
|
+
const fillsScreen = ref(false)
|
|
68
|
+
const panelEl = ref<HTMLElement>()
|
|
69
|
+
const contentEl = ref<HTMLElement>()
|
|
70
|
+
|
|
71
|
+
// Slide in/out and drags between the anchors, as springs (see _useSheetMotion).
|
|
72
|
+
const motion = useSheetMotion('y', close, {
|
|
73
|
+
anchors: (panel) => {
|
|
74
|
+
const p = partialAnchor(panel)
|
|
75
|
+
return p === null ? [0] : [0, p]
|
|
76
|
+
},
|
|
77
|
+
openAt: (panel) => (props.expanded ? 0 : partialAnchor(panel) ?? 0),
|
|
78
|
+
onAnchor: (anchor, panel) => {
|
|
79
|
+
hasPartial.value = !!panel && partialAnchor(panel) !== null
|
|
80
|
+
fillsScreen.value = !!panel && panel.offsetHeight >= window.innerHeight - 2
|
|
81
|
+
setExpanded(anchor === 0)
|
|
82
|
+
},
|
|
83
|
+
onDrag: (active) => { dragging.value = active },
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
function setExpanded(v: boolean) {
|
|
87
|
+
const changed = v !== isExpanded.value
|
|
88
|
+
isExpanded.value = v
|
|
89
|
+
if (ratio.value && changed) emit('update:expanded', v)
|
|
90
|
+
updateContentTouchAction()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
watch(() => props.expanded, (v) => {
|
|
94
|
+
const panel = panelEl.value
|
|
95
|
+
if (!props.modelValue || !panel || v === isExpanded.value) return
|
|
96
|
+
motion.snapTo(v ? 0 : partialAnchor(panel) ?? 0)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
watch(() => props.modelValue, (open) => {
|
|
100
|
+
if (!open && isExpanded.value && ratio.value) setExpanded(false)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
// Content or window size changed: stay on the same state, its anchor recomputed
|
|
104
|
+
function reanchor() {
|
|
105
|
+
const panel = panelEl.value
|
|
106
|
+
if (!panel || !props.modelValue || dragging.value) return
|
|
107
|
+
const p = partialAnchor(panel)
|
|
108
|
+
hasPartial.value = p !== null
|
|
109
|
+
fillsScreen.value = panel.offsetHeight >= window.innerHeight - 2
|
|
110
|
+
if (!isExpanded.value) motion.snapTo(p ?? 0)
|
|
111
|
+
}
|
|
112
|
+
let observer: ResizeObserver | null = null
|
|
113
|
+
watch(panelEl, (panel) => {
|
|
114
|
+
observer?.disconnect()
|
|
115
|
+
observer = null
|
|
116
|
+
if (!panel || typeof ResizeObserver === 'undefined') return
|
|
117
|
+
observer = new ResizeObserver(reanchor)
|
|
118
|
+
observer.observe(panel)
|
|
119
|
+
})
|
|
120
|
+
onMounted(() => window.addEventListener('resize', reanchor))
|
|
121
|
+
onBeforeUnmount(() => {
|
|
122
|
+
observer?.disconnect()
|
|
123
|
+
window.removeEventListener('resize', reanchor)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
// ── The handle: drag it (pointer events, touch-none), or with `partial` tap it to toggle ─────────────
|
|
127
|
+
let handleDownY = 0
|
|
128
|
+
function onHandleDown(e: PointerEvent) {
|
|
129
|
+
handleDownY = e.clientY
|
|
130
|
+
motion.onDragStart(e)
|
|
131
|
+
}
|
|
132
|
+
function toggle() {
|
|
133
|
+
const panel = panelEl.value
|
|
134
|
+
const p = panel && partialAnchor(panel)
|
|
135
|
+
if (p == null) return
|
|
136
|
+
motion.snapTo(isExpanded.value ? p : 0)
|
|
137
|
+
}
|
|
138
|
+
function onHandleClick(e: MouseEvent) {
|
|
139
|
+
if (ratio.value && Math.abs(e.clientY - handleDownY) < 4) toggle()
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ── With `partial`, the content drags the sheet too (ConsumeSwipeWithinBottomSheetBoundsNestedScrollConnection):
|
|
143
|
+
// a drag up expands it before the content scrolls, a drag down with the content at its top collapses or
|
|
144
|
+
// dismisses it. The drag runs along the content's scroll, so a touch goes through touch events and the first
|
|
145
|
+
// move decides, as in MNavigationDrawer; touch-action allows only the scroll that can actually happen (none while
|
|
146
|
+
// partially expanded, pan-down at the top), or Chrome delivers the touchmoves at 60Hz on a 120Hz phone.
|
|
147
|
+
let touch: { id: number; x: number; y: number; active: boolean; scrollFrom: number } | null = null
|
|
148
|
+
|
|
149
|
+
function updateContentTouchAction() {
|
|
150
|
+
const el = contentEl.value
|
|
151
|
+
if (!el || !ratio.value) return
|
|
152
|
+
const directional = typeof CSS !== 'undefined' && !!CSS.supports?.('touch-action', 'pan-down')
|
|
153
|
+
el.style.touchAction = !isExpanded.value
|
|
154
|
+
? 'pan-x'
|
|
155
|
+
: el.scrollTop <= 0 && directional ? 'pan-x pan-down' : 'pan-x pan-y'
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function onTouchStart(e: TouchEvent) {
|
|
159
|
+
if (!ratio.value || e.touches.length !== 1) return
|
|
160
|
+
if ((e.target as Element).closest('[data-sheet-handle]')) return
|
|
161
|
+
const t = e.touches[0]!
|
|
162
|
+
touch = { id: t.identifier, x: t.clientX, y: t.clientY, active: false, scrollFrom: 0 }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function onTouchMove(e: TouchEvent) {
|
|
166
|
+
if (!touch) return
|
|
167
|
+
const id = touch.id
|
|
168
|
+
const t = Array.from(e.changedTouches).find((x) => x.identifier === id)
|
|
169
|
+
if (!t) return
|
|
170
|
+
if (!touch.active) {
|
|
171
|
+
const dy = t.clientY - touch.y
|
|
172
|
+
const dx = t.clientX - touch.x
|
|
173
|
+
if (Math.abs(dx) > 8 && Math.abs(dx) > Math.abs(dy)) { touch = null; return }
|
|
174
|
+
if (Math.abs(dy) < 2) return
|
|
175
|
+
const content = contentEl.value
|
|
176
|
+
const scrolled = !!content && content.contains(e.target as Node) && content.scrollTop > 0
|
|
177
|
+
const sheetTakes = dy < 0 ? motion.target() > 1e-3 : !scrolled
|
|
178
|
+
if (!sheetTakes) { touch = null; return }
|
|
179
|
+
touch.active = true
|
|
180
|
+
touch.scrollFrom = content?.scrollTop ?? 0
|
|
181
|
+
motion.dragStart(e.currentTarget as Element, touch.y)
|
|
182
|
+
}
|
|
183
|
+
// Past fully expanded, the rest of the drag scrolls the content
|
|
184
|
+
const over = motion.dragMove(t.clientY)
|
|
185
|
+
if (contentEl.value && over > 0) contentEl.value.scrollTop = touch.scrollFrom + over
|
|
186
|
+
if (e.cancelable) e.preventDefault()
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function onTouchEnd(e: TouchEvent) {
|
|
190
|
+
if (!touch) return
|
|
191
|
+
const id = touch.id
|
|
192
|
+
if (!Array.from(e.changedTouches).some((x) => x.identifier === id)) return
|
|
193
|
+
const wasActive = touch.active
|
|
194
|
+
touch = null
|
|
195
|
+
if (wasActive) motion.dragEnd()
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// A mouse wheel scrolling down a partially expanded sheet expands it first
|
|
199
|
+
function onWheel(e: WheelEvent) {
|
|
200
|
+
if (!ratio.value || isExpanded.value || e.deltaY <= 0 || !hasPartial.value) return
|
|
201
|
+
e.preventDefault()
|
|
202
|
+
motion.snapTo(0)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const squareCorners = computed(() => props.squareCornersWhenFull && isExpanded.value && fillsScreen.value && !dragging.value)
|
|
28
206
|
</script>
|
|
29
207
|
|
|
30
208
|
<template>
|
|
@@ -36,17 +214,32 @@ const motion = useSheetMotion('y', close)
|
|
|
36
214
|
|
|
37
215
|
<!-- Panel -->
|
|
38
216
|
<div
|
|
217
|
+
ref="panelEl"
|
|
39
218
|
data-sheet-panel
|
|
40
|
-
class="relative mx-auto flex w-full max-w-[640px] flex-col
|
|
41
|
-
:class="
|
|
219
|
+
class="relative mx-auto flex w-full max-w-[640px] flex-col bg-surface-container-low shadow-elevation-1 transition-[border-radius] duration-200"
|
|
220
|
+
:class="[squareCorners ? 'rounded-t-none' : 'rounded-t-[28px]', ratio ? 'touch-none' : '']"
|
|
221
|
+
:style="{ maxHeight: cap }"
|
|
222
|
+
@touchstart.passive="onTouchStart"
|
|
223
|
+
@touchmove="onTouchMove"
|
|
224
|
+
@touchend="onTouchEnd"
|
|
225
|
+
@touchcancel="onTouchEnd"
|
|
226
|
+
@wheel="onWheel"
|
|
42
227
|
>
|
|
43
228
|
<!-- Drag handle -->
|
|
44
229
|
<div
|
|
45
|
-
|
|
46
|
-
|
|
230
|
+
data-sheet-handle
|
|
231
|
+
class="flex h-12 shrink-0 cursor-grab touch-none select-none items-center justify-center outline-none active:cursor-grabbing focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-secondary"
|
|
232
|
+
:role="hasPartial ? 'button' : undefined"
|
|
233
|
+
:tabindex="hasPartial ? 0 : undefined"
|
|
234
|
+
:aria-label="hasPartial ? (isExpanded ? locale.sheetCollapse : locale.sheetExpand) : undefined"
|
|
235
|
+
:aria-expanded="hasPartial ? isExpanded : undefined"
|
|
236
|
+
@pointerdown="onHandleDown"
|
|
47
237
|
@pointermove="motion.onDragMove"
|
|
48
238
|
@pointerup="motion.onDragEnd"
|
|
49
239
|
@pointercancel="motion.onDragEnd"
|
|
240
|
+
@click="onHandleClick"
|
|
241
|
+
@keydown.enter.prevent="toggle"
|
|
242
|
+
@keydown.space.prevent="toggle"
|
|
50
243
|
>
|
|
51
244
|
<div class="h-1 w-8 rounded-full bg-on-surface-variant" />
|
|
52
245
|
</div>
|
|
@@ -63,8 +256,13 @@ const motion = useSheetMotion('y', close)
|
|
|
63
256
|
</button>
|
|
64
257
|
</div>
|
|
65
258
|
|
|
66
|
-
<!-- Content -->
|
|
67
|
-
<div
|
|
259
|
+
<!-- Content (touch-action set by updateContentTouchAction with `partial`: it stops at this scroll container) -->
|
|
260
|
+
<div
|
|
261
|
+
ref="contentEl"
|
|
262
|
+
class="flex-1 overflow-y-auto px-6 pb-6"
|
|
263
|
+
:class="ratio ? 'overscroll-y-contain' : ''"
|
|
264
|
+
@scroll.passive="updateContentTouchAction"
|
|
265
|
+
>
|
|
68
266
|
<slot />
|
|
69
267
|
</div>
|
|
70
268
|
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import { ref, computed, watch, nextTick, onMounted, onBeforeUnmount } from 'vue'
|
|
11
11
|
import MIcon from './MIcon.vue'
|
|
12
12
|
import { useLocale } from '../composables/useLocale'
|
|
13
|
+
import { useScrollLock } from '../composables/_useScrollLock'
|
|
13
14
|
|
|
14
15
|
const locale = useLocale()
|
|
15
16
|
|
|
@@ -129,15 +130,12 @@ function onGlobalKeydown(e: KeyboardEvent) {
|
|
|
129
130
|
watch(
|
|
130
131
|
() => props.modelValue,
|
|
131
132
|
(open) => {
|
|
132
|
-
if (open)
|
|
133
|
-
document.body.style.overflow = 'hidden'
|
|
134
|
-
nextTick(() => inputRef.value?.focus())
|
|
135
|
-
} else {
|
|
136
|
-
document.body.style.overflow = ''
|
|
137
|
-
}
|
|
133
|
+
if (open) nextTick(() => inputRef.value?.focus())
|
|
138
134
|
},
|
|
139
135
|
)
|
|
140
136
|
|
|
137
|
+
useScrollLock(() => props.modelValue)
|
|
138
|
+
|
|
141
139
|
watch(query, () => { activeIndex.value = 0 })
|
|
142
140
|
|
|
143
141
|
onMounted(() => document.addEventListener('keydown', onGlobalKeydown))
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import { computed, Fragment, inject, nextTick, onMounted, onUnmounted, provide, ref, useSlots, watch, type VNode } from 'vue'
|
|
11
11
|
import MIcon from './MIcon.vue'
|
|
12
12
|
import MMenuDivider from './MMenuDivider.vue'
|
|
13
|
+
import { POPUP_MARGIN, fitPopup } from '../composables/_popupPosition'
|
|
13
14
|
|
|
14
15
|
defineOptions({ inheritAttrs: false })
|
|
15
16
|
|
|
@@ -75,6 +76,7 @@ const isGrouped = computed(() => !!groups.value && groups.value.length > 1)
|
|
|
75
76
|
|
|
76
77
|
const visible = ref(false)
|
|
77
78
|
const adjustedPos = ref({ x: 0, y: 0 })
|
|
79
|
+
const origin = ref('left top')
|
|
78
80
|
const panelEl = ref<HTMLElement | null>(null)
|
|
79
81
|
|
|
80
82
|
// See MMenu.vue for why this is needed: a menu nested inside another
|
|
@@ -255,6 +257,7 @@ function popNav() {
|
|
|
255
257
|
}
|
|
256
258
|
|
|
257
259
|
async function showAt(x: number, y: number) {
|
|
260
|
+
origin.value = 'left top'
|
|
258
261
|
if (siblingGroup && siblingGroup.current && siblingGroup.current !== hide) {
|
|
259
262
|
siblingGroup.current()
|
|
260
263
|
}
|
|
@@ -265,10 +268,19 @@ async function showAt(x: number, y: number) {
|
|
|
265
268
|
await nextTick()
|
|
266
269
|
if (!panelEl.value) return
|
|
267
270
|
const el = panelEl.value
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
271
|
+
// Opens right of and below the point, else flipped left or up, else clamped on screen (see _popupPosition.ts).
|
|
272
|
+
// Measured at the window's left edge: near the right edge the browser would narrow it to the room left there.
|
|
273
|
+
el.style.left = '0px'
|
|
274
|
+
const w = el.offsetWidth
|
|
275
|
+
const h = el.offsetHeight
|
|
276
|
+
const left = fitPopup([x, x - w], w, window.innerWidth)
|
|
277
|
+
const top = fitPopup([y, y - h], h, window.innerHeight)
|
|
278
|
+
adjustedPos.value = { x: left, y: top }
|
|
279
|
+
el.style.left = `${left}px`
|
|
280
|
+
// The animation grows from the point
|
|
281
|
+
const ox = left === x ? 'left' : left === x - w ? 'right' : `${Math.min(w, Math.max(0, x - left))}px`
|
|
282
|
+
const oy = top === y ? 'top' : top === y - h ? 'bottom' : `${Math.min(h, Math.max(0, y - top))}px`
|
|
283
|
+
origin.value = `${ox} ${oy}`
|
|
272
284
|
focusableItems()[0]?.focus()
|
|
273
285
|
}
|
|
274
286
|
|
|
@@ -365,7 +377,7 @@ onUnmounted(() => {
|
|
|
365
377
|
variant === 'expressive' ? 'rounded-lg' : 'rounded-xs',
|
|
366
378
|
isGrouped ? '' : 'shadow-elevation-2',
|
|
367
379
|
]"
|
|
368
|
-
:style="{ left: `${adjustedPos.x}px`, top: `${adjustedPos.y}px`, transformOrigin:
|
|
380
|
+
:style="{ left: `${adjustedPos.x}px`, top: `${adjustedPos.y}px`, transformOrigin: origin, maxWidth: `calc(100vw - ${2 * POPUP_MARGIN}px)` }"
|
|
369
381
|
@contextmenu.prevent
|
|
370
382
|
>
|
|
371
383
|
<!-- overflow-hidden (for the rounded-corner clip and to clip the push
|
|
@@ -373,8 +385,10 @@ onUnmounted(() => {
|
|
|
373
385
|
shadow-elevation-2 on the same box — overflow-hidden clips an
|
|
374
386
|
element's own box-shadow too, so the two have to be on separate
|
|
375
387
|
elements or the shadow gets cut off -->
|
|
388
|
+
<!-- Scrolls when taller than the window -->
|
|
376
389
|
<div
|
|
377
390
|
class="overflow-hidden py-1"
|
|
391
|
+
:style="{ maxHeight: `calc(100vh - ${2 * POPUP_MARGIN}px)`, overflowY: 'auto' }"
|
|
378
392
|
:class="[
|
|
379
393
|
variant === 'expressive' ? 'rounded-lg' : 'rounded-xs',
|
|
380
394
|
isGrouped ? '' : 'bg-surface-container',
|
|
@@ -9,6 +9,8 @@ import MIcon from './MIcon.vue'
|
|
|
9
9
|
import MIconButton from './MIconButton.vue'
|
|
10
10
|
import { useFieldBg } from '../composables/useFieldBg'
|
|
11
11
|
import { useLocale } from '../composables/useLocale'
|
|
12
|
+
import { POPUP_MARGIN, dropdownX, fitPopup } from '../composables/_popupPosition'
|
|
13
|
+
import { useCalendarMonth } from '../composables/_useCalendarMonth'
|
|
12
14
|
|
|
13
15
|
const props = defineProps<{
|
|
14
16
|
/** Selected date (YYYY-MM-DD, v-model) */
|
|
@@ -54,7 +56,7 @@ const emit = defineEmits<{ 'update:modelValue': [string | null] }>()
|
|
|
54
56
|
const open = ref(false)
|
|
55
57
|
const triggerEl = ref<HTMLElement | null>(null)
|
|
56
58
|
const panelEl = ref<HTMLElement | null>(null)
|
|
57
|
-
const dropPos = ref({ top: '0px', left: '0px' })
|
|
59
|
+
const dropPos = ref<Record<string, string>>({ top: '0px', left: '0px' })
|
|
58
60
|
const { resolvedFieldBg } = useFieldBg(triggerEl, () => props.fieldBg)
|
|
59
61
|
|
|
60
62
|
const viewDate = ref(props.modelValue ? new Date(props.modelValue + 'T00:00:00') : new Date())
|
|
@@ -62,72 +64,13 @@ watch(() => props.modelValue, (v) => {
|
|
|
62
64
|
if (v) viewDate.value = new Date(v + 'T00:00:00')
|
|
63
65
|
})
|
|
64
66
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
})
|
|
67
|
+
// Weekday names, the month's title and its six-week grid, shared with the other date pickers
|
|
68
|
+
const { weekdays: WEEKDAYS, monthLabel, days: calendarDays, isOutOfRange, prevMonth, nextMonth, isToday } = useCalendarMonth(viewDate, {
|
|
69
|
+
locale: () => resolvedLocale.value,
|
|
70
|
+
min: () => props.min,
|
|
71
|
+
max: () => props.max,
|
|
71
72
|
})
|
|
72
73
|
|
|
73
|
-
const monthLabel = computed(() => {
|
|
74
|
-
const f = new Intl.DateTimeFormat(resolvedLocale.value, { month: 'long', year: 'numeric' })
|
|
75
|
-
return f.format(viewDate.value)
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
const calendarDays = computed(() => {
|
|
79
|
-
const y = viewDate.value.getFullYear()
|
|
80
|
-
const m = viewDate.value.getMonth()
|
|
81
|
-
const first = new Date(y, m, 1)
|
|
82
|
-
const startDay = (first.getDay() + 6) % 7
|
|
83
|
-
const daysInMonth = new Date(y, m + 1, 0).getDate()
|
|
84
|
-
const days: { date: number; current: boolean; iso: string; disabled: boolean }[] = []
|
|
85
|
-
|
|
86
|
-
const prevMonth = new Date(y, m, 0).getDate()
|
|
87
|
-
for (let i = startDay - 1; i >= 0; i--) {
|
|
88
|
-
const d = prevMonth - i
|
|
89
|
-
const iso = fmt(y, m - 1, d)
|
|
90
|
-
days.push({ date: d, current: false, iso, disabled: isOutOfRange(iso) })
|
|
91
|
-
}
|
|
92
|
-
for (let d = 1; d <= daysInMonth; d++) {
|
|
93
|
-
const iso = fmt(y, m, d)
|
|
94
|
-
days.push({ date: d, current: true, iso, disabled: isOutOfRange(iso) })
|
|
95
|
-
}
|
|
96
|
-
const remaining = 42 - days.length
|
|
97
|
-
for (let d = 1; d <= remaining; d++) {
|
|
98
|
-
const iso = fmt(y, m + 1, d)
|
|
99
|
-
days.push({ date: d, current: false, iso, disabled: isOutOfRange(iso) })
|
|
100
|
-
}
|
|
101
|
-
return days
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
function fmt(y: number, m: number, d: number) {
|
|
105
|
-
const dt = new Date(y, m, d)
|
|
106
|
-
const yy = dt.getFullYear()
|
|
107
|
-
const mm = String(dt.getMonth() + 1).padStart(2, '0')
|
|
108
|
-
const dd = String(dt.getDate()).padStart(2, '0')
|
|
109
|
-
return `${yy}-${mm}-${dd}`
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function isOutOfRange(iso: string) {
|
|
113
|
-
if (props.min && iso < props.min) return true
|
|
114
|
-
if (props.max && iso > props.max) return true
|
|
115
|
-
return false
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const isToday = (iso: string) => iso === fmt(new Date().getFullYear(), new Date().getMonth(), new Date().getDate())
|
|
119
|
-
|
|
120
|
-
function prevMonth() {
|
|
121
|
-
const d = new Date(viewDate.value)
|
|
122
|
-
d.setMonth(d.getMonth() - 1)
|
|
123
|
-
viewDate.value = d
|
|
124
|
-
}
|
|
125
|
-
function nextMonth() {
|
|
126
|
-
const d = new Date(viewDate.value)
|
|
127
|
-
d.setMonth(d.getMonth() + 1)
|
|
128
|
-
viewDate.value = d
|
|
129
|
-
}
|
|
130
|
-
|
|
131
74
|
function selectDay(day: typeof calendarDays.value[0]) {
|
|
132
75
|
if (day.disabled) return
|
|
133
76
|
emit('update:modelValue', day.iso)
|
|
@@ -144,18 +87,33 @@ const displayValue = computed(() => {
|
|
|
144
87
|
return new Intl.DateTimeFormat(resolvedLocale.value, { day: 'numeric', month: 'short', year: 'numeric' }).format(d)
|
|
145
88
|
})
|
|
146
89
|
|
|
90
|
+
// The panel's size isn't known until it's in the DOM: placeDrop() keeps it on screen once it is, before it paints
|
|
91
|
+
// (see _popupPosition.ts): under the field, aligned with its start edge, else its end edge or the window edge;
|
|
92
|
+
// above it when it doesn't fit below. Until then it waits at the window's left edge, where nothing narrows it.
|
|
93
|
+
let anchorRect: DOMRect | null = null
|
|
147
94
|
function computeDropPos() {
|
|
148
95
|
if (!triggerEl.value) return
|
|
149
96
|
const rect = triggerEl.value.getBoundingClientRect()
|
|
150
|
-
|
|
151
|
-
const spaceBelow = window.innerHeight - rect.bottom - 8
|
|
152
|
-
const above = spaceBelow < panelH && rect.top > panelH
|
|
97
|
+
anchorRect = rect
|
|
153
98
|
dropPos.value = {
|
|
154
|
-
top:
|
|
155
|
-
left:
|
|
99
|
+
top: `${rect.bottom + 4}px`,
|
|
100
|
+
left: '0px',
|
|
101
|
+
maxWidth: `${window.innerWidth - 2 * POPUP_MARGIN}px`,
|
|
156
102
|
}
|
|
157
103
|
}
|
|
158
104
|
|
|
105
|
+
function placeDrop(el: Element) {
|
|
106
|
+
if (!anchorRect) return
|
|
107
|
+
const panel = el as HTMLElement
|
|
108
|
+
const w = panel.offsetWidth
|
|
109
|
+
const h = panel.offsetHeight
|
|
110
|
+
const left = dropdownX(anchorRect, w, window.innerWidth, 'start')
|
|
111
|
+
const top = fitPopup([anchorRect.bottom + 4, anchorRect.top - 4 - h], h, window.innerHeight)
|
|
112
|
+
dropPos.value = { ...dropPos.value, left: `${left}px`, top: `${top}px` }
|
|
113
|
+
panel.style.left = `${left}px`
|
|
114
|
+
panel.style.top = `${top}px`
|
|
115
|
+
}
|
|
116
|
+
|
|
159
117
|
function onClickOutside(e: MouseEvent) {
|
|
160
118
|
const t = e.target as Node
|
|
161
119
|
if (triggerEl.value?.contains(t)) return
|
|
@@ -230,6 +188,7 @@ onUnmounted(() => {
|
|
|
230
188
|
enter-from-class="opacity-0 -translate-y-1 scale-[0.98]"
|
|
231
189
|
leave-active-class="transition-[opacity,transform] duration-100"
|
|
232
190
|
leave-to-class="opacity-0 -translate-y-1 scale-[0.98]"
|
|
191
|
+
@enter="placeDrop"
|
|
233
192
|
>
|
|
234
193
|
<div
|
|
235
194
|
v-if="open"
|