Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): draw a focus ring on the login field with an error

Yudhi Armyndharisf75eea89c1de
diff --git a/dashboard/src/focus-ring.test.ts b/dashboard/src/focus-ring.test.ts
index 4ae1a2930..47509e930 100644
--- a/dashboard/src/focus-ring.test.ts
+++ b/dashboard/src/focus-ring.test.ts
@@ -134,7 +134,8 @@ test('every outline switched off leaves a keyboard focus cue in the --focus-ring
// These fields repaint their own border after the shared `select:focus` and `.modal-body input:focus` rules, which
// switch the outline off, so without a focus rule of their own a focused one looked like any other. A native checkbox
-// or radio shows no border at all, so the text field rules that reach it leave it only the ring.
+// or radio shows no border at all, so the text field rules that reach it leave it only the ring. The login field with
+// an error keeps its red border, so its focus cue is a ring beside it.
test('fields that repaint their own border and native checkboxes draw their own focus cue', () => {
for (const [selector, cue] of [
['.filter-row select:focus', /border-color:\s*var\(--focus-ring\)/],
@@ -143,6 +144,7 @@ test('fields that repaint their own border and native checkboxes draw their own
['.plugins-page .sessions-select:focus', /border-color:\s*var\(--focus-ring\)/],
[".filter-text input[type='text']:focus", /border-color:\s*var\(--focus-ring\)/],
['.chips-row .chips-text:focus', /border-color:\s*var\(--focus-ring\)/],
+ ['.login-container .input-wrapper input.error:focus', /outline:\s*2px solid var\(--focus-ring\)/],
["input[type='checkbox']:not(.toggle-switch input):focus-visible", /outline:\s*2px solid var\(--focus-ring\)/],
["input[type='radio']:not(.toggle-switch input):focus-visible", /outline:\s*2px solid var\(--focus-ring\)/],
] as const) {
@@ -163,17 +165,24 @@ const specificity = (selector: string): number => {
const elements = bare.match(/(?:^|[\s>+~])[a-z][\w-]*/gi)?.length ?? 0;
return ids * 10000 + classes * 100 + elements;
};
-// The field a selector ends in, whatever container it names: `.a .b input` and `.a .c input` both end in `input`.
-const lastElement = (s: string): string => element(s).split(/\s+/).pop() ?? '';
+// The tag, classes and ids of the field a selector ends in, whatever container it names: `.a .b input.c` ends in
+// `input` and `.c`.
+const subject = (s: string): string[] => (element(s).split(/\s+/).pop() ?? '').match(/[.#:]?[\w-]+/g) ?? [];
+// A rule reaches every field its focus rule does when it names at least what that rule names: `input.error` is an
+// `input`, so it reaches the fields `input:focus` does, and `input` does not reach the ones `input.error:focus` does.
+const reaches = (s: string, cue: string): boolean => subject(cue).every(part => subject(s).includes(part));
const unfocused = (s: string): string => s.replace(/:focus(?:-visible|-within)?\b/g, '');
const drawsFocusBorder = (r: Rule): boolean => /(?:^|;)\s*border(?:-color)?\s*:[^;]*\bvar\(--focus-ring\)/.test(r.body);
+// A border that carries a state (an error) cannot also be the focus border, so such a field keeps it and gets a ring.
+const drawsFocusRing = (r: Rule): boolean => /(?:^|;)\s*outline\s*:[^;]*\bvar\(--focus-ring\)/.test(r.body);
// `border: none` is a native checkbox or radio, which has no border to repaint and draws a ring instead.
const paintsBorder = (r: Rule): boolean => /(?:^|;)\s*border(?:-color)?\s*:\s*(?!none\b)\S/.test(r.body);
-// A rule for one kind of field (`input[type='color']`) that comes later in the same stylesheet than the focus rule of
-// the fields around it, at the same weight or heavier, repaints the border that rule drew, so a focused field looks like
-// an idle one. It needs a focus rule of its own, one step heavier. A field styled by a class from another stylesheet is
-// out of reach here and is pinned below.
+// A rule for one kind of field (`input[type='color']`) or one state of it (`input.error`) that comes later in the same
+// stylesheet than the focus rule of the fields around it, at the same weight or heavier, repaints the border that rule
+// drew, so a focused field looks like an idle one. It needs a focus rule of its own, one step heavier: a border in the
+// token, or a ring in the token where the border has to keep showing the state. A field styled by a class from another
+// stylesheet is out of reach here and is pinned below.
test('no later rule repaints the border a focus rule draws', () => {
const offenders = all.flatMap((rule, i) =>
rule.selectors
@@ -184,15 +193,13 @@ test('no later rule repaints the border a focus rule draws', () => {
.slice(i + 1)
.filter(later => later.file === rule.file && paintsBorder(later))
.flatMap(later => later.selectors.map(s => ({ s, at: later.file })))
- .filter(
- ({ s }) => !focusState(s) && lastElement(s) === lastElement(cue) && specificity(s) >= specificity(cue),
- ),
+ .filter(({ s }) => !focusState(s) && reaches(s, cue) && specificity(s) >= specificity(cue)),
),
);
const restored = (s: string): boolean =>
all.some(
r =>
- drawsFocusBorder(r) &&
+ (drawsFocusBorder(r) || drawsFocusRing(r)) &&
r.selectors.some(f => focusState(f) && unfocused(f) === s && specificity(f) > specificity(s)),
);
assert.deepEqual([...new Set(offenders.filter(({ s }) => !restored(s)).map(({ at, s }) => `${at}: ${s}`))], []);
diff --git a/dashboard/src/pages/Login.css b/dashboard/src/pages/Login.css
index 57649ec73..f6dfa1684 100644
--- a/dashboard/src/pages/Login.css
+++ b/dashboard/src/pages/Login.css
@@ -120,6 +120,12 @@
border-color: var(--error);
}
+/* The red border keeps showing the error while the field is focused, so the focus cue is a ring beside it. */
+.login-container .input-wrapper input.error:focus {
+ outline: 2px solid var(--focus-ring);
+ outline-offset: 2px;
+}
+
.login-container .toggle-visibility {
position: absolute;
inset-inline-end: 0.75rem;