@zooui/nuxt 0.3.1 → 0.4.1
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/CodeBlock.vue +10 -4
- package/components/DateField.vue +62 -265
- package/components/FileDropOverlay.vue +2 -1
- package/components/FilterSelect.vue +3 -6
- package/components/Highlight.vue +37 -0
- package/components/MaskedInput.vue +199 -0
- package/components/MonthField.vue +183 -0
- package/components/OptionalDate.vue +1 -1
- package/components/OrderList.vue +2 -2
- package/components/RepeatingList.vue +1 -1
- package/components/SearchInput.vue +135 -0
- package/components/Status.vue +22 -9
- package/components/UrlInput.vue +110 -0
- package/components/YearField.vue +143 -0
- package/components/confirm.ts +1 -3
- package/components/internal/EntryField.vue +317 -0
- package/components/internal/entry-field.ts +41 -0
- package/components/recipes.ts +30 -2
- package/composables/useNotify.ts +90 -0
- package/composables/useStatusStates.ts +45 -0
- package/nuxt.config.ts +7 -5
- package/package.json +14 -1
- package/plugins/error-message.ts +11 -0
- package/utils/date-entry.ts +9 -5
- package/utils/error-message.ts +96 -0
- package/utils/masked-input.ts +306 -0
- package/utils/month-entry.ts +283 -0
- package/utils/status-states.ts +97 -0
- package/utils/url-input.ts +71 -0
- package/utils/year-entry.ts +96 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// A website address field: Nuxt UI's UInput (placeholder "https://example.com") that tidies the address when it is
|
|
3
|
+
// committed, on blur or Enter. It is type="text" with inputmode="url" and autocomplete="url" (the phone keyboard and
|
|
4
|
+
// autofill of a url input) rather than type="url": text kept because it failed the check would also fail the
|
|
5
|
+
// browser's own url check, and inside a form that blocks submit with the browser's bubble before the form's rules
|
|
6
|
+
// and Nuxt UI's error styling ever run.
|
|
7
|
+
//
|
|
8
|
+
// <UFormField label="Website" :error="websiteError">
|
|
9
|
+
// <ZUrlInput v-model="form.website" v-model:error="websiteError" class="w-full" />
|
|
10
|
+
// </UFormField>
|
|
11
|
+
//
|
|
12
|
+
// TYPING writes the text to the model as it is, keystroke by keystroke; nothing is rewritten until the address is
|
|
13
|
+
// committed. COMMITTING (commitWebAddress, utils/url-input.ts) trims it, puts `https://` in front (in place of an
|
|
14
|
+
// `http://`), and checks there is a real host with a top-level domain: an address is written back normalized,
|
|
15
|
+
// blank text becomes null, and anything else is kept, trimmed, with the field marked invalid until the next commit
|
|
16
|
+
// (or until the parent sets another value). `add-www` also puts `www.` in front of a two-label host; it is off by
|
|
17
|
+
// default, because many real hosts ("example.app") have no `www` name.
|
|
18
|
+
//
|
|
19
|
+
// Committing happens in a capture-phase blur on the wrapper, ahead of UInput's own blur, which is where a surrounding
|
|
20
|
+
// UForm validates the field: its rules see the normalized address, not the raw text. Enter commits too, before the
|
|
21
|
+
// form's implicit submit.
|
|
22
|
+
//
|
|
23
|
+
// THE ERROR. `error` (v-model:error) is the message while the address is invalid, `invalid-message` ("Invalid website
|
|
24
|
+
// address" unless set), and null otherwise; a parent may also set one of its own. In a UFormField, Nuxt UI's UInput
|
|
25
|
+
// takes aria-invalid, its error color and aria-describedby from the field, over anything passed to it: so while there
|
|
26
|
+
// is an error the field the UInput sees carries it (unless the field has one of its own). Bind `v-model:error` to the
|
|
27
|
+
// UFormField's `error` and the field shows the message; unbound, a screen reader still hears it, from a visually
|
|
28
|
+
// hidden message aria-describedby names. Outside a UFormField, they are passed to the UInput directly.
|
|
29
|
+
import { formFieldInjectionKey } from '@nuxt/ui/composables'
|
|
30
|
+
import { commitWebAddress } from '../utils/url-input'
|
|
31
|
+
|
|
32
|
+
defineOptions({ inheritAttrs: false })
|
|
33
|
+
|
|
34
|
+
const props = withDefaults(
|
|
35
|
+
defineProps<{
|
|
36
|
+
/** Put `www.` in front of a two-label host ("example.com" → "www.example.com"). */
|
|
37
|
+
addWww?: boolean
|
|
38
|
+
/** The error message for text that isn't a website address. */
|
|
39
|
+
invalidMessage?: string
|
|
40
|
+
placeholder?: string
|
|
41
|
+
}>(),
|
|
42
|
+
{ addWww: false, invalidMessage: 'Invalid website address', placeholder: 'https://example.com' },
|
|
43
|
+
)
|
|
44
|
+
const model = defineModel<string | null>({ default: null })
|
|
45
|
+
const error = defineModel<string | null>('error', { default: null })
|
|
46
|
+
|
|
47
|
+
// The value this field last wrote, so a different one from the parent (a reset, a loaded record) clears the error.
|
|
48
|
+
let written: string | null | undefined
|
|
49
|
+
watch(model, (v) => {
|
|
50
|
+
if (v !== written) error.value = null
|
|
51
|
+
written = undefined
|
|
52
|
+
})
|
|
53
|
+
function write(v: string | null) {
|
|
54
|
+
written = v
|
|
55
|
+
model.value = v
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// The text in the box is what's committed, whether or not the parent took every keystroke into the model.
|
|
59
|
+
function commit(input: HTMLInputElement) {
|
|
60
|
+
const { value, valid } = commitWebAddress(input.value, { addWww: props.addWww })
|
|
61
|
+
write(value)
|
|
62
|
+
input.value = value ?? ''
|
|
63
|
+
error.value = valid ? null : props.invalidMessage
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function onBlurCapture(e: FocusEvent) {
|
|
67
|
+
if (e.target instanceof HTMLInputElement) commit(e.target)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function onKeydown(e: KeyboardEvent) {
|
|
71
|
+
if (e.key === 'Enter' && !e.isComposing && e.target instanceof HTMLInputElement) commit(e.target)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const formField = inject(formFieldInjectionKey, undefined)
|
|
75
|
+
if (formField) {
|
|
76
|
+
provide(formFieldInjectionKey, computed(() => {
|
|
77
|
+
const f = formField.value
|
|
78
|
+
return error.value && f && !f.error ? { ...f, error: error.value } : f
|
|
79
|
+
}))
|
|
80
|
+
}
|
|
81
|
+
const messageId = useId()
|
|
82
|
+
// The hidden message's id, when one is needed: none while the UFormField shows an error of its own.
|
|
83
|
+
const describedById = computed(() => {
|
|
84
|
+
if (!error.value) return undefined
|
|
85
|
+
if (!formField?.value) return `${messageId}-error`
|
|
86
|
+
return formField.value.error ? undefined : `${formField.value.ariaId}-error`
|
|
87
|
+
})
|
|
88
|
+
const attrs = useAttrs()
|
|
89
|
+
const invalidAttrs = computed(() => {
|
|
90
|
+
if (!error.value || formField?.value) return {}
|
|
91
|
+
const describedBy = [attrs['aria-describedby'], describedById.value].filter(Boolean).join(' ')
|
|
92
|
+
return { 'color': 'error', 'highlight': true, 'aria-invalid': true, 'aria-describedby': describedBy }
|
|
93
|
+
})
|
|
94
|
+
</script>
|
|
95
|
+
|
|
96
|
+
<template>
|
|
97
|
+
<div class="contents" @blur.capture="onBlurCapture">
|
|
98
|
+
<UInput
|
|
99
|
+
:model-value="model ?? undefined"
|
|
100
|
+
type="text"
|
|
101
|
+
inputmode="url"
|
|
102
|
+
autocomplete="url"
|
|
103
|
+
:placeholder="placeholder"
|
|
104
|
+
v-bind="{ ...$attrs, ...invalidAttrs }"
|
|
105
|
+
@update:model-value="write($event == null ? null : String($event))"
|
|
106
|
+
@keydown="onKeydown"
|
|
107
|
+
/>
|
|
108
|
+
<span v-if="describedById" :id="describedById" class="sr-only">{{ error }}</span>
|
|
109
|
+
</div>
|
|
110
|
+
</template>
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// A year field: type a year, press a shortcut key, or pick from a calendar of years. The model is the year as a number
|
|
3
|
+
// (2026), or null. ZDateField for a year — the same box, the same commit on Enter, Tab and blur, the same invalid mark,
|
|
4
|
+
// `dateUpdate` and clear button (the field under both is internal/EntryField.vue) — with year-entry.ts doing the reading.
|
|
5
|
+
//
|
|
6
|
+
// <ZYearField v-model="form.year" class="w-28" />
|
|
7
|
+
// <ZYearField v-model="year" :min-value="2000" :max-value="2030" clearable />
|
|
8
|
+
//
|
|
9
|
+
// Typing: "2026", or "26" and "6" (a two-digit year as ZDateField reads one: 2026, 2006). Text that isn't a year
|
|
10
|
+
// clears the field when it commits. Shortcut keys: t this year, y or - the year before, r or + the year after (_ and =
|
|
11
|
+
// too). They step from the committed year (this year when there is none) and stop at `minValue` and `maxValue`; a
|
|
12
|
+
// typed year outside them is refused, as ZDateField refuses a day.
|
|
13
|
+
import { CalendarDate } from '@internationalized/date'
|
|
14
|
+
import { useLocale } from '@nuxt/ui/composables'
|
|
15
|
+
import type { Temporal } from 'temporal-polyfill/implementation'
|
|
16
|
+
import EntryField from './internal/EntryField.vue'
|
|
17
|
+
import type { EntryEngine, InputSlotUi } from './internal/entry-field'
|
|
18
|
+
import { dateModelTypeOf, type CalendarDateLike } from '../utils/date-entry'
|
|
19
|
+
import {
|
|
20
|
+
applyYearShortcut, checkYearLimits, commitYearEntry, formatYearEntry, isYearShortcut, parseYearEntry, yearLimitMessage,
|
|
21
|
+
type YearLimitProblem,
|
|
22
|
+
} from '../utils/year-entry'
|
|
23
|
+
|
|
24
|
+
defineOptions({ inheritAttrs: false })
|
|
25
|
+
|
|
26
|
+
const props = withDefaults(
|
|
27
|
+
defineProps<{
|
|
28
|
+
clearable?: boolean
|
|
29
|
+
disabled?: boolean
|
|
30
|
+
/** The calendar's locale (BCP 47). Unset: Nuxt UI's locale. */
|
|
31
|
+
locale?: string
|
|
32
|
+
/** Unset: "YYYY". */
|
|
33
|
+
placeholder?: string
|
|
34
|
+
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl'
|
|
35
|
+
/** A leading icon; a click on it emits `iconClick`. */
|
|
36
|
+
icon?: string
|
|
37
|
+
/** The calendar button's icon. */
|
|
38
|
+
calendarIcon?: string
|
|
39
|
+
highlight?: boolean
|
|
40
|
+
ui?: { root?: string, base?: string, trailing?: string, leading?: string, leadingIcon?: string }
|
|
41
|
+
portal?: boolean | string
|
|
42
|
+
/** The two buttons' accessible names, for a product in another language. */
|
|
43
|
+
clearLabel?: string
|
|
44
|
+
chooseLabel?: string
|
|
45
|
+
/** The first year the field takes. */
|
|
46
|
+
minValue?: number
|
|
47
|
+
/** The last year the field takes. */
|
|
48
|
+
maxValue?: number
|
|
49
|
+
}>(),
|
|
50
|
+
{ portal: true, calendarIcon: 'i-lucide-calendar', clearLabel: 'Clear year', chooseLabel: 'Choose a year' },
|
|
51
|
+
)
|
|
52
|
+
const emit = defineEmits<{
|
|
53
|
+
dateUpdate: [value: number | null]
|
|
54
|
+
// A typed year refused by the limits, and null once the field is no longer marked invalid.
|
|
55
|
+
invalid: [problem: (YearLimitProblem & { message: string }) | null]
|
|
56
|
+
clear: []
|
|
57
|
+
iconClick: [event: MouseEvent]
|
|
58
|
+
}>()
|
|
59
|
+
const model = defineModel<number | null>({ default: null })
|
|
60
|
+
defineSlots<{
|
|
61
|
+
/** UInput's leading slot, in place of the `icon` when given; `ui` is the input's, for its `leadingIcon` class. */
|
|
62
|
+
leading?(props: { ui: InputSlotUi }): unknown
|
|
63
|
+
}>()
|
|
64
|
+
|
|
65
|
+
const { locale: uiLocale } = useLocale()
|
|
66
|
+
const localeCode = computed(() => props.locale ?? uiLocale.value.code)
|
|
67
|
+
|
|
68
|
+
// A parent that hands in undefined (an unset form key) means no year.
|
|
69
|
+
const year = computed(() => model.value ?? null)
|
|
70
|
+
|
|
71
|
+
const engine = computed<EntryEngine<number>>(() => {
|
|
72
|
+
const within = { minValue: props.minValue, maxValue: props.maxValue }
|
|
73
|
+
return {
|
|
74
|
+
pattern: 'YYYY',
|
|
75
|
+
show: formatYearEntry,
|
|
76
|
+
parse: parseYearEntry,
|
|
77
|
+
commit: commitYearEntry,
|
|
78
|
+
same: (a, b) => a === b,
|
|
79
|
+
check: (y) => checkYearLimits(y, within),
|
|
80
|
+
message: (p) => yearLimitMessage(p as YearLimitProblem),
|
|
81
|
+
isShortcut: isYearShortcut,
|
|
82
|
+
shortcut: (key, current, today) => applyYearShortcut(key, current, today, within),
|
|
83
|
+
today: (today: Temporal.PlainDate) => today.year,
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
const fieldCmp = useTemplateRef<{ focus: () => void }>('fieldCmp')
|
|
88
|
+
|
|
89
|
+
// ── Calendar ──────────────────────────────────────────────────────────────────────────────────────────────
|
|
90
|
+
// UCalendar's year grid holds a CalendarDate: January 1 of the year, both ways.
|
|
91
|
+
const toCalendar = (y: number) => new CalendarDate(y, 1, 1)
|
|
92
|
+
const calendarMin = computed(() => (props.minValue != null ? toCalendar(props.minValue) : undefined))
|
|
93
|
+
const calendarMax = computed(() => (props.maxValue != null ? toCalendar(props.maxValue) : undefined))
|
|
94
|
+
|
|
95
|
+
function onPick(v: unknown, pick: (y: number) => void) {
|
|
96
|
+
if (dateModelTypeOf(v) === 'calendar-date') pick((v as CalendarDateLike).year)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
defineExpose({ focus: () => fieldCmp.value?.focus() })
|
|
100
|
+
</script>
|
|
101
|
+
|
|
102
|
+
<template>
|
|
103
|
+
<EntryField
|
|
104
|
+
ref="fieldCmp"
|
|
105
|
+
v-bind="$attrs"
|
|
106
|
+
:model-value="year"
|
|
107
|
+
:engine="engine"
|
|
108
|
+
:clearable="clearable"
|
|
109
|
+
:disabled="disabled"
|
|
110
|
+
:placeholder="placeholder"
|
|
111
|
+
:size="size"
|
|
112
|
+
:icon="icon"
|
|
113
|
+
:calendar-icon="calendarIcon"
|
|
114
|
+
:highlight="highlight"
|
|
115
|
+
:ui="ui"
|
|
116
|
+
:portal="portal"
|
|
117
|
+
:clear-label="clearLabel"
|
|
118
|
+
:choose-label="chooseLabel"
|
|
119
|
+
@update:model-value="(y) => (model = y)"
|
|
120
|
+
@date-update="(y) => emit('dateUpdate', y)"
|
|
121
|
+
@invalid="(p) => emit('invalid', p as (YearLimitProblem & { message: string }) | null)"
|
|
122
|
+
@clear="emit('clear')"
|
|
123
|
+
@icon-click="emit('iconClick', $event)"
|
|
124
|
+
>
|
|
125
|
+
<template v-if="$slots.leading" #leading="slotProps">
|
|
126
|
+
<slot name="leading" v-bind="slotProps" />
|
|
127
|
+
</template>
|
|
128
|
+
<template #calendar="{ value, view, pick }">
|
|
129
|
+
<UCalendar
|
|
130
|
+
type="year"
|
|
131
|
+
:model-value="value !== null ? toCalendar(value) : undefined"
|
|
132
|
+
:placeholder="toCalendar(view)"
|
|
133
|
+
:locale="localeCode"
|
|
134
|
+
:size="size"
|
|
135
|
+
:min-value="calendarMin"
|
|
136
|
+
:max-value="calendarMax"
|
|
137
|
+
prevent-deselect
|
|
138
|
+
class="p-2"
|
|
139
|
+
@update:model-value="(v) => onPick(v, pick)"
|
|
140
|
+
/>
|
|
141
|
+
</template>
|
|
142
|
+
</EntryField>
|
|
143
|
+
</template>
|
package/components/confirm.ts
CHANGED
|
@@ -78,9 +78,7 @@ export interface ChooseOptions<V> extends ConfirmBaseOptions {
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
/** What the queue holds: either kind of request, answered with `true`/`false` or an action's value/`null`. */
|
|
81
|
-
export type ConfirmRequest =
|
|
82
|
-
| { kind: 'confirm', options: ConfirmOptions }
|
|
83
|
-
| { kind: 'choose', options: ChooseOptions<unknown> }
|
|
81
|
+
export type ConfirmRequest = { kind: 'confirm', options: ConfirmOptions } | { kind: 'choose', options: ChooseOptions<unknown> }
|
|
84
82
|
|
|
85
83
|
export type ConfirmEntry = ConfirmQueueEntry<ConfirmRequest, unknown>
|
|
86
84
|
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
<script setup lang="ts" generic="V">
|
|
2
|
+
// The field under ZDateField, ZMonthField and ZYearField: a text box with a calendar button, a clear button and a popover,
|
|
3
|
+
// which reads typed text through an EntryEngine (entry-field.ts) and holds the value the engine reads. Internal: it is
|
|
4
|
+
// not registered as a Z* component (nuxt.config.ts ignores components/internal), and each field wraps it, turning the
|
|
5
|
+
// caller's type into V and back and putting its own UCalendar in the `calendar` slot.
|
|
6
|
+
//
|
|
7
|
+
// What it does is ZDateField's, documented there (DateField.vue, docs/date-field.md): typed text commits on Enter, Tab or
|
|
8
|
+
// leaving; text that names nothing clears the field; untouched text keeps the value; a value the limits refuse is kept
|
|
9
|
+
// out, the box redrawn, and the field marked invalid (aria-invalid, the error color, a message for screen readers, and
|
|
10
|
+
// inside a UFormField its error) until the next edit; a shortcut steps from the committed value; `dateUpdate` fires once
|
|
11
|
+
// per settle, with the value just settled.
|
|
12
|
+
import { formFieldInjectionKey } from '@nuxt/ui/composables'
|
|
13
|
+
import { useToday } from '../../composables/useToday'
|
|
14
|
+
import type { EntryEngine, EntryProblem, InputSlotUi } from './entry-field'
|
|
15
|
+
|
|
16
|
+
defineOptions({ inheritAttrs: false })
|
|
17
|
+
|
|
18
|
+
const props = withDefaults(
|
|
19
|
+
defineProps<{
|
|
20
|
+
modelValue: V | null
|
|
21
|
+
engine: EntryEngine<V>
|
|
22
|
+
clearable?: boolean
|
|
23
|
+
disabled?: boolean
|
|
24
|
+
placeholder?: string
|
|
25
|
+
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl'
|
|
26
|
+
icon?: string
|
|
27
|
+
calendarIcon: string
|
|
28
|
+
highlight?: boolean
|
|
29
|
+
ui?: { root?: string, base?: string, trailing?: string, leading?: string, leadingIcon?: string }
|
|
30
|
+
portal?: boolean | string
|
|
31
|
+
clearLabel: string
|
|
32
|
+
chooseLabel: string
|
|
33
|
+
}>(),
|
|
34
|
+
{ portal: true },
|
|
35
|
+
)
|
|
36
|
+
const emit = defineEmits<{
|
|
37
|
+
'update:modelValue': [value: V | null]
|
|
38
|
+
'dateUpdate': [value: V | null]
|
|
39
|
+
'invalid': [problem: (EntryProblem<V> & { message: string }) | null]
|
|
40
|
+
'clear': []
|
|
41
|
+
'iconClick': [event: MouseEvent]
|
|
42
|
+
}>()
|
|
43
|
+
defineSlots<{
|
|
44
|
+
leading?(props: { ui: InputSlotUi }): unknown
|
|
45
|
+
/** The picker: `value` the selection (following text typed but not yet committed), `view` the period to show,
|
|
46
|
+
* `pick` to settle on a value. */
|
|
47
|
+
calendar(props: { value: V | null, view: V, pick: (value: V) => void }): unknown
|
|
48
|
+
}>()
|
|
49
|
+
|
|
50
|
+
const eng = () => props.engine
|
|
51
|
+
const show = (v: V | null) => eng().show(v)
|
|
52
|
+
const same = (a: V | null, b: V | null) => eng().same(a, b)
|
|
53
|
+
|
|
54
|
+
const open = ref(false)
|
|
55
|
+
const text = ref(show(props.modelValue))
|
|
56
|
+
|
|
57
|
+
// ── Invalid ───────────────────────────────────────────────────────────────────────────────────────────────────
|
|
58
|
+
// Shallow: a ref would unwrap V's own type.
|
|
59
|
+
const problem = shallowRef<EntryProblem<V> | null>(null)
|
|
60
|
+
const problemMessage = computed(() => (problem.value ? eng().message(problem.value) : ''))
|
|
61
|
+
function flag(p: EntryProblem<V> | null) {
|
|
62
|
+
if (!p && !problem.value) return
|
|
63
|
+
problem.value = p
|
|
64
|
+
emit('invalid', p && { reason: p.reason, date: p.date, limit: p.limit, message: problemMessage.value })
|
|
65
|
+
}
|
|
66
|
+
// In a UFormField, Nuxt UI's UInput takes aria-invalid, its error color and aria-describedby from the field, over
|
|
67
|
+
// anything passed to it: so while a value is refused the field the UInput sees carries the message as its error (unless
|
|
68
|
+
// it has one of its own, which the product may have set from `invalid`), and the message below takes the id that
|
|
69
|
+
// aria-describedby names. Outside one, they are passed to the UInput directly.
|
|
70
|
+
const formField = inject(formFieldInjectionKey, undefined)
|
|
71
|
+
if (formField) {
|
|
72
|
+
provide(formFieldInjectionKey, computed(() => {
|
|
73
|
+
const f = formField.value
|
|
74
|
+
return problem.value && f && !f.error ? { ...f, error: problemMessage.value } : f
|
|
75
|
+
}))
|
|
76
|
+
}
|
|
77
|
+
const panelId = useId()
|
|
78
|
+
const messageId = computed(() => {
|
|
79
|
+
if (!problem.value) return undefined
|
|
80
|
+
if (!formField?.value) return `${panelId}-invalid`
|
|
81
|
+
return formField.value.error ? undefined : `${formField.value.ariaId}-error`
|
|
82
|
+
})
|
|
83
|
+
const attrs = useAttrs()
|
|
84
|
+
const invalidAttrs = computed(() => {
|
|
85
|
+
if (!problem.value || formField?.value) return {}
|
|
86
|
+
const describedBy = [attrs['aria-describedby'], messageId.value].filter(Boolean).join(' ')
|
|
87
|
+
return { 'color': 'error', 'aria-invalid': true, 'aria-describedby': describedBy }
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
// The value this field last settled on, which is what `dateUpdate` reports. NEVER re-read the model for that: when
|
|
91
|
+
// the parent owns the v-model, writing it only emits `update:modelValue`, and the prop comes back on the parent's
|
|
92
|
+
// next render — so in the same tick the model is still the OLD value, and a blur that just committed "5/12" would
|
|
93
|
+
// announce null.
|
|
94
|
+
let settled = props.modelValue
|
|
95
|
+
// The value `dateUpdate` last carried, boxed so an announced null is told apart from nothing announced yet.
|
|
96
|
+
let announced: { value: V | null } | undefined
|
|
97
|
+
watch(() => props.modelValue, (v) => {
|
|
98
|
+
// The parent set a different value, so whatever we announced before is no longer what's in the field.
|
|
99
|
+
if (!same(v, settled)) {
|
|
100
|
+
announced = undefined
|
|
101
|
+
flag(null)
|
|
102
|
+
}
|
|
103
|
+
settled = v
|
|
104
|
+
text.value = show(v)
|
|
105
|
+
})
|
|
106
|
+
// A new format redraws the value it holds.
|
|
107
|
+
watch(() => props.engine.pattern, () => { text.value = show(settled) })
|
|
108
|
+
|
|
109
|
+
const inputCmp = useTemplateRef<{ inputRef: HTMLInputElement | null, $el: HTMLElement }>('inputCmp')
|
|
110
|
+
|
|
111
|
+
function set(v: V | null) {
|
|
112
|
+
if (!same(v, settled)) emit('update:modelValue', v)
|
|
113
|
+
settled = v
|
|
114
|
+
text.value = show(v)
|
|
115
|
+
return v
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// What's typed becomes the value — or nothing, when it names nothing. Leaving "5/32" in the box would show one thing
|
|
119
|
+
// while the form saved another. Untouched text keeps the value as it is, so passing through the field can't blank a
|
|
120
|
+
// stored value it wouldn't read back (a year before 1000). A value the limits refuse keeps the one there was, drawn
|
|
121
|
+
// back in the box, and marks the field invalid: undefined, for nothing settled.
|
|
122
|
+
function commitText(): { value: V | null } | undefined {
|
|
123
|
+
const next = eng().commit(text.value, settled, useToday())
|
|
124
|
+
const refused = next !== null && !same(next, settled) ? eng().check(next) : null
|
|
125
|
+
if (refused) {
|
|
126
|
+
text.value = show(settled)
|
|
127
|
+
flag(refused)
|
|
128
|
+
return undefined
|
|
129
|
+
}
|
|
130
|
+
return { value: set(next) }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function onType(v: string | number | null | undefined) {
|
|
134
|
+
text.value = v == null ? '' : String(v)
|
|
135
|
+
flag(null)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function focusInput() {
|
|
139
|
+
nextTick(() => {
|
|
140
|
+
const el = inputCmp.value?.inputRef
|
|
141
|
+
if (!el) return
|
|
142
|
+
el.focus()
|
|
143
|
+
el.setSelectionRange(el.value.length, el.value.length)
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Focus is "still ours" on the input and anywhere in the calendar popover (which is teleported, so it isn't inside the
|
|
148
|
+
// input's element).
|
|
149
|
+
function isOurs(target: EventTarget | null) {
|
|
150
|
+
if (!(target instanceof Element)) return false
|
|
151
|
+
return !!inputCmp.value?.$el?.contains(target) || !!target.closest(`[data-date-field-panel="${panelId}"]`)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function announce(v: V | null) {
|
|
155
|
+
announced = { value: v }
|
|
156
|
+
emit('dateUpdate', v)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Leaving settles the field, but a value already announced (by Enter, say) isn't announced again: an inline editor
|
|
160
|
+
// saving on dateUpdate would save it twice.
|
|
161
|
+
function leave(e: FocusEvent) {
|
|
162
|
+
if (isOurs(e.relatedTarget)) return
|
|
163
|
+
if (announced && same(announced.value, settled)) return
|
|
164
|
+
announce(settled)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function onBlur(e: FocusEvent) {
|
|
168
|
+
commitText()
|
|
169
|
+
leave(e)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function onKeydown(e: KeyboardEvent) {
|
|
173
|
+
if (e.isComposing || e.ctrlKey || e.metaKey) return
|
|
174
|
+
if (e.altKey) {
|
|
175
|
+
if (e.key === 'ArrowDown') {
|
|
176
|
+
e.preventDefault()
|
|
177
|
+
open.value = true
|
|
178
|
+
}
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
if (e.key === 'Enter') {
|
|
182
|
+
open.value = false
|
|
183
|
+
const committed = commitText()
|
|
184
|
+
if (committed) announce(committed.value)
|
|
185
|
+
// Refused: the value it kept is selected, so typing another replaces it.
|
|
186
|
+
else nextTick(() => inputCmp.value?.inputRef?.select())
|
|
187
|
+
return
|
|
188
|
+
}
|
|
189
|
+
// Tab commits here, so the text is settled before focus moves; the blur that follows announces it, once.
|
|
190
|
+
if (e.key === 'Tab') {
|
|
191
|
+
commitText()
|
|
192
|
+
open.value = false
|
|
193
|
+
return
|
|
194
|
+
}
|
|
195
|
+
if (e.key.length !== 1 || !eng().isShortcut(e.key)) return
|
|
196
|
+
e.preventDefault()
|
|
197
|
+
// Shortcuts step from the committed value, not from text typed since: the typed text is dropped. With nowhere
|
|
198
|
+
// allowed to go, the key does nothing.
|
|
199
|
+
const next = eng().shortcut(e.key, settled, useToday())
|
|
200
|
+
if (next === null) return
|
|
201
|
+
flag(null)
|
|
202
|
+
announce(set(next))
|
|
203
|
+
// Selected, so typing a value replaces the shortcut's rather than appending to it.
|
|
204
|
+
nextTick(() => inputCmp.value?.inputRef?.select())
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function clear() {
|
|
208
|
+
const v = set(null)
|
|
209
|
+
flag(null)
|
|
210
|
+
emit('clear')
|
|
211
|
+
announce(v)
|
|
212
|
+
focusInput()
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ── Calendar ──────────────────────────────────────────────────────────────────────────────────────────────────
|
|
216
|
+
// The selection follows text typed but not yet committed, so the picker and the input never disagree; untouched or unreadable text falls back to the model. Only read while the popover is open, which is
|
|
217
|
+
// only ever in the browser.
|
|
218
|
+
const typed = () => eng().parse(text.value, useToday())
|
|
219
|
+
const calendarValue = computed(() => typed() ?? props.modelValue)
|
|
220
|
+
const calendarView = computed(() => typed() ?? props.modelValue ?? eng().today(useToday()))
|
|
221
|
+
|
|
222
|
+
function pick(v: V) {
|
|
223
|
+
open.value = false
|
|
224
|
+
flag(null)
|
|
225
|
+
announce(set(v))
|
|
226
|
+
focusInput()
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const popoverContent = computed(() => ({
|
|
230
|
+
side: 'bottom' as const,
|
|
231
|
+
align: 'start' as const,
|
|
232
|
+
'data-date-field-panel': panelId,
|
|
233
|
+
// The input keeps focus while the calendar is open, so typing and shortcuts still work.
|
|
234
|
+
onOpenAutoFocus: (e: Event) => e.preventDefault(),
|
|
235
|
+
onCloseAutoFocus: (e: Event) => e.preventDefault(),
|
|
236
|
+
// A click on our own input or icon isn't "outside"; the icon toggles the calendar itself.
|
|
237
|
+
onInteractOutside: (e: CustomEvent<{ originalEvent?: Event }>) => {
|
|
238
|
+
if (isOurs(e.detail?.originalEvent?.target ?? e.target)) e.preventDefault()
|
|
239
|
+
},
|
|
240
|
+
onFocusout: leave,
|
|
241
|
+
}))
|
|
242
|
+
|
|
243
|
+
function toggle() {
|
|
244
|
+
if (props.disabled) return
|
|
245
|
+
open.value = !open.value
|
|
246
|
+
focusInput()
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
defineExpose({ focus: focusInput })
|
|
250
|
+
</script>
|
|
251
|
+
|
|
252
|
+
<template>
|
|
253
|
+
<UPopover v-model:open="open" :content="popoverContent" :portal="portal">
|
|
254
|
+
<template #anchor>
|
|
255
|
+
<!-- The placeholder keeps the input's own ink, the product's: a product may darken it (a value's shape must not
|
|
256
|
+
read as a value someone typed), through `ui.base` or its app.config's input. While the calendar is open the
|
|
257
|
+
field takes Nuxt UI's highlight ring, so it reads as the one being edited. -->
|
|
258
|
+
<UInput
|
|
259
|
+
ref="inputCmp"
|
|
260
|
+
:model-value="text"
|
|
261
|
+
type="text"
|
|
262
|
+
autocomplete="off"
|
|
263
|
+
v-bind="{ ...$attrs, ...invalidAttrs }"
|
|
264
|
+
:size="size"
|
|
265
|
+
:icon="icon"
|
|
266
|
+
:disabled="disabled"
|
|
267
|
+
:placeholder="placeholder ?? engine.pattern"
|
|
268
|
+
:highlight="highlight || open || !!problem"
|
|
269
|
+
:ui="{ ...ui, root: `group ${ui?.root ?? ''}` }"
|
|
270
|
+
@update:model-value="onType"
|
|
271
|
+
@keydown="onKeydown"
|
|
272
|
+
@blur="onBlur"
|
|
273
|
+
>
|
|
274
|
+
<template v-if="$slots.leading" #leading="slotProps">
|
|
275
|
+
<slot name="leading" v-bind="slotProps" />
|
|
276
|
+
</template>
|
|
277
|
+
<template v-else-if="icon" #leading="{ ui: inputUi }">
|
|
278
|
+
<UIcon :name="icon" data-slot="leadingIcon" :class="inputUi.leadingIcon({ class: ui?.leadingIcon })" @click="emit('iconClick', $event)" />
|
|
279
|
+
</template>
|
|
280
|
+
<template #trailing="{ ui: inputUi }">
|
|
281
|
+
<!-- Why a typed value was refused, for screen readers: the input's aria-describedby names it. -->
|
|
282
|
+
<span v-if="messageId" :id="messageId" class="sr-only">{{ problemMessage }}</span>
|
|
283
|
+
<!-- Neither button takes focus (mousedown is prevented), so the input never blurs to them. Each icon is
|
|
284
|
+
`text-current`, over Nuxt UI's dimmed trailing icon, so the button's own color reaches it. -->
|
|
285
|
+
<button
|
|
286
|
+
v-if="clearable && modelValue !== null && !disabled"
|
|
287
|
+
type="button"
|
|
288
|
+
tabindex="-1"
|
|
289
|
+
:aria-label="clearLabel"
|
|
290
|
+
class="cursor-pointer items-center text-dimmed hover:text-primary"
|
|
291
|
+
:class="open ? 'flex' : 'hidden group-focus-within:flex'"
|
|
292
|
+
@mousedown.prevent
|
|
293
|
+
@click="clear"
|
|
294
|
+
>
|
|
295
|
+
<UIcon name="i-lucide-x" :class="inputUi.trailingIcon({ class: 'text-current' })" />
|
|
296
|
+
</button>
|
|
297
|
+
<button
|
|
298
|
+
type="button"
|
|
299
|
+
tabindex="-1"
|
|
300
|
+
:aria-label="chooseLabel"
|
|
301
|
+
:disabled="disabled"
|
|
302
|
+
class="flex cursor-pointer items-center hover:text-primary disabled:cursor-not-allowed"
|
|
303
|
+
:class="open ? 'text-primary' : 'text-dimmed group-focus-within:text-primary'"
|
|
304
|
+
@mousedown.prevent
|
|
305
|
+
@click="toggle"
|
|
306
|
+
>
|
|
307
|
+
<UIcon :name="calendarIcon" :class="inputUi.trailingIcon({ class: 'text-current' })" />
|
|
308
|
+
</button>
|
|
309
|
+
</template>
|
|
310
|
+
</UInput>
|
|
311
|
+
</template>
|
|
312
|
+
|
|
313
|
+
<template #content>
|
|
314
|
+
<slot name="calendar" :value="calendarValue" :view="calendarView" :pick="pick" />
|
|
315
|
+
</template>
|
|
316
|
+
</UPopover>
|
|
317
|
+
</template>
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// What EntryField.vue needs to know about the value it holds: how to write it, read it back from typed text, compare
|
|
2
|
+
// it, check it against the limits and move it with a shortcut key. ZDateField gives it a PlainDate's (date-entry.ts),
|
|
3
|
+
// ZMonthField a PlainYearMonth's (month-entry.ts), ZYearField a year number's (year-entry.ts). Everything else — the
|
|
4
|
+
// box, the commit on Enter, Tab and blur, the invalid mark, `dateUpdate`, the clear button and the popover — is the
|
|
5
|
+
// field's, the same for all three.
|
|
6
|
+
import type { Temporal } from 'temporal-polyfill/implementation'
|
|
7
|
+
|
|
8
|
+
/** A value the limits refuse: why, the value, and the limit it crossed (null when it isn't a limit, as for a day
|
|
9
|
+
* that is unavailable). */
|
|
10
|
+
export interface EntryProblem<V> {
|
|
11
|
+
readonly reason: string
|
|
12
|
+
readonly date: V
|
|
13
|
+
readonly limit: V | null
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface EntryEngine<V> {
|
|
17
|
+
/** The pattern the box writes and reads, `MM/DD/YYYY`; the placeholder unless one is given. A change redraws the box. */
|
|
18
|
+
readonly pattern: string
|
|
19
|
+
/** The value as the box writes it; empty for none. */
|
|
20
|
+
show(value: V | null): string
|
|
21
|
+
/** What typed text names, or null. */
|
|
22
|
+
parse(text: string, today: Temporal.PlainDate): V | null
|
|
23
|
+
/** The value once the text is committed: `current` while the text is untouched, else what it names, or null. */
|
|
24
|
+
commit(text: string, current: V | null, today: Temporal.PlainDate): V | null
|
|
25
|
+
same(a: V | null, b: V | null): boolean
|
|
26
|
+
/** Why the limits refuse the value, or null. */
|
|
27
|
+
check(value: V): EntryProblem<V> | null
|
|
28
|
+
/** An English sentence for a refusal. */
|
|
29
|
+
message(problem: EntryProblem<V>): string
|
|
30
|
+
/** Whether the key is a shortcut, taken even when it has nowhere to go. */
|
|
31
|
+
isShortcut(key: string): boolean
|
|
32
|
+
/** Where a shortcut key moves `current` (today's period when null), or null when it has nowhere to go. */
|
|
33
|
+
shortcut(key: string, current: V | null, today: Temporal.PlainDate): V | null
|
|
34
|
+
/** Today's period, which the calendar shows when the field is empty. */
|
|
35
|
+
today(today: Temporal.PlainDate): V
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type SlotClass = (options?: { class?: unknown }) => string
|
|
39
|
+
/** The `ui` UInput hands its `#leading` slot: a class function per slot of the input. `leadingIcon` and `trailingIcon`
|
|
40
|
+
* are named, so a slot can call them without checking for undefined. */
|
|
41
|
+
export type InputSlotUi = { leadingIcon: SlotClass, trailingIcon: SlotClass } & Record<string, SlotClass>
|
package/components/recipes.ts
CHANGED
|
@@ -66,6 +66,19 @@ export const status = {
|
|
|
66
66
|
/** `glow`: a soft light around the healthy dot, in its own color. Only healthy lights up — a glow
|
|
67
67
|
* says "live", and the other states are the ones that are not. */
|
|
68
68
|
glow: 'shadow-[0_0_12px_var(--ui-success)]',
|
|
69
|
+
/** A product's own status: its label in the default ink, its icon (or a dot) in its color. */
|
|
70
|
+
custom: 'text-default',
|
|
71
|
+
icon: 'flex-none size-3.5',
|
|
72
|
+
dot: 'w-2.5 h-2.5 rounded-full',
|
|
73
|
+
/** Icon inks: graphics, so Nuxt UI's own color shades (3:1), not the 700/300 text inks. */
|
|
74
|
+
ink: {
|
|
75
|
+
primary: 'text-primary', secondary: 'text-secondary', success: 'text-success', info: 'text-info',
|
|
76
|
+
warning: 'text-warning', error: 'text-error', neutral: 'text-muted',
|
|
77
|
+
},
|
|
78
|
+
fill: {
|
|
79
|
+
primary: 'bg-primary', secondary: 'bg-secondary', success: 'bg-success', info: 'bg-info',
|
|
80
|
+
warning: 'bg-warning', error: 'bg-error', neutral: 'bg-(--ui-color-neutral-500)',
|
|
81
|
+
},
|
|
69
82
|
} as const
|
|
70
83
|
|
|
71
84
|
export const meter = {
|
|
@@ -92,9 +105,24 @@ export const meter = {
|
|
|
92
105
|
|
|
93
106
|
/** Zoo UI's code surface: near-black in BOTH themes — the one surface that does not follow the page. */
|
|
94
107
|
export const code = {
|
|
95
|
-
wrap: 'relative
|
|
108
|
+
wrap: 'relative py-3 pr-3 pl-4 bg-(--ui-color-neutral-900) dark:bg-(--ui-color-neutral-950) text-(--ui-color-neutral-100) border border-(--ui-color-neutral-700) rounded-md font-mono font-normal text-sm leading-[1.6] [container-type:inline-size]',
|
|
109
|
+
/** line (and a block of one line): prompt, code, lang and copy share one row. block (multi-line code): the code
|
|
110
|
+
* takes the whole width and lang and copy sit over its top-right corner, so a tall block gives up no column. */
|
|
111
|
+
layout: {
|
|
112
|
+
line: 'flex items-start gap-3',
|
|
113
|
+
block: 'block',
|
|
114
|
+
},
|
|
96
115
|
prompt: 'text-(--ui-color-primary-300) select-none flex-none',
|
|
97
|
-
pre:
|
|
116
|
+
pre: {
|
|
117
|
+
line: 'm-0 flex-1 min-w-0 overflow-x-auto [font:inherit]',
|
|
118
|
+
/* pt-6 drops the first line below the toolbar (top-2 + h-6 ends 8px under the wrap's own py-3) */
|
|
119
|
+
block: 'm-0 pt-6 overflow-x-auto [font:inherit]',
|
|
120
|
+
},
|
|
121
|
+
/** lang and copy: flex items of the row (display: contents), or the corner toolbar of multi-line code */
|
|
122
|
+
bar: {
|
|
123
|
+
line: 'contents',
|
|
124
|
+
block: 'absolute top-2 right-2 flex items-center gap-3',
|
|
125
|
+
},
|
|
98
126
|
lang: 'flex-none order-2 self-center font-mono font-medium text-xs leading-[1.2] tracking-[0.08em] uppercase text-(--ui-color-neutral-400)',
|
|
99
127
|
copy: `appearance-none flex-none order-3 h-6 px-2 inline-flex items-center gap-1.5 bg-transparent border border-(--ui-color-neutral-700) rounded-sm text-(--ui-color-neutral-400) font-sans font-medium text-xs leading-[1.2] cursor-pointer hover:text-(--ui-color-neutral-100) hover:border-(--ui-color-neutral-400) data-[done=true]:text-(--ui-color-primary-300) data-[done=true]:border-(--ui-color-primary-300) ${focus}`,
|
|
100
128
|
/** the secret: one click selects all of it, the fallback when the clipboard refuses */
|