@dumiflex/wp-kit 0.3.0 → 0.4.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.
Files changed (128) hide show
  1. package/CHANGELOG.md +77 -51
  2. package/LICENSE +674 -674
  3. package/README.md +73 -76
  4. package/package.json +78 -77
  5. package/src/Avatar.vue +83 -83
  6. package/src/Badge.vue +97 -97
  7. package/src/Banner.vue +118 -118
  8. package/src/BookmarkButton.vue +27 -27
  9. package/src/BundleMeta.vue +51 -51
  10. package/src/Button.vue +103 -103
  11. package/src/Card.vue +68 -68
  12. package/src/Checkbox.vue +109 -107
  13. package/src/Chip.vue +163 -97
  14. package/src/ChoiceCards.vue +133 -133
  15. package/src/CloseButton.vue +69 -69
  16. package/src/CodeBlock.vue +119 -119
  17. package/src/ColorField.vue +188 -206
  18. package/src/ColorPicker.vue +201 -201
  19. package/src/Combobox.vue +331 -334
  20. package/src/ConfirmDialog.vue +165 -163
  21. package/src/ConfirmModal.vue +147 -147
  22. package/src/ConstraintMatrixPreview.vue +708 -708
  23. package/src/ContextMenu.vue +386 -379
  24. package/src/CopyButton.vue +156 -156
  25. package/src/DataTable.vue +292 -292
  26. package/src/DatePicker.vue +234 -234
  27. package/src/DescriptionList.vue +73 -73
  28. package/src/DiffView.vue +68 -68
  29. package/src/Disclosure.vue +166 -166
  30. package/src/DragHandle.vue +49 -49
  31. package/src/Drawer.vue +131 -131
  32. package/src/DropZone.vue +196 -196
  33. package/src/DropdownMenu.vue +72 -72
  34. package/src/EmptyState.vue +111 -111
  35. package/src/Field.vue +28 -28
  36. package/src/HsvPicker.vue +208 -208
  37. package/src/Icon.vue +39 -39
  38. package/src/IconPicker.vue +209 -209
  39. package/src/ImageGrid.vue +125 -125
  40. package/src/InlineEdit.vue +183 -183
  41. package/src/Input.vue +99 -99
  42. package/src/Kbd.vue +56 -56
  43. package/src/Lightbox.vue +173 -173
  44. package/src/ListFilter.vue +85 -85
  45. package/src/ListRow.vue +204 -204
  46. package/src/LoadingMark.vue +175 -161
  47. package/src/Logo.vue +60 -60
  48. package/src/Modal.vue +78 -76
  49. package/src/ModalShell.vue +109 -107
  50. package/src/Pagination.vue +106 -106
  51. package/src/Popover.vue +180 -171
  52. package/src/PostCard.vue +210 -211
  53. package/src/ProgressBar.vue +90 -90
  54. package/src/RadioGroup.vue +177 -177
  55. package/src/RangeSlider.vue +183 -183
  56. package/src/RelativeDate.vue +35 -35
  57. package/src/ResizeHandle.vue +143 -143
  58. package/src/SearchInput.vue +192 -199
  59. package/src/Select.vue +644 -641
  60. package/src/Separator.vue +54 -54
  61. package/src/Skeleton.vue +82 -82
  62. package/src/Slider.vue +198 -198
  63. package/src/SortableList.vue +260 -260
  64. package/src/SplitPane.vue +68 -68
  65. package/src/StarButton.vue +40 -40
  66. package/src/Stepper.vue +141 -141
  67. package/src/SupportLinks.vue +93 -93
  68. package/src/TagInput.vue +297 -300
  69. package/src/Textarea.vue +57 -57
  70. package/src/Timeline.vue +92 -92
  71. package/src/ToastCard.vue +119 -119
  72. package/src/ToastHost.vue +40 -40
  73. package/src/Toggle.vue +36 -36
  74. package/src/TokenizedValue.vue +310 -310
  75. package/src/Toolbar.vue +110 -110
  76. package/src/VirtualList.vue +87 -87
  77. package/src/VisuallyHidden.vue +29 -29
  78. package/src/WpButton.vue +75 -75
  79. package/src/WpCheck.vue +104 -102
  80. package/src/WpIconButton.vue +107 -107
  81. package/src/WpImageZoom.vue +544 -544
  82. package/src/WpNumber.vue +178 -183
  83. package/src/WpSegmented.vue +175 -175
  84. package/src/WpSelect.vue +93 -89
  85. package/src/WpSwitch.vue +163 -163
  86. package/src/WpTabs.vue +188 -188
  87. package/src/_modal-motion.css +33 -33
  88. package/src/_skeleton.css +50 -50
  89. package/src/axis-color.ts +42 -42
  90. package/src/button.css +169 -169
  91. package/src/calendar.ts +59 -59
  92. package/src/clipboard.ts +50 -50
  93. package/src/colour-presets.ts +12 -12
  94. package/src/diff.ts +51 -51
  95. package/src/drop-zone.ts +41 -41
  96. package/src/field.css +50 -0
  97. package/src/flip.ts +237 -237
  98. package/src/floating-layer.ts +48 -0
  99. package/src/floating.ts +99 -99
  100. package/src/hsv.ts +100 -100
  101. package/src/icon-catalog.ts +52 -52
  102. package/src/image-zoom.ts +93 -93
  103. package/src/kind-colour.ts +28 -0
  104. package/src/logo-paths.ts +21 -21
  105. package/src/matrix-axis.ts +91 -91
  106. package/src/pagination.ts +27 -27
  107. package/src/platform-keys.ts +29 -29
  108. package/src/rich-tokenize.ts +357 -357
  109. package/src/select-types.ts +32 -32
  110. package/src/slider-math.ts +45 -45
  111. package/src/sortable.ts +22 -22
  112. package/src/theme/fonts/OFL-Fraunces.txt +93 -93
  113. package/src/theme/fonts/OFL-JetBrainsMono.txt +93 -93
  114. package/src/theme/fonts/OFL-Manrope.txt +93 -93
  115. package/src/theme/looks.css +428 -428
  116. package/src/theme/palettes.css +254 -254
  117. package/src/theme/tokens.css +2896 -2894
  118. package/src/tokens.ts +56 -56
  119. package/src/tooltip.css +43 -43
  120. package/src/tooltip.ts +146 -146
  121. package/src/useDialogFocus.ts +73 -73
  122. package/src/useGrowableField.ts +357 -357
  123. package/src/useModalKeyShield.ts +116 -116
  124. package/src/useToast.ts +48 -48
  125. package/src/wp-logo.svg +14 -14
  126. package/src/ChipToggle.vue +0 -92
  127. package/src/KindChip.vue +0 -53
  128. package/src/RatingBadge.vue +0 -42
