Merge pull request 'Stop dark text fields from looking pre-selected when they auto-focus.' (#117) from adilallo/fix/TextInputFocusHighlight into main
Reviewed-on: #117
This commit was merged in pull request #117.
This commit is contained in:
commit
6903cec32b
6 files changed
+111
-22
No files matched your search
@@ -21,6 +21,14 @@ import { useTranslation } from "../../../contexts/MessagesContext";
|
|||||||
import { SelectInputView } from "./SelectInput.view";
|
import { SelectInputView } from "./SelectInput.view";
|
||||||
import type { SelectInputProps, SelectOptionData } from "./SelectInput.types";
|
import type { SelectInputProps, SelectOptionData } from "./SelectInput.types";
|
||||||
|
|
||||||
|
function isFocusVisible(el: Element): boolean {
|
||||||
|
try {
|
||||||
|
return el.matches(":focus-visible");
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Figma: "Control / SelectInput". Custom-styled select dropdown
|
* Figma: "Control / SelectInput". Custom-styled select dropdown
|
||||||
* with a labelled trigger button and floating option menu.
|
* with a labelled trigger button and floating option menu.
|
||||||
@@ -277,11 +285,15 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
// Handle focus to detect mouse vs keyboard
|
|
||||||
const handleFocus = useCallback(() => {
|
const handleFocus = useCallback(() => {
|
||||||
if (disabled) return;
|
if (disabled) return;
|
||||||
|
|
||||||
const method = wasMouseDownRef.current ? "mouse" : "keyboard";
|
const el = selectRef.current;
|
||||||
|
const method = wasMouseDownRef.current
|
||||||
|
? "mouse"
|
||||||
|
: el && isFocusVisible(el)
|
||||||
|
? "keyboard"
|
||||||
|
: null;
|
||||||
|
|
||||||
if (shouldAutoManageFocus) {
|
if (shouldAutoManageFocus) {
|
||||||
setIsFocused(true);
|
setIsFocused(true);
|
||||||
@@ -308,15 +320,13 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
|
|||||||
[closeMenu, shouldAutoManageFocus],
|
[closeMenu, shouldAutoManageFocus],
|
||||||
);
|
);
|
||||||
|
|
||||||
// Determine actual state:
|
|
||||||
// - Active: when clicked (mouse focus) or when dropdown is open
|
|
||||||
// - Focus: when tabbed (keyboard focus)
|
|
||||||
// - Default: when not focused
|
|
||||||
const actualState = shouldAutoManageFocus
|
const actualState = shouldAutoManageFocus
|
||||||
? isOpen || isFocused
|
? isOpen || isFocused
|
||||||
? focusMethod === "mouse" || isOpen
|
? focusMethod === "mouse" || isOpen
|
||||||
? "active"
|
? "active"
|
||||||
: "focus"
|
: focusMethod === "keyboard"
|
||||||
|
? "focus"
|
||||||
|
: "default"
|
||||||
: "default"
|
: "default"
|
||||||
: externalState;
|
: externalState;
|
||||||
|
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ const TextAreaContainer = forwardRef<HTMLTextAreaElement, TextAreaProps>(
|
|||||||
: `${currentSize.label} `;
|
: `${currentSize.label} `;
|
||||||
|
|
||||||
const textareaClasses = `
|
const textareaClasses = `
|
||||||
scrollbar-design w-full transition-all duration-200 ease-in-out resize-none
|
appearance-none scrollbar-design w-full transition-all duration-200 ease-in-out resize-none
|
||||||
${appearance === "embedded" || appearance === "compose" ? "rounded-[var(--radius-300)]" : "border"}
|
${appearance === "embedded" || appearance === "compose" ? "rounded-[var(--radius-300)]" : "border"}
|
||||||
${appearance === "compose" ? "text-medium-paragraph" : currentSize.textarea}
|
${appearance === "compose" ? "text-medium-paragraph" : currentSize.textarea}
|
||||||
${stateStyles.textarea}
|
${stateStyles.textarea}
|
||||||
|
|||||||
@@ -6,6 +6,14 @@ import { useTranslation } from "../../../contexts/MessagesContext";
|
|||||||
import { TextInputView } from "./TextInput.view";
|
import { TextInputView } from "./TextInput.view";
|
||||||
import type { TextInputProps } from "./TextInput.types";
|
import type { TextInputProps } from "./TextInput.types";
|
||||||
|
|
||||||
|
function isFocusVisible(el: Element): boolean {
|
||||||
|
try {
|
||||||
|
return el.matches(":focus-visible");
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Figma: "Control / TextInput". Single-line text input with size
|
* Figma: "Control / TextInput". Single-line text input with size
|
||||||
* variants and managed default/active/focus/error states.
|
* variants and managed default/active/focus/error states.
|
||||||
@@ -60,15 +68,13 @@ const TextInputContainer = forwardRef<HTMLInputElement, TextInputProps>(
|
|||||||
const shouldAutoManageFocus =
|
const shouldAutoManageFocus =
|
||||||
externalState === "default" || externalState === undefined;
|
externalState === "default" || externalState === undefined;
|
||||||
|
|
||||||
// Determine actual state:
|
|
||||||
// - Active: when clicked (mouse focus)
|
|
||||||
// - Focus: when tabbed (keyboard focus)
|
|
||||||
// - Default: when not focused
|
|
||||||
const actualState = shouldAutoManageFocus
|
const actualState = shouldAutoManageFocus
|
||||||
? isFocused
|
? isFocused
|
||||||
? focusMethod === "mouse"
|
? focusMethod === "mouse"
|
||||||
? "active"
|
? "active"
|
||||||
: "focus"
|
: focusMethod === "keyboard"
|
||||||
|
? "focus"
|
||||||
|
: "default"
|
||||||
: "default"
|
: "default"
|
||||||
: externalState;
|
: externalState;
|
||||||
|
|
||||||
@@ -180,8 +186,9 @@ const TextInputContainer = forwardRef<HTMLInputElement, TextInputProps>(
|
|||||||
|
|
||||||
// Base classes without border (border is added in state styles)
|
// Base classes without border (border is added in state styles)
|
||||||
const inputClasses = `
|
const inputClasses = `
|
||||||
w-full transition-all duration-200 ease-in-out
|
appearance-none w-full transition-all duration-200 ease-in-out
|
||||||
focus:outline-none focus:ring-0
|
focus:outline-none focus:ring-0 focus:shadow-none
|
||||||
|
caret-[var(--color-content-default-primary)]
|
||||||
placeholder:text-[var(--color-content-default-tertiary)]
|
placeholder:text-[var(--color-content-default-tertiary)]
|
||||||
${sizeStyles.input}
|
${sizeStyles.input}
|
||||||
${stateStyles.input}
|
${stateStyles.input}
|
||||||
@@ -216,19 +223,18 @@ const TextInputContainer = forwardRef<HTMLInputElement, TextInputProps>(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Custom focus handler to detect mouse vs keyboard
|
|
||||||
const handleFocus = (e: React.FocusEvent<HTMLInputElement>) => {
|
const handleFocus = (e: React.FocusEvent<HTMLInputElement>) => {
|
||||||
if (disabled) return;
|
if (disabled) return;
|
||||||
|
|
||||||
// Detect if focus came from keyboard (Tab) or mouse (click)
|
const method = wasMouseDownRef.current
|
||||||
// If mouseDown was detected before focus, it's a mouse click (active)
|
? "mouse"
|
||||||
// Otherwise, it's keyboard navigation (focus)
|
: isFocusVisible(e.currentTarget)
|
||||||
const method = wasMouseDownRef.current ? "mouse" : "keyboard";
|
? "keyboard"
|
||||||
|
: null;
|
||||||
|
|
||||||
if (shouldAutoManageFocus) {
|
if (shouldAutoManageFocus) {
|
||||||
setIsFocused(true);
|
setIsFocused(true);
|
||||||
setFocusMethod(method);
|
setFocusMethod(method);
|
||||||
// Reset mouse down flag after focus is processed
|
|
||||||
wasMouseDownRef.current = false;
|
wasMouseDownRef.current = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,6 +70,39 @@
|
|||||||
transform: translateY(0);
|
transform: translateY(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Dark text fields: drop OS chrome (Safari/Chrome inner bevel on focus)
|
||||||
|
* and the yellow autofill wash. Utilities on each control still win.
|
||||||
|
*/
|
||||||
|
@layer base {
|
||||||
|
input[type="text"],
|
||||||
|
input[type="email"],
|
||||||
|
input[type="password"],
|
||||||
|
input[type="search"],
|
||||||
|
input[type="url"],
|
||||||
|
input[type="tel"],
|
||||||
|
input[type="number"],
|
||||||
|
input:not([type]),
|
||||||
|
textarea {
|
||||||
|
appearance: none;
|
||||||
|
color-scheme: dark;
|
||||||
|
caret-color: var(--color-content-default-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
input:-webkit-autofill,
|
||||||
|
input:-webkit-autofill:hover,
|
||||||
|
input:-webkit-autofill:focus,
|
||||||
|
input:-webkit-autofill:active,
|
||||||
|
textarea:-webkit-autofill,
|
||||||
|
textarea:-webkit-autofill:hover,
|
||||||
|
textarea:-webkit-autofill:focus {
|
||||||
|
-webkit-text-fill-color: var(--color-content-default-primary);
|
||||||
|
caret-color: var(--color-content-default-primary);
|
||||||
|
box-shadow: inset 0 0 0 100vmax var(--color-surface-default-secondary);
|
||||||
|
transition: background-color 99999s ease-out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@theme inline {
|
@theme inline {
|
||||||
/* Custom breakpoints */
|
/* Custom breakpoints */
|
||||||
--breakpoint-xsm: 429px;
|
--breakpoint-xsm: 429px;
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ describe("TextArea appearance", () => {
|
|||||||
const textarea = screen.getByRole("textbox", { name: /notes/i });
|
const textarea = screen.getByRole("textbox", { name: /notes/i });
|
||||||
expect(textarea).toBeInTheDocument();
|
expect(textarea).toBeInTheDocument();
|
||||||
expect(textarea).toHaveClass("border-0");
|
expect(textarea).toHaveClass("border-0");
|
||||||
|
expect(textarea).toHaveClass("appearance-none");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps a programmatic label when the visible header is off", () => {
|
it("keeps a programmatic label when the visible header is off", () => {
|
||||||
|
|||||||
@@ -1,10 +1,17 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { describe, it, expect } from "vitest";
|
import { describe, it, expect, vi } from "vitest";
|
||||||
|
import { fireEvent } from "@testing-library/react";
|
||||||
import { renderWithProviders as render } from "../utils/test-utils";
|
import { renderWithProviders as render } from "../utils/test-utils";
|
||||||
import "@testing-library/jest-dom/vitest";
|
import "@testing-library/jest-dom/vitest";
|
||||||
import TextInput from "../../app/components/controls/TextInput";
|
import TextInput from "../../app/components/controls/TextInput";
|
||||||
import { componentTestSuite } from "../utils/componentTestSuite";
|
import { componentTestSuite } from "../utils/componentTestSuite";
|
||||||
|
|
||||||
|
function keyboardFocusRing(container: HTMLElement) {
|
||||||
|
return [...container.querySelectorAll('[aria-hidden="true"]')].find((el) =>
|
||||||
|
el.className.includes("shadow-[var(--shadow-focus-default)]"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
type TextInputProps = React.ComponentProps<typeof TextInput>;
|
type TextInputProps = React.ComponentProps<typeof TextInput>;
|
||||||
|
|
||||||
componentTestSuite<TextInputProps>({
|
componentTestSuite<TextInputProps>({
|
||||||
@@ -82,6 +89,38 @@ describe("TextInput (size tests)", () => {
|
|||||||
expect(input).toHaveAttribute("form", "create-flow-community-save");
|
expect(input).toHaveAttribute("form", "create-flow-community-save");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("does not draw the keyboard ring on programmatic focus", () => {
|
||||||
|
const { container } = render(<TextInput label="Email address" />);
|
||||||
|
const input = container.querySelector("input");
|
||||||
|
expect(input).not.toBeNull();
|
||||||
|
fireEvent.focus(input!);
|
||||||
|
expect(keyboardFocusRing(container)).toBeUndefined();
|
||||||
|
expect(input).toHaveClass("appearance-none");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("draws the keyboard ring when :focus-visible", () => {
|
||||||
|
const { container } = render(<TextInput label="Email address" />);
|
||||||
|
const input = container.querySelector("input");
|
||||||
|
expect(input).not.toBeNull();
|
||||||
|
vi.spyOn(input!, "matches").mockImplementation(
|
||||||
|
(selector: string) => selector === ":focus-visible",
|
||||||
|
);
|
||||||
|
fireEvent.focus(input!);
|
||||||
|
expect(keyboardFocusRing(container)?.className).toContain(
|
||||||
|
"shadow-[var(--shadow-focus-default)]",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses the active border on pointer focus, not the keyboard ring", () => {
|
||||||
|
const { container } = render(<TextInput label="Email address" />);
|
||||||
|
const input = container.querySelector("input");
|
||||||
|
expect(input).not.toBeNull();
|
||||||
|
fireEvent.mouseDown(input!);
|
||||||
|
fireEvent.focus(input!);
|
||||||
|
expect(keyboardFocusRing(container)).toBeUndefined();
|
||||||
|
expect(input).toHaveClass("border-2");
|
||||||
|
});
|
||||||
|
|
||||||
it("keeps read-only values at full contrast", () => {
|
it("keeps read-only values at full contrast", () => {
|
||||||
const { container } = render(
|
const { container } = render(
|
||||||
<TextInput
|
<TextInput
|
||||||
|
|||||||
Reference in new issue
Block a user