@zooui/nuxt 0.2.0 → 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 CHANGED
@@ -61,15 +61,27 @@ plain Nuxt UI. Three rules:
61
61
  | `ZEcosystemMark` | A package manager as a two-letter tile |
62
62
  | `ZThemeToggle` | Light / dark / system |
63
63
  | `ZRepeatingList` | A generic list editor |
64
+ | `ZOrderList` | An ordered list with one selected row and a move toolbar, for master-detail editors |
64
65
  | `ZNumberInput` | One number input for money, percent, count and hours, with a calc mode |
65
66
  | `ZDateField` | A calendar-day field on `Temporal.PlainDate` or `CalendarDate`, with Nuxt UI's limit props |
67
+ | `ZFileDropOverlay` | Files dropped anywhere in the window, or contained in a dialog or region; nested overlays defer to the innermost |
68
+ | `ZFileList` | A form's files, stored and newly added, with an async remove, thumbnails and row slots |
69
+ | `ZConfirmDialog`, `ZConfirmButton` | `useConfirm()`'s promise-based questions, and a button that asks in a popover |
70
+ | `ZOptionalDate` | A checkbox in front of a `ZDateField`: the date can be edited only while it's ticked |
71
+ | `ZSelectMenu` | Nuxt UI's `USelectMenu` with typeahead from the top of the list, Tab that moves on, a focus ring on any focus, and optional open on focus |
72
+ | `ZFilterSelect` | A faceted multi-select filter for list and table toolbars: search, counts, All and Clear |
66
73
 
67
74
  ## Docs
68
75
 
69
76
  - [Components](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/components.md): every prop, the select-none helpers and the nav marker
70
77
  - [ZRepeatingList](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/repeating-list.md),
78
+ [ZOrderList](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/order-list.md),
71
79
  [ZNumberInput](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/number-input.md),
72
- [ZDateField](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/date-field.md)
80
+ [ZDateField](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/date-field.md),
81
+ [useConfirm and ZConfirmButton](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/confirm.md),
82
+ [ZOptionalDate](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/optional-date.md),
83
+ [ZSelectMenu](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/select-menu.md),
84
+ [ZFilterSelect](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/filter-select.md)
73
85
  - [The base and a theme](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/base-and-theme.md): what each layer carries, and why they're two layers
74
86
  - [Accents and colored edges](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/accents-and-edges.md): `data-accent` sections and the `color` edge
