@m3ui-vue/m3ui-vue 0.6.3 → 0.7.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 +12 -6
- package/dist/{MMenuItem-C7jKU-Qh.js → MMenuItem-B_5um6tj.js} +2 -2
- package/dist/{MMenuItem-C7jKU-Qh.js.map → MMenuItem-B_5um6tj.js.map} +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MFab.vue.d.ts +24 -2
- package/dist/components/MFabMenu.vue.d.ts +37 -0
- package/dist/components/MLoadingIndicator.vue.d.ts +13 -0
- package/dist/components/MPullToRefresh.vue.d.ts +30 -0
- package/dist/components/MSearchBar.vue.d.ts +57 -0
- package/dist/components/MTopAppBar.vue.d.ts +4 -0
- package/dist/composables/useLocale.d.ts +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/locales/index.js +7 -0
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +1257 -770
- package/dist/m3ui.js.map +1 -1
- package/dist/rich-text-editor.js +2 -2
- package/dist/styles.css +1 -1
- package/dist/{useLocale-BKsIWNix.js → useLocale-DbREg9Zn.js} +2 -1
- package/dist/useLocale-DbREg9Zn.js.map +1 -0
- package/package.json +1 -1
- package/src/components/MFab.vue +63 -10
- package/src/components/MFabMenu.vue +167 -0
- package/src/components/MLoadingIndicator.vue +184 -0
- package/src/components/MPullToRefresh.vue +180 -0
- package/src/components/MSearchBar.vue +237 -0
- package/src/components/MSlider.vue +82 -33
- package/src/components/MTabs.vue +72 -33
- package/src/components/MTopAppBar.vue +16 -8
- package/src/composables/useLocale.ts +2 -0
- package/src/index.ts +5 -0
- package/src/locales/de.ts +1 -0
- package/src/locales/es.ts +1 -0
- package/src/locales/fr.ts +1 -0
- package/src/locales/ja.ts +1 -0
- package/src/locales/ko.ts +1 -0
- package/src/locales/pt.ts +1 -0
- package/src/locales/zh.ts +1 -0
- package/dist/useLocale-BKsIWNix.js.map +0 -1
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, onMounted, onUnmounted, ref } from "vue";
|
|
3
|
+
import { useLocale } from '../composables/useLocale';
|
|
4
|
+
|
|
5
|
+
const props = withDefaults(
|
|
6
|
+
defineProps<{
|
|
7
|
+
size?: number;
|
|
8
|
+
contained?: boolean;
|
|
9
|
+
color?: "primary" | "secondary" | "tertiary" | "error";
|
|
10
|
+
label?: string;
|
|
11
|
+
}>(),
|
|
12
|
+
{ size: 48, contained: false, color: "primary" },
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
const locale = useLocale();
|
|
16
|
+
|
|
17
|
+
const colorMap: Record<"primary" | "secondary" | "tertiary" | "error", { active: string; container: string }> = {
|
|
18
|
+
primary: { active: "var(--color-primary)", container: "var(--color-primary-container)" },
|
|
19
|
+
secondary: { active: "var(--color-secondary)", container: "var(--color-secondary-container)" },
|
|
20
|
+
tertiary: { active: "var(--color-tertiary)", container: "var(--color-tertiary-container)" },
|
|
21
|
+
error: { active: "var(--color-error)", container: "var(--color-error-container)" },
|
|
22
|
+
};
|
|
23
|
+
const svgColor = computed(() => colorMap[props.color]);
|
|
24
|
+
|
|
25
|
+
const innerSize = computed(() => (props.contained ? Math.round(props.size * 0.55) : props.size));
|
|
26
|
+
|
|
27
|
+
// ── Shape morph geometry ───────────────────────────────────────────────────
|
|
28
|
+
// Every shape is sampled as a radius multiplier at the same K angles, so any
|
|
29
|
+
// two shapes can be linearly interpolated point-by-point into a smooth
|
|
30
|
+
// in-between silhouette — the same trick as MSpinner's wavy path, applied to
|
|
31
|
+
// a rotating cast of shapes instead of a single fixed waveform.
|
|
32
|
+
const K = 120;
|
|
33
|
+
const R = 36;
|
|
34
|
+
// Hard ceiling on the sampled radius: the spring's overshoot can transiently
|
|
35
|
+
// push a point's interpolated radius past both shapes' own peaks, and the
|
|
36
|
+
// most eccentric shape (pill) already sits close to the viewBox edge — this
|
|
37
|
+
// keeps every shape's silhouette fully inside the 0..100 viewBox at all times.
|
|
38
|
+
const MAX_R = 47;
|
|
39
|
+
const angles: number[] = [];
|
|
40
|
+
const cosT: number[] = [];
|
|
41
|
+
const sinT: number[] = [];
|
|
42
|
+
for (let i = 0; i < K; i++) {
|
|
43
|
+
const theta = (2 * Math.PI * i) / K - Math.PI / 2;
|
|
44
|
+
angles.push(theta);
|
|
45
|
+
cosT.push(Math.cos(theta));
|
|
46
|
+
sinT.push(Math.sin(theta));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Same seven-shape sequence (and rough proportions) as Google's own
|
|
50
|
+
// androidx.compose.material3.LoadingIndicator: SoftBurst → Cookie9Sided →
|
|
51
|
+
// Pentagon → Pill → Sunny → Cookie4Sided → Oval → (loops back to SoftBurst).
|
|
52
|
+
// Exact vertex data isn't public outside the Kotlin source, so each is
|
|
53
|
+
// approximated as a radius multiplier at the same K angles as the rest.
|
|
54
|
+
type ShapeFn = (theta: number) => number;
|
|
55
|
+
function ellipse(a: number, b: number): ShapeFn {
|
|
56
|
+
return (t) => (a * b) / Math.sqrt((b * Math.cos(t)) ** 2 + (a * Math.sin(t)) ** 2);
|
|
57
|
+
}
|
|
58
|
+
const shapeFns: ShapeFn[] = [
|
|
59
|
+
(t) => 1 + 0.07 * Math.cos(10 * t), // soft burst
|
|
60
|
+
(t) => 1 + 0.15 * Math.cos(9 * t), // cookie (9 lobes)
|
|
61
|
+
(t) => 1 + 0.1 * Math.cos(5 * t) + 0.02 * Math.cos(10 * t), // pentagon
|
|
62
|
+
ellipse(1.3, 0.7), // pill
|
|
63
|
+
(t) => 1 + 0.22 * Math.cos(8 * t), // sunny (8-point burst)
|
|
64
|
+
(t) => 1 + 0.16 * Math.cos(4 * t), // cookie (4 lobes)
|
|
65
|
+
ellipse(1.2, 0.8), // oval
|
|
66
|
+
];
|
|
67
|
+
const shapes: number[][] = shapeFns.map((fn) => angles.map(fn));
|
|
68
|
+
const SHAPE_COUNT = shapes.length;
|
|
69
|
+
|
|
70
|
+
// Timing/physics mirror the real component: ~650ms per shape, morphed by an
|
|
71
|
+
// underdamped spring (dampingRatio 0.6, stiffness 200) rather than a fixed
|
|
72
|
+
// easing curve — it overshoots slightly and settles, which is what reads as
|
|
73
|
+
// a "bounce". The exact same progress value drives a synced +90° rotation
|
|
74
|
+
// swing on top of a slow constant spin, so the turn is what visually carries
|
|
75
|
+
// the shape change instead of the two feeling like separate animations.
|
|
76
|
+
const MORPH_INTERVAL_MS = 650;
|
|
77
|
+
const GLOBAL_ROTATION_MS = 4666;
|
|
78
|
+
const DAMPING = 0.6;
|
|
79
|
+
const OMEGA_N = Math.sqrt(200);
|
|
80
|
+
const OMEGA_D = OMEGA_N * Math.sqrt(1 - DAMPING * DAMPING);
|
|
81
|
+
|
|
82
|
+
function springProgress(tSec: number): number {
|
|
83
|
+
if (tSec <= 0) return 0;
|
|
84
|
+
const decay = Math.exp(-DAMPING * OMEGA_N * tSec);
|
|
85
|
+
return 1 - decay * (Math.cos(OMEGA_D * tSec) + ((DAMPING * OMEGA_N) / OMEGA_D) * Math.sin(OMEGA_D * tSec));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function easeInOut(t: number) {
|
|
89
|
+
return t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const ptX = new Float64Array(K);
|
|
93
|
+
const ptY = new Float64Array(K);
|
|
94
|
+
|
|
95
|
+
// Straight segments between the K sampled points would facet visibly on the
|
|
96
|
+
// more eccentric shapes (the pill/oval's tips are high-curvature and get
|
|
97
|
+
// under-sampled by uniform angle steps) — a closed Catmull-Rom spline
|
|
98
|
+
// (converted to cubic Béziers) keeps every edge tangent-smooth regardless.
|
|
99
|
+
function buildPath(from: number[], to: number[], f: number): string {
|
|
100
|
+
for (let i = 0; i < K; i++) {
|
|
101
|
+
const r = Math.min(R * (from[i]! + (to[i]! - from[i]!) * f), MAX_R);
|
|
102
|
+
ptX[i] = 50 + r * cosT[i]!;
|
|
103
|
+
ptY[i] = 50 + r * sinT[i]!;
|
|
104
|
+
}
|
|
105
|
+
let d = `M${ptX[0]!.toFixed(2)},${ptY[0]!.toFixed(2)}`;
|
|
106
|
+
for (let i = 0; i < K; i++) {
|
|
107
|
+
const i0 = (i - 1 + K) % K;
|
|
108
|
+
const i1 = i;
|
|
109
|
+
const i2 = (i + 1) % K;
|
|
110
|
+
const i3 = (i + 2) % K;
|
|
111
|
+
const c1x = ptX[i1]! + (ptX[i2]! - ptX[i0]!) / 6;
|
|
112
|
+
const c1y = ptY[i1]! + (ptY[i2]! - ptY[i0]!) / 6;
|
|
113
|
+
const c2x = ptX[i2]! - (ptX[i3]! - ptX[i1]!) / 6;
|
|
114
|
+
const c2y = ptY[i2]! - (ptY[i3]! - ptY[i1]!) / 6;
|
|
115
|
+
d += `C${c1x.toFixed(2)},${c1y.toFixed(2)},${c2x.toFixed(2)},${c2y.toFixed(2)},${ptX[i2]!.toFixed(2)},${ptY[i2]!.toFixed(2)}`;
|
|
116
|
+
}
|
|
117
|
+
return d + "Z";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const pathEl = ref<SVGPathElement | null>(null);
|
|
121
|
+
const svgEl = ref<SVGSVGElement | null>(null);
|
|
122
|
+
const prefersReducedMotion = ref(false);
|
|
123
|
+
|
|
124
|
+
let rafId = 0;
|
|
125
|
+
let mountTime = -1;
|
|
126
|
+
let shapeIndex = 0;
|
|
127
|
+
let segStart = -1;
|
|
128
|
+
let morphRotationTarget = 90; // starts at a quarter turn, like the source component
|
|
129
|
+
|
|
130
|
+
function tick(now: number) {
|
|
131
|
+
if (mountTime < 0) mountTime = now;
|
|
132
|
+
if (segStart < 0) segStart = now;
|
|
133
|
+
|
|
134
|
+
const reduced = prefersReducedMotion.value;
|
|
135
|
+
const interval = reduced ? MORPH_INTERVAL_MS * 1.8 : MORPH_INTERVAL_MS;
|
|
136
|
+
let elapsed = now - segStart;
|
|
137
|
+
if (elapsed >= interval) {
|
|
138
|
+
shapeIndex = (shapeIndex + 1) % SHAPE_COUNT;
|
|
139
|
+
morphRotationTarget = (morphRotationTarget + 90) % 360;
|
|
140
|
+
segStart = now;
|
|
141
|
+
elapsed = 0;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const p = reduced ? easeInOut(elapsed / interval) : springProgress(elapsed / 1000);
|
|
145
|
+
const from = shapes[shapeIndex]!;
|
|
146
|
+
const to = shapes[(shapeIndex + 1) % SHAPE_COUNT]!;
|
|
147
|
+
pathEl.value?.setAttribute("d", buildPath(from, to, p));
|
|
148
|
+
|
|
149
|
+
if (!reduced && svgEl.value) {
|
|
150
|
+
const globalDeg = (((now - mountTime) / GLOBAL_ROTATION_MS) * 360) % 360;
|
|
151
|
+
svgEl.value.style.transform = `rotate(${(p * 90 + morphRotationTarget + globalDeg).toFixed(2)}deg)`;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
rafId = requestAnimationFrame(tick);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
onMounted(() => {
|
|
158
|
+
prefersReducedMotion.value = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
159
|
+
pathEl.value?.setAttribute("d", buildPath(shapes[0]!, shapes[0]!, 0));
|
|
160
|
+
rafId = requestAnimationFrame(tick);
|
|
161
|
+
});
|
|
162
|
+
onUnmounted(() => cancelAnimationFrame(rafId));
|
|
163
|
+
</script>
|
|
164
|
+
|
|
165
|
+
<template>
|
|
166
|
+
<span
|
|
167
|
+
class="inline-flex shrink-0 items-center justify-center"
|
|
168
|
+
:class="contained ? 'rounded-full' : undefined"
|
|
169
|
+
:style="{ width: `${size}px`, height: `${size}px`, backgroundColor: contained ? svgColor.container : undefined }"
|
|
170
|
+
role="status"
|
|
171
|
+
:aria-label="label ?? locale.loading"
|
|
172
|
+
>
|
|
173
|
+
<svg
|
|
174
|
+
ref="svgEl"
|
|
175
|
+
:width="innerSize"
|
|
176
|
+
:height="innerSize"
|
|
177
|
+
viewBox="0 0 100 100"
|
|
178
|
+
shape-rendering="geometricPrecision"
|
|
179
|
+
style="transform-origin: 50% 50%"
|
|
180
|
+
>
|
|
181
|
+
<path ref="pathEl" shape-rendering="geometricPrecision" :style="{ fill: svgColor.active }" />
|
|
182
|
+
</svg>
|
|
183
|
+
</span>
|
|
184
|
+
</template>
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { ref, watch, onMounted, onBeforeUnmount } from "vue";
|
|
3
|
+
import MLoadingIndicator from "./MLoadingIndicator.vue";
|
|
4
|
+
|
|
5
|
+
const props = withDefaults(
|
|
6
|
+
defineProps<{
|
|
7
|
+
refreshing?: boolean;
|
|
8
|
+
disabled?: boolean;
|
|
9
|
+
threshold?: number;
|
|
10
|
+
size?: number;
|
|
11
|
+
color?: "primary" | "secondary" | "tertiary" | "error";
|
|
12
|
+
}>(),
|
|
13
|
+
{ refreshing: false, disabled: false, threshold: 64, size: 36, color: "primary" },
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
const emit = defineEmits<{ refresh: [] }>();
|
|
17
|
+
|
|
18
|
+
// Beyond `threshold`, the drag keeps following the pointer but scaled down —
|
|
19
|
+
// same "rubber band" feel as MLightbox's swipe-to-navigate.
|
|
20
|
+
const MAX_PULL_MULTIPLIER = 1.8;
|
|
21
|
+
const RESISTANCE = 0.45;
|
|
22
|
+
|
|
23
|
+
const scrollEl = ref<HTMLElement | null>(null);
|
|
24
|
+
const indicatorEl = ref<HTMLElement | null>(null);
|
|
25
|
+
const isPulling = ref(false);
|
|
26
|
+
|
|
27
|
+
let pointerId: number | null = null;
|
|
28
|
+
let startY = 0;
|
|
29
|
+
let pulling = false;
|
|
30
|
+
let pullDistance = 0;
|
|
31
|
+
// True once a drag has crossed the threshold and been released (or refreshing
|
|
32
|
+
// was set programmatically) — the indicator stays pinned until it clears.
|
|
33
|
+
let committed = false;
|
|
34
|
+
|
|
35
|
+
// Fixed resting inset once settled/refreshing — deliberately small and
|
|
36
|
+
// independent of `threshold`/`size` math, so it always reads as "just inside
|
|
37
|
+
// the edge" rather than wherever the drag happened to end up.
|
|
38
|
+
const SETTLE_GAP = 12;
|
|
39
|
+
|
|
40
|
+
function setTransition(el: HTMLElement, animate: boolean) {
|
|
41
|
+
el.style.transition = animate ? "transform 0.25s cubic-bezier(0.2, 0, 0, 1), opacity 0.25s ease" : "none";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Live preview while actively dragging — slides in from above (clipped/
|
|
45
|
+
// hidden by the container's overflow) and grows with the pull.
|
|
46
|
+
function previewIndicator(distance: number) {
|
|
47
|
+
const el = indicatorEl.value;
|
|
48
|
+
if (!el) return;
|
|
49
|
+
setTransition(el, false);
|
|
50
|
+
const progress = Math.min(1, distance / props.threshold);
|
|
51
|
+
el.style.transform = `translateY(${distance - props.size}px) scale(${0.5 + progress * 0.5})`;
|
|
52
|
+
el.style.opacity = `${progress}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function settleIndicator(animate: boolean) {
|
|
56
|
+
const el = indicatorEl.value;
|
|
57
|
+
if (!el) return;
|
|
58
|
+
setTransition(el, animate);
|
|
59
|
+
el.style.transform = `translateY(${SETTLE_GAP}px) scale(1)`;
|
|
60
|
+
el.style.opacity = "1";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function hideIndicator(animate: boolean) {
|
|
64
|
+
const el = indicatorEl.value;
|
|
65
|
+
if (!el) return;
|
|
66
|
+
setTransition(el, animate);
|
|
67
|
+
el.style.transform = `translateY(-${props.size}px) scale(0.5)`;
|
|
68
|
+
el.style.opacity = "0";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function onPointerDown(e: PointerEvent) {
|
|
72
|
+
if (props.disabled || props.refreshing || committed || e.button !== 0) return;
|
|
73
|
+
if (!scrollEl.value || scrollEl.value.scrollTop > 0) return;
|
|
74
|
+
pointerId = e.pointerId;
|
|
75
|
+
startY = e.clientY;
|
|
76
|
+
pulling = true;
|
|
77
|
+
isPulling.value = true;
|
|
78
|
+
// Declare up front (before any touchmove fires) that this element handles
|
|
79
|
+
// its own touch gestures now — waiting and calling preventDefault() from
|
|
80
|
+
// inside pointermove is too late on some mobile browsers, which may have
|
|
81
|
+
// already started resolving the touch as a native scroll/pull-to-refresh
|
|
82
|
+
// and cancel our pointer sequence out from under us. Note this only fully
|
|
83
|
+
// holds up in contexts without a competing OS/browser-chrome gesture layer
|
|
84
|
+
// (Capacitor/Electron shells, desktop mouse) — a real mobile browser tab
|
|
85
|
+
// can still occasionally win that race and cancel the drag.
|
|
86
|
+
scrollEl.value.style.touchAction = "none";
|
|
87
|
+
scrollEl.value.setPointerCapture(e.pointerId);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function onPointerMove(e: PointerEvent) {
|
|
91
|
+
if (!pulling || e.pointerId !== pointerId) return;
|
|
92
|
+
const raw = e.clientY - startY;
|
|
93
|
+
if (raw <= 0) {
|
|
94
|
+
// Dragging back upward — not a pull. Hand touch scrolling back so the
|
|
95
|
+
// rest of this gesture can scroll the list normally.
|
|
96
|
+
pullDistance = 0;
|
|
97
|
+
hideIndicator(false);
|
|
98
|
+
if (scrollEl.value) scrollEl.value.style.touchAction = "";
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
const maxPull = props.threshold * MAX_PULL_MULTIPLIER;
|
|
103
|
+
pullDistance = Math.min(
|
|
104
|
+
raw <= props.threshold ? raw : props.threshold + (raw - props.threshold) * RESISTANCE,
|
|
105
|
+
maxPull,
|
|
106
|
+
);
|
|
107
|
+
previewIndicator(pullDistance);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function endPull(e: PointerEvent) {
|
|
111
|
+
if (!pulling || e.pointerId !== pointerId) return;
|
|
112
|
+
pulling = false;
|
|
113
|
+
isPulling.value = false;
|
|
114
|
+
if (scrollEl.value) {
|
|
115
|
+
scrollEl.value.style.touchAction = "";
|
|
116
|
+
if (scrollEl.value.hasPointerCapture(e.pointerId)) scrollEl.value.releasePointerCapture(e.pointerId);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (pullDistance >= props.threshold) {
|
|
120
|
+
committed = true;
|
|
121
|
+
settleIndicator(true);
|
|
122
|
+
emit("refresh");
|
|
123
|
+
} else {
|
|
124
|
+
hideIndicator(true);
|
|
125
|
+
}
|
|
126
|
+
pullDistance = 0;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Also settles the indicator when a refresh is triggered programmatically
|
|
130
|
+
// (not from a drag) and releases it once the caller clears `refreshing`.
|
|
131
|
+
watch(
|
|
132
|
+
() => props.refreshing,
|
|
133
|
+
(refreshing) => {
|
|
134
|
+
if (refreshing) {
|
|
135
|
+
if (!committed) {
|
|
136
|
+
committed = true;
|
|
137
|
+
settleIndicator(true);
|
|
138
|
+
}
|
|
139
|
+
} else if (committed) {
|
|
140
|
+
committed = false;
|
|
141
|
+
hideIndicator(true);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
// The indicator's transform/opacity are owned entirely by the functions
|
|
147
|
+
// above, written directly to the DOM — never bound reactively in the
|
|
148
|
+
// template. A reactive `:style` there would fight this: Vue re-patches any
|
|
149
|
+
// style property it tracks on every re-render of this component (and
|
|
150
|
+
// `isPulling` toggling — used below for the `select-none` class — forces
|
|
151
|
+
// exactly such a re-render right at pointerdown/endPull), stomping whatever
|
|
152
|
+
// transform we'd just applied back to its bound value.
|
|
153
|
+
onMounted(() => hideIndicator(false));
|
|
154
|
+
onBeforeUnmount(() => {
|
|
155
|
+
pulling = false;
|
|
156
|
+
});
|
|
157
|
+
</script>
|
|
158
|
+
|
|
159
|
+
<template>
|
|
160
|
+
<div class="relative overflow-hidden">
|
|
161
|
+
<div
|
|
162
|
+
ref="indicatorEl"
|
|
163
|
+
class="pointer-events-none absolute inset-x-0 top-0 z-10 flex justify-center opacity-0"
|
|
164
|
+
>
|
|
165
|
+
<MLoadingIndicator :size="size" :color="color" contained />
|
|
166
|
+
</div>
|
|
167
|
+
|
|
168
|
+
<div
|
|
169
|
+
ref="scrollEl"
|
|
170
|
+
class="h-full overflow-y-auto overscroll-y-contain"
|
|
171
|
+
:class="{ 'select-none': isPulling }"
|
|
172
|
+
@pointerdown="onPointerDown"
|
|
173
|
+
@pointermove="onPointerMove"
|
|
174
|
+
@pointerup="endPull"
|
|
175
|
+
@pointercancel="endPull"
|
|
176
|
+
>
|
|
177
|
+
<slot />
|
|
178
|
+
</div>
|
|
179
|
+
</div>
|
|
180
|
+
</template>
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, ref, watch, onMounted, onUnmounted, nextTick, useId } from 'vue'
|
|
3
|
+
import MIcon from './MIcon.vue'
|
|
4
|
+
import MIconButton from './MIconButton.vue'
|
|
5
|
+
import MSpinner from './MSpinner.vue'
|
|
6
|
+
import { useLocale } from '../composables/useLocale'
|
|
7
|
+
|
|
8
|
+
const props = withDefaults(
|
|
9
|
+
defineProps<{
|
|
10
|
+
modelValue?: string
|
|
11
|
+
/** Controls the expanded/collapsed state — also works uncontrolled if omitted. */
|
|
12
|
+
expanded?: boolean
|
|
13
|
+
placeholder?: string
|
|
14
|
+
/** 'docked' grows into a rounded panel anchored below the bar (default).
|
|
15
|
+
* 'fullscreen' takes over the whole viewport, matching M3's mobile
|
|
16
|
+
* search pattern — the leading icon becomes a back button either way. */
|
|
17
|
+
variant?: 'docked' | 'fullscreen'
|
|
18
|
+
leadingIcon?: string
|
|
19
|
+
/** Which side of the *collapsed* bar the leading icon sits on. The
|
|
20
|
+
* expanded panel's back button always stays at the start regardless —
|
|
21
|
+
* that's a near-universal "go back" convention. */
|
|
22
|
+
iconPosition?: 'start' | 'end'
|
|
23
|
+
/** Centers the input/placeholder text instead of the M3-standard start
|
|
24
|
+
* alignment (an iOS-style look some apps use for the collapsed bar). */
|
|
25
|
+
textAlign?: 'start' | 'center'
|
|
26
|
+
loading?: boolean
|
|
27
|
+
clearable?: boolean
|
|
28
|
+
disabled?: boolean
|
|
29
|
+
}>(),
|
|
30
|
+
{
|
|
31
|
+
modelValue: '',
|
|
32
|
+
variant: 'docked',
|
|
33
|
+
leadingIcon: 'search',
|
|
34
|
+
iconPosition: 'start',
|
|
35
|
+
textAlign: 'start',
|
|
36
|
+
loading: false,
|
|
37
|
+
clearable: true,
|
|
38
|
+
disabled: false,
|
|
39
|
+
},
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
const emit = defineEmits<{
|
|
43
|
+
'update:modelValue': [string]
|
|
44
|
+
'update:expanded': [boolean]
|
|
45
|
+
search: [string]
|
|
46
|
+
}>()
|
|
47
|
+
|
|
48
|
+
const locale = useLocale()
|
|
49
|
+
const id = useId()
|
|
50
|
+
|
|
51
|
+
const expanded = ref(props.expanded ?? false)
|
|
52
|
+
watch(
|
|
53
|
+
() => props.expanded,
|
|
54
|
+
(v) => { if (v !== undefined && v !== expanded.value) expanded.value = v },
|
|
55
|
+
)
|
|
56
|
+
watch(expanded, (v) => emit('update:expanded', v))
|
|
57
|
+
|
|
58
|
+
const barEl = ref<HTMLElement | null>(null)
|
|
59
|
+
const panelEl = ref<HTMLElement | null>(null)
|
|
60
|
+
const inputEl = ref<HTMLInputElement | null>(null)
|
|
61
|
+
const panelPos = ref({ top: '0px', left: '0px', width: '0px' })
|
|
62
|
+
|
|
63
|
+
function computePanelPos() {
|
|
64
|
+
if (!barEl.value) return
|
|
65
|
+
const rect = barEl.value.getBoundingClientRect()
|
|
66
|
+
panelPos.value = { top: `${rect.top}px`, left: `${rect.left}px`, width: `${rect.width}px` }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function open() {
|
|
70
|
+
if (props.disabled || expanded.value) return
|
|
71
|
+
if (props.variant === 'docked') computePanelPos()
|
|
72
|
+
expanded.value = true
|
|
73
|
+
nextTick(() => inputEl.value?.focus())
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function close() {
|
|
77
|
+
if (!expanded.value) return
|
|
78
|
+
expanded.value = false
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function onInput(e: Event) {
|
|
82
|
+
emit('update:modelValue', (e.target as HTMLInputElement).value)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function onClear() {
|
|
86
|
+
emit('update:modelValue', '')
|
|
87
|
+
nextTick(() => inputEl.value?.focus())
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function onKeydown(e: KeyboardEvent) {
|
|
91
|
+
if (e.key === 'Escape') {
|
|
92
|
+
close()
|
|
93
|
+
inputEl.value?.blur()
|
|
94
|
+
} else if (e.key === 'Enter') {
|
|
95
|
+
emit('search', props.modelValue)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function onOutsideClick(e: MouseEvent) {
|
|
100
|
+
if (props.variant !== 'docked' || !expanded.value) return
|
|
101
|
+
const t = e.target as Node
|
|
102
|
+
if (barEl.value?.contains(t) || panelEl.value?.contains(t)) return
|
|
103
|
+
close()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function onScroll(e: Event) {
|
|
107
|
+
if (props.variant !== 'docked' || !expanded.value) return
|
|
108
|
+
if (panelEl.value?.contains(e.target as Node)) return
|
|
109
|
+
close()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
onMounted(() => {
|
|
113
|
+
document.addEventListener('mousedown', onOutsideClick)
|
|
114
|
+
window.addEventListener('scroll', onScroll, true)
|
|
115
|
+
})
|
|
116
|
+
onUnmounted(() => {
|
|
117
|
+
document.removeEventListener('mousedown', onOutsideClick)
|
|
118
|
+
window.removeEventListener('scroll', onScroll, true)
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
watch(expanded, (v) => {
|
|
122
|
+
if (v && props.variant === 'fullscreen') document.body.style.overflow = 'hidden'
|
|
123
|
+
else document.body.style.overflow = ''
|
|
124
|
+
})
|
|
125
|
+
onUnmounted(() => { document.body.style.overflow = '' })
|
|
126
|
+
</script>
|
|
127
|
+
|
|
128
|
+
<template>
|
|
129
|
+
<div ref="barEl" class="relative w-full">
|
|
130
|
+
<div
|
|
131
|
+
v-show="!expanded"
|
|
132
|
+
class="flex h-14 items-center gap-3 rounded-full bg-surface-container-high px-4 shadow-elevation-1 transition-shadow duration-150"
|
|
133
|
+
:class="disabled ? 'pointer-events-none opacity-[0.38]' : ''"
|
|
134
|
+
>
|
|
135
|
+
<span class="inline-flex shrink-0 items-center" :class="iconPosition === 'end' ? 'order-last' : ''">
|
|
136
|
+
<slot name="leading">
|
|
137
|
+
<MIcon :name="leadingIcon" :size="20" class="text-on-surface" />
|
|
138
|
+
</slot>
|
|
139
|
+
</span>
|
|
140
|
+
<input
|
|
141
|
+
:id="id"
|
|
142
|
+
type="text"
|
|
143
|
+
role="searchbox"
|
|
144
|
+
:value="modelValue"
|
|
145
|
+
:placeholder="placeholder ?? locale.search"
|
|
146
|
+
:disabled="disabled"
|
|
147
|
+
:aria-expanded="expanded"
|
|
148
|
+
class="w-full bg-transparent text-body-large text-on-surface outline-none placeholder:text-on-surface-variant"
|
|
149
|
+
:class="textAlign === 'center' ? 'text-center' : ''"
|
|
150
|
+
@focus="open"
|
|
151
|
+
@input="onInput"
|
|
152
|
+
@keydown="onKeydown"
|
|
153
|
+
/>
|
|
154
|
+
<button
|
|
155
|
+
v-if="clearable && modelValue && !disabled"
|
|
156
|
+
type="button"
|
|
157
|
+
class="flex h-8 w-8 shrink-0 cursor-pointer items-center justify-center rounded-full text-on-surface-variant transition-colors hover:bg-on-surface/8 hover:text-on-surface"
|
|
158
|
+
@mousedown.prevent="onClear"
|
|
159
|
+
>
|
|
160
|
+
<MIcon name="close" :size="18" />
|
|
161
|
+
</button>
|
|
162
|
+
<slot name="trailing" />
|
|
163
|
+
</div>
|
|
164
|
+
</div>
|
|
165
|
+
|
|
166
|
+
<Teleport to="body">
|
|
167
|
+
<Transition :name="variant === 'fullscreen' ? 'm3-searchbar-full' : 'm3-searchbar-docked'">
|
|
168
|
+
<div
|
|
169
|
+
v-if="expanded"
|
|
170
|
+
ref="panelEl"
|
|
171
|
+
class="fixed z-500 flex flex-col overflow-hidden bg-surface-container-high"
|
|
172
|
+
:class="variant === 'fullscreen' ? 'inset-0' : 'max-h-[70vh] rounded-[28px] shadow-elevation-3'"
|
|
173
|
+
:style="variant === 'docked' ? panelPos : undefined"
|
|
174
|
+
>
|
|
175
|
+
<div
|
|
176
|
+
class="flex shrink-0 items-center gap-3 px-4"
|
|
177
|
+
:class="variant === 'fullscreen' ? 'h-[72px]' : 'h-14'"
|
|
178
|
+
>
|
|
179
|
+
<MIconButton
|
|
180
|
+
icon="arrow_back"
|
|
181
|
+
:label="locale.close"
|
|
182
|
+
variant="standard"
|
|
183
|
+
@click="close"
|
|
184
|
+
/>
|
|
185
|
+
<input
|
|
186
|
+
ref="inputEl"
|
|
187
|
+
type="text"
|
|
188
|
+
role="searchbox"
|
|
189
|
+
:value="modelValue"
|
|
190
|
+
:placeholder="placeholder ?? locale.search"
|
|
191
|
+
class="w-full bg-transparent text-body-large text-on-surface outline-none placeholder:text-on-surface-variant"
|
|
192
|
+
:class="textAlign === 'center' ? 'text-center' : ''"
|
|
193
|
+
@input="onInput"
|
|
194
|
+
@keydown="onKeydown"
|
|
195
|
+
/>
|
|
196
|
+
<MSpinner v-if="loading" :size="20" class="shrink-0 text-on-surface-variant" />
|
|
197
|
+
<button
|
|
198
|
+
v-else-if="clearable && modelValue"
|
|
199
|
+
type="button"
|
|
200
|
+
class="flex h-8 w-8 shrink-0 cursor-pointer items-center justify-center rounded-full text-on-surface-variant transition-colors hover:bg-on-surface/8 hover:text-on-surface"
|
|
201
|
+
@mousedown.prevent="onClear"
|
|
202
|
+
>
|
|
203
|
+
<MIcon name="close" :size="18" />
|
|
204
|
+
</button>
|
|
205
|
+
<slot name="trailing" />
|
|
206
|
+
</div>
|
|
207
|
+
|
|
208
|
+
<div class="h-px shrink-0 bg-outline-variant" />
|
|
209
|
+
|
|
210
|
+
<div class="flex-1 overflow-y-auto">
|
|
211
|
+
<slot />
|
|
212
|
+
</div>
|
|
213
|
+
</div>
|
|
214
|
+
</Transition>
|
|
215
|
+
</Teleport>
|
|
216
|
+
</template>
|
|
217
|
+
|
|
218
|
+
<style scoped>
|
|
219
|
+
.m3-searchbar-docked-enter-active,
|
|
220
|
+
.m3-searchbar-docked-leave-active {
|
|
221
|
+
transition: opacity 150ms ease, transform 150ms cubic-bezier(0.2, 0, 0, 1);
|
|
222
|
+
transform-origin: top center;
|
|
223
|
+
}
|
|
224
|
+
.m3-searchbar-docked-enter-from,
|
|
225
|
+
.m3-searchbar-docked-leave-to {
|
|
226
|
+
opacity: 0;
|
|
227
|
+
transform: scaleY(0.9);
|
|
228
|
+
}
|
|
229
|
+
.m3-searchbar-full-enter-active,
|
|
230
|
+
.m3-searchbar-full-leave-active {
|
|
231
|
+
transition: opacity 150ms ease;
|
|
232
|
+
}
|
|
233
|
+
.m3-searchbar-full-enter-from,
|
|
234
|
+
.m3-searchbar-full-leave-to {
|
|
235
|
+
opacity: 0;
|
|
236
|
+
}
|
|
237
|
+
</style>
|