@dumiflex/wp-kit 0.3.0 → 0.5.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 (130) hide show
  1. package/CHANGELOG.md +92 -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 +146 -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 +293 -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 +127 -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 +176 -173
  44. package/src/ListFilter.vue +85 -85
  45. package/src/ListRow.vue +204 -204
  46. package/src/LoadingMark.vue +217 -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 +98 -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/highlight.ts +199 -0
  101. package/src/hsv.ts +100 -100
  102. package/src/icon-catalog.ts +52 -52
  103. package/src/image-zoom.ts +93 -93
  104. package/src/kind-colour.ts +28 -0
  105. package/src/loading-mark-motion.ts +249 -0
  106. package/src/logo-paths.ts +21 -21
  107. package/src/matrix-axis.ts +91 -91
  108. package/src/pagination.ts +27 -27
  109. package/src/platform-keys.ts +29 -29
  110. package/src/rich-tokenize.ts +357 -357
  111. package/src/select-types.ts +32 -32
  112. package/src/slider-math.ts +45 -45
  113. package/src/sortable.ts +22 -22
  114. package/src/theme/fonts/OFL-Fraunces.txt +93 -93
  115. package/src/theme/fonts/OFL-JetBrainsMono.txt +93 -93
  116. package/src/theme/fonts/OFL-Manrope.txt +93 -93
  117. package/src/theme/looks.css +428 -428
  118. package/src/theme/palettes.css +254 -254
  119. package/src/theme/tokens.css +2896 -2894
  120. package/src/tokens.ts +56 -56
  121. package/src/tooltip.css +43 -43
  122. package/src/tooltip.ts +146 -146
  123. package/src/useDialogFocus.ts +73 -73
  124. package/src/useGrowableField.ts +357 -357
  125. package/src/useModalKeyShield.ts +116 -116
  126. package/src/useToast.ts +48 -48
  127. package/src/wp-logo.svg +14 -14
  128. package/src/ChipToggle.vue +0 -92
  129. package/src/KindChip.vue +0 -53
  130. package/src/RatingBadge.vue +0 -42
