@m3ui-vue/m3ui-vue 0.8.8 → 0.9.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/README.md +13 -3
- package/dist/{MMenuItem-5xojJYAL.js → MTextField-B0wGv1Mx.js} +374 -364
- package/dist/MTextField-B0wGv1Mx.js.map +1 -0
- package/dist/barcode.js +9 -11
- package/dist/barcode.js.map +1 -1
- package/dist/code-editor.js +2 -2
- package/dist/code-editor.js.map +1 -1
- package/dist/components/MCarousel.vue.d.ts +55 -36
- package/dist/components/MDialog.vue.d.ts +4 -2
- package/dist/components/MEmojiSelector.vue.d.ts +21 -5
- package/dist/components/MFabMenu.vue.d.ts +1 -1
- package/dist/components/MFlex.vue.d.ts +1 -1
- package/dist/components/MLightbox.vue.d.ts +15 -5
- package/dist/components/MScheduler.vue.d.ts +38 -1
- package/dist/components/MShape.vue.d.ts +1 -1
- package/dist/components/MSimpleCarousel.vue.d.ts +2 -2
- package/dist/components/MSlider.vue.d.ts +1 -1
- package/dist/components/MStack.vue.d.ts +1 -1
- package/dist/components/MToolbar.vue.d.ts +1 -1
- package/dist/components/MWindow.vue.d.ts +1 -1
- package/dist/composables/_carouselStrategy.d.ts +101 -0
- package/dist/composables/useLocale.d.ts +19 -0
- package/dist/data/emoji-names/emoji-names-de.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-en.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-es.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-fr.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-ja.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-ko.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-pt.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-zh.d.ts +2 -0
- package/dist/data/emojis.d.ts +7 -1
- package/dist/emoji-names-de-BphO5XpV.js +1877 -0
- package/dist/emoji-names-de-BphO5XpV.js.map +1 -0
- package/dist/emoji-names-en-9ZvMRvzi.js +1877 -0
- package/dist/emoji-names-en-9ZvMRvzi.js.map +1 -0
- package/dist/emoji-names-es-DvfWU3Pj.js +1877 -0
- package/dist/emoji-names-es-DvfWU3Pj.js.map +1 -0
- package/dist/emoji-names-fr-B-jhUlsP.js +1877 -0
- package/dist/emoji-names-fr-B-jhUlsP.js.map +1 -0
- package/dist/emoji-names-ja-l8cMFJo1.js +1877 -0
- package/dist/emoji-names-ja-l8cMFJo1.js.map +1 -0
- package/dist/emoji-names-ko-YP-o9G0c.js +1877 -0
- package/dist/emoji-names-ko-YP-o9G0c.js.map +1 -0
- package/dist/emoji-names-pt-DGj_Js4M.js +1877 -0
- package/dist/emoji-names-pt-DGj_Js4M.js.map +1 -0
- package/dist/emoji-names-zh-Dzecwtw_.js +1877 -0
- package/dist/emoji-names-zh-Dzecwtw_.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/locales/index.js +140 -7
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +3885 -2400
- package/dist/m3ui.js.map +1 -1
- package/dist/plugin.d.ts +6 -0
- package/dist/rich-text-editor.js +9 -9
- package/dist/styles.css +1 -1
- package/dist/terminal.js +34 -27
- package/dist/terminal.js.map +1 -1
- package/dist/{useLocale-D3ZWZ8jY.js → useLocale-CmI675Ab.js} +21 -2
- package/dist/useLocale-CmI675Ab.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/MCarousel.test.ts +183 -21
- package/src/__tests__/MDialog.test.ts +18 -0
- package/src/__tests__/MEmojiSelector.test.ts +138 -0
- package/src/__tests__/MLightbox.test.ts +313 -5
- package/src/__tests__/MScheduler.test.ts +122 -0
- package/src/__tests__/MTerminal.test.ts +63 -0
- package/src/__tests__/attrsFallthrough.test.ts +65 -0
- package/src/__tests__/plugin.test.ts +28 -1
- package/src/components/MBarcode.vue +4 -2
- package/src/components/MBottomSheet.vue +1 -1
- package/src/components/MCarousel.vue +526 -200
- package/src/components/MCodeEditor.vue +3 -2
- package/src/components/MCommandPalette.vue +1 -1
- package/src/components/MContextMenu.vue +3 -1
- package/src/components/MDatePickerModal.vue +1 -1
- package/src/components/MDateRangePickerModal.vue +1 -1
- package/src/components/MDialog.vue +19 -6
- package/src/components/MEmojiSelector.vue +479 -78
- package/src/components/MFab.vue +3 -1
- package/src/components/MLightbox.vue +750 -268
- package/src/components/MLoadingOverlay.vue +6 -1
- package/src/components/MMenu.vue +3 -1
- package/src/components/MNavigationDrawer.vue +5 -1
- package/src/components/MNavigationRail.vue +3 -0
- package/src/components/MOverlay.vue +1 -0
- package/src/components/MScheduler.vue +282 -115
- package/src/components/MSideSheet.vue +1 -1
- package/src/components/MSplitButton.vue +3 -1
- package/src/components/MSpotlightSearch.vue +1 -1
- package/src/components/MTerminal.vue +22 -10
- package/src/components/MTimePickerModal.vue +1 -1
- package/src/components/MTooltip.vue +3 -0
- package/src/components/MTour.vue +2 -2
- package/src/composables/_carouselStrategy.ts +712 -0
- package/src/composables/useLocale.ts +38 -0
- package/src/data/emoji-names/emoji-names-de.ts +1875 -0
- package/src/data/emoji-names/emoji-names-en.ts +1875 -0
- package/src/data/emoji-names/emoji-names-es.ts +1875 -0
- package/src/data/emoji-names/emoji-names-fr.ts +1875 -0
- package/src/data/emoji-names/emoji-names-ja.ts +1875 -0
- package/src/data/emoji-names/emoji-names-ko.ts +1875 -0
- package/src/data/emoji-names/emoji-names-pt.ts +1875 -0
- package/src/data/emoji-names/emoji-names-zh.ts +1875 -0
- package/src/data/emojis.ts +29 -198
- package/src/index.ts +2 -2
- package/src/locales/de.ts +19 -0
- package/src/locales/es.ts +19 -0
- package/src/locales/fr.ts +19 -0
- package/src/locales/ja.ts +19 -0
- package/src/locales/ko.ts +19 -0
- package/src/locales/pt.ts +19 -0
- package/src/locales/zh.ts +19 -0
- package/src/plugin.ts +12 -1
- package/dist/MMenuItem-5xojJYAL.js.map +0 -1
- package/dist/useLocale-D3ZWZ8jY.js.map +0 -1
|
@@ -1,275 +1,601 @@
|
|
|
1
|
-
<script
|
|
2
|
-
import { ref, computed, onMounted, onBeforeUnmount, nextTick } from 'vue'
|
|
3
|
-
import MIcon from './MIcon.vue'
|
|
4
|
-
|
|
1
|
+
<script lang="ts">
|
|
5
2
|
export interface CarouselItem {
|
|
6
3
|
src: string
|
|
7
4
|
alt?: string
|
|
8
5
|
label?: string
|
|
9
6
|
}
|
|
10
7
|
|
|
8
|
+
export type CarouselVariant = 'multi-browse' | 'uncontained' | 'hero' | 'full-screen'
|
|
9
|
+
export type CarouselFlingBehavior = 'single-advance' | 'multi-browse' | 'no-snap'
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<script setup lang="ts" generic="T = CarouselItem">
|
|
11
13
|
/*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
14
|
+
* M3 carousel, ported from Compose Material3 (carousel/Carousel.kt and the keyline engine in
|
|
15
|
+
* ../composables/_carouselStrategy.ts). Like Compose's pager-based Carousel, every item has the
|
|
16
|
+
* same size (the strategy's focal size) and they're laid out end to end; what changes while
|
|
17
|
+
* scrolling is only how each one looks: it's clipped to a mask, centered in the item, the size of
|
|
18
|
+
* the keyline it sits on, and translated so masked items touch. The scroll geometry never changes,
|
|
19
|
+
* which is what the old flex/width-based version got wrong (resizing items mid-scroll fought the
|
|
20
|
+
* browser's scroll and scroll-snap).
|
|
17
21
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* scroll container's left edge, not centered).
|
|
22
|
+
* Variants: 'multi-browse' (HorizontalMultiBrowseCarousel), 'uncontained'
|
|
23
|
+
* (HorizontalUncontainedCarousel), 'hero' (HorizontalCenteredHeroCarousel) and 'full-screen'
|
|
24
|
+
* (MDC-Android's FullScreenCarouselStrategy: one item filling the container, usually vertical).
|
|
22
25
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
26
|
+
* Scrolling is our own (Pointer Events + wheel + keys), driven in a rAF loop that writes
|
|
27
|
+
* transform/clip-path straight to each item, never through a reactive :style binding. Release
|
|
28
|
+
* behavior follows CarouselDefaults: singleAdvanceFlingBehavior (at most one item per fling) for
|
|
29
|
+
* multi-browse/hero/full-screen, noSnapFlingBehavior for uncontained, and the snap animation is
|
|
30
|
+
* spring(stiffness = StiffnessMediumLow = 400, dampingRatio = 1). The fling decay itself is an
|
|
31
|
+
* exponential friction curve, an approximation of Android's spline decay.
|
|
28
32
|
*/
|
|
33
|
+
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
34
|
+
import { useLocale } from '../composables/useLocale'
|
|
35
|
+
import {
|
|
36
|
+
Strategy,
|
|
37
|
+
fullScreenKeylineList,
|
|
38
|
+
heroKeylineList,
|
|
39
|
+
maxScrollOffset,
|
|
40
|
+
multiBrowseKeylineList,
|
|
41
|
+
placeItem,
|
|
42
|
+
snapScrollOffset,
|
|
43
|
+
uncontainedKeylineList,
|
|
44
|
+
} from '../composables/_carouselStrategy'
|
|
45
|
+
|
|
29
46
|
const props = withDefaults(
|
|
30
47
|
defineProps<{
|
|
31
|
-
items:
|
|
32
|
-
/**
|
|
33
|
-
|
|
34
|
-
|
|
48
|
+
items: T[]
|
|
49
|
+
/** Index of the current (snapped) item; use with v-model to follow or drive it. */
|
|
50
|
+
modelValue?: number
|
|
51
|
+
variant?: CarouselVariant
|
|
52
|
+
orientation?: 'horizontal' | 'vertical'
|
|
53
|
+
/**
|
|
54
|
+
* Size of large items along the scroll axis, in px. Multi-browse and hero treat it as the
|
|
55
|
+
* preferred size (the arrangement that fits best wins); uncontained uses it as is. Hero
|
|
56
|
+
* without it fills the container minus its small items; full-screen ignores it.
|
|
57
|
+
*/
|
|
58
|
+
itemSize?: number
|
|
59
|
+
/** Space between items, in px. The M3 spec and every Compose sample use 8. */
|
|
35
60
|
itemSpacing?: number
|
|
36
|
-
/**
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
61
|
+
/** Padding at both ends of the scroll axis, in px. Items scroll under it. */
|
|
62
|
+
contentPadding?: number
|
|
63
|
+
minSmallItemSize?: number
|
|
64
|
+
maxSmallItemSize?: number
|
|
65
|
+
/** What a release does. Defaults to 'no-snap' for uncontained, 'single-advance' otherwise. */
|
|
66
|
+
flingBehavior?: CarouselFlingBehavior
|
|
67
|
+
userScrollEnabled?: boolean
|
|
68
|
+
/** Height of the carousel. Defaults to 205px for a horizontal carousel that isn't full-screen. */
|
|
40
69
|
height?: string
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
interval?: number
|
|
70
|
+
/** Accessible name of the carousel. */
|
|
71
|
+
label?: string
|
|
44
72
|
}>(),
|
|
45
73
|
{
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
interval: 5000,
|
|
74
|
+
variant: 'multi-browse',
|
|
75
|
+
orientation: 'horizontal',
|
|
76
|
+
itemSpacing: 8,
|
|
77
|
+
contentPadding: 0,
|
|
78
|
+
minSmallItemSize: 40,
|
|
79
|
+
maxSmallItemSize: 56,
|
|
80
|
+
userScrollEnabled: true,
|
|
54
81
|
},
|
|
55
82
|
)
|
|
56
83
|
|
|
57
|
-
const emit = defineEmits<{
|
|
84
|
+
const emit = defineEmits<{
|
|
85
|
+
'update:modelValue': [index: number]
|
|
86
|
+
itemClick: [item: T, index: number]
|
|
87
|
+
}>()
|
|
58
88
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const activeIndex = ref(0)
|
|
89
|
+
defineSlots<{
|
|
90
|
+
item?: (scope: { item: T; index: number }) => unknown
|
|
91
|
+
}>()
|
|
63
92
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
93
|
+
const t = useLocale()
|
|
94
|
+
|
|
95
|
+
const SPRING_STIFFNESS = 400
|
|
96
|
+
const SPRING_DAMPING = 1
|
|
97
|
+
const MIN_FLING_VELOCITY = 400
|
|
98
|
+
const FLING_FRICTION = 4
|
|
99
|
+
const DEFAULT_ITEM_SIZE = 186
|
|
67
100
|
|
|
68
|
-
const
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
101
|
+
const rootEl = ref<HTMLElement | null>(null)
|
|
102
|
+
const itemEls: (HTMLElement | null)[] = []
|
|
103
|
+
const labelEls: (HTMLElement | null)[] = []
|
|
104
|
+
let labelWidths: number[] = []
|
|
105
|
+
const mainSize = ref(0)
|
|
106
|
+
const currentIndex = ref(clampIndex(props.modelValue ?? 0, props.items.length))
|
|
107
|
+
|
|
108
|
+
const horizontal = computed(() => props.orientation === 'horizontal')
|
|
109
|
+
let rtl = false
|
|
110
|
+
|
|
111
|
+
// Only the number of items matters to the geometry: depending on the array itself would rebuild
|
|
112
|
+
// the strategy (and re-snap) whenever a consumer passes a fresh inline array on re-render.
|
|
113
|
+
const itemCount = computed(() => props.items.length)
|
|
114
|
+
|
|
115
|
+
const strategy = computed(() => {
|
|
116
|
+
const size = mainSize.value
|
|
117
|
+
const count = itemCount.value
|
|
118
|
+
const spacing = props.itemSpacing
|
|
119
|
+
const keylines =
|
|
120
|
+
props.variant === 'uncontained'
|
|
121
|
+
? uncontainedKeylineList(size, props.itemSize ?? DEFAULT_ITEM_SIZE, spacing)
|
|
122
|
+
: props.variant === 'hero'
|
|
123
|
+
? heroKeylineList(size, props.itemSize ?? null, spacing, count, true, props.minSmallItemSize, props.maxSmallItemSize)
|
|
124
|
+
: props.variant === 'full-screen'
|
|
125
|
+
? fullScreenKeylineList(size, spacing)
|
|
126
|
+
: multiBrowseKeylineList(size, props.itemSize ?? DEFAULT_ITEM_SIZE, spacing, count, props.minSmallItemSize, props.maxSmallItemSize)
|
|
127
|
+
return new Strategy(keylines, size, spacing, props.contentPadding, props.contentPadding)
|
|
72
128
|
})
|
|
73
129
|
|
|
74
|
-
const
|
|
75
|
-
const
|
|
76
|
-
|
|
130
|
+
const itemMainSize = computed(() => (strategy.value.isValid ? strategy.value.itemMainAxisSize : 0))
|
|
131
|
+
const snaps = computed(() => Array.from({ length: itemCount.value }, (_, i) => snapScrollOffset(strategy.value, i, itemCount.value)))
|
|
132
|
+
const maxScroll = computed(() => (strategy.value.isValid ? maxScrollOffset(strategy.value, itemCount.value) : 0))
|
|
77
133
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
134
|
+
const resolvedFling = computed<CarouselFlingBehavior>(
|
|
135
|
+
() => props.flingBehavior ?? (props.variant === 'uncontained' ? 'no-snap' : 'single-advance'),
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
const resolvedHeight = computed(
|
|
139
|
+
() => props.height ?? (horizontal.value && props.variant !== 'full-screen' ? '205px' : undefined),
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
function itemLabel(i: number) {
|
|
143
|
+
return t.carouselItem.replace('{index}', String(i + 1)).replace('{count}', String(props.items.length))
|
|
84
144
|
}
|
|
85
145
|
|
|
86
|
-
function
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (!el) { widths.push(large); offsets.push(0); continue }
|
|
99
|
-
const offset = el.getBoundingClientRect().left - trackLeft
|
|
100
|
-
|
|
101
|
-
let width: number
|
|
102
|
-
let imgOffset: number
|
|
103
|
-
if (offset <= 0) {
|
|
104
|
-
// Exiting (or resting) on the left: mask grows in from the left, keep the right portion
|
|
105
|
-
// of the image visible (matches the real maskStart clipping from the item's start edge).
|
|
106
|
-
width = Math.max(large + offset, small)
|
|
107
|
-
imgOffset = -(large - width)
|
|
108
|
-
} else {
|
|
109
|
-
// Upcoming on the right: mask shrinks from the right, keep the left portion visible.
|
|
110
|
-
width = Math.max(large - offset, small)
|
|
111
|
-
imgOffset = 0
|
|
146
|
+
function clampIndex(i: number, count: number) {
|
|
147
|
+
return Math.min(Math.max(i, 0), Math.max(count - 1, 0))
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function nearestIndex(pos: number) {
|
|
151
|
+
let best = 0
|
|
152
|
+
let bestDist = Infinity
|
|
153
|
+
snaps.value.forEach((s, i) => {
|
|
154
|
+
const d = Math.abs(s - pos)
|
|
155
|
+
if (d < bestDist - 0.5) {
|
|
156
|
+
best = i
|
|
157
|
+
bestDist = d
|
|
112
158
|
}
|
|
113
|
-
|
|
114
|
-
|
|
159
|
+
})
|
|
160
|
+
return best
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ── Per-frame state, outside Vue's reactivity ───────────────────────────────────────────────
|
|
164
|
+
|
|
165
|
+
let scroll = 0
|
|
166
|
+
let velocity = 0
|
|
167
|
+
let anim: { kind: 'spring'; target: number } | { kind: 'decay' } | null = null
|
|
168
|
+
let animFrame = 0
|
|
169
|
+
let lastTime = 0
|
|
170
|
+
let renderFrame = 0
|
|
171
|
+
|
|
172
|
+
const round = (v: number) => Math.round(v * 100) / 100
|
|
173
|
+
|
|
174
|
+
function clampScroll(v: number) {
|
|
175
|
+
return Math.min(Math.max(v, 0), maxScroll.value)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function render() {
|
|
179
|
+
const s = strategy.value
|
|
180
|
+
const count = props.items.length
|
|
181
|
+
if (!s.isValid) return
|
|
182
|
+
const size = s.itemMainAxisSize
|
|
183
|
+
const viewport = mainSize.value
|
|
184
|
+
const max = maxScroll.value
|
|
185
|
+
const isHorizontal = horizontal.value
|
|
115
186
|
|
|
116
|
-
|
|
117
|
-
|
|
187
|
+
const index = nearestIndex(scroll)
|
|
188
|
+
if (index !== currentIndex.value) {
|
|
189
|
+
currentIndex.value = index
|
|
190
|
+
emit('update:modelValue', index)
|
|
118
191
|
}
|
|
119
192
|
|
|
120
|
-
|
|
121
|
-
|
|
193
|
+
for (let i = 0; i < count; i++) {
|
|
194
|
+
const el = itemEls[i]
|
|
195
|
+
if (!el) continue
|
|
196
|
+
const p = placeItem(s, i, scroll, max)
|
|
197
|
+
const visibleStart = p.offset + p.maskStart
|
|
198
|
+
const visibleEnd = visibleStart + p.maskSize
|
|
199
|
+
el.style.visibility = visibleEnd <= 0 || visibleStart >= viewport ? 'hidden' : ''
|
|
200
|
+
const pos = round(isHorizontal && rtl ? viewport - p.offset - size : p.offset)
|
|
201
|
+
el.style.transform = isHorizontal ? `translate3d(${pos}px, 0, 0)` : `translate3d(0, ${pos}px, 0)`
|
|
202
|
+
const start = round(p.maskStart)
|
|
203
|
+
const end = round(size - p.maskStart - p.maskSize)
|
|
204
|
+
el.style.clipPath = isHorizontal
|
|
205
|
+
? `inset(0 ${end}px 0 ${start}px round var(--radius-xl))`
|
|
206
|
+
: `inset(${start}px 0 ${end}px 0 round var(--radius-xl))`
|
|
207
|
+
// The current item is in front and the others descend with their distance from it.
|
|
208
|
+
el.style.zIndex = String(count - Math.abs(i - index))
|
|
209
|
+
el.style.setProperty('--m3-carousel-mask-start', `${start}px`)
|
|
210
|
+
el.style.setProperty('--m3-carousel-mask-size', `${round(p.maskSize)}px`)
|
|
122
211
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
212
|
+
// Default label: fades in once the mask is wide enough to show it whole (the Compose
|
|
213
|
+
// FadingHorizontalMultiBrowseCarouselSample formula).
|
|
214
|
+
const label = labelEls[i]
|
|
215
|
+
const labelWidth = labelWidths[i] ?? 0
|
|
216
|
+
if (label && labelWidth > 0) {
|
|
217
|
+
label.style.opacity = String(Math.max(labelWidth - s.maxItemSize + p.maskSize, 0) / labelWidth)
|
|
218
|
+
}
|
|
126
219
|
}
|
|
127
220
|
}
|
|
128
221
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
function
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
222
|
+
function scheduleRender() {
|
|
223
|
+
if (renderFrame) return
|
|
224
|
+
renderFrame = requestAnimationFrame(() => {
|
|
225
|
+
renderFrame = 0
|
|
226
|
+
render()
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function stopAnimation() {
|
|
231
|
+
anim = null
|
|
232
|
+
if (animFrame) cancelAnimationFrame(animFrame)
|
|
233
|
+
animFrame = 0
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function startAnimation(next: NonNullable<typeof anim>) {
|
|
237
|
+
anim = next
|
|
238
|
+
if (animFrame) return
|
|
239
|
+
lastTime = performance.now()
|
|
240
|
+
animFrame = requestAnimationFrame(step)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function step(now: number) {
|
|
244
|
+
animFrame = 0
|
|
245
|
+
if (!anim) return
|
|
246
|
+
const dt = Math.min((now - lastTime) / 1000, 1 / 30)
|
|
247
|
+
lastTime = now
|
|
248
|
+
|
|
249
|
+
if (anim.kind === 'spring') {
|
|
250
|
+
const target = anim.target
|
|
251
|
+
// Semi-implicit Euler in fixed substeps, like _useSheetMotion.
|
|
252
|
+
const h = 0.004
|
|
253
|
+
for (let left = dt; left > 0; left -= h) {
|
|
254
|
+
const sub = Math.min(h, left)
|
|
255
|
+
const accel = -SPRING_STIFFNESS * (scroll - target) - 2 * SPRING_DAMPING * Math.sqrt(SPRING_STIFFNESS) * velocity
|
|
256
|
+
velocity += accel * sub
|
|
257
|
+
scroll += velocity * sub
|
|
258
|
+
}
|
|
259
|
+
if (Math.abs(scroll - target) < 0.5 && Math.abs(velocity) < 10) {
|
|
260
|
+
scroll = target
|
|
261
|
+
velocity = 0
|
|
262
|
+
anim = null
|
|
263
|
+
}
|
|
264
|
+
} else {
|
|
265
|
+
velocity *= Math.exp(-FLING_FRICTION * dt)
|
|
266
|
+
const next = clampScroll(scroll + velocity * dt)
|
|
267
|
+
if (next !== scroll + velocity * dt) velocity = 0
|
|
268
|
+
scroll = next
|
|
269
|
+
if (Math.abs(velocity) < 20) {
|
|
270
|
+
velocity = 0
|
|
271
|
+
anim = null
|
|
272
|
+
}
|
|
144
273
|
}
|
|
274
|
+
|
|
275
|
+
render()
|
|
276
|
+
if (anim) animFrame = requestAnimationFrame(step)
|
|
145
277
|
}
|
|
146
278
|
|
|
147
|
-
function
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
279
|
+
function springTo(index: number) {
|
|
280
|
+
const target = snaps.value[clampIndex(index, props.items.length)]
|
|
281
|
+
if (target === undefined) return
|
|
282
|
+
startAnimation({ kind: 'spring', target })
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** A release with `v` px/s along the scroll offset (positive = towards later items). */
|
|
286
|
+
function fling(v: number, startIndex: number) {
|
|
287
|
+
if (resolvedFling.value === 'no-snap') {
|
|
288
|
+
velocity = v
|
|
289
|
+
if (Math.abs(v) >= MIN_FLING_VELOCITY / 4) startAnimation({ kind: 'decay' })
|
|
290
|
+
return
|
|
291
|
+
}
|
|
292
|
+
velocity = v
|
|
293
|
+
if (resolvedFling.value === 'multi-browse') {
|
|
294
|
+
springTo(nearestIndex(clampScroll(scroll + v / FLING_FRICTION)))
|
|
295
|
+
return
|
|
154
296
|
}
|
|
297
|
+
// single-advance: PagerSnapDistance.atMost(1) from the item the gesture started on.
|
|
298
|
+
let target = nearestIndex(scroll)
|
|
299
|
+
if (Math.abs(v) >= MIN_FLING_VELOCITY) {
|
|
300
|
+
const list = snaps.value
|
|
301
|
+
if (v > 0) {
|
|
302
|
+
const i = list.findIndex((s) => s > scroll + 0.5)
|
|
303
|
+
if (i !== -1) target = i
|
|
304
|
+
} else {
|
|
305
|
+
for (let i = list.length - 1; i >= 0; i--) {
|
|
306
|
+
if (list[i]! < scroll - 0.5) { target = i; break }
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
target = Math.min(Math.max(target, startIndex - 1), startIndex + 1)
|
|
310
|
+
}
|
|
311
|
+
springTo(target)
|
|
155
312
|
}
|
|
156
313
|
|
|
157
|
-
function scrollToItem(index: number) {
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
314
|
+
function scrollToItem(index: number, animate = true) {
|
|
315
|
+
const i = clampIndex(index, props.items.length)
|
|
316
|
+
const target = snaps.value[i]
|
|
317
|
+
if (target === undefined) return
|
|
318
|
+
if (animate) {
|
|
319
|
+
springTo(i)
|
|
320
|
+
} else {
|
|
321
|
+
stopAnimation()
|
|
322
|
+
scroll = target
|
|
323
|
+
velocity = 0
|
|
324
|
+
render()
|
|
325
|
+
}
|
|
161
326
|
}
|
|
162
327
|
|
|
163
328
|
function next() {
|
|
164
|
-
|
|
165
|
-
scrollToItem(i)
|
|
329
|
+
scrollToItem(currentIndex.value + 1)
|
|
166
330
|
}
|
|
331
|
+
|
|
167
332
|
function prev() {
|
|
168
|
-
|
|
169
|
-
|
|
333
|
+
scrollToItem(currentIndex.value - 1)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// ── Pointer dragging ────────────────────────────────────────────────────────────────────────
|
|
337
|
+
|
|
338
|
+
interface Press {
|
|
339
|
+
id: number
|
|
340
|
+
x: number
|
|
341
|
+
y: number
|
|
342
|
+
base: number
|
|
343
|
+
startScroll: number
|
|
344
|
+
startIndex: number
|
|
345
|
+
slop: number
|
|
346
|
+
active: boolean
|
|
347
|
+
samples: { t: number; s: number }[]
|
|
348
|
+
wasAnimating: boolean
|
|
349
|
+
}
|
|
350
|
+
let press: Press | null = null
|
|
351
|
+
let suppressClick = false
|
|
352
|
+
|
|
353
|
+
function onPointerDown(e: PointerEvent) {
|
|
354
|
+
if (!props.userScrollEnabled || e.button !== 0 || !strategy.value.isValid) return
|
|
355
|
+
if ((e.target as Element).closest('input, textarea, select, [contenteditable]')) return
|
|
356
|
+
const wasAnimating = anim !== null
|
|
357
|
+
stopAnimation()
|
|
358
|
+
press = {
|
|
359
|
+
id: e.pointerId,
|
|
360
|
+
x: e.clientX,
|
|
361
|
+
y: e.clientY,
|
|
362
|
+
base: 0,
|
|
363
|
+
startScroll: scroll,
|
|
364
|
+
startIndex: currentIndex.value,
|
|
365
|
+
slop: e.pointerType === 'touch' ? 8 : 4,
|
|
366
|
+
active: false,
|
|
367
|
+
samples: [],
|
|
368
|
+
wasAnimating,
|
|
369
|
+
}
|
|
370
|
+
window.addEventListener('pointermove', onPointerMove)
|
|
371
|
+
window.addEventListener('pointerup', onPointerUp)
|
|
372
|
+
window.addEventListener('pointercancel', onPointerCancel)
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function onPointerMove(e: PointerEvent) {
|
|
376
|
+
if (!press || e.pointerId !== press.id) return
|
|
377
|
+
const dx = e.clientX - press.x
|
|
378
|
+
const dy = e.clientY - press.y
|
|
379
|
+
const main = horizontal.value ? dx : dy
|
|
380
|
+
const cross = horizontal.value ? dy : dx
|
|
381
|
+
if (!press.active) {
|
|
382
|
+
if (Math.abs(cross) > press.slop && Math.abs(cross) > Math.abs(main)) {
|
|
383
|
+
endPress()
|
|
384
|
+
return
|
|
385
|
+
}
|
|
386
|
+
if (Math.abs(main) <= press.slop) return
|
|
387
|
+
press.active = true
|
|
388
|
+
press.base = main
|
|
389
|
+
try { rootEl.value?.setPointerCapture(e.pointerId) } catch { /* not capturable */ }
|
|
390
|
+
document.body.style.userSelect = 'none'
|
|
391
|
+
}
|
|
392
|
+
const sign = horizontal.value && rtl ? 1 : -1
|
|
393
|
+
scroll = clampScroll(press.startScroll + sign * (main - press.base))
|
|
394
|
+
const now = performance.now()
|
|
395
|
+
press.samples.push({ t: now, s: scroll })
|
|
396
|
+
while (press.samples.length > 2 && now - press.samples[0]!.t > 100) press.samples.shift()
|
|
397
|
+
scheduleRender()
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function releaseVelocity(p: Press) {
|
|
401
|
+
const first = p.samples[0]
|
|
402
|
+
const last = p.samples[p.samples.length - 1]
|
|
403
|
+
if (!first || !last || last.t - first.t < 1 || performance.now() - last.t > 80) return 0
|
|
404
|
+
return ((last.s - first.s) / (last.t - first.t)) * 1000
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function onPointerUp(e: PointerEvent) {
|
|
408
|
+
if (!press || e.pointerId !== press.id) return
|
|
409
|
+
const p = press
|
|
410
|
+
endPress()
|
|
411
|
+
if (p.active) {
|
|
412
|
+
suppressClick = true
|
|
413
|
+
setTimeout(() => (suppressClick = false), 0)
|
|
414
|
+
fling(releaseVelocity(p), p.startIndex)
|
|
415
|
+
} else if (p.wasAnimating) {
|
|
416
|
+
// A tap that caught a moving carousel: settle it instead of leaving it mid-way.
|
|
417
|
+
fling(0, currentIndex.value)
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function onPointerCancel(e: PointerEvent) {
|
|
422
|
+
if (!press || e.pointerId !== press.id) return
|
|
423
|
+
const p = press
|
|
424
|
+
endPress()
|
|
425
|
+
if (p.active || p.wasAnimating) fling(0, p.startIndex)
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function endPress() {
|
|
429
|
+
press = null
|
|
430
|
+
document.body.style.userSelect = ''
|
|
431
|
+
window.removeEventListener('pointermove', onPointerMove)
|
|
432
|
+
window.removeEventListener('pointerup', onPointerUp)
|
|
433
|
+
window.removeEventListener('pointercancel', onPointerCancel)
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function onClickCapture(e: MouseEvent) {
|
|
437
|
+
if (!suppressClick) return
|
|
438
|
+
e.stopPropagation()
|
|
439
|
+
e.preventDefault()
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function onItemClick(item: T, index: number) {
|
|
443
|
+
emit('itemClick', item, index)
|
|
444
|
+
const s = strategy.value
|
|
445
|
+
if (!s.isValid) return
|
|
446
|
+
const p = placeItem(s, index, scroll, maxScroll.value)
|
|
447
|
+
const start = p.offset + p.maskStart
|
|
448
|
+
const fullyVisible = p.maskSize >= s.itemMainAxisSize - 0.5 && start >= -0.5 && start + p.maskSize <= mainSize.value + 0.5
|
|
449
|
+
if (!fullyVisible) scrollToItem(index)
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// ── Wheel and keyboard ──────────────────────────────────────────────────────────────────────
|
|
453
|
+
|
|
454
|
+
let wheelTimer: ReturnType<typeof setTimeout> | null = null
|
|
455
|
+
|
|
456
|
+
function onWheel(e: WheelEvent) {
|
|
457
|
+
if (!props.userScrollEnabled || !strategy.value.isValid) return
|
|
458
|
+
let delta = 0
|
|
459
|
+
if (horizontal.value) {
|
|
460
|
+
if (Math.abs(e.deltaX) > Math.abs(e.deltaY)) delta = e.deltaX
|
|
461
|
+
else if (e.shiftKey) delta = e.deltaY
|
|
462
|
+
if (rtl) delta = -delta
|
|
463
|
+
} else {
|
|
464
|
+
delta = e.deltaY
|
|
465
|
+
}
|
|
466
|
+
if (e.deltaMode === 1) delta *= 16
|
|
467
|
+
if (!delta) return
|
|
468
|
+
const next = clampScroll(scroll + delta)
|
|
469
|
+
// At an edge, let the page scroll instead.
|
|
470
|
+
if (next === scroll) return
|
|
471
|
+
e.preventDefault()
|
|
472
|
+
stopAnimation()
|
|
473
|
+
scroll = next
|
|
474
|
+
velocity = 0
|
|
475
|
+
scheduleRender()
|
|
476
|
+
if (wheelTimer) clearTimeout(wheelTimer)
|
|
477
|
+
wheelTimer = setTimeout(() => {
|
|
478
|
+
wheelTimer = null
|
|
479
|
+
if (resolvedFling.value !== 'no-snap') springTo(nearestIndex(scroll))
|
|
480
|
+
}, 120)
|
|
170
481
|
}
|
|
171
482
|
|
|
172
483
|
function onKeydown(e: KeyboardEvent) {
|
|
173
|
-
|
|
174
|
-
|
|
484
|
+
const forward = horizontal.value ? (rtl ? 'ArrowLeft' : 'ArrowRight') : 'ArrowDown'
|
|
485
|
+
const backward = horizontal.value ? (rtl ? 'ArrowRight' : 'ArrowLeft') : 'ArrowUp'
|
|
486
|
+
let target: number | null = null
|
|
487
|
+
if (e.key === forward) target = currentIndex.value + 1
|
|
488
|
+
else if (e.key === backward) target = currentIndex.value - 1
|
|
489
|
+
else if (e.key === 'Home') target = 0
|
|
490
|
+
else if (e.key === 'End') target = props.items.length - 1
|
|
491
|
+
if (target === null) return
|
|
492
|
+
e.preventDefault()
|
|
493
|
+
scrollToItem(target)
|
|
175
494
|
}
|
|
176
495
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
496
|
+
// ── Measuring and lifecycle ─────────────────────────────────────────────────────────────────
|
|
497
|
+
|
|
498
|
+
function measure() {
|
|
499
|
+
const el = rootEl.value
|
|
500
|
+
if (!el) return
|
|
501
|
+
rtl = getComputedStyle(el).direction === 'rtl'
|
|
502
|
+
mainSize.value = horizontal.value ? el.clientWidth : el.clientHeight
|
|
181
503
|
}
|
|
182
|
-
|
|
183
|
-
|
|
504
|
+
|
|
505
|
+
function measureLabels() {
|
|
506
|
+
labelWidths = labelEls.map((el) => el?.offsetWidth ?? 0)
|
|
184
507
|
}
|
|
185
508
|
|
|
509
|
+
// Resizes, prop changes and item-count changes rebuild the strategy: keep the current item snapped.
|
|
510
|
+
watch(
|
|
511
|
+
strategy,
|
|
512
|
+
() => {
|
|
513
|
+
stopAnimation()
|
|
514
|
+
velocity = 0
|
|
515
|
+
currentIndex.value = clampIndex(currentIndex.value, props.items.length)
|
|
516
|
+
scroll = resolvedFling.value === 'no-snap' ? clampScroll(scroll) : (snaps.value[currentIndex.value] ?? 0)
|
|
517
|
+
measureLabels()
|
|
518
|
+
render()
|
|
519
|
+
},
|
|
520
|
+
{ flush: 'post' },
|
|
521
|
+
)
|
|
522
|
+
|
|
523
|
+
watch(
|
|
524
|
+
() => props.modelValue,
|
|
525
|
+
(v) => {
|
|
526
|
+
if (v !== undefined && v !== currentIndex.value) scrollToItem(v)
|
|
527
|
+
},
|
|
528
|
+
)
|
|
529
|
+
|
|
186
530
|
let resizeObserver: ResizeObserver | null = null
|
|
531
|
+
|
|
187
532
|
onMounted(() => {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
533
|
+
const el = rootEl.value!
|
|
534
|
+
el.addEventListener('wheel', onWheel, { passive: false })
|
|
535
|
+
resizeObserver = new ResizeObserver(measure)
|
|
536
|
+
resizeObserver.observe(el)
|
|
537
|
+
measure()
|
|
538
|
+
nextTick(() => {
|
|
539
|
+
measureLabels()
|
|
540
|
+
scroll = snaps.value[currentIndex.value] ?? 0
|
|
541
|
+
render()
|
|
542
|
+
})
|
|
198
543
|
})
|
|
544
|
+
|
|
199
545
|
onBeforeUnmount(() => {
|
|
200
|
-
|
|
201
|
-
|
|
546
|
+
stopAnimation()
|
|
547
|
+
endPress()
|
|
548
|
+
if (renderFrame) cancelAnimationFrame(renderFrame)
|
|
549
|
+
if (wheelTimer) clearTimeout(wheelTimer)
|
|
202
550
|
resizeObserver?.disconnect()
|
|
551
|
+
rootEl.value?.removeEventListener('wheel', onWheel)
|
|
203
552
|
})
|
|
204
553
|
|
|
205
|
-
defineExpose({ next, prev
|
|
554
|
+
defineExpose({ scrollToItem, next, prev })
|
|
206
555
|
</script>
|
|
207
556
|
|
|
208
557
|
<template>
|
|
209
558
|
<div
|
|
210
|
-
|
|
559
|
+
ref="rootEl"
|
|
560
|
+
class="m3-carousel relative overflow-hidden outline-none focus-visible:outline-3 focus-visible:outline-offset-2 focus-visible:outline-secondary"
|
|
561
|
+
:class="horizontal ? 'touch-pan-y' : 'touch-pan-x'"
|
|
562
|
+
:style="{ height: resolvedHeight }"
|
|
563
|
+
role="region"
|
|
564
|
+
:aria-roledescription="t.carousel"
|
|
565
|
+
:aria-label="label"
|
|
211
566
|
tabindex="0"
|
|
567
|
+
@pointerdown="onPointerDown"
|
|
568
|
+
@click.capture="onClickCapture"
|
|
569
|
+
@dragstart.prevent
|
|
212
570
|
@keydown="onKeydown"
|
|
213
|
-
@mouseenter="stopAutoplay"
|
|
214
|
-
@mouseleave="startAutoplay"
|
|
215
571
|
>
|
|
216
572
|
<div
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
:
|
|
220
|
-
|
|
573
|
+
v-for="(item, i) in items"
|
|
574
|
+
:key="i"
|
|
575
|
+
:ref="(el) => (itemEls[i] = el as HTMLElement | null)"
|
|
576
|
+
class="absolute left-0 top-0 overflow-hidden bg-surface-container-highest"
|
|
577
|
+
:style="horizontal ? { width: `${itemMainSize}px`, height: '100%' } : { width: '100%', height: `${itemMainSize}px` }"
|
|
578
|
+
role="group"
|
|
579
|
+
:aria-label="itemLabel(i)"
|
|
580
|
+
data-carousel-item
|
|
581
|
+
@click="onItemClick(item, i)"
|
|
221
582
|
>
|
|
222
|
-
<
|
|
223
|
-
v-for="(item, i) in items"
|
|
224
|
-
:key="i"
|
|
225
|
-
:ref="(el) => setItemRef(el, i)"
|
|
226
|
-
class="rounded-2xl"
|
|
227
|
-
:style="itemStyle(i)"
|
|
228
|
-
>
|
|
583
|
+
<slot name="item" :item="item" :index="i">
|
|
229
584
|
<img
|
|
230
|
-
:src="item.src"
|
|
231
|
-
:alt="item.alt ?? item.label ?? ''"
|
|
232
|
-
|
|
233
|
-
|
|
585
|
+
:src="(item as CarouselItem).src"
|
|
586
|
+
:alt="(item as CarouselItem).alt ?? (item as CarouselItem).label ?? ''"
|
|
587
|
+
draggable="false"
|
|
588
|
+
class="pointer-events-none size-full select-none object-cover"
|
|
234
589
|
/>
|
|
235
|
-
<
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
590
|
+
<template v-if="(item as CarouselItem).label">
|
|
591
|
+
<div class="pointer-events-none absolute inset-x-0 bottom-0 h-24 bg-gradient-to-t from-black/60 to-transparent" />
|
|
592
|
+
<span
|
|
593
|
+
:ref="(el) => (labelEls[i] = el as HTMLElement | null)"
|
|
594
|
+
class="pointer-events-none absolute bottom-4 whitespace-nowrap text-title-medium text-white"
|
|
595
|
+
style="inset-inline-start: calc(var(--m3-carousel-mask-start, 0px) + 16px)"
|
|
596
|
+
>{{ (item as CarouselItem).label }}</span>
|
|
597
|
+
</template>
|
|
598
|
+
</slot>
|
|
242
599
|
</div>
|
|
243
|
-
|
|
244
|
-
<template v-if="showArrows && items.length > 1">
|
|
245
|
-
<button
|
|
246
|
-
type="button"
|
|
247
|
-
class="absolute left-3 top-1/2 z-10 flex h-10 w-10 -translate-y-1/2 cursor-pointer items-center justify-center rounded-full bg-surface/80 text-on-surface shadow-elevation-1 opacity-0 backdrop-blur-sm transition-all duration-200 hover:bg-surface hover:shadow-elevation-2 group-hover:opacity-100"
|
|
248
|
-
@click="prev"
|
|
249
|
-
>
|
|
250
|
-
<MIcon name="chevron_left" :size="24" />
|
|
251
|
-
</button>
|
|
252
|
-
<button
|
|
253
|
-
type="button"
|
|
254
|
-
class="absolute right-3 top-1/2 z-10 flex h-10 w-10 -translate-y-1/2 cursor-pointer items-center justify-center rounded-full bg-surface/80 text-on-surface shadow-elevation-1 opacity-0 backdrop-blur-sm transition-all duration-200 hover:bg-surface hover:shadow-elevation-2 group-hover:opacity-100"
|
|
255
|
-
@click="next"
|
|
256
|
-
>
|
|
257
|
-
<MIcon name="chevron_right" :size="24" />
|
|
258
|
-
</button>
|
|
259
|
-
</template>
|
|
260
600
|
</div>
|
|
261
601
|
</template>
|
|
262
|
-
|
|
263
|
-
<style scoped>
|
|
264
|
-
.carousel-track {
|
|
265
|
-
scroll-snap-type: x mandatory;
|
|
266
|
-
-webkit-overflow-scrolling: touch;
|
|
267
|
-
scrollbar-width: none;
|
|
268
|
-
}
|
|
269
|
-
.carousel-track::-webkit-scrollbar {
|
|
270
|
-
display: none;
|
|
271
|
-
}
|
|
272
|
-
.carousel-track > * {
|
|
273
|
-
scroll-snap-align: start;
|
|
274
|
-
}
|
|
275
|
-
</style>
|