package/src/Drawer.vue CHANGED
@@ -1,131 +1,131 @@
1
- <script setup lang="ts">
2
- /**
3
- * A panel that slides in from the side of the page: inspectors, history,
4
- * settings that sit beside the work instead of over it. Escape closes it,
5
- * Tab stays inside it and focus goes back where it was. With `modal` off
6
- * there is no backdrop, Tab moves on to the page and the page stays usable.
7
- */
8
- import CloseButton from "./CloseButton.vue";
9
- import { ref, useId, useSlots } from "vue";
10
- import { useDialogFocus } from "./useDialogFocus";
11
-
12
- interface Props {
13
- open: boolean;
14
- title?: string | undefined;
15
- side?: "right" | "left" | undefined;
16
- /** Any CSS width; capped at the viewport. */
17
- width?: string | undefined;
18
- /** Dim the page and close on a click outside. */
19
- modal?: boolean | undefined;
20
- }
21
- const props = withDefaults(defineProps<Props>(), { side: "right", width: "420px", modal: true });
22
- const emit = defineEmits<{ (e: "update:open", v: boolean): void }>();
23
-
24
- const slots = useSlots();
25
- const panel = ref<HTMLElement | null>(null);
26
- const titleId = `wp-drawer-title-${useId()}`;
27
-
28
- function close(): void {
29
- emit("update:open", false);
30
- }
31
-
32
- // The backdrop is a click target only; keyboard users close with Escape.
33
- function bindBackdrop(el: unknown): void {
34
- if (el instanceof HTMLElement) el.onmousedown = (ev) => { if (ev.target === el) close(); };
35
- }
36
-
37
- useDialogFocus(panel, () => props.open, {
38
- onEscape: close,
39
- trap: () => props.modal,
40
- // The first control in the body, not the close button.
41
- initial: (root, els) => {
42
- const body = root.querySelector(".wp-drawer__body");
43
- return els.find((el) => body?.contains(el)) ?? root;
44
- },
45
- });
46
- </script>
47
-
48
- <template>
49
- <Teleport to="body">
50
- <Transition name="wp-drawer-fade">
51
- <div v-if="open && modal" :ref="bindBackdrop" class="wp-drawer__backdrop" data-test="drawer-backdrop" />
52
- </Transition>
53
- <Transition :name="`wp-drawer-${side}`">
54
- <aside
55
- v-if="open"
56
- ref="panel"
57
- class="wp-drawer"
58
- :class="`wp-drawer--${side}`"
59
- :style="{ width: `min(${width}, 100vw)` }"
60
- role="dialog"
61
- :aria-modal="modal || undefined"
62
- :aria-labelledby="title ? titleId : undefined"
63
- tabindex="-1"
64
- >
65
- <header v-if="title || slots.header" class="wp-drawer__head">
66
- <h3 v-if="title" :id="titleId" class="wp-drawer__title">{{ title }}</h3>
67
- <slot name="header" />
68
- <span class="wp-spacer" />
69
- <CloseButton kbd="Esc" data-test="drawer-close" @click="close" />
70
- </header>
71
- <div class="wp-drawer__body">
72
- <slot />
73
- </div>
74
- <footer v-if="slots.footer" class="wp-drawer__foot">
75
- <slot name="footer" />
76
- </footer>
77
- </aside>
78
- </Transition>
79
- </Teleport>
80
- </template>
81
-
82
- <style>
83
- .wp-drawer__backdrop {
84
- position: fixed;
85
- inset: 0;
86
- z-index: 9000;
87
- background: var(--wp-overlay-bg);
88
- }
89
- .wp-drawer {
90
- position: fixed;
91
- top: 0;
92
- bottom: 0;
93
- z-index: 9001;
94
- display: flex;
95
- flex-direction: column;
96
- background: var(--wp-bg-1);
97
- color: var(--wp-text);
98
- box-shadow: var(--wp-shadow-xl);
99
- outline: none;
100
- }
101
- .wp-drawer--right { right: 0; border-left: 1px solid var(--wp-border); }
102
- .wp-drawer--left { left: 0; border-right: 1px solid var(--wp-border); }
103
- .wp-drawer__head {
104
- display: flex;
105
- align-items: center;
106
- gap: var(--wp-space-3);
107
- min-height: 52px;
108
- padding: 0 var(--wp-space-4) 0 var(--wp-space-5);
109
- border-bottom: 1px solid var(--wp-border);
110
- }
111
- .wp-drawer__title { margin: 0; font-size: var(--wp-text-md); font-weight: 600; }
112
- .wp-drawer__body { flex: 1; min-height: 0; overflow: auto; padding: var(--wp-space-5); }
113
- .wp-drawer__foot {
114
- display: flex;
115
- justify-content: flex-end;
116
- gap: var(--wp-space-3);
117
- padding: var(--wp-space-4) var(--wp-space-5);
118
- border-top: 1px solid var(--wp-border);
119
- }
120
- .wp-drawer-right-enter-active, .wp-drawer-right-leave-active,
121
- .wp-drawer-left-enter-active, .wp-drawer-left-leave-active { transition: transform 180ms ease-out; }
122
- .wp-drawer-right-enter-from, .wp-drawer-right-leave-to { transform: translateX(100%); }
123
- .wp-drawer-left-enter-from, .wp-drawer-left-leave-to { transform: translateX(-100%); }
124
- .wp-drawer-fade-enter-active, .wp-drawer-fade-leave-active { transition: opacity 180ms ease-out; }
125
- .wp-drawer-fade-enter-from, .wp-drawer-fade-leave-to { opacity: 0; }
126
- @media (prefers-reduced-motion: reduce) {
127
- .wp-drawer-right-enter-active, .wp-drawer-right-leave-active,
128
- .wp-drawer-left-enter-active, .wp-drawer-left-leave-active,
129
- .wp-drawer-fade-enter-active, .wp-drawer-fade-leave-active { transition: none; }
130
- }
131
- </style>
1
+ <script setup lang="ts">
2
+ /**
3
+ * A panel that slides in from the side of the page: inspectors, history,
4
+ * settings that sit beside the work instead of over it. Escape closes it,
5
+ * Tab stays inside it and focus goes back where it was. With `modal` off
6
+ * there is no backdrop, Tab moves on to the page and the page stays usable.
7
+ */
8
+ import CloseButton from "./CloseButton.vue";
9
+ import { ref, useId, useSlots } from "vue";
10
+ import { useDialogFocus } from "./useDialogFocus";
11
+
12
+ interface Props {
13
+ open: boolean;
14
+ title?: string | undefined;
15
+ side?: "right" | "left" | undefined;
16
+ /** Any CSS width; capped at the viewport. */
17
+ width?: string | undefined;
18
+ /** Dim the page and close on a click outside. */
19
+ modal?: boolean | undefined;
20
+ }
21
+ const props = withDefaults(defineProps<Props>(), { side: "right", width: "420px", modal: true });
22
+ const emit = defineEmits<{ (e: "update:open", v: boolean): void }>();
23
+
24
+ const slots = useSlots();
25
+ const panel = ref<HTMLElement | null>(null);
26
+ const titleId = `wp-drawer-title-${useId()}`;
27
+
28
+ function close(): void {
29
+ emit("update:open", false);
30
+ }
31
+
32
+ // The backdrop is a click target only; keyboard users close with Escape.
33
+ function bindBackdrop(el: unknown): void {
34
+ if (el instanceof HTMLElement) el.onmousedown = (ev) => { if (ev.target === el) close(); };
35
+ }
36
+
37
+ useDialogFocus(panel, () => props.open, {
38
+ onEscape: close,
39
+ trap: () => props.modal,
40
+ // The first control in the body, not the close button.
41
+ initial: (root, els) => {
42
+ const body = root.querySelector(".wp-drawer__body");
43
+ return els.find((el) => body?.contains(el)) ?? root;
44
+ },
45
+ });
46
+ </script>
47
+
48
+ <template>
49
+ <Teleport to="body">
50
+ <Transition name="wp-drawer-fade">
51
+ <div v-if="open && modal" :ref="bindBackdrop" class="wp-drawer__backdrop" data-test="drawer-backdrop" />
52
+ </Transition>
53
+ <Transition :name="`wp-drawer-${side}`">
54
+ <aside
55
+ v-if="open"
56
+ ref="panel"
57
+ class="wp-drawer"
58
+ :class="`wp-drawer--${side}`"
59
+ :style="{ width: `min(${width}, 100vw)` }"
60
+ role="dialog"
61
+ :aria-modal="modal || undefined"
62
+ :aria-labelledby="title ? titleId : undefined"
63
+ tabindex="-1"
64
+ >
65
+ <header v-if="title || slots.header" class="wp-drawer__head">
66
+ <h3 v-if="title" :id="titleId" class="wp-drawer__title">{{ title }}</h3>
67
+ <slot name="header" />
68
+ <span class="wp-spacer" />
69
+ <CloseButton kbd="Esc" data-test="drawer-close" @click="close" />
70
+ </header>
71
+ <div class="wp-drawer__body">
72
+ <slot />
73
+ </div>
74
+ <footer v-if="slots.footer" class="wp-drawer__foot">
75
+ <slot name="footer" />
76
+ </footer>
77
+ </aside>
78
+ </Transition>
79
+ </Teleport>
80
+ </template>
81
+
82
+ <style>
83
+ .wp-drawer__backdrop {
84
+ position: fixed;
85
+ inset: 0;
86
+ z-index: 9000;
87
+ background: var(--wp-overlay-bg);
88
+ }
89
+ .wp-drawer {
90
+ position: fixed;
91
+ top: 0;
92
+ bottom: 0;
93
+ z-index: 9001;
94
+ display: flex;
95
+ flex-direction: column;
96
+ background: var(--wp-bg-1);
97
+ color: var(--wp-text);
98
+ box-shadow: var(--wp-shadow-xl);
99
+ outline: none;
100
+ }
101
+ .wp-drawer--right { right: 0; border-left: 1px solid var(--wp-border); }
102
+ .wp-drawer--left { left: 0; border-right: 1px solid var(--wp-border); }
103
+ .wp-drawer__head {
104
+ display: flex;
105
+ align-items: center;
106
+ gap: var(--wp-space-3);
107
+ min-height: 52px;
108
+ padding: 0 var(--wp-space-4) 0 var(--wp-space-5);
109
+ border-bottom: 1px solid var(--wp-border);
110
+ }
111
+ .wp-drawer__title { margin: 0; font-size: var(--wp-text-md); font-weight: 600; }
112
+ .wp-drawer__body { flex: 1; min-height: 0; overflow: auto; padding: var(--wp-space-5); }
113
+ .wp-drawer__foot {
114
+ display: flex;
115
+ justify-content: flex-end;
116
+ gap: var(--wp-space-3);
117
+ padding: var(--wp-space-4) var(--wp-space-5);
118
+ border-top: 1px solid var(--wp-border);
119
+ }
120
+ .wp-drawer-right-enter-active, .wp-drawer-right-leave-active,
121
+ .wp-drawer-left-enter-active, .wp-drawer-left-leave-active { transition: transform 180ms ease-out; }
122
+ .wp-drawer-right-enter-from, .wp-drawer-right-leave-to { transform: translateX(100%); }
123
+ .wp-drawer-left-enter-from, .wp-drawer-left-leave-to { transform: translateX(-100%); }
124
+ .wp-drawer-fade-enter-active, .wp-drawer-fade-leave-active { transition: opacity 180ms ease-out; }
125
+ .wp-drawer-fade-enter-from, .wp-drawer-fade-leave-to { opacity: 0; }
126
+ @media (prefers-reduced-motion: reduce) {
127
+ .wp-drawer-right-enter-active, .wp-drawer-right-leave-active,
128
+ .wp-drawer-left-enter-active, .wp-drawer-left-leave-active,
129
+ .wp-drawer-fade-enter-active, .wp-drawer-fade-leave-active { transition: none; }
130
+ }
131
+ </style>
package/src/DropZone.vue CHANGED
@@ -1,196 +1,196 @@
1
- <script setup lang="ts">
2
- /**
3
- * Drop zone: where files go, by dragging them in or with Browse. It lights
4
- * up when a file it takes is dragged over, turns red with the reason when
5
- * it won't take it, and says what it takes. Works on the canvas and in the
6
- * manager.
7
- *
8
- * <DropZone accept=".txt,.yaml,.yml,.zip" multiple hint="Wildcard files or a zip" @files="load" />
9
- * <DropZone overlay accept="image/*" label="Drop images to attach" @files="attach"><ChatThread /></DropZone>
10
- *
11
- * `overlay` wraps content (a chat, a panel) and only shows while a file is
12
- * dragged over it. Files the zone can't take (wrong type, over `max-size`,
13
- * more than one without `multiple`) come back on `reject` with a reason,
14
- * and the rest on `files`. Browse keeps it usable without a mouse.
15
- */
16
- import { computed, ref } from "vue";
17
- import { dragItemMaybeAccepted, fileAccepted, formatBytes } from "./drop-zone";
18
- import WpButton from "./WpButton.vue";
19
-
20
- export interface RejectedFile { file: File; reason: string }
21
-
22
- const props = withDefaults(defineProps<{
23
- /** Like the file input's accept: "image/*,.txt". Empty takes anything. */
24
- accept?: string | undefined;
25
- multiple?: boolean | undefined;
26
- /** Largest file in bytes. */
27
- maxSize?: number | undefined;
28
- disabled?: boolean | undefined;
29
- /** Wrap the slot and show only while a file is dragged over it. */
30
- overlay?: boolean | undefined;
31
- /** The main line. */
32
- label?: string | undefined;
33
- /** The line under it, e.g. what it takes. */
34
- hint?: string | undefined;
35
- /** Words for the Browse button. */
36
- browseLabel?: string | undefined;
37
- size?: "sm" | "md" | undefined;
38
- }>(), {
39
- accept: undefined, maxSize: undefined, label: undefined, hint: undefined,
40
- browseLabel: "Browse", size: "md",
41
- });
42
-
43
- const emit = defineEmits<{ (e: "files", files: File[]): void; (e: "reject", files: RejectedFile[]): void }>();
44
-
45
- const inputEl = ref<HTMLInputElement | null>(null);
46
- const over = ref<"none" | "ok" | "refuse">("none");
47
- const refuseWhy = ref("");
48
- let depth = 0;
49
-
50
- const mainLabel = computed(() => props.label ?? (props.multiple ? "Drop files here" : "Drop a file here"));
51
-
52
- function hasFiles(ev: DragEvent): boolean {
53
- return !!ev.dataTransfer && [...ev.dataTransfer.types].includes("Files");
54
- }
55
-
56
- function judge(ev: DragEvent): void {
57
- const items = [...(ev.dataTransfer?.items ?? [])].filter((i) => i.kind === "file");
58
- if (!props.multiple && items.length > 1) { over.value = "refuse"; refuseWhy.value = "One file at a time"; return; }
59
- if (items.some((i) => !dragItemMaybeAccepted(i.type, props.accept))) {
60
- over.value = "refuse";
61
- refuseWhy.value = props.hint ? `Only ${props.hint}` : "This kind of file isn't taken here";
62
- return;
63
- }
64
- over.value = "ok";
65
- }
66
-
67
- function onDragEnter(ev: DragEvent): void {
68
- if (props.disabled || !hasFiles(ev)) return;
69
- ev.preventDefault();
70
- depth++;
71
- judge(ev);
72
- }
73
-
74
- function onDragOver(ev: DragEvent): void {
75
- if (props.disabled || !hasFiles(ev)) return;
76
- ev.preventDefault();
77
- if (ev.dataTransfer) ev.dataTransfer.dropEffect = over.value === "refuse" ? "none" : "copy";
78
- }
79
-
80
- function onDragLeave(ev: DragEvent): void {
81
- if (props.disabled || !hasFiles(ev)) return;
82
- depth = Math.max(0, depth - 1);
83
- if (!depth) over.value = "none";
84
- }
85
-
86
- function take(list: File[]): void {
87
- const ok: File[] = [];
88
- const bad: RejectedFile[] = [];
89
- for (const f of list) {
90
- if (!fileAccepted(f, props.accept)) bad.push({ file: f, reason: "Not a file this takes" });
91
- else if (props.maxSize !== undefined && f.size > props.maxSize) bad.push({ file: f, reason: `Over ${formatBytes(props.maxSize)}` });
92
- else if (!props.multiple && ok.length) bad.push({ file: f, reason: "One file at a time" });
93
- else ok.push(f);
94
- }
95
- if (ok.length) emit("files", ok);
96
- if (bad.length) emit("reject", bad);
97
- }
98
-
99
- function onDrop(ev: DragEvent): void {
100
- if (props.disabled || !hasFiles(ev)) return;
101
- ev.preventDefault();
102
- depth = 0;
103
- over.value = "none";
104
- take([...(ev.dataTransfer?.files ?? [])]);
105
- }
106
-
107
- function browse(): void {
108
- if (!props.disabled) inputEl.value?.click();
109
- }
110
-
111
- function onPicked(ev: Event): void {
112
- const el = ev.target as HTMLInputElement;
113
- take([...(el.files ?? [])]);
114
- el.value = "";
115
- }
116
-
117
- defineExpose({ browse });
118
- </script>
119
-
120
- <!-- A drop target: Browse is the keyboard and screen reader way in. -->
121
- <!-- eslint-disable vuejs-accessibility/no-static-element-interactions -->
122
- <template>
123
- <div
124
- v-if="overlay"
125
- class="wp-drop-wrap"
126
- :data-over="over === 'none' ? undefined : over"
127
- @dragenter="onDragEnter"
128
- @dragover="onDragOver"
129
- @dragleave="onDragLeave"
130
- @drop="onDrop"
131
- >
132
- <slot />
133
- <div v-if="over !== 'none'" class="wp-drop wp-drop--overlay" :data-over="over" :data-size="size" aria-hidden="true">
134
- <i class="pi wp-drop__icon" :class="over === 'refuse' ? 'pi-ban' : 'pi-upload'" />
135
- <span class="wp-drop__label">{{ over === "refuse" ? refuseWhy : mainLabel }}</span>
136
- </div>
137
- <input ref="inputEl" class="wp-drop__input" type="file" :accept="accept" :multiple="multiple" tabindex="-1" aria-hidden="true" @change="onPicked">
138
- </div>
139
- <div
140
- v-else
141
- class="wp-drop"
142
- :data-over="over === 'none' ? undefined : over"
143
- :data-size="size"
144
- :data-disabled="disabled || undefined"
145
- @dragenter="onDragEnter"
146
- @dragover="onDragOver"
147
- @dragleave="onDragLeave"
148
- @drop="onDrop"
149
- >
150
- <i class="pi wp-drop__icon" :class="over === 'refuse' ? 'pi-ban' : 'pi-upload'" aria-hidden="true" />
151
- <span class="wp-drop__text">
152
- <span class="wp-drop__label">{{ over === "refuse" ? refuseWhy : mainLabel }}</span>
153
- <span v-if="hint && over !== 'refuse'" class="wp-drop__hint">{{ hint }}{{ maxSize ? ` · up to ${formatBytes(maxSize)}` : "" }}</span>
154
- </span>
155
- <WpButton class="wp-drop__browse" variant="secondary" size="sm" icon="pi-folder-open" :disabled="disabled" @click="browse">{{ browseLabel }}</WpButton>
156
- <input ref="inputEl" class="wp-drop__input" type="file" :accept="accept" :multiple="multiple" tabindex="-1" aria-hidden="true" @change="onPicked">
157
- </div>
158
- </template>
159
-
160
- <style scoped>
161
- .wp-drop {
162
- --_tone: var(--wp-border-strong);
163
- display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--wp-space-3);
164
- box-sizing: border-box; min-width: 0; padding: var(--wp-space-7) var(--wp-space-5);
165
- border: 1.5px dashed var(--_tone); border-radius: var(--wp-radius-md);
166
- background: var(--wp-bg-1); color: var(--wp-text-muted); text-align: center;
167
- font: 400 var(--wp-text-sm)/1.4 var(--wp-font-sans);
168
- transition: border-color var(--wp-motion-hover), background var(--wp-motion-hover);
169
- }
170
- .wp-drop[data-size="sm"] {
171
- flex-direction: row; justify-content: flex-start; gap: var(--wp-space-4); text-align: left;
172
- padding: var(--wp-space-3) var(--wp-space-4); border-radius: var(--wp-radius); font-size: var(--wp-text-xs);
173
- }
174
- .wp-drop[data-over="ok"] { --_tone: var(--wp-accent); background: color-mix(in srgb, var(--wp-accent) 8%, var(--wp-bg-1)); color: var(--wp-text); }
175
- .wp-drop[data-over="refuse"] { --_tone: var(--wp-danger); background: color-mix(in srgb, var(--wp-danger) 8%, var(--wp-bg-1)); color: var(--wp-danger-text); }
176
- .wp-drop[data-disabled] { opacity: 0.45; }
177
- .wp-drop__icon { font-size: 20px; color: var(--_tone); }
178
- .wp-drop[data-over="ok"] .wp-drop__icon { color: var(--wp-accent-text); }
179
- .wp-drop[data-size="sm"] .wp-drop__icon { font-size: 14px; }
180
- .wp-drop__text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
181
- .wp-drop[data-size="sm"] .wp-drop__text { flex: 1; }
182
- .wp-drop__label { color: inherit; font-weight: 500; }
183
- .wp-drop:not([data-over]) .wp-drop__label { color: var(--wp-text); }
184
- .wp-drop__hint { color: var(--wp-text-dim); font-size: var(--wp-text-xs); }
185
- .wp-drop__browse { flex: none; }
186
- .wp-drop__input { display: none; }
187
- .wp-drop-wrap { position: relative; min-width: 0; min-height: 0; }
188
- .wp-drop--overlay {
189
- position: absolute; inset: var(--wp-space-2); z-index: 5; pointer-events: none;
190
- background: color-mix(in srgb, var(--wp-bg) 82%, transparent); backdrop-filter: blur(2px);
191
- }
192
- .wp-drop--overlay[data-over="ok"] { background: color-mix(in srgb, var(--wp-accent) 12%, var(--wp-bg)); }
193
- @media (prefers-reduced-motion: reduce) {
194
- .wp-drop { transition: none; }
195
- }
196
- </style>
1
+ <script setup lang="ts">
2
+ /**
3
+ * Drop zone: where files go, by dragging them in or with Browse. It lights
4
+ * up when a file it takes is dragged over, turns red with the reason when
5
+ * it won't take it, and says what it takes. Works on the canvas and in the
6
+ * manager.
7
+ *
8
+ * <DropZone accept=".txt,.yaml,.yml,.zip" multiple hint="Wildcard files or a zip" @files="load" />
9
+ * <DropZone overlay accept="image/*" label="Drop images to attach" @files="attach"><ChatThread /></DropZone>
10
+ *
11
+ * `overlay` wraps content (a chat, a panel) and only shows while a file is
12
+ * dragged over it. Files the zone can't take (wrong type, over `max-size`,
13
+ * more than one without `multiple`) come back on `reject` with a reason,
14
+ * and the rest on `files`. Browse keeps it usable without a mouse.
15
+ */
16
+ import { computed, ref } from "vue";
17
+ import { dragItemMaybeAccepted, fileAccepted, formatBytes } from "./drop-zone";
18
+ import WpButton from "./WpButton.vue";
19
+
20
+ export interface RejectedFile { file: File; reason: string }
21
+
22
+ const props = withDefaults(defineProps<{
23
+ /** Like the file input's accept: "image/*,.txt". Empty takes anything. */
24
+ accept?: string | undefined;
25
+ multiple?: boolean | undefined;
26
+ /** Largest file in bytes. */
27
+ maxSize?: number | undefined;
28
+ disabled?: boolean | undefined;
29
+ /** Wrap the slot and show only while a file is dragged over it. */
30
+ overlay?: boolean | undefined;
31
+ /** The main line. */
32
+ label?: string | undefined;
33
+ /** The line under it, e.g. what it takes. */
34
+ hint?: string | undefined;
35
+ /** Words for the Browse button. */
36
+ browseLabel?: string | undefined;
37
+ size?: "sm" | "md" | undefined;
38
+ }>(), {
39
+ accept: undefined, maxSize: undefined, label: undefined, hint: undefined,
40
+ browseLabel: "Browse", size: "md",
41
+ });
42
+
43
+ const emit = defineEmits<{ (e: "files", files: File[]): void; (e: "reject", files: RejectedFile[]): void }>();
44
+
45
+ const inputEl = ref<HTMLInputElement | null>(null);
46
+ const over = ref<"none" | "ok" | "refuse">("none");
47
+ const refuseWhy = ref("");
48
+ let depth = 0;
49
+
50
+ const mainLabel = computed(() => props.label ?? (props.multiple ? "Drop files here" : "Drop a file here"));
51
+
52
+ function hasFiles(ev: DragEvent): boolean {
53
+ return !!ev.dataTransfer && [...ev.dataTransfer.types].includes("Files");
54
+ }
55
+
56
+ function judge(ev: DragEvent): void {
57
+ const items = [...(ev.dataTransfer?.items ?? [])].filter((i) => i.kind === "file");
58
+ if (!props.multiple && items.length > 1) { over.value = "refuse"; refuseWhy.value = "One file at a time"; return; }
59
+ if (items.some((i) => !dragItemMaybeAccepted(i.type, props.accept))) {
60
+ over.value = "refuse";
61
+ refuseWhy.value = props.hint ? `Only ${props.hint}` : "This kind of file isn't taken here";
62
+ return;
63
+ }
64
+ over.value = "ok";
65
+ }
66
+
67
+ function onDragEnter(ev: DragEvent): void {
68
+ if (props.disabled || !hasFiles(ev)) return;
69
+ ev.preventDefault();
70
+ depth++;
71
+ judge(ev);
72
+ }
73
+
74
+ function onDragOver(ev: DragEvent): void {
75
+ if (props.disabled || !hasFiles(ev)) return;
76
+ ev.preventDefault();
77
+ if (ev.dataTransfer) ev.dataTransfer.dropEffect = over.value === "refuse" ? "none" : "copy";
78
+ }
79
+
80
+ function onDragLeave(ev: DragEvent): void {
81
+ if (props.disabled || !hasFiles(ev)) return;
82
+ depth = Math.max(0, depth - 1);
83
+ if (!depth) over.value = "none";
84
+ }
85
+
86
+ function take(list: File[]): void {
87
+ const ok: File[] = [];
88
+ const bad: RejectedFile[] = [];
89
+ for (const f of list) {
90
+ if (!fileAccepted(f, props.accept)) bad.push({ file: f, reason: "Not a file this takes" });
91
+ else if (props.maxSize !== undefined && f.size > props.maxSize) bad.push({ file: f, reason: `Over ${formatBytes(props.maxSize)}` });
92
+ else if (!props.multiple && ok.length) bad.push({ file: f, reason: "One file at a time" });
93
+ else ok.push(f);
94
+ }
95
+ if (ok.length) emit("files", ok);
96
+ if (bad.length) emit("reject", bad);
97
+ }
98
+
99
+ function onDrop(ev: DragEvent): void {
100
+ if (props.disabled || !hasFiles(ev)) return;
101
+ ev.preventDefault();
102
+ depth = 0;
103
+ over.value = "none";
104
+ take([...(ev.dataTransfer?.files ?? [])]);
105
+ }
106
+
107
+ function browse(): void {
108
+ if (!props.disabled) inputEl.value?.click();
109
+ }
110
+
111
+ function onPicked(ev: Event): void {
112
+ const el = ev.target as HTMLInputElement;
113
+ take([...(el.files ?? [])]);
114
+ el.value = "";
115
+ }
116
+
117
+ defineExpose({ browse });
118
+ </script>
119
+
120
+ <!-- A drop target: Browse is the keyboard and screen reader way in. -->
121
+ <!-- eslint-disable vuejs-accessibility/no-static-element-interactions -->
122
+ <template>
123
+ <div
124
+ v-if="overlay"
125
+ class="wp-drop-wrap"
126
+ :data-over="over === 'none' ? undefined : over"
127
+ @dragenter="onDragEnter"
128
+ @dragover="onDragOver"
129
+ @dragleave="onDragLeave"
130
+ @drop="onDrop"
131
+ >
132
+ <slot />
133
+ <div v-if="over !== 'none'" class="wp-drop wp-drop--overlay" :data-over="over" :data-size="size" aria-hidden="true">
134
+ <i class="pi wp-drop__icon" :class="over === 'refuse' ? 'pi-ban' : 'pi-upload'" />
135
+ <span class="wp-drop__label">{{ over === "refuse" ? refuseWhy : mainLabel }}</span>
136
+ </div>
137
+ <input ref="inputEl" class="wp-drop__input" type="file" :accept="accept" :multiple="multiple" tabindex="-1" aria-hidden="true" @change="onPicked">
138
+ </div>
139
+ <div
140
+ v-else
141
+ class="wp-drop"
142
+ :data-over="over === 'none' ? undefined : over"
143
+ :data-size="size"
144
+ :data-disabled="disabled || undefined"
145
+ @dragenter="onDragEnter"
146
+ @dragover="onDragOver"
147
+ @dragleave="onDragLeave"
148
+ @drop="onDrop"
149
+ >
150
+ <i class="pi wp-drop__icon" :class="over === 'refuse' ? 'pi-ban' : 'pi-upload'" aria-hidden="true" />
151
+ <span class="wp-drop__text">
152
+ <span class="wp-drop__label">{{ over === "refuse" ? refuseWhy : mainLabel }}</span>
153
+ <span v-if="hint && over !== 'refuse'" class="wp-drop__hint">{{ hint }}{{ maxSize ? ` · up to ${formatBytes(maxSize)}` : "" }}</span>
154
+ </span>
155
+ <WpButton class="wp-drop__browse" variant="secondary" size="sm" icon="pi-folder-open" :disabled="disabled" @click="browse">{{ browseLabel }}</WpButton>
156
+ <input ref="inputEl" class="wp-drop__input" type="file" :accept="accept" :multiple="multiple" tabindex="-1" aria-hidden="true" @change="onPicked">
157
+ </div>
158
+ </template>
159
+
160
+ <style scoped>
161
+ .wp-drop {
162
+ --_tone: var(--wp-border-strong);
163
+ display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--wp-space-3);
164
+ box-sizing: border-box; min-width: 0; padding: var(--wp-space-7) var(--wp-space-5);
165
+ border: 1.5px dashed var(--_tone); border-radius: var(--wp-radius-md);
166
+ background: var(--wp-bg-1); color: var(--wp-text-muted); text-align: center;
167
+ font: 400 var(--wp-text-sm)/1.4 var(--wp-font-sans);
168
+ transition: border-color var(--wp-motion-hover), background var(--wp-motion-hover);
169
+ }
170
+ .wp-drop[data-size="sm"] {
171
+ flex-direction: row; justify-content: flex-start; gap: var(--wp-space-4); text-align: left;
172
+ padding: var(--wp-space-3) var(--wp-space-4); border-radius: var(--wp-radius); font-size: var(--wp-text-xs);
173
+ }
174
+ .wp-drop[data-over="ok"] { --_tone: var(--wp-accent); background: color-mix(in srgb, var(--wp-accent) 8%, var(--wp-bg-1)); color: var(--wp-text); }
175
+ .wp-drop[data-over="refuse"] { --_tone: var(--wp-danger); background: color-mix(in srgb, var(--wp-danger) 8%, var(--wp-bg-1)); color: var(--wp-danger-text); }
176
+ .wp-drop[data-disabled] { opacity: 0.45; }
177
+ .wp-drop__icon { font-size: 20px; color: var(--_tone); }
178
+ .wp-drop[data-over="ok"] .wp-drop__icon { color: var(--wp-accent-text); }
179
+ .wp-drop[data-size="sm"] .wp-drop__icon { font-size: 14px; }
180
+ .wp-drop__text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
181
+ .wp-drop[data-size="sm"] .wp-drop__text { flex: 1; }
182
+ .wp-drop__label { color: inherit; font-weight: 500; }
183
+ .wp-drop:not([data-over]) .wp-drop__label { color: var(--wp-text); }
184
+ .wp-drop__hint { color: var(--wp-text-dim); font-size: var(--wp-text-xs); }
185
+ .wp-drop__browse { flex: none; }
186
+ .wp-drop__input { display: none; }
187
+ .wp-drop-wrap { position: relative; min-width: 0; min-height: 0; }
188
+ .wp-drop--overlay {
189
+ position: absolute; inset: var(--wp-space-2); z-index: 5; pointer-events: none;
190
+ background: color-mix(in srgb, var(--wp-bg) 82%, transparent); backdrop-filter: blur(2px);
191
+ }
192
+ .wp-drop--overlay[data-over="ok"] { background: color-mix(in srgb, var(--wp-accent) 12%, var(--wp-bg)); }
193
+ @media (prefers-reduced-motion: reduce) {
194
+ .wp-drop { transition: none; }
195
+ }
196
+ </style>