@keystone-sites/marketing-design-system 1.0.24 → 1.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/primitives/business-picker.tsx +21 -7
- package/primitives/grader.tsx +34 -69
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keystone-sites/marketing-design-system",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.25",
|
|
4
4
|
"description": "Keystone's marketing design system: tokens, the tick grid, type, primitives, site chrome, and the landing kit. Source-shipped CSS and TSX for Next.js consumers.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -53,9 +53,13 @@ interface BusinessPickerProps {
|
|
|
53
53
|
|
|
54
54
|
interface ActiveRow {
|
|
55
55
|
index: number;
|
|
56
|
+
/** The query the index belongs to; a new answer highlights no row. */
|
|
56
57
|
resultsFor: string;
|
|
57
58
|
}
|
|
58
59
|
|
|
60
|
+
/** No row highlighted: Enter adds nothing until the visitor moves to one. */
|
|
61
|
+
const NO_ROW = -1;
|
|
62
|
+
|
|
59
63
|
/** The address trimmed to what tells one business from its namesake:
|
|
60
64
|
* the street, or the website's domain. */
|
|
61
65
|
function contextOf(item: GraderSuggestion): string {
|
|
@@ -92,14 +96,15 @@ export function BusinessPicker({
|
|
|
92
96
|
const [query, setQuery] = useState("");
|
|
93
97
|
const [focused, setFocused] = useState(false);
|
|
94
98
|
const [dismissed, setDismissed] = useState(false);
|
|
95
|
-
const [active, setActive] = useState<ActiveRow>({ index:
|
|
99
|
+
const [active, setActive] = useState<ActiveRow>({ index: NO_ROW, resultsFor: "" });
|
|
96
100
|
|
|
97
101
|
const full = chosen.length >= max;
|
|
98
102
|
const search = useGraderSearch(query, !full);
|
|
103
|
+
/** The row the visitor highlighted among the current results, or none. */
|
|
99
104
|
const activeIndex =
|
|
100
105
|
active.resultsFor === search.resultsFor
|
|
101
|
-
? Math.min(active.index,
|
|
102
|
-
:
|
|
106
|
+
? Math.min(active.index, search.results.length - 1)
|
|
107
|
+
: NO_ROW;
|
|
103
108
|
const open = focused && !dismissed && !full && search.results.length > 0;
|
|
104
109
|
const searching = search.status === "loading";
|
|
105
110
|
const has = (item: GraderSuggestion) => chosen.some((c) => c.id === item.id);
|
|
@@ -125,12 +130,19 @@ export function BusinessPicker({
|
|
|
125
130
|
if (!count) return;
|
|
126
131
|
e.preventDefault();
|
|
127
132
|
setDismissed(false);
|
|
128
|
-
|
|
129
|
-
|
|
133
|
+
// Down from no row lands on the first; up from none or the first wraps to the last.
|
|
134
|
+
const index =
|
|
135
|
+
e.key === "ArrowDown"
|
|
136
|
+
? (activeIndex + 1) % count
|
|
137
|
+
: activeIndex <= 0
|
|
138
|
+
? count - 1
|
|
139
|
+
: activeIndex - 1;
|
|
140
|
+
setActive({ index, resultsFor: search.resultsFor });
|
|
130
141
|
break;
|
|
131
142
|
}
|
|
132
143
|
case "Enter": {
|
|
133
|
-
// The picker lives inside a form: Enter
|
|
144
|
+
// The picker lives inside a form: Enter adds the highlighted row, never
|
|
145
|
+
// submits — and never takes the top row on the visitor's behalf.
|
|
134
146
|
e.preventDefault();
|
|
135
147
|
const match = open ? search.results[activeIndex] : undefined;
|
|
136
148
|
if (match) add(match);
|
|
@@ -242,7 +254,9 @@ export function BusinessPicker({
|
|
|
242
254
|
aria-busy={searching || undefined}
|
|
243
255
|
aria-expanded={open}
|
|
244
256
|
aria-controls={open ? listId : undefined}
|
|
245
|
-
aria-activedescendant={
|
|
257
|
+
aria-activedescendant={
|
|
258
|
+
open && activeIndex !== NO_ROW ? optionId(activeIndex) : undefined
|
|
259
|
+
}
|
|
246
260
|
aria-invalid={error ? true : undefined}
|
|
247
261
|
aria-describedby={error ? errorId : undefined}
|
|
248
262
|
placeholder={chosen.length ? "" : placeholder}
|
package/primitives/grader.tsx
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
/** Business grader search.
|
|
4
|
-
* A no-JS submission still opens the Grader search.
|
|
5
|
-
*
|
|
4
|
+
* A no-JS submission still opens the Grader search. With JS, only a
|
|
5
|
+
* suggestion the visitor picked — clicked, or highlighted and entered —
|
|
6
|
+
* opens the Grader: submitting typed text never takes the top result on
|
|
7
|
+
* the visitor's behalf, so an email address or a stray name goes
|
|
8
|
+
* nowhere until a business is chosen from the list.
|
|
6
9
|
* A host that is the Grader itself passes `onChoose` and navigates in place. */
|
|
7
10
|
|
|
8
|
-
import {
|
|
11
|
+
import { useId, useState } from "react";
|
|
9
12
|
import type { FocusEvent, FormEvent, KeyboardEvent } from "react";
|
|
10
13
|
import { ButtonArrow } from "./buttons";
|
|
11
14
|
import { GraderSelectMenu, type GraderMenuLabels } from "./grader-menu";
|
|
@@ -43,10 +46,13 @@ interface GraderInputProps {
|
|
|
43
46
|
|
|
44
47
|
interface ActiveRow {
|
|
45
48
|
index: number;
|
|
46
|
-
/** The query the index belongs to; a new answer
|
|
49
|
+
/** The query the index belongs to; a new answer highlights no row. */
|
|
47
50
|
resultsFor: string;
|
|
48
51
|
}
|
|
49
52
|
|
|
53
|
+
/** No row highlighted: a new answer opens with nothing picked. */
|
|
54
|
+
const NO_ROW = -1;
|
|
55
|
+
|
|
50
56
|
export function GraderInput({
|
|
51
57
|
size = "lg",
|
|
52
58
|
chrome = "teal",
|
|
@@ -70,21 +76,18 @@ export function GraderInput({
|
|
|
70
76
|
const [focused, setFocused] = useState(false);
|
|
71
77
|
/** Escape hides the menu until the next edit or arrow key. */
|
|
72
78
|
const [dismissed, setDismissed] = useState(false);
|
|
73
|
-
const [active, setActive] = useState<ActiveRow>({ index:
|
|
79
|
+
const [active, setActive] = useState<ActiveRow>({ index: NO_ROW, resultsFor: "" });
|
|
74
80
|
const [chosen, setChosen] = useState<GraderSuggestion | null>(null);
|
|
75
81
|
const [error, setError] = useState(false);
|
|
76
|
-
const [pending, setPending] = useState(false);
|
|
77
|
-
const pendingTab = useRef<Window | null>(null);
|
|
78
|
-
/** A submit is waiting on the first ready result. */
|
|
79
|
-
const deferred = useRef(false);
|
|
80
82
|
|
|
81
83
|
const search = useGraderSearch(query, !chosen);
|
|
82
84
|
const trimmed = query.trim();
|
|
85
|
+
/** The row the visitor highlighted among the current results, or none. */
|
|
83
86
|
const activeIndex =
|
|
84
87
|
active.resultsFor === search.resultsFor
|
|
85
|
-
? Math.min(active.index,
|
|
86
|
-
:
|
|
87
|
-
const open = focused && !dismissed && !chosen && !error &&
|
|
88
|
+
? Math.min(active.index, search.results.length - 1)
|
|
89
|
+
: NO_ROW;
|
|
90
|
+
const open = focused && !dismissed && !chosen && !error && search.results.length > 0;
|
|
88
91
|
const searching = search.status === "loading";
|
|
89
92
|
|
|
90
93
|
const inPlace = Boolean(onChoose);
|
|
@@ -101,65 +104,22 @@ export function GraderInput({
|
|
|
101
104
|
openGrader(item);
|
|
102
105
|
}
|
|
103
106
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (search.status === "ready") {
|
|
108
|
-
const match = search.results[0];
|
|
109
|
-
if (match) {
|
|
110
|
-
setChosen(match);
|
|
111
|
-
setQuery(match.name);
|
|
112
|
-
} else {
|
|
113
|
-
setError(true);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Resolve a deferred submission: navigate the pre-opened tab to the
|
|
119
|
-
// match, or close it when none exists. In place, the match navigates
|
|
120
|
-
// this document instead.
|
|
121
|
-
useEffect(() => {
|
|
122
|
-
if (pending || !deferred.current) return;
|
|
123
|
-
deferred.current = false;
|
|
124
|
-
const tab = pendingTab.current;
|
|
125
|
-
pendingTab.current = null;
|
|
126
|
-
if (inPlace) {
|
|
127
|
-
if (chosen) onChoose?.(chosen);
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
if (!tab) return;
|
|
131
|
-
if (chosen && !tab.closed) {
|
|
132
|
-
tab.opener = null;
|
|
133
|
-
tab.location.href = graderScanUrl(chosen);
|
|
134
|
-
} else {
|
|
135
|
-
tab.close();
|
|
136
|
-
}
|
|
137
|
-
}, [pending, chosen, inPlace, onChoose]);
|
|
138
|
-
|
|
139
|
-
// Never leave a blank tab behind if the island unmounts mid-wait.
|
|
140
|
-
useEffect(
|
|
141
|
-
() => () => {
|
|
142
|
-
pendingTab.current?.close();
|
|
143
|
-
pendingTab.current = null;
|
|
144
|
-
},
|
|
145
|
-
[],
|
|
146
|
-
);
|
|
147
|
-
|
|
107
|
+
/** Enter or the submit button: the chosen or highlighted business opens
|
|
108
|
+
* the Grader; typed text alone only shows the suggestions — or says there
|
|
109
|
+
* are none — and waits for a pick. */
|
|
148
110
|
function submit() {
|
|
149
|
-
if (!trimmed ||
|
|
111
|
+
if (!trimmed || searching) return;
|
|
150
112
|
if (chosen && chosen.name === trimmed) {
|
|
151
113
|
openGrader(chosen);
|
|
152
114
|
return;
|
|
153
115
|
}
|
|
154
|
-
if (search.
|
|
155
|
-
|
|
156
|
-
if (!inPlace) pendingTab.current = window.open("", "_blank");
|
|
157
|
-
setPending(true);
|
|
116
|
+
if (search.results.length === 0) {
|
|
117
|
+
setError(true);
|
|
158
118
|
return;
|
|
159
119
|
}
|
|
160
|
-
const
|
|
161
|
-
if (
|
|
162
|
-
else
|
|
120
|
+
const highlighted = open ? search.results[activeIndex] : undefined;
|
|
121
|
+
if (highlighted) choose(highlighted);
|
|
122
|
+
else setDismissed(false);
|
|
163
123
|
}
|
|
164
124
|
|
|
165
125
|
function onSubmit(e: FormEvent<HTMLFormElement>) {
|
|
@@ -184,8 +144,14 @@ export function GraderInput({
|
|
|
184
144
|
if (!count) return;
|
|
185
145
|
e.preventDefault();
|
|
186
146
|
setDismissed(false);
|
|
187
|
-
|
|
188
|
-
|
|
147
|
+
// Down from no row lands on the first; up from none or the first wraps to the last.
|
|
148
|
+
const index =
|
|
149
|
+
e.key === "ArrowDown"
|
|
150
|
+
? (activeIndex + 1) % count
|
|
151
|
+
: activeIndex <= 0
|
|
152
|
+
? count - 1
|
|
153
|
+
: activeIndex - 1;
|
|
154
|
+
setActive({ index, resultsFor: search.resultsFor });
|
|
189
155
|
break;
|
|
190
156
|
}
|
|
191
157
|
case "Home":
|
|
@@ -250,7 +216,7 @@ export function GraderInput({
|
|
|
250
216
|
aria-busy={searching || undefined}
|
|
251
217
|
aria-expanded={open}
|
|
252
218
|
aria-controls={open ? listId : undefined}
|
|
253
|
-
aria-activedescendant={open ? optionId(activeIndex) : undefined}
|
|
219
|
+
aria-activedescendant={open && activeIndex !== NO_ROW ? optionId(activeIndex) : undefined}
|
|
254
220
|
aria-invalid={error || undefined}
|
|
255
221
|
aria-describedby={error ? errorId : undefined}
|
|
256
222
|
placeholder={placeholder}
|
|
@@ -264,8 +230,7 @@ export function GraderInput({
|
|
|
264
230
|
shape={shape}
|
|
265
231
|
// The box's submit is ink in every state; the pill's goes gray on error.
|
|
266
232
|
chrome={shape === "box" ? "ink" : error ? "gray" : chrome}
|
|
267
|
-
|
|
268
|
-
loading={pending || searching}
|
|
233
|
+
loading={searching}
|
|
269
234
|
label={submitLabel}
|
|
270
235
|
/>
|
|
271
236
|
</div>
|