75
87
  - [Mechanisms](https://github.com/zooui-dev/zooui/blob/main/packages/nuxt/docs/mechanisms.md): `light-dark()`, the hydration label fix, icon bundling, shipping as source
@@ -0,0 +1,124 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * A button that asks before it acts: a click opens "Are you sure?" in a UPopover beside it, and `confirm` is emitted
4
+ * only when that is answered yes. No, Escape and a click outside close it and emit `cancel`.
5
+ *
6
+ * <ZConfirmButton label="Remove" icon="i-lucide-trash-2" color="error" variant="ghost" @confirm="remove(row)" />
7
+ *
8
+ * Every UButton prop, attribute and slot passes straight through to the button; only the question's own words and
9
+ * colors are props here. Focus goes to No when the question opens, so Enter doesn't confirm by accident, and back to
10
+ * the button when it closes. The popover is a `dialog` named by the button, as reka names it, and described by the
11
+ * question and its description. For a question that needs more than a line, use useConfirm() and <ZConfirmDialog>.
12
+ *
13
+ * `confirm-enabled` false skips the question: it is then the plain UButton, and a click emits `confirm` at once, for
14
+ * a caller that asks only sometimes (a row with nothing in it, say).
15
+ */
16
+ import type { PopoverProps } from '@nuxt/ui'
17
+ import { computed, useId, watch } from 'vue'
18
+ import { CONFIRM_ERROR_ICON, type ConfirmColor, type ConfirmVariant } from './confirm'
19
+
20
+ defineOptions({ inheritAttrs: false })
21
+
22
+ const props = withDefaults(defineProps<{
23
+ /** The question the popover asks. */
24
+ question?: string
25
+ /** A line under the question saying what happens. */
26
+ description?: string
27
+ /** The confirming button's label. */
28
+ confirmLabel?: string
29
+ /** The declining button's label. */
30
+ cancelLabel?: string
31
+ /** The confirming button's color. */
32
+ confirmColor?: ConfirmColor
33
+ /** The confirming button's variant. */
34
+ confirmVariant?: ConfirmVariant
35
+ /** An icon before the question; `false` for none. */
36
+ questionIcon?: string | false
37
+ /** Whether to ask at all. False: a plain button whose click emits `confirm` at once. */
38
+ confirmEnabled?: boolean
39
+ /** Where the popover opens: UPopover's `content` (side, align, offsets). */
40
+ content?: PopoverProps['content']
41
+ /** UPopover's arrow pointing at the button: its width and height, or `false` for none. */
42
+ arrow?: PopoverProps['arrow']
43
+ }>(), {
44
+ question: 'Are you sure?',
45
+ description: undefined,
46
+ confirmLabel: 'Yes',
47
+ cancelLabel: 'No',
48
+ confirmColor: 'error',
49
+ confirmVariant: 'outline',
50
+ questionIcon: CONFIRM_ERROR_ICON,
51
+ confirmEnabled: true,
52
+ content: () => ({ side: 'bottom', align: 'start' }),
53
+ arrow: () => ({ width: 16, height: 8 }),
54
+ })
55
+
56
+ const emit = defineEmits<{
57
+ /** The question was answered yes. */
58
+ confirm: []
59
+ /** The question was answered no, or dismissed with Escape or a click outside. */
60
+ cancel: []
61
+ }>()
62
+
63
+ /** Whether the question is showing; `v-model:open` to open or close it from outside. */
64
+ const open = defineModel<boolean>('open', { default: false })
65
+
66
+ const questionId = useId()
67
+ const descriptionId = useId()
68
+
69
+ // `cancel` for every way the popover closes except yes: No, Escape, a click outside, `open` set false from outside
70
+ let confirmed = false
71
+ watch(open, (now) => {
72
+ if (now) confirmed = false
73
+ else if (!confirmed) emit('cancel')
74
+ })
75
+
76
+ // asking turned off while the question is open: close it (a cancel), so it doesn't reappear when asking comes back
77
+ watch(() => props.confirmEnabled, (on) => { if (!on) open.value = false })
78
+
79
+ function confirm() {
80
+ if (!open.value) return
81
+ confirmed = true
82
+ open.value = false
83
+ emit('confirm')
84
+ }
85
+
86
+ // reka names the popover after its trigger ("Remove row"), so the question and its line are its description
87
+ const contentProps = computed(() => ({
88
+ ...props.content,
89
+ 'aria-describedby': props.description ? `${questionId} ${descriptionId}` : questionId,
90
+ }))
91
+ </script>
92
+
93
+ <template>
94
+ <UButton v-if="!confirmEnabled" v-bind="$attrs" @click="emit('confirm')">
95
+ <template v-for="(_, name) in $slots" #[name]="scope">
96
+ <slot :name="name" v-bind="scope ?? {}" />
97
+ </template>
98
+ </UButton>
99
+ <UPopover v-else v-model:open="open" :content="contentProps" :arrow="arrow">
100
+ <UButton v-bind="$attrs">
101
+ <template v-for="(_, name) in $slots" #[name]="scope">
102
+ <slot :name="name" v-bind="scope ?? {}" />
103
+ </template>
104
+ </UButton>
105
+
106
+ <template #content>
107
+ <div class="flex flex-col gap-4 p-3" data-confirm-popover>
108
+ <div class="flex items-center gap-4">
109
+ <span v-if="questionIcon" class="flex shrink-0" aria-hidden="true" :data-confirm-question-icon="questionIcon">
110
+ <UIcon :name="questionIcon" class="size-6" />
111
+ </span>
112
+ <div class="flex flex-col gap-2">
113
+ <p :id="questionId" class="text-sm font-bold text-highlighted" data-confirm-question>{{ question }}</p>
114
+ <p v-if="description" :id="descriptionId" class="text-xs" data-confirm-description>{{ description }}</p>
115
+ </div>
116
+ </div>
117
+ <div class="flex items-center justify-end gap-3">
118
+ <UButton size="xs" color="neutral" variant="outline" :label="cancelLabel" data-confirm-cancel @click="open = false" />
119
+ <UButton size="xs" :color="confirmColor" :variant="confirmVariant" :label="confirmLabel" data-confirm-action @click="confirm" />
120
+ </div>
121
+ </div>
122
+ </template>
123
+ </UPopover>
124
+ </template>
@@ -0,0 +1,201 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * The one place useConfirm()'s questions render. Mount it once, inside <UApp> beside the page:
4
+ *
5
+ * <UApp><NuxtPage /><ZConfirmDialog /></UApp>
6
+ *
7
+ * A ZModal announced as an `alertdialog`, titled by the question and described by its description. The description
8
+ * sits in the body with the icon's badge beside it, and the details beneath it. A question with no description shows
9
+ * its icon in a smaller badge beside the title instead, and any details stay in the body. Each question is
10
+ * shown in turn: a call made while one is open waits until it is answered (utils/confirm-queue.ts). Escape, the close
11
+ * button and a click outside are a cancel: `confirm` resolves `false`, `choose` `null`. A question asked with
12
+ * `dismissible: false` ignores Escape and a click outside; Cancel and the close button still cancel it.
13
+ *
14
+ * FOCUS starts on Cancel when the question is destructive (`destructive`, or `color` or `confirmColor` `error`), so
15
+ * Enter can't do the harm; otherwise on the last action, the confirming one. The dialog closes, and focus goes back where it was, before
16
+ * the next question opens.
17
+ */
18
+ import { computed, onBeforeUnmount, ref, shallowRef, useId, watch } from 'vue'
19
+ import { EDGE_COLORS, type EdgeColor } from './edge'
20
+ import {
21
+ confirmController,
22
+ confirmDetailParts,
23
+ confirmIcon,
24
+ isDestructive,
25
+ type ConfirmAction,
26
+ type ConfirmColor,
27
+ type ConfirmEntry,
28
+ } from './confirm'
29
+
30
+ const props = withDefaults(defineProps<{
31
+ /** The dismissing button's label, where a question doesn't give its own. */
32
+ cancelLabel?: string
33
+ /** The confirming button's label for `confirm()`, where a question doesn't give its own. */
34
+ confirmLabel?: string
35
+ }>(), { cancelLabel: 'Cancel', confirmLabel: 'Confirm' })
36
+
37
+ const controller = confirmController(useNuxtApp())
38
+ // counted for useConfirm()'s "no host" warning; in setup, so a page asking from its own onMounted is already counted
39
+ if (import.meta.client) {
40
+ controller.hosts++
41
+ onBeforeUnmount(() => { controller.hosts-- })
42
+ }
43
+
44
+ /** What the modal shows: kept through its leave animation, so the next question opens only once this one has gone. */
45
+ const shown = shallowRef<ConfirmEntry>()
46
+ const open = ref(false)
47
+
48
+ function show(entry: ConfirmEntry | undefined) {
49
+ if (!entry || shown.value) return
50
+ shown.value = entry
51
+ open.value = true
52
+ }
53
+ watch(controller.current, (entry) => {
54
+ // settled from outside the dialog (dismissAll): close it, and afterLeave shows whatever is next
55
+ if (open.value && shown.value && entry?.id !== shown.value.id) open.value = false
56
+ show(entry)
57
+ }, { immediate: true })
58
+
59
+ function afterLeave() {
60
+ shown.value = undefined
61
+ show(controller.current.value)
62
+ }
63
+
64
+ /** Answer the shown question, once: a second click on a closing dialog is ignored here and by the queue. */
65
+ function settle(answer: (id: number) => void) {
66
+ const entry = shown.value
67
+ if (!entry || !open.value) return
68
+ open.value = false
69
+ answer(entry.id)
70
+ }
71
+ const dismiss = () => settle((id) => controller.queue.dismiss(id))
72
+ const pick = (value: unknown) => settle((id) => controller.queue.answer(id, value))
73
+
74
+ const options = computed(() => shown.value?.request.options)
75
+ const destructive = computed(() => !!options.value && isDestructive(options.value))
76
+
77
+ const actions = computed<ConfirmAction<unknown>[]>(() => {
78
+ const request = shown.value?.request
79
+ if (!request) return []
80
+ const color = request.options.color
81
+ if (request.kind === 'confirm') {
82
+ const { confirmLabel, confirmColor, confirmVariant } = request.options
83
+ return [{ label: confirmLabel ?? props.confirmLabel, value: true, color: confirmColor ?? color ?? 'primary', variant: confirmVariant }]
84
+ }
85
+ return request.options.actions.map((action) => ({ ...action, color: action.color ?? color ?? 'primary' }))
86
+ })
87
+
88
+ const isEdgeColor = (c: ConfirmColor | undefined): c is EdgeColor => (EDGE_COLORS as readonly string[]).includes(c ?? '')
89
+ const edgeColor = computed(() => (isEdgeColor(options.value?.color) ? options.value.color : undefined))
90
+
91
+ /** The icon's badge, on Nuxt UI's tokens; written out in full so Tailwind's scan finds them. */
92
+ const BADGE: Record<string, string> = {
93
+ primary: 'bg-primary/10 text-primary',
94
+ secondary: 'bg-secondary/10 text-secondary',
95
+ success: 'bg-success/10 text-success',
96
+ info: 'bg-info/10 text-info',
97
+ warning: 'bg-warning/10 text-warning',
98
+ error: 'bg-error/10 text-error',
99
+ neutral: 'bg-elevated text-highlighted',
100
+ }
101
+ const badge = computed(() => BADGE[options.value?.color ?? 'neutral'] ?? BADGE.neutral)
102
+ const icon = computed(() => options.value && confirmIcon(options.value))
103
+ /** With no description to sit beside, the icon goes beside the title. */
104
+ const titleIcon = computed(() => (options.value?.description ? undefined : icon.value))
105
+ const details = computed(() => options.value?.details?.map(confirmDetailParts) ?? [])
106
+
107
+ /** Into the dialog: Cancel for a destructive question, else the last (confirming) action. */
108
+ function placeFocus(event: Event) {
109
+ const root = event.currentTarget instanceof Element ? event.currentTarget : document
110
+ const target = root.querySelector<HTMLElement>('[data-confirm-autofocus]')
111
+ if (!target) return
112
+ event.preventDefault()
113
+ target.focus()
114
+ }
115
+
116
+ // the description is in the body, not UModal's header, so the dialog is pointed at it by id: still its description
117
+ const descriptionId = useId()
118
+ const content = computed(() => ({
119
+ 'role': 'alertdialog',
120
+ 'data-confirm-dialog': '',
121
+ 'aria-describedby': options.value?.description ? descriptionId : undefined,
122
+ 'onOpenAutoFocus': placeFocus,
123
+ }))
124
+
125
+ /** The dialog's width for each `size`. */
126
+ const WIDTH = { sm: 'sm:max-w-md', md: 'sm:max-w-xl', lg: 'sm:max-w-3xl' } as const
127
+ const ui = computed(() => ({ content: WIDTH[options.value?.size ?? 'sm'] ?? WIDTH.sm, footer: 'justify-between gap-2' }))
128
+ </script>
129
+
130
+ <template>
131
+ <ZModal
132
+ :open="open"
133
+ :title="options?.title"
134
+ :color="edgeColor"
135
+ :dismissible="options?.dismissible !== false"
136
+ :content="content"
137
+ :ui="ui"
138
+ @update:open="(value: boolean) => { if (!value) dismiss() }"
139
+ @after:leave="afterLeave"
140
+ >
141
+ <!-- No description: a 32px badge beside the title. The title stays the dialog's name; the badge is aria-hidden. -->
142
+ <template v-if="titleIcon" #title>
143
+ <span class="flex items-center gap-3">
144
+ <span class="grid size-8 shrink-0 place-items-center rounded-full" :class="badge" :data-confirm-icon="titleIcon" aria-hidden="true">
145
+ <UIcon :name="titleIcon" class="size-4" />
146
+ </span>
147
+ <span>{{ options?.title }}</span>
148
+ </span>
149
+ </template>
150
+
151
+ <!-- The badge beside the description, and the details beneath, indented to the description's column
152
+ (40px badge + 12px gap). With no description the badge is beside the title, and the details are indented to
153
+ the title's column (32px badge + 12px gap). -->
154
+ <template v-if="options?.description || details.length" #body>
155
+ <div class="flex flex-col gap-3">
156
+ <div v-if="options?.description" class="flex items-start gap-3">
157
+ <span v-if="icon" class="grid size-10 shrink-0 place-items-center rounded-full" :class="badge" :data-confirm-icon="icon">
158
+ <UIcon :name="icon" class="size-5" aria-hidden="true" />
159
+ </span>
160
+ <p :id="descriptionId" class="text-sm text-toned" :class="{ 'pt-2': icon }" data-confirm-description>
161
+ {{ options.description }}
162
+ </p>
163
+ </div>
164
+ <!-- toned, not muted: muted on the elevated ground falls under 4.5:1 on some Forest schemes in light mode -->
165
+ <ul v-if="details.length" class="flex flex-col gap-1.5 text-sm text-toned" :class="titleIcon ? 'pl-11' : { 'pl-13': icon }" data-confirm-details>
166
+ <li v-for="(detail, i) in details" :key="i" class="rounded-md bg-elevated px-3 py-2">
167
+ <template v-if="detail.title">
168
+ <span class="block font-medium text-highlighted" data-confirm-detail-title>{{ detail.title }}</span>
169
+ <span v-if="detail.text" class="block whitespace-pre-line text-toned" data-confirm-detail-text>{{ detail.text }}</span>
170
+ </template>
171
+ <template v-else>{{ detail.text }}</template>
172
+ </li>
173
+ </ul>
174
+ </div>
175
+ </template>
176
+
177
+ <template #footer>
178
+ <UButton
179
+ :label="options?.cancelLabel ?? cancelLabel"
180
+ color="neutral"
181
+ variant="outline"
182
+ data-confirm-cancel
183
+ :data-confirm-autofocus="destructive ? '' : undefined"
184
+ @click="dismiss"
185
+ />
186
+ <div class="flex flex-wrap justify-end gap-2" data-confirm-actions>
187
+ <UButton
188
+ v-for="(action, i) in actions"
189
+ :key="i"
190
+ :label="action.label"
191
+ :icon="action.icon"
192
+ :color="action.color"
193
+ :variant="action.variant ?? 'solid'"
194
+ data-confirm-action
195
+ :data-confirm-autofocus="!destructive && i === actions.length - 1 ? '' : undefined"
196
+ @click="pick(action.value)"
197
+ />
198
+ </div>
199
+ </template>
200
+ </ZModal>
201
+ </template>
@@ -23,11 +23,12 @@
23
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
24
24
  // (dateEntryFormatFor), so `en` gives MM/DD/YYYY. The placeholder is the pattern.
25
25
  //
26
- // Typing (in MM/DD/YYYY): "5" or "12" is that day this month, "0512" is May 12, "051226" and "5/12/26" and
26
+ // Typing (in MM/DD/YYYY): "5" or "12" is that day this month, "0512" is May 12, "051226", "05122026", "5/12/26" and
27
27
  // "5/12/2026" carry the year; other formats read the same shapes in their own order. Enter, Tab or leaving the
28
28
  // field commits; text that isn't a date clears the field. Shortcut keys (see applyDateShortcut): t today, m/h
29
29
  // start/end of month, y/r start/end of year, + and - a day either way (_ too, so a format whose separator is "-"
30
- // 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.
31
32
  //
32
33
  // LIMITS. `minValue`, `maxValue` and `isDateUnavailable`, named as Nuxt UI's UCalendar names them, go to the calendar,
33
34
  // which disables those days. A typed date they refuse is not committed: the field keeps the date it had and draws it
@@ -43,8 +44,8 @@
43
44
  // the parent's v-model hasn't caught up yet, and fires once per settle: leaving right after Enter, a pick, a
44
45
  // shortcut or a clear doesn't announce the same date again. Plain forms can ignore it and read v-model.
45
46
  //
46
- // Also: a leading `icon` whose click emits `iconClick`, a `calendarIcon` in place of the calendar,
47
- // and one-letter weekday headers (the locale's narrow weekday).
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).
48
49
  import { CalendarDate } from '@internationalized/date'
