@dumiflex/wp-kit 0.0.0-stage → 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.
Files changed (128) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/LICENSE +674 -0
  3. package/README.md +76 -3
  4. package/package.json +77 -6
  5. package/src/Avatar.vue +83 -0
  6. package/src/Badge.vue +97 -0
  7. package/src/Banner.vue +118 -0
  8. package/src/BookmarkButton.vue +27 -0
  9. package/src/BundleMeta.vue +51 -0
  10. package/src/Button.vue +103 -0
  11. package/src/Card.vue +68 -0
  12. package/src/Checkbox.vue +107 -0
  13. package/src/Chip.vue +97 -0
  14. package/src/ChipToggle.vue +92 -0
  15. package/src/ChoiceCards.vue +133 -0
  16. package/src/CloseButton.vue +69 -0
  17. package/src/CodeBlock.vue +119 -0
  18. package/src/ColorField.vue +206 -0
  19. package/src/ColorPicker.vue +201 -0
  20. package/src/Combobox.vue +334 -0
  21. package/src/ConfirmDialog.vue +163 -0
  22. package/src/ConfirmModal.vue +147 -0
  23. package/src/ConstraintMatrixPreview.vue +708 -0
  24. package/src/ContextMenu.vue +379 -0
  25. package/src/CopyButton.vue +156 -0
  26. package/src/DataTable.vue +292 -0
  27. package/src/DatePicker.vue +234 -0
  28. package/src/DescriptionList.vue +73 -0
  29. package/src/DiffView.vue +68 -0
  30. package/src/Disclosure.vue +166 -0
  31. package/src/DragHandle.vue +49 -0
  32. package/src/Drawer.vue +131 -0
  33. package/src/DropZone.vue +196 -0
  34. package/src/DropdownMenu.vue +72 -0
  35. package/src/EmptyState.vue +111 -0
  36. package/src/Field.vue +28 -0
  37. package/src/HsvPicker.vue +208 -0
  38. package/src/Icon.vue +39 -0
  39. package/src/IconPicker.vue +209 -0
  40. package/src/ImageGrid.vue +125 -0
  41. package/src/InlineEdit.vue +183 -0
  42. package/src/Input.vue +99 -0
  43. package/src/Kbd.vue +56 -0
  44. package/src/KindChip.vue +53 -0
  45. package/src/Lightbox.vue +173 -0
  46. package/src/ListFilter.vue +85 -0
  47. package/src/ListRow.vue +204 -0
  48. package/src/LoadingMark.vue +161 -0
  49. package/src/Logo.vue +60 -0
  50. package/src/Modal.vue +76 -0
  51. package/src/ModalShell.vue +107 -0
  52. package/src/Pagination.vue +106 -0
  53. package/src/Popover.vue +171 -0
  54. package/src/PostCard.vue +211 -0
  55. package/src/ProgressBar.vue +90 -0
  56. package/src/RadioGroup.vue +177 -0
  57. package/src/RangeSlider.vue +183 -0
  58. package/src/RatingBadge.vue +42 -0
  59. package/src/RelativeDate.vue +35 -0
  60. package/src/ResizeHandle.vue +143 -0
  61. package/src/SearchInput.vue +199 -0
  62. package/src/Select.vue +641 -0
  63. package/src/Separator.vue +54 -0
  64. package/src/Skeleton.vue +82 -0
  65. package/src/Slider.vue +198 -0
  66. package/src/SortableList.vue +260 -0
  67. package/src/SplitPane.vue +68 -0
  68. package/src/StarButton.vue +40 -0
  69. package/src/Stepper.vue +141 -0
  70. package/src/SupportLinks.vue +93 -0
  71. package/src/TagInput.vue +300 -0
  72. package/src/Textarea.vue +57 -0
  73. package/src/Timeline.vue +92 -0
  74. package/src/ToastCard.vue +119 -0
  75. package/src/ToastHost.vue +40 -0
  76. package/src/Toggle.vue +36 -0
  77. package/src/TokenizedValue.vue +310 -0
  78. package/src/Toolbar.vue +110 -0
  79. package/src/VirtualList.vue +87 -0
  80. package/src/VisuallyHidden.vue +29 -0
  81. package/src/WpButton.vue +75 -0
  82. package/src/WpCheck.vue +102 -0
  83. package/src/WpIconButton.vue +107 -0
  84. package/src/WpImageZoom.vue +544 -0
  85. package/src/WpNumber.vue +183 -0
  86. package/src/WpSegmented.vue +175 -0
  87. package/src/WpSelect.vue +89 -0
  88. package/src/WpSwitch.vue +163 -0
  89. package/src/WpTabs.vue +188 -0
  90. package/src/_modal-motion.css +33 -0
  91. package/src/_skeleton.css +50 -0
  92. package/src/axis-color.ts +42 -0
  93. package/src/button.css +169 -0
  94. package/src/calendar.ts +59 -0
  95. package/src/clipboard.ts +50 -0
  96. package/src/colour-presets.ts +12 -0
  97. package/src/diff.ts +51 -0
  98. package/src/drop-zone.ts +41 -0
  99. package/src/flip.ts +237 -0
  100. package/src/floating.ts +99 -0
  101. package/src/hsv.ts +100 -0
  102. package/src/icon-catalog.ts +52 -0
  103. package/src/image-zoom.ts +93 -0
  104. package/src/logo-paths.ts +21 -0
  105. package/src/matrix-axis.ts +91 -0
  106. package/src/pagination.ts +27 -0
  107. package/src/platform-keys.ts +29 -0
  108. package/src/rich-tokenize.ts +357 -0
  109. package/src/select-types.ts +32 -0
  110. package/src/slider-math.ts +45 -0
  111. package/src/sortable.ts +22 -0
  112. package/src/theme/fonts/OFL-Fraunces.txt +93 -0
  113. package/src/theme/fonts/OFL-JetBrainsMono.txt +93 -0
  114. package/src/theme/fonts/OFL-Manrope.txt +93 -0
  115. package/src/theme/fonts/fraunces-latin-wght-normal.woff2 +0 -0
  116. package/src/theme/fonts/jetbrains-mono-latin.woff2 +0 -0
  117. package/src/theme/fonts/manrope-latin-wght-normal.woff2 +0 -0
  118. package/src/theme/looks.css +428 -0
  119. package/src/theme/palettes.css +254 -0
  120. package/src/theme/tokens.css +2894 -0
  121. package/src/tokens.ts +56 -0
  122. package/src/tooltip.css +43 -0
  123. package/src/tooltip.ts +146 -0
  124. package/src/useDialogFocus.ts +73 -0
  125. package/src/useGrowableField.ts +357 -0
  126. package/src/useModalKeyShield.ts +116 -0
  127. package/src/useToast.ts +48 -0
  128. package/src/wp-logo.svg +14 -0
