@zooui/nuxt 0.2.0 → 0.3.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 +13 -1
- package/components/ConfirmButton.vue +124 -0
- package/components/ConfirmDialog.vue +201 -0
- package/components/DateField.vue +16 -8
- 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 +9 -2
- package/utils/file-drop.ts +151 -0
- package/utils/filter-match.ts +62 -0
- package/utils/order-list.ts +42 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What useConfirm() (composables/useConfirm.ts) and <ZConfirmDialog> share: the request types, the presets, and the
|
|
3
|
+
* queue each Nuxt app keeps. Here rather than in composables/ so the icons the presets name are checked against
|
|
4
|
+
* Z_ICONS (scripts/check-icons.test.mjs) with the rest of the components' icons.
|
|
5
|
+
*
|
|
6
|
+
* ONE QUEUE PER NUXT APP, never one per module. A module-level queue would be shared by every request a server
|
|
7
|
+
* renders, so a dialog asked for while rendering one person's page could open in another's. The queue is kept in a
|
|
8
|
+
* WeakMap keyed by the app, which Nuxt creates once in the browser and once per request on the server, and goes
|
|
9
|
+
* with it.
|
|
10
|
+
*/
|
|
11
|
+
import type { ButtonProps } from '@nuxt/ui'
|
|
12
|
+
import { shallowRef, type ShallowRef } from 'vue'
|
|
13
|
+
import { createConfirmQueue, type ConfirmQueue, type ConfirmQueueEntry } from '../utils/confirm-queue'
|
|
14
|
+
|
|
15
|
+
/** A color Nuxt UI's buttons take: its seven, and any the product adds in `ui.colors`. (Nuxt UI types it as a key,
|
|
16
|
+
* which takes in `number | symbol`; a color is only ever a name.) */
|
|
17
|
+
export type ConfirmColor = Extract<NonNullable<ButtonProps['color']>, string>
|
|
18
|
+
export type ConfirmVariant = NonNullable<ButtonProps['variant']>
|
|
19
|
+
|
|
20
|
+
/** What every dialog says. */
|
|
21
|
+
export interface ConfirmBaseOptions {
|
|
22
|
+
/** The question, as the dialog's title: "Publish the report?" */
|
|
23
|
+
title: string
|
|
24
|
+
/** A sentence under the title saying what happens. */
|
|
25
|
+
description?: string
|
|
26
|
+
/** Lines listed under the description: the records affected, say. A line with a `\n` in it, or a `{ title, text }`,
|
|
27
|
+
* is two-part: a heading, and the text beneath it. */
|
|
28
|
+
details?: ConfirmDetail[]
|
|
29
|
+
/** An icon in `color`, beside the description; with no description, a smaller one beside the title.
|
|
30
|
+
* Left out, `error` and `warning` bring their own (CONFIRM_COLOR_ICONS); `false` shows none.
|
|
31
|
+
* `confirmDelete` and `confirmDiscard` set their own. */
|
|
32
|
+
icon?: string | false
|
|
33
|
+
/** The dialog's color: its edge, its icon and, unless `confirmColor` says otherwise, its confirming button.
|
|
34
|
+
* `error` makes it destructive. */
|
|
35
|
+
color?: ConfirmColor
|
|
36
|
+
/** Destructive: focus starts on Cancel, so Enter can't do the harm. Defaults to `color` or `confirmColor` being
|
|
37
|
+
* `error`. */
|
|
38
|
+
destructive?: boolean
|
|
39
|
+
/** The dismissing button's label. Default: "Cancel". */
|
|
40
|
+
cancelLabel?: string
|
|
41
|
+
/** Whether Escape and a click outside cancel. Default: true. When false, only Cancel and the close button do. */
|
|
42
|
+
dismissible?: boolean
|
|
43
|
+
/** The dialog's width: `sm` (the default), `md` or `lg`. */
|
|
44
|
+
size?: ConfirmSize
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type ConfirmSize = 'sm' | 'md' | 'lg'
|
|
48
|
+
|
|
49
|
+
/** One line of `details`: a string (a `\n` splits it into heading and text), or the two parts given apart. */
|
|
50
|
+
export type ConfirmDetail = string | { title: string, text?: string }
|
|
51
|
+
|
|
52
|
+
/** A yes-or-no question. */
|
|
53
|
+
export interface ConfirmOptions extends ConfirmBaseOptions {
|
|
54
|
+
/** The confirming button's label. Default: "Confirm". */
|
|
55
|
+
confirmLabel?: string
|
|
56
|
+
/** The confirming button's color, where it differs from the dialog's: a warning dialog with a primary button.
|
|
57
|
+
* Default: `color`, else `primary`. `error` makes the question destructive. */
|
|
58
|
+
confirmColor?: ConfirmColor
|
|
59
|
+
/** The confirming button's variant. Default: `solid`. */
|
|
60
|
+
confirmVariant?: ConfirmVariant
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** One of several answers `choose()` offers. */
|
|
64
|
+
export interface ConfirmAction<V> {
|
|
65
|
+
label: string
|
|
66
|
+
/** What `choose()` resolves to when this action is picked. */
|
|
67
|
+
value: V
|
|
68
|
+
/** Default: the dialog's color, else `primary`. */
|
|
69
|
+
color?: ConfirmColor
|
|
70
|
+
/** Default: `solid`. */
|
|
71
|
+
variant?: ConfirmVariant
|
|
72
|
+
icon?: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A question with several answers: Save, Discard, Cancel. */
|
|
76
|
+
export interface ChooseOptions<V> extends ConfirmBaseOptions {
|
|
77
|
+
actions: ConfirmAction<V>[]
|
|
78
|
+
}
|
|
79
|
+
|
|
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> }
|
|
84
|
+
|
|
85
|
+
export type ConfirmEntry = ConfirmQueueEntry<ConfirmRequest, unknown>
|
|
86
|
+
|
|
87
|
+
export const CONFIRM_DELETE_ICON = 'i-lucide-trash-2'
|
|
88
|
+
export const CONFIRM_WARNING_ICON = 'i-lucide-triangle-alert'
|
|
89
|
+
export const CONFIRM_ERROR_ICON = 'i-lucide-circle-alert'
|
|
90
|
+
|
|
91
|
+
/** The icon a question gets from its color when it names none. */
|
|
92
|
+
export const CONFIRM_COLOR_ICONS: Partial<Record<ConfirmColor, string>> = {
|
|
93
|
+
error: CONFIRM_ERROR_ICON,
|
|
94
|
+
warning: CONFIRM_WARNING_ICON,
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The icon shown beside the description: the question's own, else its color's, and none for `icon: false`. */
|
|
98
|
+
export function confirmIcon(options: ConfirmBaseOptions): string | undefined {
|
|
99
|
+
if (options.icon === false) return undefined
|
|
100
|
+
return options.icon || (options.color && CONFIRM_COLOR_ICONS[options.color])
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** A detail as its parts: `"Time Zone Mismatch\nThis file is…"` is a heading and its text; a plain line is text. */
|
|
104
|
+
export function confirmDetailParts(detail: ConfirmDetail): { title?: string, text: string } {
|
|
105
|
+
if (typeof detail !== 'string') return { title: detail.title, text: detail.text ?? '' }
|
|
106
|
+
const at = detail.indexOf('\n')
|
|
107
|
+
return at < 0 ? { text: detail } : { title: detail.slice(0, at), text: detail.slice(at + 1) }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** `confirmDelete('the Lakeside order')`: an error-colored, destructive question with Delete and Cancel. */
|
|
111
|
+
export function deleteOptions(what?: string, overrides?: Partial<ConfirmOptions>): ConfirmOptions {
|
|
112
|
+
return {
|
|
113
|
+
title: what ? `Delete ${what}` : 'Delete',
|
|
114
|
+
description: what
|
|
115
|
+
? `Are you sure you want to delete ${what}? This action cannot be undone.`
|
|
116
|
+
: 'Are you sure? This action cannot be undone.',
|
|
117
|
+
icon: CONFIRM_DELETE_ICON,
|
|
118
|
+
color: 'error',
|
|
119
|
+
confirmLabel: 'Delete',
|
|
120
|
+
...overrides,
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** `confirmDiscard()`: unsaved changes, with a red Discard and Keep Editing. Destructive: focus starts on Keep Editing. */
|
|
125
|
+
export function discardOptions(overrides?: Partial<ConfirmOptions>): ConfirmOptions {
|
|
126
|
+
return {
|
|
127
|
+
title: 'Unsaved Changes',
|
|
128
|
+
description: 'You have unsaved changes. Discard them?',
|
|
129
|
+
icon: CONFIRM_WARNING_ICON,
|
|
130
|
+
color: 'warning',
|
|
131
|
+
confirmColor: 'error',
|
|
132
|
+
destructive: true,
|
|
133
|
+
confirmLabel: 'Discard',
|
|
134
|
+
cancelLabel: 'Keep Editing',
|
|
135
|
+
...overrides,
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Whether a request is destructive: its own say, else whether it, or its confirming button, is error-colored. */
|
|
140
|
+
export const isDestructive = (options: ConfirmBaseOptions & Pick<ConfirmOptions, 'confirmColor'>) =>
|
|
141
|
+
options.destructive ?? (options.color === 'error' || options.confirmColor === 'error')
|
|
142
|
+
|
|
143
|
+
/** A Nuxt app's queue, with its head as a Vue ref for the host to render, and how many hosts are mounted. */
|
|
144
|
+
export interface ConfirmController {
|
|
145
|
+
queue: ConfirmQueue<ConfirmRequest, unknown>
|
|
146
|
+
current: ShallowRef<ConfirmEntry | undefined>
|
|
147
|
+
hosts: number
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const controllers = new WeakMap<object, ConfirmController>()
|
|
151
|
+
|
|
152
|
+
/** The queue of the Nuxt app given (one per request on the server), made the first time it is asked for. */
|
|
153
|
+
export function confirmController(app: object): ConfirmController {
|
|
154
|
+
let controller = controllers.get(app)
|
|
155
|
+
if (!controller) {
|
|
156
|
+
const queue = createConfirmQueue<ConfirmRequest, unknown>()
|
|
157
|
+
const current = shallowRef(queue.current)
|
|
158
|
+
queue.subscribe(() => { current.value = queue.current })
|
|
159
|
+
controller = { queue, current, hosts: 0 }
|
|
160
|
+
controllers.set(app, controller)
|
|
161
|
+
}
|
|
162
|
+
return controller
|
|
163
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ZRepeatingList's Cancel, apart from the component (tests: scripts/row-snapshot.test.mjs): a copy of a row taken when
|
|
3
|
+
* it opens, and that copy put back into the SAME object, so the row keeps its identity (and its key) and the caller's
|
|
4
|
+
* references to it stay good. Plain objects and arrays are copied all the way down and restored in place; anything
|
|
5
|
+
* else (a Date, a Temporal.PlainDate, a class instance) is kept by reference, as values a field replaces rather than
|
|
6
|
+
* edits.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
type Bag = Record<PropertyKey, unknown>
|
|
10
|
+
|
|
11
|
+
const isPlain = (v: unknown): v is Bag => {
|
|
12
|
+
if (v === null || typeof v !== 'object' || Array.isArray(v)) return false
|
|
13
|
+
const proto = Object.getPrototypeOf(v)
|
|
14
|
+
return proto === Object.prototype || proto === null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A copy of `value`: plain objects and arrays deep, everything else as it is. */
|
|
18
|
+
export function snapshot<T>(value: T): T {
|
|
19
|
+
if (Array.isArray(value)) return value.map((v) => snapshot(v)) as T
|
|
20
|
+
if (isPlain(value)) {
|
|
21
|
+
const copy: Bag = Object.create(Object.getPrototypeOf(value))
|
|
22
|
+
for (const k of Object.keys(value)) copy[k] = snapshot(value[k])
|
|
23
|
+
return copy as T
|
|
24
|
+
}
|
|
25
|
+
return value
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Puts `snap` (from `snapshot`) back into `target`: its fields set again, fields added since removed, and a nested
|
|
30
|
+
* plain object or array restored into the one already there, so it keeps its identity too.
|
|
31
|
+
*/
|
|
32
|
+
export function restore<T extends object>(target: T, snap: T): T {
|
|
33
|
+
if (Array.isArray(target) && Array.isArray(snap)) {
|
|
34
|
+
target.length = snap.length
|
|
35
|
+
snap.forEach((v, i) => { target[i] = restoreValue(target[i], v) })
|
|
36
|
+
return target
|
|
37
|
+
}
|
|
38
|
+
const t = target as Bag, s = snap as Bag
|
|
39
|
+
for (const k of Object.keys(t)) if (!(k in s)) Reflect.deleteProperty(t, k)
|
|
40
|
+
for (const k of Object.keys(s)) t[k] = restoreValue(t[k], s[k])
|
|
41
|
+
return target
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function restoreValue(current: unknown, snap: unknown): unknown {
|
|
45
|
+
if (Array.isArray(snap)) return Array.isArray(current) ? restore(current, snap) : snapshot(snap)
|
|
46
|
+
if (isPlain(snap)) return isPlain(current) ? restore(current, snap) : snapshot(snap)
|
|
47
|
+
return snap
|
|
48
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ZSelectMenu's typeahead, apart from the component so the rules read in one place: which items a typed search can
|
|
3
|
+
* land on, how an item's label and value are read (as Nuxt UI's USelectMenu reads them, by `labelKey` and `valueKey`,
|
|
4
|
+
* dotted paths included), and which item a search matches. The matching searches from the top of the list, whatever
|
|
5
|
+
* is selected: a search matches the first item whose label starts with it; pressing one letter again and again cycles
|
|
6
|
+
* through the items that start with it, onward from the current one.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** How the menu reads its items: the props of the same names on USelectMenu. */
|
|
10
|
+
export interface TypeaheadKeys {
|
|
11
|
+
labelKey?: string
|
|
12
|
+
valueKey?: string
|
|
13
|
+
/** USelectMenu's `by`: the field, or the function, that tells two values apart. */
|
|
14
|
+
by?: string | ((a: unknown, b: unknown) => boolean)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const isObject = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null
|
|
18
|
+
|
|
19
|
+
function get(item: unknown, path: string): unknown {
|
|
20
|
+
let v: unknown = item
|
|
21
|
+
for (const key of path.split('.')) v = isObject(v) ? v[key] : undefined
|
|
22
|
+
return v
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The items a search can land on: groups flattened, without labels, separators and disabled items. */
|
|
26
|
+
export function selectableItems<T>(items: readonly (T | readonly T[])[] | undefined): T[] {
|
|
27
|
+
const out: T[] = []
|
|
28
|
+
for (const entry of items ?? []) {
|
|
29
|
+
for (const item of (Array.isArray(entry) ? entry : [entry]) as T[]) {
|
|
30
|
+
const v: unknown = item
|
|
31
|
+
if (v == null) continue
|
|
32
|
+
if (isObject(v) && (v.type === 'label' || v.type === 'separator' || v.disabled)) continue
|
|
33
|
+
out.push(item)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return out
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** An item's label: `labelKey` (default `label`) on an object, the item itself otherwise. */
|
|
40
|
+
export function itemLabel(item: unknown, keys: TypeaheadKeys = {}): string {
|
|
41
|
+
if (!isObject(item)) return item == null ? '' : String(item)
|
|
42
|
+
const label = get(item, keys.labelKey ?? 'label')
|
|
43
|
+
return label == null ? '' : String(label)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** What choosing an item puts in the model: its `valueKey` field when there is one, the item itself otherwise. */
|
|
47
|
+
export function itemValue(item: unknown, keys: TypeaheadKeys = {}): unknown {
|
|
48
|
+
return keys.valueKey && isObject(item) ? get(item, keys.valueKey) : item
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Whether two model values are the same choice, by `by` when given, as USelectMenu compares them. */
|
|
52
|
+
export function sameValue(a: unknown, b: unknown, keys: TypeaheadKeys = {}): boolean {
|
|
53
|
+
if (a === b) return true
|
|
54
|
+
if (!isObject(a) || !isObject(b)) return false
|
|
55
|
+
if (typeof keys.by === 'function') return keys.by(a, b)
|
|
56
|
+
if (typeof keys.by === 'string') return get(a, keys.by) === get(b, keys.by)
|
|
57
|
+
return JSON.stringify(a) === JSON.stringify(b)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The item a typed search lands on, or undefined when nothing matches (or the match is the current item).
|
|
62
|
+
* `search` is everything typed since the last pause; `current` is the model's value.
|
|
63
|
+
*/
|
|
64
|
+
export function typeaheadMatch<T>(items: readonly T[], search: string, current: unknown, keys: TypeaheadKeys = {}): T | undefined {
|
|
65
|
+
if (!search) return undefined
|
|
66
|
+
const lower = search.toLocaleLowerCase()
|
|
67
|
+
const startsWith = (prefix: string) => (item: T) => itemLabel(item, keys).toLocaleLowerCase().startsWith(prefix)
|
|
68
|
+
const isCurrent = (item: T) => sameValue(itemValue(item, keys), current, keys)
|
|
69
|
+
let match: T | undefined
|
|
70
|
+
if (lower.length > 1 && [...lower].every((c) => c === lower[0])) {
|
|
71
|
+
// "bbb" is pressing b three times: the next item that starts with b after the current one, round to the first.
|
|
72
|
+
const matches = items.filter(startsWith(lower[0]!))
|
|
73
|
+
match = matches[(matches.findIndex(isCurrent) + 1) % matches.length]
|
|
74
|
+
} else {
|
|
75
|
+
// From the top of the list, not from the current item.
|
|
76
|
+
match = items.find(startsWith(lower))
|
|
77
|
+
}
|
|
78
|
+
return match === undefined || isCurrent(match) ? undefined : match
|
|
79
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Promise-based confirmation: ask, await the answer, carry on.
|
|
3
|
+
*
|
|
4
|
+
* const { confirm, choose, confirmDelete, confirmDiscard } = useConfirm()
|
|
5
|
+
* if (await confirmDelete('the Lakeside order')) await remove(order)
|
|
6
|
+
* const next = await choose({ title: 'Save before leaving?', actions: [
|
|
7
|
+
* { label: 'Discard', value: 'discard', color: 'neutral', variant: 'outline' },
|
|
8
|
+
* { label: 'Save', value: 'save' },
|
|
9
|
+
* ] }) // 'save' | 'discard' | null
|
|
10
|
+
*
|
|
11
|
+
* The dialog renders in <ZConfirmDialog>, mounted once inside <UApp>. `confirm` resolves `true` when confirmed and
|
|
12
|
+
* `false` when cancelled; `choose` resolves to the picked action's `value`, or `null`. Escape, the close button and a
|
|
13
|
+
* click outside are all a cancel.
|
|
14
|
+
*
|
|
15
|
+
* One dialog at a time: a call made while one is open QUEUES behind it, and is shown when it is answered
|
|
16
|
+
* (utils/confirm-queue.ts says why it doesn't replace). `dismissAll()` cancels the open one and every one waiting.
|
|
17
|
+
*
|
|
18
|
+
* Per request: the queue belongs to the Nuxt app (components/confirm.ts), never to the module. On the server, where
|
|
19
|
+
* no one can answer, every call resolves at once as a cancel.
|
|
20
|
+
*/
|
|
21
|
+
import {
|
|
22
|
+
confirmController,
|
|
23
|
+
deleteOptions,
|
|
24
|
+
discardOptions,
|
|
25
|
+
type ChooseOptions,
|
|
26
|
+
type ConfirmOptions,
|
|
27
|
+
} from '../components/confirm'
|
|
28
|
+
|
|
29
|
+
export type { ChooseOptions, ConfirmAction, ConfirmBaseOptions, ConfirmColor, ConfirmDetail, ConfirmOptions, ConfirmSize, ConfirmVariant } from '../components/confirm'
|
|
30
|
+
|
|
31
|
+
export function useConfirm() {
|
|
32
|
+
const controller = confirmController(useNuxtApp())
|
|
33
|
+
|
|
34
|
+
function ask<V>(request: Parameters<typeof controller.queue.ask>[0], dismissed: V): Promise<V> {
|
|
35
|
+
if (import.meta.server) return Promise.resolve(dismissed)
|
|
36
|
+
if (import.meta.dev && !controller.hosts) {
|
|
37
|
+
console.warn('[zooui] useConfirm(): no <ZConfirmDialog> is mounted, so this question will never be shown. Mount one inside <UApp>.')
|
|
38
|
+
}
|
|
39
|
+
return controller.queue.ask(request, dismissed) as Promise<V>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Ask a yes-or-no question: `true` when confirmed, `false` when cancelled or dismissed. */
|
|
43
|
+
function confirm(options: ConfirmOptions): Promise<boolean> {
|
|
44
|
+
return ask({ kind: 'confirm', options }, false)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Offer several answers: the picked action's `value`, or `null` when cancelled or dismissed. */
|
|
48
|
+
function choose<V>(options: ChooseOptions<V>): Promise<V | null> {
|
|
49
|
+
return ask<V | null>({ kind: 'choose', options }, null)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** "Delete {what}", error-colored, with Delete and Cancel; focus starts on Cancel. */
|
|
53
|
+
function confirmDelete(what?: string, overrides?: Partial<ConfirmOptions>): Promise<boolean> {
|
|
54
|
+
return confirm(deleteOptions(what, overrides))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** "Unsaved Changes", with a red Discard and Keep Editing; focus starts on Keep Editing. */
|
|
58
|
+
function confirmDiscard(overrides?: Partial<ConfirmOptions>): Promise<boolean> {
|
|
59
|
+
return confirm(discardOptions(overrides))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Cancel the open question and every one waiting (on sign-out, say). */
|
|
63
|
+
function dismissAll() {
|
|
64
|
+
controller.queue.dismissAll()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return { confirm, choose, confirmDelete, confirmDiscard, dismissAll }
|
|
68
|
+
}
|
package/nuxt.config.ts
CHANGED
|
@@ -33,7 +33,11 @@ interface LayerNuxt {
|
|
|
33
33
|
/** Icons the Z* components render, for the client bundle below. scripts/check-icons.test.mjs keeps it in step with
|
|
34
34
|
* components/. */
|
|
35
35
|
const Z_ICONS = ['lucide:sun', 'lucide:moon', 'lucide:monitor', 'lucide:triangle-alert', 'lucide:check',
|
|
36
|
-
'lucide:circle-check', 'lucide:pencil', 'lucide:x', 'lucide:trash-2', 'lucide:plus', 'lucide:chevron-up', 'lucide:chevron-down', 'lucide:calendar'
|
|
36
|
+
'lucide:circle-check', 'lucide:pencil', 'lucide:x', 'lucide:trash-2', 'lucide:plus', 'lucide:chevron-up', 'lucide:chevron-down', 'lucide:calendar',
|
|
37
|
+
'lucide:file', 'lucide:file-image', 'lucide:file-video', 'lucide:file-audio', 'lucide:file-spreadsheet',
|
|
38
|
+
'lucide:file-archive', 'lucide:file-text',
|
|
39
|
+
'lucide:list-filter', 'lucide:search', 'lucide:check-check', 'lucide:arrow-up-to-line', 'lucide:arrow-down-to-line',
|
|
40
|
+
'lucide:circle-alert']
|
|
37
41
|
|
|
38
42
|
export default defineNuxtConfig({
|
|
39
43
|
modules: [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zooui/nuxt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Zoo UI for Nuxt: a theme-free Nuxt layer with Nuxt UI as it ships, Zoo UI's Z* components and colored-edge surfaces, and the mechanisms every product needs, all on Nuxt UI's tokens so they take the product's own colors. Add a theme layer such as @zooui/theme-forest for a ready-made look.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"@internationalized/date": "^3.12.4",
|
|
42
42
|
"@nuxt/ui": "~4.11.1",
|
|
43
43
|
"decimal.js": "^10.6.0",
|
|
44
|
+
"tailwind-merge": "^3.7.0",
|
|
44
45
|
"tailwindcss": "^4.0.0",
|
|
45
46
|
"temporal-polyfill": "^1.0.5"
|
|
46
47
|
},
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// The queue behind useConfirm() and <ZConfirmDialog>: requests wait in line, one is answered at a time, and each
|
|
2
|
+
// caller's promise settles with its own answer. Framework-free, so it is unit-tested on its own
|
|
3
|
+
// (scripts/confirm-queue.test.mjs); the composable keeps one queue per Nuxt app, which on the server is one per request.
|
|
4
|
+
//
|
|
5
|
+
// A SECOND CALL QUEUES, it does not replace. A caller awaiting `confirm()` has stopped mid-action to ask; replacing its
|
|
6
|
+
// dialog would answer that question for the person ("cancel") without them seeing it. So each request is shown in
|
|
7
|
+
// the order it was made, the next once the one before it is answered, and every promise settles exactly once.
|
|
8
|
+
// `dismissAll()` is the way out for a caller that must clear the line, such as on sign-out: every request still
|
|
9
|
+
// waiting, the open one included, settles with its own dismissed value.
|
|
10
|
+
|
|
11
|
+
/** One request in the queue: what to show, and the value it settles with when dismissed (Escape, close, cancel). */
|
|
12
|
+
export interface ConfirmQueueEntry<R, V> {
|
|
13
|
+
/** Unique within the queue, so an answer meant for a dialog that has since gone cannot settle the next one. */
|
|
14
|
+
readonly id: number
|
|
15
|
+
readonly request: R
|
|
16
|
+
readonly dismissed: V
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ConfirmQueue<R, V> {
|
|
20
|
+
/** The request being asked now, or undefined when the queue is empty. */
|
|
21
|
+
readonly current: ConfirmQueueEntry<R, V> | undefined
|
|
22
|
+
/** How many requests are waiting behind the current one. */
|
|
23
|
+
readonly waiting: number
|
|
24
|
+
/** Add a request; the promise settles with the value it is answered with, or `dismissed`. */
|
|
25
|
+
ask(request: R, dismissed: V): Promise<V>
|
|
26
|
+
/** Answer the request `id` with `value`, if it is still the current one; otherwise nothing happens. */
|
|
27
|
+
answer(id: number, value: V): void
|
|
28
|
+
/** Dismiss the request `id`, if it is still the current one: it settles with its own dismissed value. */
|
|
29
|
+
dismiss(id: number): void
|
|
30
|
+
/** Dismiss every request, the current one first, then the rest in order. */
|
|
31
|
+
dismissAll(): void
|
|
32
|
+
/** Called whenever `current` changes. Returns the unsubscribe. */
|
|
33
|
+
subscribe(listener: () => void): () => void
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface Pending<R, V> extends ConfirmQueueEntry<R, V> {
|
|
37
|
+
settle(value: V): void
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function createConfirmQueue<R, V>(): ConfirmQueue<R, V> {
|
|
41
|
+
const line: Pending<R, V>[] = []
|
|
42
|
+
const listeners = new Set<() => void>()
|
|
43
|
+
let nextId = 1
|
|
44
|
+
|
|
45
|
+
const notify = () => { for (const listener of listeners) listener() }
|
|
46
|
+
|
|
47
|
+
function settleHead(id: number, value: (head: Pending<R, V>) => V) {
|
|
48
|
+
const head = line[0]
|
|
49
|
+
if (!head || head.id !== id) return
|
|
50
|
+
line.shift()
|
|
51
|
+
head.settle(value(head))
|
|
52
|
+
notify()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
get current() {
|
|
57
|
+
const head = line[0]
|
|
58
|
+
return head && { id: head.id, request: head.request, dismissed: head.dismissed }
|
|
59
|
+
},
|
|
60
|
+
get waiting() {
|
|
61
|
+
return Math.max(0, line.length - 1)
|
|
62
|
+
},
|
|
63
|
+
ask(request, dismissed) {
|
|
64
|
+
return new Promise<V>((resolve) => {
|
|
65
|
+
line.push({ id: nextId++, request, dismissed, settle: resolve })
|
|
66
|
+
if (line.length === 1) notify()
|
|
67
|
+
})
|
|
68
|
+
},
|
|
69
|
+
answer(id, value) {
|
|
70
|
+
settleHead(id, () => value)
|
|
71
|
+
},
|
|
72
|
+
dismiss(id) {
|
|
73
|
+
settleHead(id, (head) => head.dismissed)
|
|
74
|
+
},
|
|
75
|
+
dismissAll() {
|
|
76
|
+
if (!line.length) return
|
|
77
|
+
for (const entry of line.splice(0)) entry.settle(entry.dismissed)
|
|
78
|
+
notify()
|
|
79
|
+
},
|
|
80
|
+
subscribe(listener) {
|
|
81
|
+
listeners.add(listener)
|
|
82
|
+
return () => { listeners.delete(listener) }
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
}
|
package/utils/date-entry.ts
CHANGED
|
@@ -129,11 +129,12 @@ function dayFromParts(order: DateEntryOrder, parts: string[], today: PlainDate):
|
|
|
129
129
|
* In MM/DD/YYYY (the default):
|
|
130
130
|
*
|
|
131
131
|
* - `5`, `12` (up to 31) — that day of today's month
|
|
132
|
-
* - `0512` — May 12 this year; `051226` — May 12, 2026
|
|
132
|
+
* - `0512` — May 12 this year; `051226` and `05122026` — May 12, 2026
|
|
133
133
|
* - `5/12` — this year; `5/12/26`, `5/12/2026`
|
|
134
134
|
*
|
|
135
135
|
* Other formats read the same shapes in their own order: in DD/MM/YYYY `1205` and `12/5` are May 12, and
|
|
136
|
-
* `120526`
|
|
136
|
+
* `120526` and `12052026` are May 12, 2026; in YYYY-MM-DD `0512` and `5-12` are May 12 and `260512`, `20260512` and
|
|
137
|
+
* `2026-5-12` carry the year.
|
|
137
138
|
* A bare day of the month reads the same in all of them. Returns null for anything that doesn't name a real day,
|
|
138
139
|
* including blank text and a year of all zeros (`5/12/0`, `5/12/00`, `051200`).
|
|
139
140
|
*/
|
|
@@ -149,6 +150,12 @@ export function parseDateEntry(text: string, today: PlainDate, format: DateEntry
|
|
|
149
150
|
return dayOf(today.year, today.month, Number(value))
|
|
150
151
|
if (value.length === 4) return dayFromParts(order, [value.slice(0, 2), value.slice(2, 4)], today)
|
|
151
152
|
if (value.length === 6) return dayFromParts(order, [value.slice(0, 2), value.slice(2, 4), value.slice(4, 6)], today)
|
|
153
|
+
// Eight digits carry a four-digit year, wherever the format puts it: MMDDYYYY, DDMMYYYY, YYYYMMDD.
|
|
154
|
+
if (value.length === 8) {
|
|
155
|
+
let at = 0
|
|
156
|
+
const parts = [...order].map((field) => value.slice(at, (at += field === 'Y' ? 4 : 2)))
|
|
157
|
+
return dayFromParts(order, parts, today)
|
|
158
|
+
}
|
|
152
159
|
return null
|
|
153
160
|
}
|
|
154
161
|
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Files dropped on a page, sorted the way a native <input type="file"> would take them: `accept` in the input's own
|
|
2
|
+
// syntax (MIME types, `type/*`, `.ext`, comma-separated), and one file unless `multiple`; then a product's own `filter`
|
|
3
|
+
// (a size or name rule) and, optionally, no name twice. Used by ZFileDropOverlay; a product can hold its own drop zones
|
|
4
|
+
// or inputs to the same rule.
|
|
5
|
+
//
|
|
6
|
+
// Framework-free: Nuxt auto-imports it in a product.
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A file the drop did not take, and why: its type isn't in `accept`, `filter` refused it, its name is already held
|
|
10
|
+
* (`duplicate`), or it came after the first without `multiple`.
|
|
11
|
+
*/
|
|
12
|
+
export interface RejectedFile {
|
|
13
|
+
file: File
|
|
14
|
+
reason: 'accept' | 'filter' | 'duplicate' | 'multiple'
|
|
15
|
+
/** The message `filter` returned for it, when it returned one. */
|
|
16
|
+
message?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** A product's own rule for a file: `true` takes it; `false`, or a message saying why, refuses it. */
|
|
20
|
+
export type FileFilter = (file: File) => boolean | string
|
|
21
|
+
|
|
22
|
+
/** A row of ZFileList: a `File` just added, or a file already stored, by its id, with a thumbnail URL if it has one. */
|
|
23
|
+
export type FileListItem = File | { id: string | number; name: string; size: number; type?: string; thumbnail?: string }
|
|
24
|
+
|
|
25
|
+
/** How sortDroppedFiles sorts. */
|
|
26
|
+
export interface SortDroppedFilesOptions {
|
|
27
|
+
/** The native input's `accept`. Unset, every type is taken. */
|
|
28
|
+
accept?: string | readonly string[]
|
|
29
|
+
/** Take every file the rules let through; without it, the first. */
|
|
30
|
+
multiple?: boolean
|
|
31
|
+
/** A product's own rule, run on each file `accept` takes. */
|
|
32
|
+
filter?: FileFilter
|
|
33
|
+
/** Names already held (the list the files join): a file of one of these names is refused as `duplicate`. Implies
|
|
34
|
+
* `dedupe: 'name'`. */
|
|
35
|
+
existing?: Iterable<string>
|
|
36
|
+
/** `'name'`: refuse a file whose name is in `existing` or came earlier in the same drop. */
|
|
37
|
+
dedupe?: 'name'
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** What a file needs for matching: a `File`, or anything with its name and MIME type. */
|
|
41
|
+
type Named = Pick<File, 'name' | 'type'>
|
|
42
|
+
|
|
43
|
+
/** The valid tokens of an `accept` value, lower-cased; tokens the input would ignore are dropped. */
|
|
44
|
+
function acceptTokens(accept: string | readonly string[] | undefined): string[] {
|
|
45
|
+
const list = accept === undefined ? [] : typeof accept === 'string' ? accept.split(',') : accept.flatMap((a) => a.split(','))
|
|
46
|
+
return list
|
|
47
|
+
.map((t) => t.trim().toLowerCase())
|
|
48
|
+
.filter((t) => /^\.[^./\s][^/\s]*$/.test(t) || /^[a-z0-9!#$&^_.+-]+\/(\*|[a-z0-9!#$&^_.+-]+)$/.test(t))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether `accept` takes the file, as the native input reads it: `.pdf` matches the name's ending (any case),
|
|
53
|
+
* `image/*` the MIME type's first half, `application/pdf` the whole type. An empty `accept`, or one with no valid
|
|
54
|
+
* token, takes every file.
|
|
55
|
+
*/
|
|
56
|
+
export function fileMatchesAccept(file: Named, accept?: string | readonly string[]): boolean {
|
|
57
|
+
const tokens = acceptTokens(accept)
|
|
58
|
+
if (!tokens.length) return true
|
|
59
|
+
const name = file.name.toLowerCase()
|
|
60
|
+
const type = file.type.toLowerCase().split(';')[0]!.trim()
|
|
61
|
+
return tokens.some((t) => {
|
|
62
|
+
if (t.startsWith('.')) return name.endsWith(t)
|
|
63
|
+
if (t.endsWith('/*')) return type.startsWith(t.slice(0, -1))
|
|
64
|
+
return type === t
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The dropped files, split into the ones taken and the ones refused, in drop order. Each file is checked against
|
|
70
|
+
* `accept`, then `filter`, then (with `existing` or `dedupe`) the names already held, and refused at the first that
|
|
71
|
+
* says no. Without `multiple`, the first file to pass is the one taken, and later ones are refused as `multiple`.
|
|
72
|
+
*/
|
|
73
|
+
export function sortDroppedFiles(
|
|
74
|
+
files: Iterable<File>,
|
|
75
|
+
options: SortDroppedFilesOptions = {},
|
|
76
|
+
): { accepted: File[]; rejected: RejectedFile[] } {
|
|
77
|
+
const accepted: File[] = []
|
|
78
|
+
const rejected: RejectedFile[] = []
|
|
79
|
+
const dedupe = options.dedupe === 'name' || options.existing !== undefined
|
|
80
|
+
const held = new Set(options.existing ?? [])
|
|
81
|
+
for (const file of files) {
|
|
82
|
+
if (!fileMatchesAccept(file, options.accept)) {
|
|
83
|
+
rejected.push({ file, reason: 'accept' })
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
const verdict = options.filter ? options.filter(file) : true
|
|
87
|
+
if (verdict !== true) {
|
|
88
|
+
rejected.push(typeof verdict === 'string' && verdict ? { file, reason: 'filter', message: verdict } : { file, reason: 'filter' })
|
|
89
|
+
} else if (dedupe && held.has(file.name)) {
|
|
90
|
+
rejected.push({ file, reason: 'duplicate' })
|
|
91
|
+
} else if (!options.multiple && accepted.length) {
|
|
92
|
+
rejected.push({ file, reason: 'multiple' })
|
|
93
|
+
} else {
|
|
94
|
+
accepted.push(file)
|
|
95
|
+
held.add(file.name)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return { accepted, rejected }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The files a drop carries: `dataTransfer.files`, or, when a browser leaves that empty (some do, for some sources),
|
|
103
|
+
* the file entries of `dataTransfer.items`.
|
|
104
|
+
*/
|
|
105
|
+
export function droppedFiles(dataTransfer: Pick<DataTransfer, 'files' | 'items'> | null | undefined): File[] {
|
|
106
|
+
if (!dataTransfer) return []
|
|
107
|
+
const files = Array.from(dataTransfer.files ?? [])
|
|
108
|
+
if (files.length) return files
|
|
109
|
+
const fromItems: File[] = []
|
|
110
|
+
for (const item of Array.from(dataTransfer.items ?? [])) {
|
|
111
|
+
if (item.kind !== 'file') continue
|
|
112
|
+
const file = item.getAsFile()
|
|
113
|
+
if (file) fromItems.push(file)
|
|
114
|
+
}
|
|
115
|
+
return fromItems
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Whether a drag carries files from outside the page, rather than text, a link or an element dragged within it. */
|
|
119
|
+
export function dragCarriesFiles(dataTransfer: Pick<DataTransfer, 'types'> | null | undefined): boolean {
|
|
120
|
+
return !!dataTransfer && Array.from(dataTransfer.types).includes('Files')
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const SIZE_UNITS = ['kilobyte', 'megabyte', 'gigabyte', 'terabyte'] as const
|
|
124
|
+
const BINARY_UNITS = ['KB', 'MB', 'GB', 'TB'] as const
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A file's size for people, in the locale's digits. By default in steps of 1024, as Windows and most file managers
|
|
128
|
+
* count, with one decimal at most: `512 B`, `1.2 KB`, `239.3 KB`, `1.7 MB` (`1,2 KB` in German). With
|
|
129
|
+
* `binary: false`, decimal units as their names say (1 kB = 1000 bytes), in the locale's own words, one decimal under
|
|
130
|
+
* 10 and none above: `512 bytes`, `1.2 kB`, `245 kB`.
|
|
131
|
+
*/
|
|
132
|
+
export function formatFileSize(bytes: number, locale?: string, options: { binary?: boolean } = {}): string {
|
|
133
|
+
const binary = options.binary ?? true
|
|
134
|
+
const step = binary ? 1024 : 1000
|
|
135
|
+
const size = Number.isFinite(bytes) ? Math.max(0, bytes) : 0
|
|
136
|
+
if (size < step) {
|
|
137
|
+
const whole = Math.round(size)
|
|
138
|
+
if (binary) return `${new Intl.NumberFormat(locale).format(whole)} B`
|
|
139
|
+
return new Intl.NumberFormat(locale, { style: 'unit', unit: 'byte', unitDisplay: 'long' }).format(whole)
|
|
140
|
+
}
|
|
141
|
+
let value = size / step
|
|
142
|
+
let unit = 0
|
|
143
|
+
const digits = (v: number) => (binary || v < 10 ? 1 : 0)
|
|
144
|
+
// 999,960 bytes is 1 MB, not "1,000 kB": step up whenever rounding reaches the next unit.
|
|
145
|
+
while (unit < SIZE_UNITS.length - 1 && Number(value.toFixed(digits(value))) >= step) {
|
|
146
|
+
value /= step
|
|
147
|
+
unit += 1
|
|
148
|
+
}
|
|
149
|
+
if (binary) return `${new Intl.NumberFormat(locale, { maximumFractionDigits: 1 }).format(value)} ${BINARY_UNITS[unit]}`
|
|
150
|
+
return new Intl.NumberFormat(locale, { style: 'unit', unit: SIZE_UNITS[unit], unitDisplay: 'short', maximumFractionDigits: digits(value) }).format(value)
|
|
151
|
+
}
|