49
50
  import { formFieldInjectionKey, useLocale } from '@nuxt/ui/composables'
50
51
  import type { Temporal } from 'temporal-polyfill/implementation'
@@ -105,6 +106,10 @@ const emit = defineEmits<{
105
106
  iconClick: [event: MouseEvent]
106
107
  }>()
107
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
+ }>()
108
113
 
109
114
  const { locale: uiLocale } = useLocale()
110
115
  const localeCode = computed(() => props.locale ?? uiLocale.value.code)
@@ -281,9 +286,9 @@ function onKeydown(e: KeyboardEvent) {
281
286
  if (e.key.length !== 1 || !isDateShortcut(e.key, entryFormat.value)) return
282
287
  e.preventDefault()
283
288
  const today = useToday()
284
- // Shortcuts step from what's on screen, even if it hasn't been committed yet. With no allowed day to go to, the
285
- // key does nothing.
286
- const next = applyDateShortcut(e.key, parseDateEntry(text.value, today, entryFormat.value) ?? settled, today, entryFormat.value, limits.value)
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)
287
292
  if (!next) return
288
293
  flag(null)
289
294
  announce(set(next))
@@ -382,7 +387,10 @@ defineExpose({ focus: focusInput })
382
387
  @keydown="onKeydown"
383
388
  @blur="onBlur"
384
389
  >
385
- <template v-if="icon" #leading="{ ui: inputUi }">
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 }">
386
394
  <UIcon :name="icon" data-slot="leadingIcon" :class="inputUi.leadingIcon({ class: ui?.leadingIcon })" @click="emit('iconClick', $event)" />
387
395
  </template>
388
396
  <template #trailing="{ ui: inputUi }">