@m3ui-vue/m3ui-vue 0.8.0 → 0.8.2
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 +9 -1
- package/dist/{MMenuItem-vSLbJGqe.js → MMenuItem-BAWmUTT3.js} +367 -189
- package/dist/MMenuItem-BAWmUTT3.js.map +1 -0
- package/dist/code-editor.js +1 -1
- package/dist/code-editor.js.map +1 -1
- package/dist/components/MButton.vue.d.ts +7 -0
- package/dist/components/MCalendar.vue.d.ts +0 -1
- package/dist/components/MCarousel.vue.d.ts +12 -14
- package/dist/components/MContextMenu.vue.d.ts +25 -4
- package/dist/components/MDatePicker.vue.d.ts +1 -3
- package/dist/components/MDatePickerModal.vue.d.ts +0 -1
- package/dist/components/MDateRangePicker.vue.d.ts +1 -3
- package/dist/components/MDateRangePickerModal.vue.d.ts +0 -1
- package/dist/components/MKanban.vue.d.ts +1 -0
- package/dist/components/MMenu.vue.d.ts +20 -2
- package/dist/components/MMenuDivider.vue.d.ts +13 -1
- package/dist/components/MMenuItem.vue.d.ts +3 -0
- package/dist/components/MScheduler.vue.d.ts +0 -1
- package/dist/components/MShape.vue.d.ts +51 -0
- package/dist/components/MShortNavigationBar.vue.d.ts +34 -0
- package/dist/components/MSimpleCarousel.vue.d.ts +45 -0
- package/dist/components/MToggleButton.vue.d.ts +29 -0
- package/dist/components/MTree.vue.d.ts +0 -1
- package/dist/components/MTreeTable.vue.d.ts +1 -0
- package/dist/index.d.ts +8 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +5599 -3973
- package/dist/m3ui.js.map +1 -1
- package/dist/rich-text-editor.js +8 -8
- package/dist/styles/palettes.css +22 -22
- package/dist/styles.css +1 -1
- package/package.json +4 -1
- package/src/__tests__/MCarousel.test.ts +33 -0
- package/src/__tests__/MShape.test.ts +36 -0
- package/src/__tests__/MToggleButton.test.ts +27 -0
- package/src/components/MAutocomplete.vue +5 -1
- package/src/components/MButton.vue +15 -2
- package/src/components/MCalendar.vue +9 -5
- package/src/components/MCarousel.vue +123 -173
- package/src/components/MCodeEditor.vue +6 -1
- package/src/components/MCommandPalette.vue +4 -0
- package/src/components/MContextMenu.vue +287 -13
- package/src/components/MDatePicker.vue +12 -9
- package/src/components/MDatePickerModal.vue +9 -4
- package/src/components/MDateRangePicker.vue +12 -9
- package/src/components/MDateRangePickerModal.vue +9 -4
- package/src/components/MKanban.vue +5 -1
- package/src/components/MMenu.vue +308 -20
- package/src/components/MMenuDivider.vue +16 -0
- package/src/components/MMenuItem.vue +60 -18
- package/src/components/MMultiAutocomplete.vue +5 -1
- package/src/components/MMultiSelect.vue +5 -1
- package/src/components/MScheduler.vue +10 -6
- package/src/components/MSearchBar.vue +5 -1
- package/src/components/MSelect.vue +5 -1
- package/src/components/MShape.vue +107 -0
- package/src/components/MShortNavigationBar.vue +113 -0
- package/src/components/MSimpleCarousel.vue +325 -0
- package/src/components/MSpotlightSearch.vue +6 -0
- package/src/components/MToggleButton.vue +188 -0
- package/src/components/MTopAppBar.vue +6 -2
- package/src/components/MTree.vue +4 -2
- package/src/components/MTreeTable.vue +5 -1
- package/src/composables/useColorPalette.ts +1 -1
- package/src/index.ts +8 -1
- package/src/styles/palettes.css +22 -22
- package/dist/MMenuItem-vSLbJGqe.js.map +0 -1
|
@@ -11,6 +11,10 @@ export interface SelectOption {
|
|
|
11
11
|
disabled?: boolean
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
// Root is a fragment (field div + a sibling Teleport for the dropdown panel), so Vue can't
|
|
15
|
+
// auto-apply a consumer's class/attrs to either one — forward them onto the field div below.
|
|
16
|
+
defineOptions({ inheritAttrs: false })
|
|
17
|
+
|
|
14
18
|
const props = withDefaults(
|
|
15
19
|
defineProps<{
|
|
16
20
|
modelValue: unknown
|
|
@@ -198,7 +202,7 @@ const labelClasses = computed(() => {
|
|
|
198
202
|
</script>
|
|
199
203
|
|
|
200
204
|
<template>
|
|
201
|
-
<div class="flex flex-col gap-1">
|
|
205
|
+
<div class="flex flex-col gap-1" v-bind="$attrs">
|
|
202
206
|
<div
|
|
203
207
|
ref="fieldEl"
|
|
204
208
|
class="relative"
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
export { shapeNames } from 'shape-morph'
|
|
3
|
+
</script>
|
|
4
|
+
|
|
5
|
+
<script setup lang="ts">
|
|
6
|
+
import { onUnmounted, ref, useId, watch } from 'vue'
|
|
7
|
+
import { AnimatedMorph, getShape, toSvgPath, type ShapeName } from 'shape-morph'
|
|
8
|
+
|
|
9
|
+
export type { ShapeName }
|
|
10
|
+
|
|
11
|
+
// No default width/height here (matches MAspectRatio) — size it via `class`/`style` on the
|
|
12
|
+
// component itself, e.g. `class="h-16 w-16"`. A hardcoded `h-full w-full` would fight Tailwind's
|
|
13
|
+
// own (undefined) utility ordering against whatever sizing class the consumer passes through.
|
|
14
|
+
const props = withDefaults(
|
|
15
|
+
defineProps<{
|
|
16
|
+
/** One of M3's 35 named "expressive" shapes (MaterialShapes.kt), e.g. 'Cookie9Sided', 'Heart'. */
|
|
17
|
+
shape: ShapeName
|
|
18
|
+
/**
|
|
19
|
+
* Fill color used when there's no default slot content to clip (a decorative solid shape).
|
|
20
|
+
* SVG `fill`, not a `background-color` — a `bg-*` Tailwind class on the component itself has
|
|
21
|
+
* no effect here; use `fill` (or a `text-*` class, since the default is `currentColor`).
|
|
22
|
+
*/
|
|
23
|
+
fill?: string
|
|
24
|
+
/** Spring-morph into the new outline when `shape` changes — false snaps instantly instead. */
|
|
25
|
+
animate?: boolean
|
|
26
|
+
/**
|
|
27
|
+
* Multiplier on the morph spring's speed (1 = default). Scales stiffness by speed² and the
|
|
28
|
+
* damping coefficient by speed, which time-scales the spring uniformly without changing its
|
|
29
|
+
* character (same amount of overshoot/settle shape, just faster or slower).
|
|
30
|
+
*/
|
|
31
|
+
speed?: number
|
|
32
|
+
}>(),
|
|
33
|
+
{
|
|
34
|
+
fill: 'currentColor',
|
|
35
|
+
animate: true,
|
|
36
|
+
speed: 1,
|
|
37
|
+
},
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
const clipId = `m3-shape-${useId()}`
|
|
41
|
+
|
|
42
|
+
// Normalized 0-1 coordinates (size: 1) so the path works as an objectBoundingBox clipPath,
|
|
43
|
+
// which scales correctly with the element's actual rendered size instead of a fixed px box.
|
|
44
|
+
const pathD = ref(toSvgPath(getShape(props.shape), 1))
|
|
45
|
+
|
|
46
|
+
let morph: AnimatedMorph | null = null
|
|
47
|
+
let settledShape = props.shape
|
|
48
|
+
|
|
49
|
+
// MaterialShapes is itself an Expressive-only M3 feature (MaterialShapes.kt requires
|
|
50
|
+
// @ExperimentalMaterial3ExpressiveApi), so this spring is ExpressiveMotionTokens.DefaultSpatial
|
|
51
|
+
// (dampingRatio 0.8, stiffness 380) — the scheme's general-purpose spatial spring, since no
|
|
52
|
+
// single Compose composable's own bespoke spring (e.g. LoadingIndicator's 0.6/200) is the
|
|
53
|
+
// "correct" one to copy for a generic shape primitive like this. shape-morph's AnimatedMorph
|
|
54
|
+
// spring uses a plain damping *coefficient*, not Compose's damping *ratio* — converted via
|
|
55
|
+
// damping = 2 * dampingRatio * sqrt(stiffness) = 2 * 0.8 * sqrt(380) ≈ 31.2.
|
|
56
|
+
const BASE_STIFFNESS = 380
|
|
57
|
+
const BASE_DAMPING = 31.2
|
|
58
|
+
|
|
59
|
+
function springFor(speed: number) {
|
|
60
|
+
return { stiffness: BASE_STIFFNESS * speed ** 2, damping: BASE_DAMPING * speed }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
watch(() => props.shape, (next, prev) => {
|
|
64
|
+
if (!prev || next === prev) return
|
|
65
|
+
morph?.dispose()
|
|
66
|
+
if (!props.animate) {
|
|
67
|
+
pathD.value = toSvgPath(getShape(next), 1)
|
|
68
|
+
settledShape = next
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
// AnimatedMorph only morphs between two named/RoundedPolygon endpoints, not from an
|
|
72
|
+
// arbitrary in-flight shape, so a change that interrupts a running morph restarts from
|
|
73
|
+
// the last shape that had actually settled rather than the current on-screen outline.
|
|
74
|
+
morph = new AnimatedMorph(settledShape, next, {
|
|
75
|
+
spring: springFor(props.speed),
|
|
76
|
+
size: 1,
|
|
77
|
+
onFrame: (frame) => { pathD.value = frame.pathD },
|
|
78
|
+
})
|
|
79
|
+
// The constructor only emits the start shape at progress 0 — setting `progress` is what
|
|
80
|
+
// actually kicks off the spring animation toward the end shape (see shape-morph's own
|
|
81
|
+
// useMorph, which drives this the same way). Skipping this makes the shape jump straight
|
|
82
|
+
// to its final frame with no visible interpolation, one render behind the prop change.
|
|
83
|
+
morph.progress = 1
|
|
84
|
+
settledShape = next
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
onUnmounted(() => morph?.dispose())
|
|
88
|
+
</script>
|
|
89
|
+
|
|
90
|
+
<template>
|
|
91
|
+
<span class="relative inline-block">
|
|
92
|
+
<svg width="0" height="0" class="absolute">
|
|
93
|
+
<defs>
|
|
94
|
+
<clipPath :id="clipId" clipPathUnits="objectBoundingBox">
|
|
95
|
+
<path :d="pathD" />
|
|
96
|
+
</clipPath>
|
|
97
|
+
</defs>
|
|
98
|
+
</svg>
|
|
99
|
+
|
|
100
|
+
<div v-if="$slots.default" class="h-full w-full" :style="{ clipPath: `url(#${clipId})` }">
|
|
101
|
+
<slot />
|
|
102
|
+
</div>
|
|
103
|
+
<svg v-else viewBox="0 0 1 1" class="h-full w-full">
|
|
104
|
+
<path :d="pathD" :fill="fill" />
|
|
105
|
+
</svg>
|
|
106
|
+
</span>
|
|
107
|
+
</template>
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed } from 'vue'
|
|
3
|
+
import MIcon from './MIcon.vue'
|
|
4
|
+
import MBadge from './MBadge.vue'
|
|
5
|
+
|
|
6
|
+
export interface ShortNavBarItem {
|
|
7
|
+
value: string | number
|
|
8
|
+
label: string
|
|
9
|
+
icon: string
|
|
10
|
+
badge?: number
|
|
11
|
+
badgeDot?: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const props = withDefaults(
|
|
15
|
+
defineProps<{
|
|
16
|
+
modelValue: string | number
|
|
17
|
+
items: ShortNavBarItem[]
|
|
18
|
+
/**
|
|
19
|
+
* 'equal' (ShortNavigationBarArrangement.EqualWeight): items evenly fill the bar — recommended
|
|
20
|
+
* for small-width screens. 'centered' (.Centered): items are grouped toward the center instead
|
|
21
|
+
* of stretching edge to edge — recommended for medium-width screens, paired with iconPosition
|
|
22
|
+
* 'start'. Real Compose computes this via a custom MeasurePolicy at layout time; ported here as
|
|
23
|
+
* the same closed-form percentage formula (calculateCenteredContentHorizontalPadding), since it
|
|
24
|
+
* only ever depends on the item count, not on measured content size.
|
|
25
|
+
*/
|
|
26
|
+
arrangement?: 'equal' | 'centered'
|
|
27
|
+
/** NavigationItemIconPosition — 'top' stacks icon above label (ShortNavigationBarItem's
|
|
28
|
+
* default); 'start' places the icon beside the label inside a single wider pill, meant for
|
|
29
|
+
* the 'centered' arrangement on wider/landscape layouts. */
|
|
30
|
+
iconPosition?: 'top' | 'start'
|
|
31
|
+
}>(),
|
|
32
|
+
{
|
|
33
|
+
arrangement: 'equal',
|
|
34
|
+
iconPosition: 'top',
|
|
35
|
+
},
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
defineEmits<{ 'update:modelValue': [string | number] }>()
|
|
39
|
+
|
|
40
|
+
// calculateCenteredContentHorizontalPadding: 3 items -> 20% each side, 4 -> 15%, 5 -> 10%,
|
|
41
|
+
// 6 -> 5%, 7+ -> 0. Matches the real formula verbatim: ((100 - 10*(n+3)) / 2) / 100.
|
|
42
|
+
const centeredPadding = computed(() => {
|
|
43
|
+
const n = props.items.length
|
|
44
|
+
if (props.arrangement !== 'centered' || n > 6) return 0
|
|
45
|
+
return ((100 - 10 * (n + 3)) / 2)
|
|
46
|
+
})
|
|
47
|
+
</script>
|
|
48
|
+
|
|
49
|
+
<template>
|
|
50
|
+
<nav
|
|
51
|
+
class="flex h-16 w-full items-center border-t border-outline-variant bg-surface-container shadow-elevation-2"
|
|
52
|
+
:style="arrangement === 'centered' ? { paddingLeft: `${centeredPadding}%`, paddingRight: `${centeredPadding}%` } : undefined"
|
|
53
|
+
>
|
|
54
|
+
<button
|
|
55
|
+
v-for="item in items"
|
|
56
|
+
:key="item.value"
|
|
57
|
+
type="button"
|
|
58
|
+
class="group flex flex-1 cursor-pointer items-center justify-center self-stretch transition-colors focus-visible:outline-none"
|
|
59
|
+
:class="iconPosition === 'top' ? 'flex-col gap-1' : 'flex-row gap-1 py-1.5'"
|
|
60
|
+
@click="$emit('update:modelValue', item.value)"
|
|
61
|
+
>
|
|
62
|
+
<template v-if="iconPosition === 'top'">
|
|
63
|
+
<!-- Top icon position: 56x32dp pill above a separate label, matching MNavigationBar -->
|
|
64
|
+
<span
|
|
65
|
+
class="inline-flex h-8 items-center justify-center rounded-2xl transition-all duration-200"
|
|
66
|
+
:class="
|
|
67
|
+
item.value === modelValue
|
|
68
|
+
? 'w-14 bg-secondary-container text-on-secondary-container'
|
|
69
|
+
: 'w-0 bg-secondary-container/0 text-on-surface-variant group-hover:w-14 group-hover:bg-on-surface/8'
|
|
70
|
+
"
|
|
71
|
+
>
|
|
72
|
+
<MBadge v-if="item.badge != null" :count="item.badge">
|
|
73
|
+
<MIcon :name="item.icon" :size="24" />
|
|
74
|
+
</MBadge>
|
|
75
|
+
<MBadge v-else-if="item.badgeDot" dot>
|
|
76
|
+
<MIcon :name="item.icon" :size="24" />
|
|
77
|
+
</MBadge>
|
|
78
|
+
<MIcon v-else :name="item.icon" :size="24" />
|
|
79
|
+
</span>
|
|
80
|
+
<span
|
|
81
|
+
class="text-label-medium font-medium"
|
|
82
|
+
:class="item.value === modelValue ? 'text-secondary' : 'text-on-surface-variant'"
|
|
83
|
+
>
|
|
84
|
+
{{ item.label }}
|
|
85
|
+
</span>
|
|
86
|
+
</template>
|
|
87
|
+
|
|
88
|
+
<template v-else>
|
|
89
|
+
<!-- Start icon position: one 40dp-tall pill wrapping icon+label together, 16dp
|
|
90
|
+
leading/trailing padding (ActiveIndicatorLeadingSpace/TrailingSpace) — selected label
|
|
91
|
+
uses the icon's own color here (OnSecondaryContainer), not the Top position's
|
|
92
|
+
separate Secondary label color (ShortNavigationBarItemDefaults.colors() doc comment). -->
|
|
93
|
+
<span
|
|
94
|
+
class="inline-flex h-10 items-center justify-center gap-1 rounded-2xl px-4 transition-colors duration-200"
|
|
95
|
+
:class="
|
|
96
|
+
item.value === modelValue
|
|
97
|
+
? 'bg-secondary-container text-on-secondary-container'
|
|
98
|
+
: 'text-on-surface-variant group-hover:bg-on-surface/8'
|
|
99
|
+
"
|
|
100
|
+
>
|
|
101
|
+
<MBadge v-if="item.badge != null" :count="item.badge">
|
|
102
|
+
<MIcon :name="item.icon" :size="24" />
|
|
103
|
+
</MBadge>
|
|
104
|
+
<MBadge v-else-if="item.badgeDot" dot>
|
|
105
|
+
<MIcon :name="item.icon" :size="24" />
|
|
106
|
+
</MBadge>
|
|
107
|
+
<MIcon v-else :name="item.icon" :size="24" />
|
|
108
|
+
<span class="text-label-medium font-medium">{{ item.label }}</span>
|
|
109
|
+
</span>
|
|
110
|
+
</template>
|
|
111
|
+
</button>
|
|
112
|
+
</nav>
|
|
113
|
+
</template>
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { ref, computed, onMounted, onBeforeUnmount, watch, nextTick } from 'vue'
|
|
3
|
+
import MIcon from './MIcon.vue'
|
|
4
|
+
|
|
5
|
+
export interface SimpleCarouselItem {
|
|
6
|
+
src: string
|
|
7
|
+
alt?: string
|
|
8
|
+
label?: string
|
|
9
|
+
supportingText?: string
|
|
10
|
+
ratio?: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const props = withDefaults(
|
|
14
|
+
defineProps<{
|
|
15
|
+
items: SimpleCarouselItem[]
|
|
16
|
+
layout?: 'hero' | 'multi-browse' | 'uncontained'
|
|
17
|
+
height?: string
|
|
18
|
+
showArrows?: boolean
|
|
19
|
+
showIndicators?: boolean
|
|
20
|
+
autoplay?: boolean
|
|
21
|
+
interval?: number
|
|
22
|
+
gap?: number
|
|
23
|
+
visibleItems?: number
|
|
24
|
+
animated?: boolean
|
|
25
|
+
}>(),
|
|
26
|
+
{
|
|
27
|
+
layout: 'hero',
|
|
28
|
+
height: '320px',
|
|
29
|
+
showArrows: true,
|
|
30
|
+
showIndicators: true,
|
|
31
|
+
autoplay: false,
|
|
32
|
+
interval: 5000,
|
|
33
|
+
gap: 8,
|
|
34
|
+
visibleItems: 3,
|
|
35
|
+
animated: false,
|
|
36
|
+
},
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
const emit = defineEmits<{ change: [index: number] }>()
|
|
40
|
+
|
|
41
|
+
const trackEl = ref<HTMLElement>()
|
|
42
|
+
const itemEls = ref<HTMLElement[]>([])
|
|
43
|
+
const activeIndex = ref(0)
|
|
44
|
+
const scrollPos = ref(0)
|
|
45
|
+
const isPageBased = computed(() => props.layout === 'multi-browse' || props.layout === 'uncontained')
|
|
46
|
+
|
|
47
|
+
const totalPages = computed(() => {
|
|
48
|
+
if (!isPageBased.value) return props.items.length
|
|
49
|
+
if (!trackEl.value) return 1
|
|
50
|
+
const track = trackEl.value
|
|
51
|
+
return Math.max(1, Math.ceil(track.scrollWidth / track.clientWidth))
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const activePage = computed(() => {
|
|
55
|
+
if (!isPageBased.value) return activeIndex.value
|
|
56
|
+
if (!trackEl.value) return 0
|
|
57
|
+
const track = trackEl.value
|
|
58
|
+
if (track.scrollWidth <= track.clientWidth) return 0
|
|
59
|
+
const _ = scrollPos.value
|
|
60
|
+
return Math.round(track.scrollLeft / track.clientWidth)
|
|
61
|
+
})
|
|
62
|
+
const itemTransforms = ref<string[]>([])
|
|
63
|
+
const imageTransforms = ref<string[]>([])
|
|
64
|
+
let autoTimer: ReturnType<typeof setInterval> | null = null
|
|
65
|
+
let rafId = 0
|
|
66
|
+
let scrollTimeout: ReturnType<typeof setTimeout> | null = null
|
|
67
|
+
|
|
68
|
+
function setItemRef(el: any, i: number) {
|
|
69
|
+
if (el) itemEls.value[i] = el as HTMLElement
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ── Scroll handling (only transforms, never layout) ────────────
|
|
73
|
+
function onScroll() {
|
|
74
|
+
if (rafId) return
|
|
75
|
+
rafId = requestAnimationFrame(() => {
|
|
76
|
+
rafId = 0
|
|
77
|
+
updateTransforms()
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
if (scrollTimeout) clearTimeout(scrollTimeout)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function updateTransforms() {
|
|
84
|
+
if (!trackEl.value) return
|
|
85
|
+
const track = trackEl.value
|
|
86
|
+
scrollPos.value = track.scrollLeft
|
|
87
|
+
const trackRect = track.getBoundingClientRect()
|
|
88
|
+
const trackW = trackRect.width
|
|
89
|
+
const centerX = trackRect.left + trackW / 2
|
|
90
|
+
const transforms: string[] = []
|
|
91
|
+
const imgTransforms: string[] = []
|
|
92
|
+
let closest = 0
|
|
93
|
+
let closestDist = Infinity
|
|
94
|
+
|
|
95
|
+
const refX = props.layout === 'multi-browse' ? trackRect.left : centerX
|
|
96
|
+
|
|
97
|
+
for (let i = 0; i < itemEls.value.length; i++) {
|
|
98
|
+
const el = itemEls.value[i]
|
|
99
|
+
if (!el) { transforms.push(''); imgTransforms.push(''); continue }
|
|
100
|
+
const rect = el.getBoundingClientRect()
|
|
101
|
+
const itemCenterX = rect.left + rect.width / 2
|
|
102
|
+
const dist = props.layout === 'multi-browse'
|
|
103
|
+
? Math.abs(rect.left - refX)
|
|
104
|
+
: Math.abs(itemCenterX - centerX)
|
|
105
|
+
const norm = Math.min(dist / (trackW * 0.5), 1)
|
|
106
|
+
|
|
107
|
+
if (props.layout === 'hero') {
|
|
108
|
+
imgTransforms.push(`translateX(${(itemCenterX - centerX) * -0.08}px) scale(1.1)`)
|
|
109
|
+
transforms.push('')
|
|
110
|
+
} else if (props.layout === 'uncontained' && props.animated) {
|
|
111
|
+
const s = 1 - norm * 0.12
|
|
112
|
+
transforms.push(`scale(${s})`)
|
|
113
|
+
imgTransforms.push('')
|
|
114
|
+
} else {
|
|
115
|
+
transforms.push('')
|
|
116
|
+
imgTransforms.push('')
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (dist < closestDist) {
|
|
120
|
+
closestDist = dist
|
|
121
|
+
closest = i
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
itemTransforms.value = transforms
|
|
126
|
+
imageTransforms.value = imgTransforms
|
|
127
|
+
|
|
128
|
+
if (closest !== activeIndex.value) {
|
|
129
|
+
activeIndex.value = closest
|
|
130
|
+
emit('change', closest)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ── Fixed item widths (never change during scroll) ─────────────
|
|
135
|
+
function itemWidth(i: number) {
|
|
136
|
+
switch (props.layout) {
|
|
137
|
+
case 'hero':
|
|
138
|
+
return `calc(100% - 48px)`
|
|
139
|
+
case 'multi-browse':
|
|
140
|
+
return `calc(${100 / props.visibleItems}% - ${props.gap * (props.visibleItems - 1) / props.visibleItems}px)`
|
|
141
|
+
case 'uncontained':
|
|
142
|
+
return `${(props.items[i]?.ratio ?? 1) * 260}px`
|
|
143
|
+
default:
|
|
144
|
+
return '80%'
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function itemStyle(i: number) {
|
|
149
|
+
const style: Record<string, string> = {
|
|
150
|
+
flexShrink: '0',
|
|
151
|
+
width: itemWidth(i),
|
|
152
|
+
}
|
|
153
|
+
const t = itemTransforms.value[i]
|
|
154
|
+
if (t) {
|
|
155
|
+
style.transform = t
|
|
156
|
+
style.transition = 'transform 120ms ease-out'
|
|
157
|
+
}
|
|
158
|
+
return style
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function imgStyle(i: number) {
|
|
162
|
+
const t = imageTransforms.value[i]
|
|
163
|
+
if (!t) return {}
|
|
164
|
+
return { transform: t }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// ── Navigation ─────────────────────────────────────────────────
|
|
168
|
+
function scrollToItem(index: number) {
|
|
169
|
+
const el = itemEls.value[index]
|
|
170
|
+
if (!el || !trackEl.value) return
|
|
171
|
+
const track = trackEl.value
|
|
172
|
+
const scrollLeft = el.offsetLeft - (track.clientWidth - el.offsetWidth) / 2
|
|
173
|
+
track.scrollTo({ left: scrollLeft, behavior: 'smooth' })
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function scrollToPage(page: number) {
|
|
177
|
+
if (!trackEl.value) return
|
|
178
|
+
const track = trackEl.value
|
|
179
|
+
track.scrollTo({ left: page * track.clientWidth, behavior: 'smooth' })
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function next() {
|
|
183
|
+
if (isPageBased.value) {
|
|
184
|
+
if (!trackEl.value) return
|
|
185
|
+
trackEl.value.scrollBy({ left: trackEl.value.clientWidth, behavior: 'smooth' })
|
|
186
|
+
} else {
|
|
187
|
+
const i = activeIndex.value < props.items.length - 1 ? activeIndex.value + 1 : 0
|
|
188
|
+
scrollToItem(i)
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function prev() {
|
|
193
|
+
if (isPageBased.value) {
|
|
194
|
+
if (!trackEl.value) return
|
|
195
|
+
trackEl.value.scrollBy({ left: -trackEl.value.clientWidth, behavior: 'smooth' })
|
|
196
|
+
} else {
|
|
197
|
+
const i = activeIndex.value > 0 ? activeIndex.value - 1 : props.items.length - 1
|
|
198
|
+
scrollToItem(i)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function startAutoplay() {
|
|
203
|
+
stopAutoplay()
|
|
204
|
+
if (props.autoplay && props.items.length > 1) {
|
|
205
|
+
autoTimer = setInterval(next, props.interval)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function stopAutoplay() {
|
|
210
|
+
if (autoTimer) { clearInterval(autoTimer); autoTimer = null }
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function onKeydown(e: KeyboardEvent) {
|
|
214
|
+
if (e.key === 'ArrowRight') next()
|
|
215
|
+
else if (e.key === 'ArrowLeft') prev()
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const trackPadding = computed(() => {
|
|
219
|
+
if (props.layout === 'hero') return '0 24px'
|
|
220
|
+
if (props.layout === 'uncontained') return '0 24px'
|
|
221
|
+
return '0'
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
watch(() => props.autoplay, startAutoplay)
|
|
225
|
+
onMounted(() => {
|
|
226
|
+
startAutoplay()
|
|
227
|
+
nextTick(() => updateTransforms())
|
|
228
|
+
})
|
|
229
|
+
onBeforeUnmount(() => {
|
|
230
|
+
stopAutoplay()
|
|
231
|
+
if (rafId) cancelAnimationFrame(rafId)
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
defineExpose({ next, prev, scrollToItem, scrollToPage })
|
|
235
|
+
</script>
|
|
236
|
+
|
|
237
|
+
<template>
|
|
238
|
+
<div
|
|
239
|
+
class="group relative overflow-hidden"
|
|
240
|
+
tabindex="0"
|
|
241
|
+
@keydown="onKeydown"
|
|
242
|
+
@mouseenter="stopAutoplay"
|
|
243
|
+
@mouseleave="startAutoplay"
|
|
244
|
+
>
|
|
245
|
+
<div
|
|
246
|
+
ref="trackEl"
|
|
247
|
+
class="carousel-track flex overflow-x-auto"
|
|
248
|
+
:class="layout === 'multi-browse' ? 'snap-start' : 'snap-center'"
|
|
249
|
+
:style="{ height, gap: `${gap}px`, padding: trackPadding }"
|
|
250
|
+
@scroll.passive="onScroll"
|
|
251
|
+
>
|
|
252
|
+
<div
|
|
253
|
+
v-for="(item, i) in items"
|
|
254
|
+
:key="i"
|
|
255
|
+
:ref="(el) => setItemRef(el, i)"
|
|
256
|
+
class="relative overflow-hidden rounded-2xl"
|
|
257
|
+
:style="itemStyle(i)"
|
|
258
|
+
>
|
|
259
|
+
<img
|
|
260
|
+
:src="item.src"
|
|
261
|
+
:alt="item.alt ?? item.label ?? ''"
|
|
262
|
+
class="pointer-events-none h-full w-full object-cover"
|
|
263
|
+
:style="imgStyle(i)"
|
|
264
|
+
/>
|
|
265
|
+
|
|
266
|
+
<div
|
|
267
|
+
v-if="item.label || item.supportingText"
|
|
268
|
+
class="absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/70 via-black/30 to-transparent px-5 pb-4 pt-12"
|
|
269
|
+
>
|
|
270
|
+
<p v-if="item.label" class="text-title-medium font-medium text-white">{{ item.label }}</p>
|
|
271
|
+
<p v-if="item.supportingText" class="mt-0.5 text-body-small text-white/80">{{ item.supportingText }}</p>
|
|
272
|
+
</div>
|
|
273
|
+
</div>
|
|
274
|
+
</div>
|
|
275
|
+
|
|
276
|
+
<template v-if="showArrows && items.length > 1">
|
|
277
|
+
<button
|
|
278
|
+
type="button"
|
|
279
|
+
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"
|
|
280
|
+
@click="prev"
|
|
281
|
+
>
|
|
282
|
+
<MIcon name="chevron_left" :size="24" />
|
|
283
|
+
</button>
|
|
284
|
+
<button
|
|
285
|
+
type="button"
|
|
286
|
+
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"
|
|
287
|
+
@click="next"
|
|
288
|
+
>
|
|
289
|
+
<MIcon name="chevron_right" :size="24" />
|
|
290
|
+
</button>
|
|
291
|
+
</template>
|
|
292
|
+
|
|
293
|
+
</div>
|
|
294
|
+
|
|
295
|
+
<div
|
|
296
|
+
v-if="showIndicators && totalPages > 1"
|
|
297
|
+
class="flex justify-center gap-1.5 pt-3"
|
|
298
|
+
>
|
|
299
|
+
<button
|
|
300
|
+
v-for="p in totalPages"
|
|
301
|
+
:key="p"
|
|
302
|
+
type="button"
|
|
303
|
+
class="h-2 cursor-pointer rounded-full transition-all duration-300"
|
|
304
|
+
:class="(p - 1) === activePage ? 'w-5 bg-on-surface' : 'w-2 bg-on-surface/30 hover:bg-on-surface/50'"
|
|
305
|
+
@click="isPageBased ? scrollToPage(p - 1) : scrollToItem(p - 1)"
|
|
306
|
+
/>
|
|
307
|
+
</div>
|
|
308
|
+
</template>
|
|
309
|
+
|
|
310
|
+
<style scoped>
|
|
311
|
+
.carousel-track {
|
|
312
|
+
scroll-snap-type: x mandatory;
|
|
313
|
+
-webkit-overflow-scrolling: touch;
|
|
314
|
+
scrollbar-width: none;
|
|
315
|
+
}
|
|
316
|
+
.carousel-track::-webkit-scrollbar {
|
|
317
|
+
display: none;
|
|
318
|
+
}
|
|
319
|
+
.snap-center > * {
|
|
320
|
+
scroll-snap-align: center;
|
|
321
|
+
}
|
|
322
|
+
.snap-start > * {
|
|
323
|
+
scroll-snap-align: start;
|
|
324
|
+
}
|
|
325
|
+
</style>
|
|
@@ -104,6 +104,12 @@ function scrollToActive() {
|
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
function onGlobalKeydown(e: KeyboardEvent) {
|
|
107
|
+
// Every mounted MSpotlightSearch listens for its own hotkey independently —
|
|
108
|
+
// if more than one is mounted at once (e.g. an app-wide instance plus a
|
|
109
|
+
// page-local demo), a single keypress would otherwise open all of them.
|
|
110
|
+
// The first instance to handle it calls preventDefault(), which the rest
|
|
111
|
+
// check for and bail on, so only one ever claims a given keypress.
|
|
112
|
+
if (e.defaultPrevented) return
|
|
107
113
|
const tag = (e.target as HTMLElement).tagName
|
|
108
114
|
if (tag === 'INPUT' || tag === 'TEXTAREA' || (e.target as HTMLElement).isContentEditable) return
|
|
109
115
|
if (e.key === props.hotkey && !e.metaKey && !e.ctrlKey && !e.altKey) {
|