@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/Chip.vue CHANGED
@@ -1,97 +1,163 @@
1
- <script setup lang="ts">
2
- /**
3
- * A small tag: a post's tag, a category, a status. `removable` adds a ✕
4
- * that emits `remove`. `as="button"` (or `as="a"` / a link component such as
5
- * RouterLink) makes the whole chip something you press, for a chip that
6
- * takes you somewhere ("show everything tagged portrait"); a pressable
7
- * chip has no ✕. For a filter that turns on and off, use ChipToggle.
8
- */
9
- import { computed, type Component } from "vue";
10
- import Icon from "./Icon.vue";
11
-
12
- type Tone = "default" | "accent" | "success" | "warn" | "danger" | "info";
13
-
14
- interface Props {
15
- tone?: Tone | undefined;
16
- icon?: string | null | undefined;
17
- removable?: boolean | undefined;
18
- /**
19
- * Free-form colour (hex or any CSS colour), e.g. a category's. When set it
20
- * overrides the tone and renders a tinted pill: background = colour @ 18%,
21
- * border = colour @ 42%, and the text blends the colour toward
22
- * `--wp-text` so a pale pick stays readable in light mode and a dark pick
23
- * in dark mode. `null` reads as no colour (a category without one).
24
- * This is what CategoryChip used to be: `<Chip :color :icon>{{ name }}</Chip>`.
25
- */
26
- color?: string | null | undefined;
27
- /** What to render: a span (default), a button, or a link. */
28
- as?: string | Component | undefined;
29
- }
30
- const props = withDefaults(defineProps<Props>(), { tone: "default", as: "span" });
31
-
32
- const emit = defineEmits<{
33
- (e: "remove"): void;
34
- }>();
35
-
36
- const pressable = computed(() => props.as !== "span");
37
- const classes = computed(() => [
38
- "wp-chip",
39
- pressable.value && "wp-chip--action",
40
- props.color ? "wp-chip--color" : props.tone !== "default" && `wp-chip--${props.tone}`,
41
- ]);
42
-
43
- const colorStyle = computed(() => {
44
- if (!props.color) return undefined;
45
- const c = props.color;
46
- return {
47
- color: `color-mix(in oklab, ${c} 65%, var(--wp-text))`,
48
- background: `color-mix(in oklab, ${c} 18%, transparent)`,
49
- borderColor: `color-mix(in oklab, ${c} 42%, transparent)`,
50
- };
51
- });
52
-
53
- function onRemove(e: MouseEvent) {
54
- e.stopPropagation();
55
- emit("remove");
56
- }
57
- </script>
58
-
59
- <template>
60
- <component
61
- :is="as"
62
- :class="classes"
63
- :style="colorStyle"
64
- :type="as === 'button' ? 'button' : undefined"
65
- >
66
- <!-- 10px sub-ICON_SM — pill density pass dropped chip font 11→10.5
67
- and the previous 12px icon read oversized next to the smaller
68
- label. Matches the new chip body weight. -->
69
- <Icon v-if="icon" :name="icon" :size="10" />
70
- <slot />
71
- <button
72
- v-if="removable && !pressable"
73
- type="button"
74
- class="wp-chip__close"
75
- aria-label="Remove"
76
- @click="onRemove"
77
- >
78
- <!-- Close glyph also drops to 10px so chip-with-X stays balanced
79
- against the slimmer body. Hit target stays 20×20 via the
80
- button's min sizing in tokens.css. -->
81
- <Icon name="times" :size="10" />
82
- </button>
83
- </component>
84
- </template>
85
-
86
- <style scoped>
87
- .wp-chip--action {
88
- font-family: inherit;
89
- text-decoration: none;
90
- cursor: pointer;
91
- transition: border-color var(--wp-motion-quick) ease, color var(--wp-motion-quick) ease, filter var(--wp-motion-quick) ease;
92
- }
93
- .wp-chip--action:hover { border-color: var(--wp-border-strong); color: var(--wp-text); }
94
- .wp-chip--action.wp-chip--color:hover,
95
- .wp-chip--action.wp-chip--accent:hover { color: inherit; filter: brightness(1.08); }
96
- .wp-chip--action:focus-visible { outline: none; box-shadow: var(--wp-focus-ring); }
97
- </style>
1
+ <script setup lang="ts">
2
+ /**
3
+ * The one pill in the kit: a tag, a status, a count, a category, a module
4
+ * type, a filter you turn on and off. Everything else is a prop:
5
+ *
6
+ * <Chip>portrait</Chip> a label
7
+ * <Chip tone="warn" icon="pi-exclamation-triangle">2 conflicts</Chip>
8
+ * <Chip :color="cat.color" :icon="cat.icon">{{ cat.name }}</Chip>
9
+ * <Chip :color="kindColour(type)">{{ type }}</Chip> a module type
10
+ * <Chip tone="success" dot>recommended</Chip>
11
+ * <Chip removable @remove="drop(t)">{{ t }}</Chip>
12
+ * <Chip as="button" icon="tag" @click="open(t)">{{ t }}</Chip> takes you somewhere
13
+ * <Chip :pressed="tag === t" :count="12" size="sm" @click="pick(t)">{{ t }}</Chip> a filter
14
+ *
15
+ * `pressed` (true or false) makes it a toggle: a `<button>` with
16
+ * `aria-pressed`, neutral when off and lit in the accent (or `color`) when
17
+ * on; with `color` its icon wears the colour even when off, so a row of type
18
+ * filters reads at a glance. `count` puts a small number after the label.
19
+ * `size`: `xs` (default, a label in a line of text), `sm` (24px, a filter
20
+ * row) or `md` (32px, next to a md control).
21
+ */
22
+ import { computed, type Component } from "vue";
23
+ import Icon from "./Icon.vue";
24
+
25
+ type Tone = "default" | "accent" | "success" | "warn" | "danger" | "info";
26
+
27
+ interface Props {
28
+ tone?: Tone | undefined;
29
+ icon?: string | null | undefined;
30
+ removable?: boolean | undefined;
31
+ /**
32
+ * Free-form colour (hex or any CSS colour), e.g. a category's or a kind's.
33
+ * On a label it overrides the tone and tints the pill: background = colour
34
+ * @ 18%, border = colour @ 42%, and the text blends the colour toward
35
+ * `--wp-text` so a pale pick stays readable in light mode and a dark pick
36
+ * in dark mode. On a toggle it is the "on" colour. `null` reads as none.
37
+ */
38
+ color?: string | null | undefined;
39
+ /** What to render: a span (default), a button, or a link. A toggle is always a button. */
40
+ as?: string | Component | undefined;
41
+ /** Makes it a toggle (`aria-pressed`). Leave unset for a plain label. */
42
+ pressed?: boolean | undefined;
43
+ /** A small number after the label. */
44
+ count?: number | string | undefined;
45
+ /** A status dot before the label, in the chip's colour. */
46
+ dot?: boolean | undefined;
47
+ size?: "xs" | "sm" | "md" | undefined;
48
+ disabled?: boolean | undefined;
49
+ }
50
+ const props = withDefaults(defineProps<Props>(), { tone: "default", as: "span", size: "xs", pressed: undefined });
51
+
52
+ const emit = defineEmits<{
53
+ (e: "remove"): void;
54
+ }>();
55
+
56
+ const toggle = computed(() => props.pressed !== undefined);
57
+ const tag = computed(() => (toggle.value ? "button" : props.as));
58
+ const pressable = computed(() => tag.value !== "span");
59
+ const classes = computed(() => [
60
+ "wp-chip",
61
+ `wp-chip--${props.size}`,
62
+ pressable.value && "wp-chip--action",
63
+ toggle.value && "wp-chip--toggle",
64
+ toggle.value && props.color && "wp-chip--tinted",
65
+ !toggle.value && (props.color ? "wp-chip--color" : props.tone !== "default" && `wp-chip--${props.tone}`),
66
+ ]);
67
+
68
+ const colorStyle = computed(() => {
69
+ if (!props.color) return undefined;
70
+ const c = props.color;
71
+ if (toggle.value) return { "--wp-chip-c": c };
72
+ return {
73
+ color: `color-mix(in oklab, ${c} 65%, var(--wp-text))`,
74
+ background: `color-mix(in oklab, ${c} 18%, transparent)`,
75
+ borderColor: `color-mix(in oklab, ${c} 42%, transparent)`,
76
+ };
77
+ });
78
+
79
+ function onRemove(e: MouseEvent) {
80
+ e.stopPropagation();
81
+ emit("remove");
82
+ }
83
+ </script>
84
+
85
+ <template>
86
+ <component
87
+ :is="tag"
88
+ :class="classes"
89
+ :style="colorStyle"
90
+ :type="tag === 'button' ? 'button' : undefined"
91
+ :aria-pressed="toggle ? pressed : undefined"
92
+ :disabled="tag === 'button' ? disabled : undefined"
93
+ >
94
+ <span v-if="dot" class="wp-chip__dot" aria-hidden="true" />
95
+ <!-- 10px sub-ICON_SM: the chip font is 10.5px and a 12px icon read
96
+ oversized next to it. Larger chips get a larger icon. -->
97
+ <Icon v-if="icon" class="wp-chip__icon" :name="icon" :size="size === 'xs' ? 10 : 11" />
98
+ <slot />
99
+ <span v-if="count !== undefined" class="wp-chip__count">{{ count }}</span>
100
+ <button
101
+ v-if="removable && !pressable"
102
+ type="button"
103
+ class="wp-chip__close"
104
+ aria-label="Remove"
105
+ @click="onRemove"
106
+ >
107
+ <!-- Close glyph also drops to 10px so chip-with-X stays balanced
108
+ against the slimmer body. Hit target stays 20×20 via the
109
+ button's min sizing in tokens.css. -->
110
+ <Icon name="times" :size="10" />
111
+ </button>
112
+ </component>
113
+ </template>
114
+
115
+ <style scoped>
116
+ /* xs is the base `.wp-chip` in theme/tokens.css (19px). sm and md sit on the
117
+ control height scale so a filter row lines up with the fields beside it. */
118
+ .wp-chip--sm, .wp-chip--md {
119
+ height: var(--wp-control-h-sm);
120
+ padding: 0 var(--wp-space-4);
121
+ gap: var(--wp-space-2);
122
+ font-size: var(--wp-text-sm);
123
+ line-height: 1;
124
+ }
125
+ .wp-chip--md { height: var(--wp-control-h-md); padding: 0 var(--wp-space-5); font-size: var(--wp-text-base); }
126
+
127
+ .wp-chip--action {
128
+ font-family: inherit;
129
+ text-decoration: none;
130
+ cursor: pointer;
131
+ transition: background var(--wp-motion-quick) ease, border-color var(--wp-motion-quick) ease, color var(--wp-motion-quick) ease, filter var(--wp-motion-quick) ease;
132
+ }
133
+ .wp-chip--action:hover:not(:disabled) { border-color: var(--wp-border-strong); color: var(--wp-text); }
134
+ .wp-chip--action.wp-chip--color:hover,
135
+ .wp-chip--action.wp-chip--accent:hover { color: inherit; filter: brightness(1.08); }
136
+ .wp-chip--action:focus-visible { outline: none; box-shadow: var(--wp-focus-ring); }
137
+ .wp-chip--action:disabled { opacity: 0.45; cursor: not-allowed; }
138
+
139
+ /* A toggle: neutral when off, lit in the accent or its colour when on. */
140
+ .wp-chip--toggle { --wp-chip-c: var(--wp-accent-500); }
141
+ .wp-chip--tinted .wp-chip__icon { color: var(--wp-chip-c); }
142
+ .wp-chip--toggle[aria-pressed="true"] {
143
+ border-color: color-mix(in oklab, var(--wp-chip-c) 45%, transparent);
144
+ background: color-mix(in oklab, var(--wp-chip-c) 16%, transparent);
145
+ color: var(--wp-accent-text-strong);
146
+ }
147
+ .wp-chip--tinted[aria-pressed="true"] { color: var(--wp-chip-c); }
148
+
149
+ .wp-chip__count {
150
+ padding: 0 var(--wp-space-2);
151
+ border-radius: var(--wp-radius-cap, 999px);
152
+ background: var(--wp-bg-4, var(--wp-bg-2));
153
+ color: var(--wp-text-dim);
154
+ font-family: var(--wp-font-mono);
155
+ font-size: 0.9em;
156
+ line-height: 1.4;
157
+ }
158
+ .wp-chip--sm .wp-chip__count, .wp-chip--md .wp-chip__count { padding: 1px var(--wp-space-3); font-size: var(--wp-text-xs); }
159
+ .wp-chip--toggle[aria-pressed="true"] .wp-chip__count {
160
+ background: color-mix(in oklab, var(--wp-chip-c) 22%, transparent);
161
+ color: inherit;
162
+ }
163
+ </style>
@@ -1,133 +1,133 @@
1
- <script setup lang="ts" generic="T extends string">
2
- /**
3
- * Choice cards: pick one of a handful of options that each need a picture to
4
- * be understood (a colour theme, a look, a layout). Each card shows the
5
- * `preview` slot, a name and, unless `compact`, a one-line hint (compact puts
6
- * the hint in the card's tooltip).
7
- *
8
- * It is a radio group (APG), like the segmented control: only the chosen card
9
- * is in the Tab order, the arrow keys move to the next card and choose it,
10
- * Home/End jump to the ends.
11
- *
12
- * <ChoiceCards v-model="look" :options="LOOKS" aria-label="Look">
13
- * <template #preview="{ option }"><LookDrawing :id="option.value" /></template>
14
- * </ChoiceCards>
15
- */
16
- import { nextTick, ref } from "vue";
17
-
18
- export interface ChoiceCardOption<V extends string = string> {
19
- value: V;
20
- label: string;
21
- hint?: string | undefined;
22
- /** Inline style for the card, e.g. CSS variables its preview reads. */
23
- style?: Record<string, string> | undefined;
24
- }
25
-
26
- const props = defineProps<{
27
- modelValue: T;
28
- options: ChoiceCardOption<T>[];
29
- /** Three to a row, smaller, hint as tooltip: for narrow panels. */
30
- compact?: boolean | undefined;
31
- ariaLabel?: string | undefined;
32
- /** `data-test` of each card is `<testPrefix>-<value>`. */
33
- testPrefix?: string | undefined;
34
- }>();
35
-
36
- const emit = defineEmits<{ (e: "update:modelValue", v: T): void }>();
37
- defineSlots<{ preview?(p: { option: ChoiceCardOption<T>; on: boolean }): unknown }>();
38
- const rootEl = ref<HTMLElement | null>(null);
39
-
40
- function pick(v: T): void {
41
- if (v !== props.modelValue) emit("update:modelValue", v);
42
- }
43
-
44
- function tabStop(): T | undefined {
45
- return (props.options.find((o) => o.value === props.modelValue) ?? props.options[0])?.value;
46
- }
47
-
48
- function onKeydown(ev: KeyboardEvent): void {
49
- const all = props.options;
50
- if (!all.length) return;
51
- const at = all.findIndex((o) => o.value === props.modelValue);
52
- let next: number;
53
- switch (ev.key) {
54
- case "ArrowRight": case "ArrowDown": next = (at + 1) % all.length; break;
55
- case "ArrowLeft": case "ArrowUp": next = at < 0 ? all.length - 1 : (at - 1 + all.length) % all.length; break;
56
- case "Home": next = 0; break;
57
- case "End": next = all.length - 1; break;
58
- default: return;
59
- }
60
- ev.preventDefault();
61
- pick(all[next].value);
62
- void nextTick(() => rootEl.value?.querySelectorAll<HTMLElement>("[role=radio]")[next]?.focus());
63
- }
64
- </script>
65
-
66
- <template>
67
- <div
68
- ref="rootEl"
69
- class="wp-choice-cards"
70
- :class="{ 'wp-choice-cards--compact': compact }"
71
- role="radiogroup"
72
- :aria-label="ariaLabel"
73
- >
74
- <button
75
- v-for="o in options"
76
- :key="o.value"
77
- :title="compact ? o.hint : undefined"
78
- type="button"
79
- role="radio"
80
- class="wp-choice-card"
81
- :aria-checked="modelValue === o.value"
82
- :tabindex="o.value === tabStop() ? 0 : -1"
83
- :data-test="testPrefix ? `${testPrefix}-${o.value}` : undefined"
84
- :style="o.style"
85
- @click="pick(o.value)"
86
- @keydown="onKeydown"
87
- >
88
- <slot name="preview" :option="o" :on="modelValue === o.value" />
89
- <span class="wp-choice-card__name">{{ o.label }}</span>
90
- <span v-if="!compact && o.hint" class="wp-choice-card__hint">{{ o.hint }}</span>
91
- </button>
92
- </div>
93
- </template>
94
-
95
- <style scoped>
96
- .wp-choice-cards {
97
- display: grid;
98
- grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
99
- gap: var(--wp-space-4);
100
- width: 100%;
101
- }
102
- .wp-choice-cards--compact { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--wp-space-3); }
103
- .wp-choice-card {
104
- display: flex;
105
- flex-direction: column;
106
- align-items: stretch;
107
- gap: var(--wp-space-3);
108
- min-width: 0;
109
- padding: var(--wp-space-4);
110
- border: 1px solid var(--wp-border);
111
- border-radius: var(--wp-radius);
112
- background: var(--wp-bg-2);
113
- color: var(--wp-text);
114
- font: inherit;
115
- text-align: left;
116
- cursor: pointer;
117
- transition: border-color var(--wp-motion-hover), background var(--wp-motion-hover);
118
- }
119
- .wp-choice-card:hover { border-color: var(--wp-border-strong); background: var(--wp-bg-3); }
120
- .wp-choice-card[aria-checked="true"] {
121
- border-color: var(--wp-accent-500);
122
- box-shadow: inset 0 0 0 1px var(--wp-accent-500);
123
- background: color-mix(in oklab, var(--wp-accent-500) 10%, var(--wp-bg-2));
124
- }
125
- .wp-choice-card:focus-visible { outline: none; box-shadow: var(--wp-focus-ring); }
126
- .wp-choice-card__name { font-size: var(--wp-text-base); font-weight: 600; line-height: var(--wp-line-base); }
127
- .wp-choice-card__hint { font-size: var(--wp-text-xs); line-height: var(--wp-line-xs); color: var(--wp-text-muted); }
128
- .wp-choice-cards--compact .wp-choice-card { padding: var(--wp-space-3); gap: var(--wp-space-2); }
129
- .wp-choice-cards--compact .wp-choice-card__name { font-size: var(--wp-text-xs); }
130
- @media (prefers-reduced-motion: reduce) {
131
- .wp-choice-card { transition: none; }
132
- }
133
- </style>
1
+ <script setup lang="ts" generic="T extends string">
2
+ /**
3
+ * Choice cards: pick one of a handful of options that each need a picture to
4
+ * be understood (a colour theme, a look, a layout). Each card shows the
5
+ * `preview` slot, a name and, unless `compact`, a one-line hint (compact puts
6
+ * the hint in the card's tooltip).
7
+ *
8
+ * It is a radio group (APG), like the segmented control: only the chosen card
9
+ * is in the Tab order, the arrow keys move to the next card and choose it,
10
+ * Home/End jump to the ends.
11
+ *
12
+ * <ChoiceCards v-model="look" :options="LOOKS" aria-label="Look">
13
+ * <template #preview="{ option }"><LookDrawing :id="option.value" /></template>
14
+ * </ChoiceCards>
15
+ */
16
+ import { nextTick, ref } from "vue";
17
+
18
+ export interface ChoiceCardOption<V extends string = string> {
19
+ value: V;
20
+ label: string;
21
+ hint?: string | undefined;
22
+ /** Inline style for the card, e.g. CSS variables its preview reads. */
23
+ style?: Record<string, string> | undefined;
24
+ }
25
+
26
+ const props = defineProps<{
27
+ modelValue: T;
28
+ options: ChoiceCardOption<T>[];
29
+ /** Three to a row, smaller, hint as tooltip: for narrow panels. */
30
+ compact?: boolean | undefined;
31
+ ariaLabel?: string | undefined;
32
+ /** `data-test` of each card is `<testPrefix>-<value>`. */
33
+ testPrefix?: string | undefined;
34
+ }>();
35
+
36
+ const emit = defineEmits<{ (e: "update:modelValue", v: T): void }>();
37
+ defineSlots<{ preview?(p: { option: ChoiceCardOption<T>; on: boolean }): unknown }>();
38
+ const rootEl = ref<HTMLElement | null>(null);
39
+
40
+ function pick(v: T): void {
41
+ if (v !== props.modelValue) emit("update:modelValue", v);
42
+ }
43
+
44
+ function tabStop(): T | undefined {
45
+ return (props.options.find((o) => o.value === props.modelValue) ?? props.options[0])?.value;
46
+ }
47
+
48
+ function onKeydown(ev: KeyboardEvent): void {
49
+ const all = props.options;
50
+ if (!all.length) return;
51
+ const at = all.findIndex((o) => o.value === props.modelValue);
52
+ let next: number;
53
+ switch (ev.key) {
54
+ case "ArrowRight": case "ArrowDown": next = (at + 1) % all.length; break;
55
+ case "ArrowLeft": case "ArrowUp": next = at < 0 ? all.length - 1 : (at - 1 + all.length) % all.length; break;
56
+ case "Home": next = 0; break;
57
+ case "End": next = all.length - 1; break;
58
+ default: return;
59
+ }
60
+ ev.preventDefault();
61
+ pick(all[next].value);
62
+ void nextTick(() => rootEl.value?.querySelectorAll<HTMLElement>("[role=radio]")[next]?.focus());
63
+ }
64
+ </script>
65
+
66
+ <template>
67
+ <div
68
+ ref="rootEl"
69
+ class="wp-choice-cards"
70
+ :class="{ 'wp-choice-cards--compact': compact }"
71
+ role="radiogroup"
72
+ :aria-label="ariaLabel"
73
+ >
74
+ <button
75
+ v-for="o in options"
76
+ :key="o.value"
77
+ :title="compact ? o.hint : undefined"
78
+ type="button"
79
+ role="radio"
80
+ class="wp-choice-card"
81
+ :aria-checked="modelValue === o.value"
82
+ :tabindex="o.value === tabStop() ? 0 : -1"
83
+ :data-test="testPrefix ? `${testPrefix}-${o.value}` : undefined"
84
+ :style="o.style"
85
+ @click="pick(o.value)"
86
+ @keydown="onKeydown"
87
+ >
88
+ <slot name="preview" :option="o" :on="modelValue === o.value" />
89
+ <span class="wp-choice-card__name">{{ o.label }}</span>
90
+ <span v-if="!compact && o.hint" class="wp-choice-card__hint">{{ o.hint }}</span>
91
+ </button>
92
+ </div>
93
+ </template>
94
+
95
+ <style scoped>
96
+ .wp-choice-cards {
97
+ display: grid;
98
+ grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
99
+ gap: var(--wp-space-4);
100
+ width: 100%;
101
+ }
102
+ .wp-choice-cards--compact { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--wp-space-3); }
103
+ .wp-choice-card {
104
+ display: flex;
105
+ flex-direction: column;
106
+ align-items: stretch;
107
+ gap: var(--wp-space-3);
108
+ min-width: 0;
109
+ padding: var(--wp-space-4);
110
+ border: 1px solid var(--wp-border);
111
+ border-radius: var(--wp-radius);
112
+ background: var(--wp-bg-2);
113
+ color: var(--wp-text);
114
+ font: inherit;
115
+ text-align: left;
116
+ cursor: pointer;
117
+ transition: border-color var(--wp-motion-hover), background var(--wp-motion-hover);
118
+ }
119
+ .wp-choice-card:hover { border-color: var(--wp-border-strong); background: var(--wp-bg-3); }
120
+ .wp-choice-card[aria-checked="true"] {
121
+ border-color: var(--wp-accent-500);
122
+ box-shadow: inset 0 0 0 1px var(--wp-accent-500);
123
+ background: color-mix(in oklab, var(--wp-accent-500) 10%, var(--wp-bg-2));
124
+ }
125
+ .wp-choice-card:focus-visible { outline: none; box-shadow: var(--wp-focus-ring); }
126
+ .wp-choice-card__name { font-size: var(--wp-text-base); font-weight: 600; line-height: var(--wp-line-base); }
127
+ .wp-choice-card__hint { font-size: var(--wp-text-xs); line-height: var(--wp-line-xs); color: var(--wp-text-muted); }
128
+ .wp-choice-cards--compact .wp-choice-card { padding: var(--wp-space-3); gap: var(--wp-space-2); }
129
+ .wp-choice-cards--compact .wp-choice-card__name { font-size: var(--wp-text-xs); }
130
+ @media (prefers-reduced-motion: reduce) {
131
+ .wp-choice-card { transition: none; }
132
+ }
133
+ </style>