package/src/ImageGrid.vue CHANGED
@@ -1,125 +1,127 @@
1
- <script setup lang="ts">
2
- /**
3
- * A grid of image tiles that fills its width (tiles at least `min-size`
4
- * wide). A click or Enter opens the image in a Lightbox, where ← / → step
5
- * through the rest. With `selectable` each tile gets a check box in its
6
- * corner and `v-model:selected` holds the picked ids.
7
- *
8
- * <ImageGrid :images="outputs" />
9
- * <ImageGrid v-model:selected="keep" :images="frames" selectable :min-size="96" />
10
- *
11
- * Tiles are buttons in reading order (one Tab stop each); a tile with no
12
- * `src` draws a Skeleton-like block while it loads.
13
- */
14
- import { computed, ref } from "vue";
15
- import Lightbox from "./Lightbox.vue";
16
- import WpCheck from "./WpCheck.vue";
17
-
18
- export interface GridImage { id: string; src: string; alt: string; thumb?: string; caption?: string }
19
-
20
- const props = withDefaults(defineProps<{
21
- images: GridImage[];
22
- /** Smallest tile width in px. */
23
- minSize?: number | undefined;
24
- /** Width / height of a tile. */
25
- aspect?: number | undefined;
26
- gap?: "sm" | "md" | undefined;
27
- selectable?: boolean | undefined;
28
- selected?: string[] | undefined;
29
- /** Open the Lightbox on a click (off: emit `open` and do your own thing). */
30
- lightbox?: boolean | undefined;
31
- ariaLabel?: string | undefined;
32
- }>(), { minSize: 120, aspect: 1, gap: "md", selectable: false, selected: () => [], lightbox: true, ariaLabel: "Images" });
33
-
34
- const emit = defineEmits<{
35
- (e: "update:selected", v: string[]): void;
36
- (e: "open", index: number): void;
37
- }>();
38
-
39
- const open = ref(false);
40
- const at = ref(0);
41
- const shots = computed(() => props.images.map((i) => ({ src: i.src, alt: i.alt, caption: i.caption })));
42
-
43
- function show(i: number): void {
44
- emit("open", i);
45
- if (!props.lightbox) return;
46
- at.value = i;
47
- open.value = true;
48
- }
49
-
50
- function toggle(id: string, on: boolean): void {
51
- emit("update:selected", on ? [...props.selected, id] : props.selected.filter((x) => x !== id));
52
- }
53
- </script>
54
-
55
- <template>
56
- <div class="wp-image-grid">
57
- <ul
58
- class="wp-image-grid__list"
59
- :data-gap="gap"
60
- :aria-label="ariaLabel"
61
- :style="{ '--_min': `${minSize}px`, '--_aspect': String(aspect) }"
62
- >
63
- <li
64
- v-for="(img, i) in images"
65
- :key="img.id"
66
- class="wp-image-grid__tile"
67
- :data-selected="(selectable && props.selected.includes(img.id)) || undefined"
68
- >
69
- <button
70
- type="button"
71
- class="wp-image-grid__open"
72
- :aria-label="selectable ? `Open ${img.alt}` : img.alt"
73
- @click="show(i)"
74
- >
75
- <img v-if="img.thumb || img.src" class="wp-image-grid__img" :src="img.thumb || img.src" alt="" loading="lazy">
76
- <span v-else class="wp-image-grid__empty" aria-hidden="true" />
77
- </button>
78
- <WpCheck
79
- v-if="selectable"
80
- class="wp-image-grid__check"
81
- :model-value="props.selected.includes(img.id)"
82
- :aria-label="`Select ${img.alt}`"
83
- @update:model-value="toggle(img.id, $event)"
84
- />
85
- <span v-if="img.caption" class="wp-image-grid__caption">{{ img.caption }}</span>
86
- </li>
87
- </ul>
88
- <Lightbox v-if="lightbox" v-model:open="open" v-model:index="at" :images="shots" />
89
- </div>
90
- </template>
91
-
92
- <style scoped>
93
- .wp-image-grid { min-width: 0; }
94
- .wp-image-grid__list {
95
- display: grid; grid-template-columns: repeat(auto-fill, minmax(var(--_min), 1fr));
96
- gap: var(--wp-space-3); margin: 0; padding: 0; list-style: none;
97
- }
98
- .wp-image-grid__list[data-gap="sm"] { gap: var(--wp-space-1); }
99
- .wp-image-grid__tile { position: relative; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
100
- .wp-image-grid__open {
101
- display: block; width: 100%; aspect-ratio: var(--_aspect); padding: 0; overflow: hidden;
102
- border: 1px solid var(--wp-border); border-radius: var(--wp-radius);
103
- background: var(--wp-bg-2); cursor: zoom-in;
104
- transition: border-color var(--wp-motion-hover), box-shadow var(--wp-motion-hover);
105
- }
106
- .wp-image-grid__open:hover { border-color: var(--wp-border-strong); }
107
- .wp-image-grid__open:focus-visible { outline: none; border-color: var(--wp-border-focus); box-shadow: 0 0 0 3px var(--wp-focus-ring); }
108
- .wp-image-grid__tile[data-selected] .wp-image-grid__open { border-color: var(--wp-accent); box-shadow: 0 0 0 2px var(--wp-accent-glow); }
109
- .wp-image-grid__img { display: block; width: 100%; height: 100%; object-fit: cover; }
110
- .wp-image-grid__empty {
111
- display: block; width: 100%; height: 100%;
112
- background: linear-gradient(90deg, var(--wp-bg-2) 0%, var(--wp-bg-3) 50%, var(--wp-bg-2) 100%);
113
- background-size: 200% 100%; animation: wp-image-grid-shimmer var(--wp-motion-shimmer) infinite;
114
- }
115
- @keyframes wp-image-grid-shimmer { to { background-position: -200% 0; } }
116
- .wp-image-grid__check { position: absolute; top: 6px; left: 6px; }
117
- .wp-image-grid__caption {
118
- overflow: hidden; color: var(--wp-text-muted); font: 400 var(--wp-text-xs)/1.3 var(--wp-font-sans);
119
- text-overflow: ellipsis; white-space: nowrap;
120
- }
121
- @media (prefers-reduced-motion: reduce) {
122
- .wp-image-grid__open { transition: none; }
123
- .wp-image-grid__empty { animation: none; }
124
- }
125
- </style>
1
+ <script setup lang="ts">
2
+ /**
3
+ * A grid of image tiles that fills its width (tiles at least `min-size`
4
+ * wide). A click or Enter opens the image in a Lightbox, where ← / → step
5
+ * through the rest. With `selectable` each tile gets a check box in its
6
+ * corner and `v-model:selected` holds the picked ids.
7
+ *
8
+ * <ImageGrid :images="outputs" />
9
+ * <ImageGrid v-model:selected="keep" :images="frames" selectable :min-size="96" />
10
+ *
11
+ * Tiles are buttons in reading order (one Tab stop each). A tile with no
12
+ * `src` yet (a frame still rendering) draws a Skeleton and can't be opened;
13
+ * the Lightbox steps only through the images that are there.
14
+ */
15
+ import { computed, ref } from "vue";
16
+ import Lightbox from "./Lightbox.vue";
17
+ import Skeleton from "./Skeleton.vue";
18
+ import WpCheck from "./WpCheck.vue";
19
+
20
+ export interface GridImage { id: string; src: string; alt: string; thumb?: string; caption?: string }
21
+
22
+ const props = withDefaults(defineProps<{
23
+ images: GridImage[];
24
+ /** Smallest tile width in px. */
25
+ minSize?: number | undefined;
26
+ /** Width / height of a tile. */
27
+ aspect?: number | undefined;
28
+ gap?: "sm" | "md" | undefined;
29
+ selectable?: boolean | undefined;
30
+ selected?: string[] | undefined;
31
+ /** Open the Lightbox on a click (off: emit `open` and do your own thing). */
32
+ lightbox?: boolean | undefined;
33
+ ariaLabel?: string | undefined;
34
+ }>(), { minSize: 120, aspect: 1, gap: "md", selectable: false, selected: () => [], lightbox: true, ariaLabel: "Images" });
35
+
36
+ const emit = defineEmits<{
37
+ (e: "update:selected", v: string[]): void;
38
+ (e: "open", index: number): void;
39
+ }>();
40
+
41
+ const open = ref(false);
42
+ const at = ref(0);
43
+ /** Only the images that are there: a tile still loading has nothing to show. */
44
+ const ready = computed(() => props.images.map((img, i) => ({ img, i })).filter(({ img }) => !!img.src));
45
+ const shots = computed(() => ready.value.map(({ img }) => ({ src: img.src, alt: img.alt, caption: img.caption })));
46
+
47
+ function show(i: number): void {
48
+ if (!props.images[i]?.src) return;
49
+ emit("open", i);
50
+ if (!props.lightbox) return;
51
+ at.value = ready.value.findIndex((r) => r.i === i);
52
+ open.value = true;
53
+ }
54
+
55
+ function toggle(id: string, on: boolean): void {
56
+ emit("update:selected", on ? [...props.selected, id] : props.selected.filter((x) => x !== id));
57
+ }
58
+ </script>
59
+
60
+ <template>
61
+ <div class="wp-image-grid">
62
+ <ul
63
+ class="wp-image-grid__list"
64
+ :data-gap="gap"
65
+ :aria-label="ariaLabel"
66
+ :style="{ '--_min': `${minSize}px`, '--_aspect': String(aspect) }"
67
+ >
68
+ <li
69
+ v-for="(img, i) in images"
70
+ :key="img.id"
71
+ class="wp-image-grid__tile"
72
+ :data-selected="(selectable && props.selected.includes(img.id)) || undefined"
73
+ >
74
+ <button
75
+ type="button"
76
+ class="wp-image-grid__open"
77
+ :aria-label="!img.src ? `${img.alt}, still loading` : selectable ? `Open ${img.alt}` : img.alt"
78
+ :aria-disabled="!img.src || undefined"
79
+ :data-loading="!img.src || undefined"
80
+ @click="show(i)"
81
+ >
82
+ <img v-if="img.thumb || img.src" class="wp-image-grid__img" :src="img.thumb || img.src" alt="" loading="lazy">
83
+ <Skeleton v-else class="wp-image-grid__empty" shape="block" width="100%" height="100%" />
84
+ </button>
85
+ <WpCheck
86
+ v-if="selectable"
87
+ class="wp-image-grid__check"
88
+ :model-value="props.selected.includes(img.id)"
89
+ :aria-label="`Select ${img.alt}`"
90
+ @update:model-value="toggle(img.id, $event)"
91
+ />
92
+ <span v-if="img.caption" class="wp-image-grid__caption">{{ img.caption }}</span>
93
+ </li>
94
+ </ul>
95
+ <Lightbox v-if="lightbox" v-model:open="open" v-model:index="at" :images="shots" />
96
+ </div>
97
+ </template>
98
+
99
+ <style scoped>
100
+ .wp-image-grid { min-width: 0; }
101
+ .wp-image-grid__list {
102
+ display: grid; grid-template-columns: repeat(auto-fill, minmax(var(--_min), 1fr));
103
+ gap: var(--wp-space-3); margin: 0; padding: 0; list-style: none;
104
+ }
105
+ .wp-image-grid__list[data-gap="sm"] { gap: var(--wp-space-1); }
106
+ .wp-image-grid__tile { position: relative; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
107
+ .wp-image-grid__open {
108
+ display: block; width: 100%; aspect-ratio: var(--_aspect); padding: 0; overflow: hidden;
109
+ border: 1px solid var(--wp-border); border-radius: var(--wp-radius);
110
+ background: var(--wp-bg-2); cursor: zoom-in;
111
+ transition: border-color var(--wp-motion-hover), box-shadow var(--wp-motion-hover);
112
+ }
113
+ .wp-image-grid__open:hover { border-color: var(--wp-border-strong); }
114
+ .wp-image-grid__open:focus-visible { outline: none; border-color: var(--wp-border-focus); box-shadow: var(--wp-focus-ring); }
115
+ .wp-image-grid__tile[data-selected] .wp-image-grid__open { border-color: var(--wp-accent); box-shadow: 0 0 0 2px var(--wp-accent-glow); }
116
+ .wp-image-grid__img { display: block; width: 100%; height: 100%; object-fit: cover; }
117
+ .wp-image-grid__open[data-loading] { cursor: progress; }
118
+ .wp-image-grid__empty { width: 100%; height: 100%; }
119
+ .wp-image-grid__check { position: absolute; top: 6px; left: 6px; }
120
+ .wp-image-grid__caption {
121
+ overflow: hidden; color: var(--wp-text-muted); font: 400 var(--wp-text-xs)/1.3 var(--wp-font-sans);
122
+ text-overflow: ellipsis; white-space: nowrap;
123
+ }
124
+ @media (prefers-reduced-motion: reduce) {
125
+ .wp-image-grid__open { transition: none; }
126
+ }
127
+ </style>
@@ -1,183 +1,183 @@
1
- <script setup lang="ts">
2
- /**
3
- * Inline edit: a name that turns into a text box where it stands. Click it
4
- * (or focus it and press Enter or F2), type, then Enter or a click away
5
- * saves and Escape puts the old text back. Works on the canvas and in the
6
- * manager.
7
- *
8
- * <InlineEdit v-model="mod.name" label="Wildcard name" />
9
- * <InlineEdit v-model="title" label="Chat title" :validate="(v) => taken(v) ? 'That name is taken' : null" />
10
- *
11
- * Read view is a button named "Rename <label>: <value>", with a pencil on
12
- * hover and focus. An empty or invalid value keeps the box open with the
13
- * message under it; `update:modelValue` only fires on a saved change.
14
- */
15
- import { computed, nextTick, ref, useId } from "vue";
16
-
17
- const props = withDefaults(defineProps<{
18
- modelValue: string;
19
- /** What is being renamed, e.g. "Wildcard name". */
20
- label: string;
21
- placeholder?: string | undefined;
22
- /** Return a message to refuse the value, or null to take it. */
23
- validate?: ((v: string) => string | null) | undefined;
24
- /** Refuse an empty value. On by default. */
25
- required?: boolean | undefined;
26
- disabled?: boolean | undefined;
27
- size?: "sm" | "md" | "lg" | undefined;
28
- /** Trim spaces from both ends before saving. */
29
- trim?: boolean | undefined;
30
- maxlength?: number | undefined;
31
- }>(), { placeholder: "Untitled", required: true, size: "md", trim: true, maxlength: undefined });
32
-
33
- const emit = defineEmits<{
34
- (e: "update:modelValue", v: string): void;
35
- (e: "edit"): void;
36
- (e: "cancel"): void;
37
- }>();
38
-
39
- const editing = ref(false);
40
- const draft = ref("");
41
- const error = ref<string | null>(null);
42
- const inputEl = ref<HTMLInputElement | null>(null);
43
- const readEl = ref<HTMLButtonElement | null>(null);
44
- const errId = `wp-inline-edit-${useId()}`;
45
-
46
- const shown = computed(() => props.modelValue || props.placeholder);
47
-
48
- async function open(): Promise<void> {
49
- if (props.disabled || editing.value) return;
50
- draft.value = props.modelValue;
51
- error.value = null;
52
- editing.value = true;
53
- emit("edit");
54
- await nextTick();
55
- inputEl.value?.focus();
56
- inputEl.value?.select();
57
- }
58
-
59
- function check(v: string): string | null {
60
- if (props.required && !v) return `${props.label} can't be empty`;
61
- return props.validate?.(v) ?? null;
62
- }
63
-
64
- async function close(): Promise<void> {
65
- editing.value = false;
66
- error.value = null;
67
- await nextTick();
68
- readEl.value?.focus();
69
- }
70
-
71
- function save(refocus = true): void {
72
- if (!editing.value) return;
73
- const v = props.trim ? draft.value.trim() : draft.value;
74
- const msg = check(v);
75
- if (msg) {
76
- error.value = msg;
77
- return;
78
- }
79
- if (v !== props.modelValue) emit("update:modelValue", v);
80
- if (refocus) void close();
81
- else { editing.value = false; error.value = null; }
82
- }
83
-
84
- function cancel(): void {
85
- if (!editing.value) return;
86
- emit("cancel");
87
- void close();
88
- }
89
-
90
- function onBlur(): void {
91
- // A click away saves; a value that can't be saved is put back instead of
92
- // leaving a box open that nobody is looking at.
93
- if (!editing.value) return;
94
- const v = props.trim ? draft.value.trim() : draft.value;
95
- if (check(v)) { editing.value = false; error.value = null; emit("cancel"); return; }
96
- save(false);
97
- }
98
-
99
- function onKeydown(ev: KeyboardEvent): void {
100
- if (ev.key === "Enter") { ev.preventDefault(); save(); }
101
- else if (ev.key === "Escape") { ev.preventDefault(); ev.stopPropagation(); cancel(); }
102
- }
103
-
104
- function onReadKey(ev: KeyboardEvent): void {
105
- if (ev.key === "F2") { ev.preventDefault(); void open(); }
106
- }
107
- </script>
108
-
109
- <template>
110
- <span class="wp-inline-edit" :data-size="size" :data-editing="editing || undefined">
111
- <template v-if="editing">
112
- <input
113
- ref="inputEl"
114
- v-model="draft"
115
- class="wp-inline-edit__input"
116
- type="text"
117
- :aria-label="label"
118
- :aria-invalid="error ? 'true' : undefined"
119
- :aria-describedby="error ? errId : undefined"
120
- :placeholder="placeholder"
121
- :maxlength="maxlength"
122
- @keydown="onKeydown"
123
- @blur="onBlur"
124
- >
125
- <span v-if="error" :id="errId" class="wp-inline-edit__error" role="alert">{{ error }}</span>
126
- </template>
127
- <button
128
- v-else
129
- ref="readEl"
130
- type="button"
131
- class="wp-inline-edit__read"
132
- :data-empty="!modelValue || undefined"
133
- :aria-label="`Rename ${label}: ${modelValue || placeholder}`"
134
- :disabled="disabled"
135
- @click="open"
136
- @keydown="onReadKey"
137
- >
138
- <span class="wp-inline-edit__text">{{ shown }}</span>
139
- <i class="pi pi-pencil wp-inline-edit__pen" aria-hidden="true" />
140
- </button>
141
- </span>
142
- </template>
143
-
144
- <style scoped>
145
- .wp-inline-edit {
146
- --_h: var(--wp-control-h-md);
147
- position: relative; display: inline-flex; flex-direction: column; align-items: flex-start; min-width: 0; max-width: 100%;
148
- /* The padding of the hover box sits outside the text's line. */
149
- margin-left: calc(var(--wp-space-3) * -1);
150
- font: inherit; vertical-align: middle;
151
- }
152
- .wp-inline-edit[data-size="sm"] { --_h: var(--wp-control-h-sm); }
153
- .wp-inline-edit[data-size="lg"] { --_h: var(--wp-control-h-lg); }
154
- .wp-inline-edit__read, .wp-inline-edit__input {
155
- box-sizing: border-box; height: var(--_h); margin: 0; min-width: 0;
156
- padding: 0 var(--wp-space-3); border-radius: var(--wp-radius-sm);
157
- font: inherit; color: var(--wp-text);
158
- }
159
- .wp-inline-edit__read {
160
- display: inline-flex; align-items: center; gap: var(--wp-space-3); max-width: 100%;
161
- border: 1px solid transparent; background: transparent; cursor: text; text-align: left;
162
- transition: background var(--wp-motion-hover), border-color var(--wp-motion-hover);
163
- }
164
- .wp-inline-edit__text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
165
- .wp-inline-edit__read[data-empty] .wp-inline-edit__text { color: var(--wp-text-dim); font-style: italic; }
166
- .wp-inline-edit__pen { flex: none; font-size: 0.8em; color: var(--wp-text-dim); opacity: 0; transition: opacity var(--wp-motion-hover); }
167
- .wp-inline-edit__read:hover:not(:disabled) { background: var(--wp-row-hover); border-color: var(--wp-border-soft); }
168
- .wp-inline-edit__read:hover .wp-inline-edit__pen, .wp-inline-edit__read:focus-visible .wp-inline-edit__pen { opacity: 1; }
169
- .wp-inline-edit__read:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--wp-border-focus); }
170
- .wp-inline-edit__read:disabled { cursor: default; }
171
- .wp-inline-edit__input {
172
- width: 100%; min-width: 12ch;
173
- border: 1px solid var(--wp-border-focus); background: var(--wp-bg-1); outline: none;
174
- box-shadow: 0 0 0 3px var(--wp-focus-ring);
175
- }
176
- .wp-inline-edit__input[aria-invalid="true"] { border-color: var(--wp-error-border); box-shadow: 0 0 0 3px color-mix(in srgb, var(--wp-danger) 22%, transparent); }
177
- .wp-inline-edit__error {
178
- margin-top: 2px; color: var(--wp-error-text); font: 400 var(--wp-text-xs)/1.3 var(--wp-font-sans);
179
- }
180
- @media (prefers-reduced-motion: reduce) {
181
- .wp-inline-edit__read, .wp-inline-edit__pen { transition: none; }
182
- }
183
- </style>
1
+ <script setup lang="ts">
2
+ /**
3
+ * Inline edit: a name that turns into a text box where it stands. Click it
4
+ * (or focus it and press Enter or F2), type, then Enter or a click away
5
+ * saves and Escape puts the old text back. Works on the canvas and in the
6
+ * manager.
7
+ *
8
+ * <InlineEdit v-model="mod.name" label="Wildcard name" />
9
+ * <InlineEdit v-model="title" label="Chat title" :validate="(v) => taken(v) ? 'That name is taken' : null" />
10
+ *
11
+ * Read view is a button named "Rename <label>: <value>", with a pencil on
12
+ * hover and focus. An empty or invalid value keeps the box open with the
13
+ * message under it; `update:modelValue` only fires on a saved change.
14
+ */
15
+ import { computed, nextTick, ref, useId } from "vue";
16
+
17
+ const props = withDefaults(defineProps<{
18
+ modelValue: string;
19
+ /** What is being renamed, e.g. "Wildcard name". */
20
+ label: string;
21
+ placeholder?: string | undefined;
22
+ /** Return a message to refuse the value, or null to take it. */
23
+ validate?: ((v: string) => string | null) | undefined;
24
+ /** Refuse an empty value. On by default. */
25
+ required?: boolean | undefined;
26
+ disabled?: boolean | undefined;
27
+ size?: "sm" | "md" | "lg" | undefined;
28
+ /** Trim spaces from both ends before saving. */
29
+ trim?: boolean | undefined;
30
+ maxlength?: number | undefined;
31
+ }>(), { placeholder: "Untitled", required: true, size: "md", trim: true, maxlength: undefined });
32
+
33
+ const emit = defineEmits<{
34
+ (e: "update:modelValue", v: string): void;
35
+ (e: "edit"): void;
36
+ (e: "cancel"): void;
37
+ }>();
38
+
39
+ const editing = ref(false);
40
+ const draft = ref("");
41
+ const error = ref<string | null>(null);
42
+ const inputEl = ref<HTMLInputElement | null>(null);
43
+ const readEl = ref<HTMLButtonElement | null>(null);
44
+ const errId = `wp-inline-edit-${useId()}`;
45
+
46
+ const shown = computed(() => props.modelValue || props.placeholder);
47
+
48
+ async function open(): Promise<void> {
49
+ if (props.disabled || editing.value) return;
50
+ draft.value = props.modelValue;
51
+ error.value = null;
52
+ editing.value = true;
53
+ emit("edit");
54
+ await nextTick();
55
+ inputEl.value?.focus();
56
+ inputEl.value?.select();
57
+ }
58
+
59
+ function check(v: string): string | null {
60
+ if (props.required && !v) return `${props.label} can't be empty`;
61
+ return props.validate?.(v) ?? null;
62
+ }
63
+
64
+ async function close(): Promise<void> {
65
+ editing.value = false;
66
+ error.value = null;
67
+ await nextTick();
68
+ readEl.value?.focus();
69
+ }
70
+
71
+ function save(refocus = true): void {
72
+ if (!editing.value) return;
73
+ const v = props.trim ? draft.value.trim() : draft.value;
74
+ const msg = check(v);
75
+ if (msg) {
76
+ error.value = msg;
77
+ return;
78
+ }
79
+ if (v !== props.modelValue) emit("update:modelValue", v);
80
+ if (refocus) void close();
81
+ else { editing.value = false; error.value = null; }
82
+ }
83
+
84
+ function cancel(): void {
85
+ if (!editing.value) return;
86
+ emit("cancel");
87
+ void close();
88
+ }
89
+
90
+ function onBlur(): void {
91
+ // A click away saves; a value that can't be saved is put back instead of
92
+ // leaving a box open that nobody is looking at.
93
+ if (!editing.value) return;
94
+ const v = props.trim ? draft.value.trim() : draft.value;
95
+ if (check(v)) { editing.value = false; error.value = null; emit("cancel"); return; }
96
+ save(false);
97
+ }
98
+
99
+ function onKeydown(ev: KeyboardEvent): void {
100
+ if (ev.key === "Enter") { ev.preventDefault(); save(); }
101
+ else if (ev.key === "Escape") { ev.preventDefault(); ev.stopPropagation(); cancel(); }
102
+ }
103
+
104
+ function onReadKey(ev: KeyboardEvent): void {
105
+ if (ev.key === "F2") { ev.preventDefault(); void open(); }
106
+ }
107
+ </script>
108
+
109
+ <template>
110
+ <span class="wp-inline-edit" :data-size="size" :data-editing="editing || undefined">
111
+ <template v-if="editing">
112
+ <input
113
+ ref="inputEl"
114
+ v-model="draft"
115
+ class="wp-inline-edit__input"
116
+ type="text"
117
+ :aria-label="label"
118
+ :aria-invalid="error ? 'true' : undefined"
119
+ :aria-describedby="error ? errId : undefined"
120
+ :placeholder="placeholder"
121
+ :maxlength="maxlength"
122
+ @keydown="onKeydown"
123
+ @blur="onBlur"
124
+ >
125
+ <span v-if="error" :id="errId" class="wp-inline-edit__error" role="alert">{{ error }}</span>
126
+ </template>
127
+ <button
128
+ v-else
129
+ ref="readEl"
130
+ type="button"
131
+ class="wp-inline-edit__read"
132
+ :data-empty="!modelValue || undefined"
133
+ :aria-label="`Rename ${label}: ${modelValue || placeholder}`"
134
+ :disabled="disabled"
135
+ @click="open"
136
+ @keydown="onReadKey"
137
+ >
138
+ <span class="wp-inline-edit__text">{{ shown }}</span>
139
+ <i class="pi pi-pencil wp-inline-edit__pen" aria-hidden="true" />
140
+ </button>
141
+ </span>
142
+ </template>
143
+
144
+ <style scoped>
145
+ .wp-inline-edit {
146
+ --_h: var(--wp-control-h-md);
147
+ position: relative; display: inline-flex; flex-direction: column; align-items: flex-start; min-width: 0; max-width: 100%;
148
+ /* The padding of the hover box sits outside the text's line. */
149
+ margin-left: calc(var(--wp-space-3) * -1);
150
+ font: inherit; vertical-align: middle;
151
+ }
152
+ .wp-inline-edit[data-size="sm"] { --_h: var(--wp-control-h-sm); }
153
+ .wp-inline-edit[data-size="lg"] { --_h: var(--wp-control-h-lg); }
154
+ .wp-inline-edit__read, .wp-inline-edit__input {
155
+ box-sizing: border-box; height: var(--_h); margin: 0; min-width: 0;
156
+ padding: 0 var(--wp-space-3); border-radius: var(--wp-radius-sm);
157
+ font: inherit; color: var(--wp-text);
158
+ }
159
+ .wp-inline-edit__read {
160
+ display: inline-flex; align-items: center; gap: var(--wp-space-3); max-width: 100%;
161
+ border: 1px solid transparent; background: transparent; cursor: text; text-align: left;
162
+ transition: background var(--wp-motion-hover), border-color var(--wp-motion-hover);
163
+ }
164
+ .wp-inline-edit__text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
165
+ .wp-inline-edit__read[data-empty] .wp-inline-edit__text { color: var(--wp-text-dim); font-style: italic; }
166
+ .wp-inline-edit__pen { flex: none; font-size: 0.8em; color: var(--wp-text-dim); opacity: 0; transition: opacity var(--wp-motion-hover); }
167
+ .wp-inline-edit__read:hover:not(:disabled) { background: var(--wp-row-hover); border-color: var(--wp-border-soft); }
168
+ .wp-inline-edit__read:hover .wp-inline-edit__pen, .wp-inline-edit__read:focus-visible .wp-inline-edit__pen { opacity: 1; }
169
+ .wp-inline-edit__read:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--wp-border-focus); }
170
+ .wp-inline-edit__read:disabled { cursor: default; }
171
+ .wp-inline-edit__input {
172
+ width: 100%; min-width: 12ch;
173
+ border: 1px solid var(--wp-border-focus); background: var(--wp-bg-1); outline: none;
174
+ box-shadow: var(--wp-focus-ring);
175
+ }
176
+ .wp-inline-edit__input[aria-invalid="true"] { border-color: var(--wp-error-border); box-shadow: 0 0 0 3px color-mix(in srgb, var(--wp-danger) 22%, transparent); }
177
+ .wp-inline-edit__error {
178
+ margin-top: 2px; color: var(--wp-error-text); font: 400 var(--wp-text-xs)/1.3 var(--wp-font-sans);
179
+ }
180
+ @media (prefers-reduced-motion: reduce) {
181
+ .wp-inline-edit__read, .wp-inline-edit__pen { transition: none; }
182
+ }
183
+ </style>