@zooui/nuxt 0.1.1 → 0.3.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 +14 -2
- package/components/ConfirmButton.vue +124 -0
- package/components/ConfirmDialog.vue +201 -0
- package/components/DateField.vue +176 -42
- package/components/FileDropOverlay.vue +275 -0
- package/components/FileList.vue +155 -0
- package/components/FilterSelect.vue +340 -0
- package/components/OptionalDate.vue +180 -0
- package/components/OrderList.vue +495 -0
- package/components/RepeatingList.vue +133 -21
- package/components/SelectMenu.vue +325 -0
- package/components/confirm.ts +163 -0
- package/components/row-snapshot.ts +48 -0
- package/components/select-typeahead.ts +79 -0
- package/composables/useConfirm.ts +68 -0
- package/nuxt.config.ts +5 -1
- package/package.json +2 -1
- package/utils/confirm-queue.ts +85 -0
- package/utils/date-entry.ts +189 -8
- package/utils/file-drop.ts +151 -0
- package/utils/filter-match.ts +62 -0
- package/utils/order-list.ts +42 -0
package/components/DateField.vue
CHANGED
|
@@ -1,26 +1,42 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// A calendar-day field: type a date, press a shortcut key, or pick from a calendar. The model is a Temporal.PlainDate (
|
|
3
|
-
// column comes back as, so a form round-trips the server's value without ever touching a string or a
|
|
1
|
+
<script setup lang="ts" generic="T extends Temporal.PlainDate | CalendarDateLike | null">
|
|
2
|
+
// A calendar-day field: type a date, press a shortcut key, or pick from a calendar. The model is a Temporal.PlainDate (the
|
|
3
|
+
// same type a `date` column comes back as, so a form round-trips the server's value without ever touching a string or a
|
|
4
|
+
// time zone) or an @internationalized/date CalendarDate (the type Nuxt UI's UCalendar and UInputDate use), or null.
|
|
5
|
+
//
|
|
6
|
+
// THE CALLER'S TYPE. Whatever the model holds is what comes back, in `update:modelValue`, `dateUpdate` and `invalid`
|
|
7
|
+
// alike; a CalendarDate is built with the caller's own class. A model that starts out null hands back `modelType`'s
|
|
8
|
+
// type ('plain-date' by default), and one that held a day and was cleared keeps the type it had. Only a Gregorian
|
|
9
|
+
// CalendarDate is taken: one in another calendar throws (toPlainDate), since converting it by its fields would land
|
|
10
|
+
// on another day. Inside, everything is a PlainDate; the conversion is at the edges.
|
|
4
11
|
//
|
|
5
12
|
// WHY NO `Date`. A JavaScript Date is an instant, not a day. Every conversion between the two goes through a
|
|
6
13
|
// time zone, and every one of those is a chance for the 12th to become the 11th. So nothing here constructs one:
|
|
7
|
-
// the typed text is parsed by parseDateEntry (utils/date-entry.ts, pure Temporal), and
|
|
8
|
-
//
|
|
9
|
-
// "Today" comes from useToday().
|
|
14
|
+
// the typed text is parsed by parseDateEntry (utils/date-entry.ts, pure Temporal), and a CalendarDate is converted
|
|
15
|
+
// by its fields alone (year, month, day), never via toDate()/getLocalTimeZone(). "Today" comes from useToday().
|
|
10
16
|
//
|
|
11
17
|
// <ZDateField v-model="form.endDate" clearable class="w-full" />
|
|
12
18
|
// <ZDateField v-model="due" format="DD.MM.YYYY" locale="de" />
|
|
19
|
+
// <ZDateField v-model="due" :min-value="opened" :max-value="closes" :is-date-unavailable="isHoliday" />
|
|
13
20
|
//
|
|
14
21
|
// FORMAT AND LOCALE. `locale` names the calendar's language and week (Nuxt UI's locale when unset, `en` unless the
|
|
15
22
|
// app's <UApp :locale> says otherwise). `format` is how the box writes and reads a day — a pattern such as
|
|
16
23
|
// "MM/DD/YYYY", "DD/MM/YYYY", "DD.MM.YYYY" or "YYYY-MM-DD" — and when unset it is the locale's own numeric order
|
|
17
24
|
// (dateEntryFormatFor), so `en` gives MM/DD/YYYY. The placeholder is the pattern.
|
|
18
25
|
//
|
|
19
|
-
// Typing (in MM/DD/YYYY): "5" or "12" is that day this month, "0512" is May 12, "051226"
|
|
26
|
+
// Typing (in MM/DD/YYYY): "5" or "12" is that day this month, "0512" is May 12, "051226", "05122026", "5/12/26" and
|
|
20
27
|
// "5/12/2026" carry the year; other formats read the same shapes in their own order. Enter, Tab or leaving the
|
|
21
28
|
// field commits; text that isn't a date clears the field. Shortcut keys (see applyDateShortcut): t today, m/h
|
|
22
29
|
// start/end of month, y/r start/end of year, + and - a day either way (_ too, so a format whose separator is "-"
|
|
23
|
-
// keeps a key for the day before).
|
|
30
|
+
// keeps a key for the day before). A shortcut steps from the committed date (today when there is none), never from
|
|
31
|
+
// text typed but not yet committed: with 03/15/2026 saved, "04/0" then + is 03/16/2026.
|
|
32
|
+
//
|
|
33
|
+
// LIMITS. `minValue`, `maxValue` and `isDateUnavailable`, named as Nuxt UI's UCalendar names them, go to the calendar,
|
|
34
|
+
// which disables those days. A typed date they refuse is not committed: the field keeps the date it had and draws it
|
|
35
|
+
// back in the box (as with text that isn't a date, the box never shows a day the form isn't holding), and is marked
|
|
36
|
+
// invalid — aria-invalid, Nuxt UI's error color, a message for screen readers — until the next edit, pick, shortcut or
|
|
37
|
+
// clear. `invalid` carries the problem (why, the date, the limit, an English message) for the product to show, and
|
|
38
|
+
// null once it's gone; a refused Enter doesn't announce `dateUpdate`. The shortcuts stop at the limits and skip
|
|
39
|
+
// unavailable days (applyDateShortcut says how, `t` included).
|
|
24
40
|
//
|
|
25
41
|
// `dateUpdate` fires when a date is settled — picked, cleared, set by a shortcut, committed with Enter, or when
|
|
26
42
|
// focus leaves the field for somewhere other than its own calendar (Tab included, once) — which is the moment
|
|
@@ -28,17 +44,25 @@
|
|
|
28
44
|
// the parent's v-model hasn't caught up yet, and fires once per settle: leaving right after Enter, a pick, a
|
|
29
45
|
// shortcut or a clear doesn't announce the same date again. Plain forms can ignore it and read v-model.
|
|
30
46
|
//
|
|
31
|
-
// Also: a leading `icon` whose click emits `iconClick
|
|
32
|
-
// and one-letter weekday headers (the locale's narrow weekday).
|
|
33
|
-
import type { CalendarDate as CalendarDay } from '@internationalized/date'
|
|
47
|
+
// Also: a leading `icon` whose click emits `iconClick` (or a `#leading` slot in its place), a `calendarIcon` in place
|
|
48
|
+
// of the calendar, and one-letter weekday headers (the locale's narrow weekday).
|
|
34
49
|
import { CalendarDate } from '@internationalized/date'
|
|
35
|
-
import { useLocale } from '@nuxt/ui/composables'
|
|
36
|
-
import { Temporal } from 'temporal-polyfill/implementation'
|
|
50
|
+
import { formFieldInjectionKey, useLocale } from '@nuxt/ui/composables'
|
|
51
|
+
import type { Temporal } from 'temporal-polyfill/implementation'
|
|
37
52
|
import { useToday } from '../composables/useToday'
|
|
38
|
-
import {
|
|
53
|
+
import {
|
|
54
|
+
applyDateShortcut, checkDateLimits, commitDateEntry, dateEntryFormat, dateEntryFormatFor, dateLimitMessage, dateModelTypeOf, formatDateEntry,
|
|
55
|
+
fromPlainDate, isDateShortcut, parseDateEntry, toPlainDate,
|
|
56
|
+
type CalendarDateLike, type DateLimitProblem, type DateLimits, type DateModelType,
|
|
57
|
+
} from '../utils/date-entry'
|
|
39
58
|
|
|
40
59
|
defineOptions({ inheritAttrs: false })
|
|
41
60
|
|
|
61
|
+
// A CalendarDate is typed by the fields read from it (CalendarDateLike), not its class: Vue's ref() unwraps a class
|
|
62
|
+
// with private fields into a type the class no longer accepts, so a `ref<CalendarDate | null>` would not type-check
|
|
63
|
+
// against the class itself.
|
|
64
|
+
type Day = NonNullable<T>
|
|
65
|
+
|
|
42
66
|
const props = withDefaults(
|
|
43
67
|
defineProps<{
|
|
44
68
|
clearable?: boolean
|
|
@@ -61,37 +85,112 @@ const props = withDefaults(
|
|
|
61
85
|
/** The two buttons' accessible names, for a product in another language. */
|
|
62
86
|
clearLabel?: string
|
|
63
87
|
chooseLabel?: string
|
|
88
|
+
/** The type to hand back while the model is null and has held no day: a CalendarDate v-model that starts out
|
|
89
|
+
* null needs `calendar-date`. */
|
|
90
|
+
modelType?: DateModelType
|
|
91
|
+
/** The first day the field takes, a PlainDate or a CalendarDate. */
|
|
92
|
+
minValue?: Temporal.PlainDate | CalendarDateLike
|
|
93
|
+
/** The last day the field takes, a PlainDate or a CalendarDate. */
|
|
94
|
+
maxValue?: Temporal.PlainDate | CalendarDateLike
|
|
95
|
+
/** Days the field refuses between the two; called with a day of the model's type. */
|
|
96
|
+
isDateUnavailable?: (date: Day) => boolean
|
|
64
97
|
}>(),
|
|
65
|
-
{ portal: true, calendarIcon: 'i-lucide-calendar', clearLabel: 'Clear date', chooseLabel: 'Choose a date' },
|
|
98
|
+
{ portal: true, calendarIcon: 'i-lucide-calendar', clearLabel: 'Clear date', chooseLabel: 'Choose a date', modelType: 'plain-date' },
|
|
66
99
|
)
|
|
67
100
|
const emit = defineEmits<{
|
|
68
|
-
dateUpdate: [value:
|
|
101
|
+
dateUpdate: [value: T | null]
|
|
102
|
+
// A typed day refused by the limits, and null once the field is no longer marked invalid.
|
|
103
|
+
invalid: [problem: (DateLimitProblem<Day> & { message: string }) | null]
|
|
69
104
|
clear: []
|
|
70
105
|
// A click on the leading `icon` (`iconClick`).
|
|
71
106
|
iconClick: [event: MouseEvent]
|
|
72
107
|
}>()
|
|
73
|
-
const model = defineModel<
|
|
108
|
+
const model = defineModel<T | null>({ default: null })
|
|
109
|
+
defineSlots<{
|
|
110
|
+
/** UInput's leading slot, in place of the `icon` when given; `ui` is the input's, for its `leadingIcon` class. */
|
|
111
|
+
leading?(props: { ui: Record<string, (options?: { class?: unknown }) => string> }): unknown
|
|
112
|
+
}>()
|
|
74
113
|
|
|
75
114
|
const { locale: uiLocale } = useLocale()
|
|
76
115
|
const localeCode = computed(() => props.locale ?? uiLocale.value.code)
|
|
77
116
|
const entryFormat = computed(() => (props.format ? dateEntryFormat(props.format) : dateEntryFormatFor(localeCode.value)))
|
|
78
117
|
const show = (d: Temporal.PlainDate | null) => formatDateEntry(d, entryFormat.value)
|
|
79
118
|
|
|
119
|
+
// The caller's type, kept through an empty spell, and the CalendarDate it last held (whose class builds the next).
|
|
120
|
+
let remembered: DateModelType | undefined
|
|
121
|
+
let like: CalendarDateLike | undefined
|
|
122
|
+
function remember(v: unknown) {
|
|
123
|
+
const type = dateModelTypeOf(v)
|
|
124
|
+
if (type) remembered = type
|
|
125
|
+
if (type === 'calendar-date') like = v as CalendarDateLike
|
|
126
|
+
}
|
|
127
|
+
remember(model.value)
|
|
128
|
+
const kind = () => dateModelTypeOf(model.value) ?? remembered ?? props.modelType
|
|
129
|
+
/** A day as the caller's type. */
|
|
130
|
+
const out = (d: Temporal.PlainDate | null) => fromPlainDate(d, kind(), like) as T | null
|
|
131
|
+
const outDay = (d: Temporal.PlainDate) => fromPlainDate(d, kind(), like) as Day
|
|
132
|
+
|
|
80
133
|
const open = ref(false)
|
|
81
|
-
const text = ref(show(model.value))
|
|
134
|
+
const text = ref(show(toPlainDate(model.value)))
|
|
82
135
|
|
|
83
136
|
const sameDay = (a: Temporal.PlainDate | null, b: Temporal.PlainDate | null) => a === b || (!!a && !!b && a.equals(b))
|
|
84
137
|
|
|
138
|
+
const limits = computed<DateLimits>(() => {
|
|
139
|
+
const unavailable = props.isDateUnavailable
|
|
140
|
+
return {
|
|
141
|
+
minValue: toPlainDate(props.minValue),
|
|
142
|
+
maxValue: toPlainDate(props.maxValue),
|
|
143
|
+
isDateUnavailable: unavailable && ((d: Temporal.PlainDate) => unavailable(outDay(d))),
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
// ── Invalid ───────────────────────────────────────────────────────────────────────────────────────────────────
|
|
148
|
+
const problem = ref<DateLimitProblem | null>(null)
|
|
149
|
+
const problemMessage = computed(() => (problem.value ? dateLimitMessage(problem.value, entryFormat.value) : ''))
|
|
150
|
+
function flag(p: DateLimitProblem | null) {
|
|
151
|
+
if (!p && !problem.value) return
|
|
152
|
+
problem.value = p
|
|
153
|
+
emit('invalid', p && { reason: p.reason, date: outDay(p.date), limit: p.limit && outDay(p.limit), message: problemMessage.value })
|
|
154
|
+
}
|
|
155
|
+
// In a UFormField, Nuxt UI's UInput takes aria-invalid, its error color and aria-describedby from the field, over
|
|
156
|
+
// anything passed to it: so while a day is refused the field the UInput sees carries the message as its error (unless
|
|
157
|
+
// it has one of its own, which the product may have set from `invalid`), and the message below takes the id that
|
|
158
|
+
// aria-describedby names. Outside one, they are passed to the UInput directly.
|
|
159
|
+
const formField = inject(formFieldInjectionKey, undefined)
|
|
160
|
+
if (formField) {
|
|
161
|
+
provide(formFieldInjectionKey, computed(() => {
|
|
162
|
+
const f = formField.value
|
|
163
|
+
return problem.value && f && !f.error ? { ...f, error: problemMessage.value } : f
|
|
164
|
+
}))
|
|
165
|
+
}
|
|
166
|
+
const panelId = useId()
|
|
167
|
+
const messageId = computed(() => {
|
|
168
|
+
if (!problem.value) return undefined
|
|
169
|
+
if (!formField?.value) return `${panelId}-invalid`
|
|
170
|
+
return formField.value.error ? undefined : `${formField.value.ariaId}-error`
|
|
171
|
+
})
|
|
172
|
+
const attrs = useAttrs()
|
|
173
|
+
const invalidAttrs = computed(() => {
|
|
174
|
+
if (!problem.value || formField?.value) return {}
|
|
175
|
+
const describedBy = [attrs['aria-describedby'], messageId.value].filter(Boolean).join(' ')
|
|
176
|
+
return { 'color': 'error', 'aria-invalid': true, 'aria-describedby': describedBy }
|
|
177
|
+
})
|
|
178
|
+
|
|
85
179
|
// The date this field last settled on, which is what `dateUpdate` reports. NEVER re-read `model.value` for
|
|
86
180
|
// that: when the parent owns the v-model, writing it only emits `update:modelValue`, and the prop comes back
|
|
87
181
|
// on the parent's next render — so in the same tick `model.value` is still the OLD date, and a blur that just
|
|
88
182
|
// committed "5/12" would announce null.
|
|
89
|
-
let settled = model.value
|
|
183
|
+
let settled = toPlainDate(model.value)
|
|
90
184
|
// The date `dateUpdate` last carried, boxed so an announced null is told apart from nothing announced yet.
|
|
91
185
|
let announced: { date: Temporal.PlainDate | null } | undefined
|
|
92
|
-
watch(model, (
|
|
186
|
+
watch(model, (v) => {
|
|
187
|
+
remember(v)
|
|
188
|
+
const d = toPlainDate(v)
|
|
93
189
|
// The parent set a different date, so whatever we announced before is no longer what's in the field.
|
|
94
|
-
if (!sameDay(d, settled))
|
|
190
|
+
if (!sameDay(d, settled)) {
|
|
191
|
+
announced = undefined
|
|
192
|
+
flag(null)
|
|
193
|
+
}
|
|
95
194
|
settled = d
|
|
96
195
|
text.value = show(d)
|
|
97
196
|
})
|
|
@@ -99,10 +198,9 @@ watch(model, (d) => {
|
|
|
99
198
|
watch(entryFormat, () => { text.value = show(settled) })
|
|
100
199
|
|
|
101
200
|
const inputCmp = useTemplateRef<{ inputRef: HTMLInputElement | null, $el: HTMLElement }>('inputCmp')
|
|
102
|
-
const panelId = useId()
|
|
103
201
|
|
|
104
202
|
function set(d: Temporal.PlainDate | null) {
|
|
105
|
-
if (!sameDay(d, settled)) model.value = d
|
|
203
|
+
if (!sameDay(d, settled)) model.value = out(d)
|
|
106
204
|
settled = d
|
|
107
205
|
text.value = show(d)
|
|
108
206
|
return d
|
|
@@ -110,9 +208,22 @@ function set(d: Temporal.PlainDate | null) {
|
|
|
110
208
|
|
|
111
209
|
// What's typed becomes the value — or nothing, when it isn't a date. Leaving "5/32" in the box would show one
|
|
112
210
|
// thing while the form saved another. Untouched text keeps the date as it is, so passing through the field
|
|
113
|
-
// can't blank a stored date it wouldn't read back (a year before 1000).
|
|
114
|
-
|
|
115
|
-
|
|
211
|
+
// can't blank a stored date it wouldn't read back (a year before 1000). A day the limits refuse keeps the date
|
|
212
|
+
// there was, drawn back in the box, and marks the field invalid: undefined, for nothing settled.
|
|
213
|
+
function commitText(): { date: Temporal.PlainDate | null } | undefined {
|
|
214
|
+
const next = commitDateEntry(text.value, settled, useToday(), entryFormat.value)
|
|
215
|
+
const refused = next && !sameDay(next, settled) ? checkDateLimits(next, limits.value) : null
|
|
216
|
+
if (refused) {
|
|
217
|
+
text.value = show(settled)
|
|
218
|
+
flag(refused)
|
|
219
|
+
return undefined
|
|
220
|
+
}
|
|
221
|
+
return { date: set(next) }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function onType(v: string | number | null | undefined) {
|
|
225
|
+
text.value = v == null ? '' : String(v)
|
|
226
|
+
flag(null)
|
|
116
227
|
}
|
|
117
228
|
|
|
118
229
|
function focusInput() {
|
|
@@ -133,7 +244,7 @@ function isOurs(target: EventTarget | null) {
|
|
|
133
244
|
|
|
134
245
|
function announce(d: Temporal.PlainDate | null) {
|
|
135
246
|
announced = { date: d }
|
|
136
|
-
emit('dateUpdate', d)
|
|
247
|
+
emit('dateUpdate', out(d))
|
|
137
248
|
}
|
|
138
249
|
|
|
139
250
|
// Leaving settles the field, but a date already announced (by Enter, say) isn't announced again: an inline
|
|
@@ -160,7 +271,10 @@ function onKeydown(e: KeyboardEvent) {
|
|
|
160
271
|
}
|
|
161
272
|
if (e.key === 'Enter') {
|
|
162
273
|
open.value = false
|
|
163
|
-
|
|
274
|
+
const committed = commitText()
|
|
275
|
+
if (committed) announce(committed.date)
|
|
276
|
+
// Refused: the date it kept is selected, so typing another replaces it.
|
|
277
|
+
else nextTick(() => inputCmp.value?.inputRef?.select())
|
|
164
278
|
return
|
|
165
279
|
}
|
|
166
280
|
// Tab commits here, so the text is settled before focus moves; the blur that follows announces it, once.
|
|
@@ -169,12 +283,14 @@ function onKeydown(e: KeyboardEvent) {
|
|
|
169
283
|
open.value = false
|
|
170
284
|
return
|
|
171
285
|
}
|
|
172
|
-
if (e.key.length !== 1) return
|
|
286
|
+
if (e.key.length !== 1 || !isDateShortcut(e.key, entryFormat.value)) return
|
|
287
|
+
e.preventDefault()
|
|
173
288
|
const today = useToday()
|
|
174
|
-
// Shortcuts step from
|
|
175
|
-
|
|
289
|
+
// Shortcuts step from the committed date, not from text typed since: the typed text is dropped. With no allowed day
|
|
290
|
+
// to go to, the key does nothing.
|
|
291
|
+
const next = applyDateShortcut(e.key, settled, today, entryFormat.value, limits.value)
|
|
176
292
|
if (!next) return
|
|
177
|
-
|
|
293
|
+
flag(null)
|
|
178
294
|
announce(set(next))
|
|
179
295
|
// Selected, so typing a date replaces the shortcut's rather than appending to it.
|
|
180
296
|
nextTick(() => inputCmp.value?.inputRef?.select())
|
|
@@ -182,32 +298,41 @@ function onKeydown(e: KeyboardEvent) {
|
|
|
182
298
|
|
|
183
299
|
function clear() {
|
|
184
300
|
const d = set(null)
|
|
301
|
+
flag(null)
|
|
185
302
|
emit('clear')
|
|
186
303
|
announce(d)
|
|
187
304
|
focusInput()
|
|
188
305
|
}
|
|
189
306
|
|
|
190
307
|
// ── Calendar ──────────────────────────────────────────────────────────────────────────────────────────────
|
|
191
|
-
// CalendarDate
|
|
308
|
+
// UCalendar takes Nuxt UI's CalendarDate, a bare year/month/day, so the conversion is by fields both ways.
|
|
192
309
|
const toCalendar = (d: Temporal.PlainDate) => new CalendarDate(d.year, d.month, d.day)
|
|
193
310
|
|
|
194
311
|
// The selection follows text typed but not yet committed, as the month and the shortcuts do, so the picker and the
|
|
195
312
|
// input never disagree; untouched or unreadable text falls back to the model.
|
|
196
313
|
const calendarValue = computed(() => {
|
|
197
|
-
const day = parseDateEntry(text.value, useToday(), entryFormat.value) ?? model.value
|
|
314
|
+
const day = parseDateEntry(text.value, useToday(), entryFormat.value) ?? toPlainDate(model.value)
|
|
198
315
|
return day ? toCalendar(day) : undefined
|
|
199
316
|
})
|
|
200
317
|
// The month the calendar shows: the date in the box (typed or committed), else this month. Only read while
|
|
201
318
|
// the popover is open, which is only ever in the browser.
|
|
202
319
|
const calendarMonth = computed(() =>
|
|
203
|
-
toCalendar(parseDateEntry(text.value, useToday(), entryFormat.value) ?? model.value ?? useToday()),
|
|
320
|
+
toCalendar(parseDateEntry(text.value, useToday(), entryFormat.value) ?? toPlainDate(model.value) ?? useToday()),
|
|
204
321
|
)
|
|
322
|
+
// The limits, for the calendar to disable those days.
|
|
323
|
+
const calendarMin = computed(() => (limits.value.minValue ? toCalendar(limits.value.minValue as Temporal.PlainDate) : undefined))
|
|
324
|
+
const calendarMax = computed(() => (limits.value.maxValue ? toCalendar(limits.value.maxValue as Temporal.PlainDate) : undefined))
|
|
325
|
+
const calendarUnavailable = computed(() => {
|
|
326
|
+
const unavailable = limits.value.isDateUnavailable
|
|
327
|
+
return unavailable ? (v: CalendarDateLike) => unavailable(toPlainDate(v)!) : undefined
|
|
328
|
+
})
|
|
205
329
|
|
|
206
330
|
function onPick(v: unknown) {
|
|
207
|
-
|
|
208
|
-
|
|
331
|
+
const type = dateModelTypeOf(v)
|
|
332
|
+
if (type !== 'calendar-date') return
|
|
209
333
|
open.value = false
|
|
210
|
-
|
|
334
|
+
flag(null)
|
|
335
|
+
announce(set(toPlainDate(v as CalendarDateLike)))
|
|
211
336
|
focusInput()
|
|
212
337
|
}
|
|
213
338
|
|
|
@@ -248,23 +373,29 @@ defineExpose({ focus: focusInput })
|
|
|
248
373
|
takes Nuxt UI's highlight ring, so it reads as the one being edited. -->
|
|
249
374
|
<UInput
|
|
250
375
|
ref="inputCmp"
|
|
251
|
-
|
|
376
|
+
:model-value="text"
|
|
252
377
|
type="text"
|
|
253
378
|
autocomplete="off"
|
|
254
|
-
v-bind="
|
|
379
|
+
v-bind="{ ...$attrs, ...invalidAttrs }"
|
|
255
380
|
:size="size"
|
|
256
381
|
:icon="icon"
|
|
257
382
|
:disabled="disabled"
|
|
258
383
|
:placeholder="placeholder ?? entryFormat.pattern"
|
|
259
|
-
:highlight="highlight || open"
|
|
384
|
+
:highlight="highlight || open || !!problem"
|
|
260
385
|
:ui="{ ...ui, root: `group ${ui?.root ?? ''}` }"
|
|
386
|
+
@update:model-value="onType"
|
|
261
387
|
@keydown="onKeydown"
|
|
262
388
|
@blur="onBlur"
|
|
263
389
|
>
|
|
264
|
-
<template v-if="
|
|
390
|
+
<template v-if="$slots.leading" #leading="slotProps">
|
|
391
|
+
<slot name="leading" v-bind="slotProps as never" />
|
|
392
|
+
</template>
|
|
393
|
+
<template v-else-if="icon" #leading="{ ui: inputUi }">
|
|
265
394
|
<UIcon :name="icon" data-slot="leadingIcon" :class="inputUi.leadingIcon({ class: ui?.leadingIcon })" @click="emit('iconClick', $event)" />
|
|
266
395
|
</template>
|
|
267
396
|
<template #trailing="{ ui: inputUi }">
|
|
397
|
+
<!-- Why a typed day was refused, for screen readers: the input's aria-describedby names it. -->
|
|
398
|
+
<span v-if="messageId" :id="messageId" class="sr-only">{{ problemMessage }}</span>
|
|
268
399
|
<!-- Neither button takes focus (mousedown is prevented), so the input never blurs to them. Each icon is
|
|
269
400
|
`text-current`, over Nuxt UI's dimmed trailing icon, so the button's own color reaches it. -->
|
|
270
401
|
<button
|
|
@@ -302,6 +433,9 @@ defineExpose({ focus: focusInput })
|
|
|
302
433
|
:locale="localeCode"
|
|
303
434
|
:size="size"
|
|
304
435
|
:view-control="headingButton"
|
|
436
|
+
:min-value="calendarMin"
|
|
437
|
+
:max-value="calendarMax"
|
|
438
|
+
:is-date-unavailable="calendarUnavailable"
|
|
305
439
|
weekday-format="narrow"
|
|
306
440
|
prevent-deselect
|
|
307
441
|
class="p-2"
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* Drag events an overlay nearer the event's target has already counted. Every instance shares it: listeners on an
|
|
4
|
+
* inner target run before those on an outer one (or the window), so the innermost overlay under the pointer claims each
|
|
5
|
+
* event and the outer ones leave it alone. That splits the drag's count by region: the dialog's overlay counts what
|
|
6
|
+
* happens over the dialog, the page's what happens over the rest, and each shows only while the pointer is over its own.
|
|
7
|
+
*/
|
|
8
|
+
const claimed = new WeakSet<Event>()
|
|
9
|
+
function claim(e: Event) {
|
|
10
|
+
if (claimed.has(e)) return false
|
|
11
|
+
claimed.add(e)
|
|
12
|
+
return true
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The targets of every contained overlay attached now, for the modal check below. */
|
|
16
|
+
const containedTargets = new Set<HTMLElement>()
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The open modal dialog `el` is in, if any: a `<dialog>` opened with showModal(), an `aria-modal` dialog, or one that
|
|
20
|
+
* hides the rest of the page from assistive tech, which is how reka-ui marks its modals (Nuxt UI's UModal, USlideover):
|
|
21
|
+
* it sets `data-aria-hidden` on everything outside them rather than `aria-modal` on them.
|
|
22
|
+
*/
|
|
23
|
+
function modalOf(el: HTMLElement): HTMLElement | null {
|
|
24
|
+
const dialog = el.closest<HTMLElement>('dialog, [role="dialog"], [role="alertdialog"]')
|
|
25
|
+
if (!dialog?.isConnected) return null
|
|
26
|
+
if (dialog instanceof HTMLDialogElement) {
|
|
27
|
+
try { return dialog.matches(':modal') ? dialog : null } catch { return null }
|
|
28
|
+
}
|
|
29
|
+
if (dialog.getAttribute('aria-modal') === 'true') return dialog
|
|
30
|
+
for (let n: HTMLElement | null = dialog; n?.parentElement && n !== document.body; n = n.parentElement) {
|
|
31
|
+
for (const sibling of n.parentElement.children) {
|
|
32
|
+
if (sibling !== n && sibling.getAttribute('data-aria-hidden') === 'true') return dialog
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Whether an overlay contained in an open modal that `container` is outside holds the page: this one stands down. */
|
|
39
|
+
function outrankedByModal(container: HTMLElement) {
|
|
40
|
+
for (const target of containedTargets) {
|
|
41
|
+
if (target === container) continue
|
|
42
|
+
const modal = modalOf(target)
|
|
43
|
+
if (modal && !modal.contains(container)) return true
|
|
44
|
+
}
|
|
45
|
+
return false
|
|
46
|
+
}
|
|
47
|
+
</script>
|
|
48
|
+
|
|
49
|
+
<script setup lang="ts">
|
|
50
|
+
// Files dropped anywhere in the window — or in one element, a dialog say — rather than on a box: while files are
|
|
51
|
+
// dragged over it an overlay says what dropping does, and the drop hands the files back, sorted by `accept` and
|
|
52
|
+
// `multiple` into @drop and @rejected. Nuxt UI's UFileUpload is the box; this is the page. Written on Nuxt UI's tokens.
|
|
53
|
+
//
|
|
54
|
+
// <ZFileDropOverlay accept="image/*,.pdf" multiple label="Drop photos and PDFs to attach them" @drop="upload" @rejected="warn" />
|
|
55
|
+
// <div ref="body"> … <ZFileDropOverlay :target="body" @drop="add" /></div> <!-- contained in the element -->
|
|
56
|
+
//
|
|
57
|
+
// The drag is counted, not tracked by target: dragenter and dragleave fire for every element the pointer crosses,
|
|
58
|
+
// enter on the new one before leave on the old, so the overlay is shown while the count is above zero and the page's
|
|
59
|
+
// own children cannot make it flicker. Only drags that carry files count; text, links and elements dragged within the
|
|
60
|
+
// page are left alone. The overlay is only a picture (`pointer-events: none`): the drag's events stay on the page's
|
|
61
|
+
// own elements, so showing or hiding it never moves the pointer off one, a drop zone under it still gets its drop, and
|
|
62
|
+
// an overlay nested inside it can be reached.
|
|
63
|
+
import { dragCarriesFiles, droppedFiles, sortDroppedFiles, type FileFilter, type RejectedFile } from '../utils/file-drop'
|
|
64
|
+
|
|
65
|
+
defineOptions({ inheritAttrs: false })
|
|
66
|
+
|
|
67
|
+
const props = withDefaults(
|
|
68
|
+
defineProps<{
|
|
69
|
+
/** Files to take, as the native input's `accept`: MIME types, `image/*`, `.pdf`, comma-separated or a list. */
|
|
70
|
+
accept?: string | string[]
|
|
71
|
+
/** Take every matching file; without it the first is taken and the rest go to @rejected. */
|
|
72
|
+
multiple?: boolean
|
|
73
|
+
/** A rule of the product's own, run on each file `accept` takes (a size limit, a name rule): `true` takes it;
|
|
74
|
+
* `false`, or a message saying why, sends it to @rejected as `filter`, with the message. */
|
|
75
|
+
filter?: FileFilter
|
|
76
|
+
/** No overlay and no @drop; a file dropped is still kept from opening in the tab. */
|
|
77
|
+
disabled?: boolean
|
|
78
|
+
/** What the overlay says, and what a screen reader hears when it appears. */
|
|
79
|
+
label?: string
|
|
80
|
+
/** A second line under the label. */
|
|
81
|
+
description?: string
|
|
82
|
+
/** The overlay's icon, such as `i-lucide-upload`; none by default. */
|
|
83
|
+
icon?: string | false
|
|
84
|
+
/** Take drops in this element only, with the overlay drawn inside it, rather than the window: an element (a template
|
|
85
|
+
* ref to one), a component, or a selector. `null` takes no drops, for a ref that isn't set yet. A selector is
|
|
86
|
+
* looked up when the overlay mounts: for an element rendered later (a dialog's content), pass a template ref. */
|
|
87
|
+
target?: HTMLElement | { $el: unknown } | string | null
|
|
88
|
+
}>(),
|
|
89
|
+
{
|
|
90
|
+
accept: undefined,
|
|
91
|
+
multiple: false,
|
|
92
|
+
filter: undefined,
|
|
93
|
+
disabled: false,
|
|
94
|
+
label: 'Drop your files',
|
|
95
|
+
description: undefined,
|
|
96
|
+
icon: false,
|
|
97
|
+
target: undefined,
|
|
98
|
+
},
|
|
99
|
+
)
|
|
100
|
+
const emit = defineEmits<{
|
|
101
|
+
/** The files taken, in drop order; not emitted when none were. */
|
|
102
|
+
drop: [files: File[]]
|
|
103
|
+
/** The files refused, each with its reason (and `filter`'s message); not emitted when none were. */
|
|
104
|
+
rejected: [files: RejectedFile[]]
|
|
105
|
+
}>()
|
|
106
|
+
defineSlots<{
|
|
107
|
+
/** The overlay's message, in place of the icon, label and description. */
|
|
108
|
+
default?(props: { label: string; accept?: string | string[]; multiple: boolean }): unknown
|
|
109
|
+
}>()
|
|
110
|
+
|
|
111
|
+
/** Files are being dragged over the window (or the target), outside any overlay nested in it. */
|
|
112
|
+
const dragging = ref(false)
|
|
113
|
+
/** Escape was pressed during this drag: the overlay stays hidden until the drag ends. */
|
|
114
|
+
const dismissed = ref(false)
|
|
115
|
+
/** An overlay contained in an open modal this one is outside holds the page: this one shows and takes nothing, as if
|
|
116
|
+
* disabled. Read again on every drag event, so it lifts by itself when the modal closes. */
|
|
117
|
+
const standingDown = ref(false)
|
|
118
|
+
const visible = computed(() => dragging.value && !dismissed.value && !props.disabled && !standingDown.value)
|
|
119
|
+
|
|
120
|
+
let depth = 0
|
|
121
|
+
function reset() {
|
|
122
|
+
depth = 0
|
|
123
|
+
dragging.value = false
|
|
124
|
+
dismissed.value = false
|
|
125
|
+
standingDown.value = false
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The element the overlay is contained in, when `target` names one; null for the window. */
|
|
129
|
+
function resolveTarget(): HTMLElement | null {
|
|
130
|
+
const t = props.target
|
|
131
|
+
if (!t) return null
|
|
132
|
+
if (typeof t === 'string') return document.querySelector<HTMLElement>(t)
|
|
133
|
+
if (t instanceof HTMLElement) return t
|
|
134
|
+
return t.$el instanceof HTMLElement ? t.$el : null
|
|
135
|
+
}
|
|
136
|
+
const scoped = shallowRef<HTMLElement | null>(null)
|
|
137
|
+
|
|
138
|
+
// Every handler claims the file drag's event first: one an overlay nested deeper already claimed is its own, so this
|
|
139
|
+
// one neither counts nor answers it. Claimed whether or not this one is disabled, so the count stays whole when
|
|
140
|
+
// `disabled` changes mid-drag.
|
|
141
|
+
function onEnter(e: DragEvent) {
|
|
142
|
+
if (!dragCarriesFiles(e.dataTransfer) || !claim(e)) return
|
|
143
|
+
depth += 1
|
|
144
|
+
standingDown.value = !!bound && outrankedByModal(bound)
|
|
145
|
+
dragging.value = true
|
|
146
|
+
}
|
|
147
|
+
function onLeave(e: DragEvent) {
|
|
148
|
+
if (!dragCarriesFiles(e.dataTransfer) || !claim(e)) return
|
|
149
|
+
if (!depth) return
|
|
150
|
+
depth -= 1
|
|
151
|
+
if (!depth) reset()
|
|
152
|
+
}
|
|
153
|
+
function onOver(e: DragEvent) {
|
|
154
|
+
if (!dragCarriesFiles(e.dataTransfer) || !claim(e)) return
|
|
155
|
+
// A drop zone under the pointer that took the event (a UFileUpload) keeps its own dropEffect: 'none' here would
|
|
156
|
+
// cancel its drop, and 'copy' would let through one it refused.
|
|
157
|
+
if (e.defaultPrevented) return
|
|
158
|
+
standingDown.value = !!bound && outrankedByModal(bound)
|
|
159
|
+
// Without this the drop never fires and the browser opens the file in the tab.
|
|
160
|
+
e.preventDefault()
|
|
161
|
+
if (e.dataTransfer) e.dataTransfer.dropEffect = visible.value ? 'copy' : 'none'
|
|
162
|
+
}
|
|
163
|
+
function onDrop(e: DragEvent) {
|
|
164
|
+
if (!dragCarriesFiles(e.dataTransfer)) return
|
|
165
|
+
// The drop ends the drag for every overlay; only the one it was claimed by can take it.
|
|
166
|
+
if (!claim(e)) return reset()
|
|
167
|
+
// A drop zone under the pointer took these files itself: handing them out again would add them twice.
|
|
168
|
+
const takenByZone = e.defaultPrevented
|
|
169
|
+
e.preventDefault()
|
|
170
|
+
const take = visible.value && !takenByZone && !(bound && outrankedByModal(bound))
|
|
171
|
+
reset()
|
|
172
|
+
if (!take || !e.dataTransfer) return
|
|
173
|
+
const { accepted, rejected } = sortDroppedFiles(droppedFiles(e.dataTransfer), { accept: props.accept, multiple: props.multiple, filter: props.filter })
|
|
174
|
+
if (accepted.length) emit('drop', accepted)
|
|
175
|
+
if (rejected.length) emit('rejected', rejected)
|
|
176
|
+
}
|
|
177
|
+
function onKeydown(e: KeyboardEvent) {
|
|
178
|
+
if (e.key === 'Escape' && visible.value) dismissed.value = true
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
let bound: HTMLElement | null = null
|
|
182
|
+
/** The target's own `position` before it was made a positioning context for the overlay, to put back. */
|
|
183
|
+
let ownPosition: string | null = null
|
|
184
|
+
function bind(el: HTMLElement) {
|
|
185
|
+
el.addEventListener('dragenter', onEnter)
|
|
186
|
+
el.addEventListener('dragleave', onLeave)
|
|
187
|
+
el.addEventListener('dragover', onOver)
|
|
188
|
+
el.addEventListener('drop', onDrop)
|
|
189
|
+
if (el !== document.documentElement) containedTargets.add(el)
|
|
190
|
+
if (el !== document.documentElement && getComputedStyle(el).position === 'static') {
|
|
191
|
+
ownPosition = el.style.position
|
|
192
|
+
el.style.position = 'relative'
|
|
193
|
+
}
|
|
194
|
+
bound = el
|
|
195
|
+
}
|
|
196
|
+
function unbind() {
|
|
197
|
+
if (!bound) return
|
|
198
|
+
bound.removeEventListener('dragenter', onEnter)
|
|
199
|
+
bound.removeEventListener('dragleave', onLeave)
|
|
200
|
+
bound.removeEventListener('dragover', onOver)
|
|
201
|
+
bound.removeEventListener('drop', onDrop)
|
|
202
|
+
containedTargets.delete(bound)
|
|
203
|
+
if (ownPosition !== null) bound.style.position = ownPosition
|
|
204
|
+
ownPosition = null
|
|
205
|
+
bound = null
|
|
206
|
+
}
|
|
207
|
+
function rebind() {
|
|
208
|
+
unbind()
|
|
209
|
+
reset()
|
|
210
|
+
scoped.value = resolveTarget()
|
|
211
|
+
// A target that names nothing (yet) takes no drops, rather than the whole window.
|
|
212
|
+
if (props.target !== undefined && !scoped.value) return
|
|
213
|
+
bind(scoped.value ?? document.documentElement)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Client only: nothing is attached during SSR, and everything is taken off on unmount.
|
|
217
|
+
let mounted = false
|
|
218
|
+
onMounted(() => {
|
|
219
|
+
mounted = true
|
|
220
|
+
rebind()
|
|
221
|
+
document.addEventListener('keydown', onKeydown)
|
|
222
|
+
})
|
|
223
|
+
watch(() => props.target, () => { if (mounted) rebind() }, { flush: 'post' })
|
|
224
|
+
onBeforeUnmount(() => {
|
|
225
|
+
mounted = false
|
|
226
|
+
unbind()
|
|
227
|
+
document.removeEventListener('keydown', onKeydown)
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
// The announcement, set as the overlay appears and cleared as it goes, so the next drag is heard again.
|
|
231
|
+
const announcement = ref('')
|
|
232
|
+
watch(visible, (v) => { announcement.value = v ? props.label : '' })
|
|
233
|
+
|
|
234
|
+
defineExpose({
|
|
235
|
+
/** Whether the overlay is showing. */
|
|
236
|
+
visible,
|
|
237
|
+
/** Hide the overlay for the rest of this drag, as Escape does. */
|
|
238
|
+
dismiss: () => { if (dragging.value) dismissed.value = true },
|
|
239
|
+
})
|
|
240
|
+
</script>
|
|
241
|
+
|
|
242
|
+
<template>
|
|
243
|
+
<span class="sr-only" role="status" aria-live="polite" data-file-drop-announce>{{ announcement }}</span>
|
|
244
|
+
<!-- Over the window, in <body>; contained, inside the target itself, so it sits in the target's stacking context
|
|
245
|
+
(a dialog's, over the dialog's content) with no measuring. -->
|
|
246
|
+
<Teleport :to="scoped ?? 'body'">
|
|
247
|
+
<!-- Fades in with the message growing from a little smaller, and out a little faster; none with reduced motion. -->
|
|
248
|
+
<Transition
|
|
249
|
+
enter-active-class="transition-opacity duration-150 ease-out *:transition-transform *:duration-150 *:ease-out motion-reduce:duration-0 motion-reduce:*:duration-0"
|
|
250
|
+
enter-from-class="opacity-0 *:scale-95"
|
|
251
|
+
leave-active-class="transition-opacity duration-100 ease-in *:transition-transform *:duration-100 *:ease-in motion-reduce:duration-0 motion-reduce:*:duration-0"
|
|
252
|
+
leave-to-class="opacity-0 *:scale-95"
|
|
253
|
+
>
|
|
254
|
+
<div
|
|
255
|
+
v-if="visible"
|
|
256
|
+
v-bind="$attrs"
|
|
257
|
+
data-file-drop-overlay
|
|
258
|
+
:data-contained="scoped ? '' : undefined"
|
|
259
|
+
aria-hidden="true"
|
|
260
|
+
:class="[
|
|
261
|
+
'pointer-events-none flex items-center justify-center bg-default/75 backdrop-blur-sm',
|
|
262
|
+
scoped ? 'absolute inset-0 z-10 rounded-[inherit] p-3' : 'fixed inset-0 z-50 p-6',
|
|
263
|
+
]"
|
|
264
|
+
>
|
|
265
|
+
<div class="flex max-w-full flex-col items-center gap-3 rounded-lg border-2 border-dashed border-accented bg-elevated px-10 py-8 text-center shadow-lg" data-file-drop-message>
|
|
266
|
+
<slot :label="label" :accept="accept" :multiple="multiple">
|
|
267
|
+
<UIcon v-if="icon" :name="icon" class="size-10 text-primary" data-file-drop-icon />
|
|
268
|
+
<p class="text-base font-semibold text-highlighted">{{ label }}</p>
|
|
269
|
+
<p v-if="description" class="text-sm text-muted">{{ description }}</p>
|
|
270
|
+
</slot>
|
|
271
|
+
</div>
|
|
272
|
+
</div>
|
|
273
|
+
</Transition>
|
|
274
|
+
</Teleport>
|
|
275
|
+
</template>
|