@@ -0,0 +1,166 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * Disclosure: a "show more" section that folds open under its heading. The
4
+ * heading is a button with `aria-expanded` + `aria-controls` (APG
5
+ * disclosure), the chevron turns when open, and the body grows to its height
6
+ * (no motion with `prefers-reduced-motion`). Works on the canvas and in the
7
+ * manager.
8
+ *
9
+ * <Disclosure v-model:open="open" label="Advanced">…</Disclosure>
10
+ * <Disclosure label="Rules" :count="3"><template #summary>…</template>…</Disclosure>
11
+ *
12
+ * Leave out `v-model:open` and it keeps its own state (`default-open` sets
13
+ * where it starts). `#actions` sits at the right end of the heading, outside
14
+ * the button, for controls that should not fold the section; `#lead` sits
15
+ * before the button for the same (a "select all" checkbox lined up with
16
+ * the rows' own). `lazy` builds
17
+ * the body only while it is open. `flush` makes the heading a full-width
18
+ * band with a rule under it and drops the body's indent, for a section of a
19
+ * table or list that fills its card (a group of options, a rule).
20
+ */
21
+ import { computed, ref, useId, useSlots } from "vue";
22
+
23
+ const props = withDefaults(defineProps<{
24
+ open?: boolean | undefined;
25
+ /** Where it starts when nobody binds `open`. */
26
+ defaultOpen?: boolean | undefined;
27
+ label?: string | undefined;
28
+ /** A small count after the label. */
29
+ count?: number | string | undefined;
30
+ size?: "sm" | "md" | undefined;
31
+ disabled?: boolean | undefined;
32
+ /** Draw a border and a padded body, for a section that stands alone. */
33
+ boxed?: boolean | undefined;
34
+ /** Full-width heading band, body without indent: a section of a table or list. */
35
+ flush?: boolean | undefined;
36
+ /** Build the body only while open (a long list, a costly panel). */
37
+ lazy?: boolean | undefined;
38
+ /** Extra attributes on the heading button, e.g. `data-test`. */
39
+ triggerAttrs?: Record<string, string> | undefined;
40
+ }>(), { open: undefined, size: "md", triggerAttrs: undefined });
41
+
42
+ const emit = defineEmits<{ (e: "update:open", v: boolean): void }>();
43
+ const slots = useSlots();
44
+
45
+ const own = ref(props.defaultOpen);
46
+ const isOpen = computed(() => props.open ?? own.value);
47
+ const bodyId = `wp-disclosure-${useId()}`;
48
+
49
+ function toggle(): void {
50
+ if (props.disabled) return;
51
+ const next = !isOpen.value;
52
+ own.value = next;
53
+ emit("update:open", next);
54
+ }
55
+ </script>
56
+
57
+ <template>
58
+ <div class="wp-disclosure" :data-size="size" :data-open="isOpen" :data-boxed="boxed || undefined" :data-flush="flush || undefined">
59
+ <div class="wp-disclosure__head">
60
+ <div v-if="slots.lead" class="wp-disclosure__lead"><slot name="lead" /></div>
61
+ <button
62
+ v-bind="triggerAttrs ?? {}"
63
+ type="button"
64
+ class="wp-disclosure__trigger"
65
+ :aria-expanded="isOpen"
66
+ :aria-controls="bodyId"
67
+ :disabled="disabled"
68
+ @click="toggle"
69
+ >
70
+ <i class="pi pi-chevron-right wp-disclosure__chev" aria-hidden="true" />
71
+ <slot name="summary">
72
+ <span class="wp-disclosure__label">{{ label }}</span>
73
+ </slot>
74
+ <span v-if="count !== undefined" class="wp-disclosure__count">{{ count }}</span>
75
+ </button>
76
+ <div v-if="slots.actions" class="wp-disclosure__actions"><slot name="actions" /></div>
77
+ </div>
78
+ <div :id="bodyId" class="wp-disclosure__body" :inert="!isOpen || undefined">
79
+ <div class="wp-disclosure__inner">
80
+ <div v-if="isOpen || !lazy" class="wp-disclosure__content"><slot /></div>
81
+ </div>
82
+ </div>
83
+ </div>
84
+ </template>
85
+
86
+ <style scoped>
87
+ .wp-disclosure { min-width: 0; }
88
+ .wp-disclosure[data-boxed] {
89
+ border: 1px solid var(--wp-border);
90
+ border-radius: var(--wp-radius-md);
91
+ background: var(--wp-bg-1);
92
+ }
93
+ .wp-disclosure__head { display: flex; align-items: center; gap: var(--wp-space-3); min-width: 0; }
94
+ .wp-disclosure[data-boxed] .wp-disclosure__head { padding: 0 var(--wp-space-3); }
95
+ .wp-disclosure__trigger {
96
+ display: inline-flex;
97
+ align-items: center;
98
+ gap: var(--wp-space-3);
99
+ flex: 1;
100
+ min-width: 0;
101
+ min-height: var(--wp-control-h-md);
102
+ margin: 0;
103
+ padding: 0 var(--wp-space-2);
104
+ border: 0;
105
+ border-radius: var(--wp-radius-sm);
106
+ background: transparent;
107
+ color: var(--wp-text);
108
+ font: 600 var(--wp-text-sm)/1.3 var(--wp-font-sans);
109
+ text-align: left;
110
+ cursor: pointer;
111
+ }
112
+ .wp-disclosure[data-size="sm"] .wp-disclosure__trigger {
113
+ min-height: var(--wp-control-h-sm);
114
+ font-size: var(--wp-text-xs);
115
+ gap: var(--wp-space-2);
116
+ }
117
+ .wp-disclosure__trigger:hover:not(:disabled) { background: var(--wp-row-hover); }
118
+ .wp-disclosure__trigger:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--wp-border-focus); }
119
+ .wp-disclosure__trigger:disabled { opacity: 0.45; cursor: not-allowed; }
120
+ .wp-disclosure__label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
121
+ .wp-disclosure__chev {
122
+ flex: none;
123
+ font-size: 0.75em;
124
+ color: var(--wp-text-muted);
125
+ transition: transform var(--wp-motion-swap) var(--wp-motion-curve-collapse);
126
+ }
127
+ .wp-disclosure[data-open="true"] .wp-disclosure__chev { transform: rotate(90deg); }
128
+ .wp-disclosure__count {
129
+ padding: 1px 6px;
130
+ border-radius: var(--wp-radius-cap, 999px);
131
+ background: var(--wp-bg-3);
132
+ color: var(--wp-text-muted);
133
+ font-size: var(--wp-text-xs);
134
+ font-weight: 500;
135
+ font-variant-numeric: tabular-nums;
136
+ }
137
+ .wp-disclosure__lead { display: inline-flex; align-items: center; flex: none; }
138
+ .wp-disclosure__actions { display: inline-flex; align-items: center; gap: var(--wp-space-2); flex: none; }
139
+ /* Grid rows animate from 0fr to 1fr, so the body grows to its own height
140
+ without measuring it. */
141
+ .wp-disclosure__body {
142
+ display: grid;
143
+ grid-template-rows: 0fr;
144
+ transition: grid-template-rows var(--wp-motion-swap) var(--wp-motion-curve-collapse);
145
+ }
146
+ .wp-disclosure[data-open="true"] > .wp-disclosure__body { grid-template-rows: 1fr; }
147
+ .wp-disclosure__inner { min-height: 0; overflow: hidden; }
148
+ .wp-disclosure__content { padding: var(--wp-space-3) 0 var(--wp-space-2) calc(var(--wp-space-2) + 1.1em + var(--wp-space-3)); }
149
+ .wp-disclosure[data-size="sm"] .wp-disclosure__content { padding: var(--wp-space-2) 0 var(--wp-space-2) calc(var(--wp-space-2) + 1em); }
150
+ .wp-disclosure[data-boxed] .wp-disclosure__content { padding: var(--wp-space-2) var(--wp-space-5) var(--wp-space-5); }
151
+ .wp-disclosure[data-flush] > .wp-disclosure__head {
152
+ border-bottom: 1px solid var(--wp-border);
153
+ background: var(--wp-bg-2);
154
+ }
155
+ .wp-disclosure[data-flush] .wp-disclosure__trigger {
156
+ padding: 0 var(--wp-space-5);
157
+ border-radius: 0;
158
+ font-size: var(--wp-text-xs);
159
+ }
160
+ .wp-disclosure[data-flush] .wp-disclosure__trigger:focus-visible { box-shadow: inset 0 0 0 2px var(--wp-border-focus); }
161
+ .wp-disclosure[data-flush] .wp-disclosure__content,
162
+ .wp-disclosure[data-flush][data-size="sm"] .wp-disclosure__content { padding: 0; }
163
+ @media (prefers-reduced-motion: reduce) {
164
+ .wp-disclosure__chev, .wp-disclosure__body { transition: none; }
165
+ }
166
+ </style>
@@ -0,0 +1,49 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * The six-dot grip on a row you drag to reorder (a module row, a bundle
4
+ * child, an injector binding). It only shows where to grab: the row owns the
5
+ * drag (HTML drag-and-drop or pointer events), so the grip is hidden from
6
+ * screen readers and the row gives keyboard users their own way to move it.
7
+ * For a list that the kit should reorder itself, with keyboard moves, use
8
+ * SortableList.
9
+ *
10
+ * <DragHandle /> the canvas row size
11
+ * <DragHandle size="md" /> a manager row
12
+ *
13
+ * Reads only the shared tokens, so it works on the canvas and on a page.
14
+ */
15
+ withDefaults(defineProps<{
16
+ size?: "sm" | "md" | undefined;
17
+ /** Tooltip on hover. */
18
+ title?: string | undefined;
19
+ }>(), { size: "sm", title: "Drag to reorder" });
20
+ </script>
21
+
22
+ <template>
23
+ <span class="wp-drag-grip" :data-size="size" :title="title" aria-hidden="true">
24
+ <svg viewBox="0 0 6 12" fill="currentColor" focusable="false">
25
+ <circle cx="1.5" cy="2" r="1" />
26
+ <circle cx="4.5" cy="2" r="1" />
27
+ <circle cx="1.5" cy="6" r="1" />
28
+ <circle cx="4.5" cy="6" r="1" />
29
+ <circle cx="1.5" cy="10" r="1" />
30
+ <circle cx="4.5" cy="10" r="1" />
31
+ </svg>
32
+ </span>
33
+ </template>
34
+
35
+ <style scoped>
36
+ .wp-drag-grip {
37
+ display: inline-grid;
38
+ place-items: center;
39
+ flex: none;
40
+ width: 12px;
41
+ height: 20px;
42
+ color: var(--wp-text-dim);
43
+ cursor: grab;
44
+ }
45
+ .wp-drag-grip[data-size="md"] { width: 16px; height: 24px; }
46
+ .wp-drag-grip svg { width: 6px; height: 12px; }
47
+ .wp-drag-grip[data-size="md"] svg { width: 8px; height: 16px; }
48
+ .wp-drag-grip:hover { color: var(--wp-text-muted); }
49
+ </style>
package/src/Drawer.vue ADDED
@@ -0,0 +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>
@@ -0,0 +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>
@@ -0,0 +1,72 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * A list of actions behind a button: the "⋯" menu, "More", "Add…". The
4
+ * trigger toggles it; arrows, Home/End and Enter work; Escape or a pick
5
+ * closes it and focus goes back to the trigger. Items are ContextMenu
6
+ * entries (sections, icons, dividers, danger, subtitles), so a row's
7
+ * right-click menu and its ⋯ menu can share one list.
8
+ *
9
+ * <DropdownMenu :items="rowActions" label="Hair colour actions">
10
+ * <template #trigger="{ props }"><Button v-bind="props" variant="ghost" icon="pi-ellipsis-h" aria-label="More" /></template>
11
+ * </DropdownMenu>
12
+ */
13
+ import { computed, ref } from "vue";
14
+ import ContextMenu, { type ContextMenuEntry, type ContextMenuHeader } from "./ContextMenu.vue";
15
+ import type { Placement } from "./floating";
16
+
17
+ const props = withDefaults(defineProps<{
18
+ items: ContextMenuEntry[];
19
+ header?: ContextMenuHeader | undefined;
20
+ placement?: Placement | undefined;
21
+ /** Names the menu for screen readers when the trigger is an icon. */
22
+ label?: string | undefined;
23
+ }>(), { placement: "bottom-start" });
24
+
25
+ const open = ref(false);
26
+ const anchorEl = ref<HTMLElement | null>(null);
27
+
28
+ function trigger(): HTMLElement | null {
29
+ return (anchorEl.value?.firstElementChild as HTMLElement | null) ?? anchorEl.value;
30
+ }
31
+
32
+ function close(): void {
33
+ if (!open.value) return;
34
+ open.value = false;
35
+ trigger()?.focus();
36
+ }
37
+
38
+ const triggerProps = computed(() => ({
39
+ "aria-haspopup": "menu" as const,
40
+ "aria-expanded": open.value,
41
+ "aria-label": props.label,
42
+ onClick: () => { open.value = !open.value; },
43
+ onKeydown: (ev: KeyboardEvent) => {
44
+ if ((ev.key === "ArrowDown" || ev.key === "ArrowUp") && !open.value) {
45
+ ev.preventDefault();
46
+ open.value = true;
47
+ }
48
+ },
49
+ }));
50
+ </script>
51
+
52
+ <template>
53
+ <span ref="anchorEl" class="wp-dropdown-anchor">
54
+ <slot name="trigger" :props="triggerProps" :open="open" />
55
+ </span>
56
+ <ContextMenu
57
+ :visible="open"
58
+ :x="0"
59
+ :y="0"
60
+ :anchor="trigger()"
61
+ :placement="placement"
62
+ :items="items"
63
+ :header="header"
64
+ @close="close"
65
+ />
66
+ </template>
67
+
68
+ <style>
69
+ @layer wp-extension {
70
+ .wp-dropdown-anchor { display: inline-flex; }
71
+ }
72
+ </style>