Stop dark text fields from looking pre-selected when they auto-focus. #117
No files matched your search
@@ -21,6 +21,14 @@ import { useTranslation } from "../../../contexts/MessagesContext";
|
||||
import { SelectInputView } from "./SelectInput.view";
|
||||
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
|
||||
* 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(() => {
|
||||
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) {
|
||||
setIsFocused(true);
|
||||
@@ -308,15 +320,13 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
|
||||
[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
|
||||
? isOpen || isFocused
|
||||
? focusMethod === "mouse" || isOpen
|
||||
? "active"
|
||||
: "focus"
|
||||
: focusMethod === "keyboard"
|
||||
? "focus"
|
||||
: "default"
|
||||
: "default"
|
||||
: externalState;
|
||||
|
||||
|
||||
@@ -175,7 +175,7 @@ const TextAreaContainer = forwardRef<HTMLTextAreaElement, TextAreaProps>(
|
||||
: `${currentSize.label} `;
|
||||
|
||||
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 === "compose" ? "text-medium-paragraph" : currentSize.textarea}
|
||||
${stateStyles.textarea}
|
||||
|
||||
@@ -6,6 +6,14 @@ import { useTranslation } from "../../../contexts/MessagesContext";
|
||||
import { TextInputView } from "./TextInput.view";
|
||||
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
|
||||
* variants and managed default/active/focus/error states.
|
||||
@@ -60,15 +68,13 @@ const TextInputContainer = forwardRef<HTMLInputElement, TextInputProps>(
|
||||
const shouldAutoManageFocus =
|
||||
externalState === "default" || externalState === undefined;
|
||||
|
||||
// Determine actual state:
|
||||
// - Active: when clicked (mouse focus)
|
||||
// - Focus: when tabbed (keyboard focus)
|
||||
// - Default: when not focused
|
||||
const actualState = shouldAutoManageFocus
|
||||
? isFocused
|
||||
? focusMethod === "mouse"
|
||||
? "active"
|
||||
: "focus"
|
||||
: focusMethod === "keyboard"
|
||||
? "focus"
|
||||
: "default"
|
||||
: "default"
|
||||
: externalState;
|
||||
|
||||
@@ -180,8 +186,9 @@ const TextInputContainer = forwardRef<HTMLInputElement, TextInputProps>(
|
||||
|
||||
// Base classes without border (border is added in state styles)
|
||||
const inputClasses = `
|
||||
w-full transition-all duration-200 ease-in-out
|
||||
focus:outline-none focus:ring-0
|
||||
appearance-none w-full transition-all duration-200 ease-in-out
|
||||
focus:outline-none focus:ring-0 focus:shadow-none
|
||||
caret-[var(--color-content-default-primary)]
|
||||
placeholder:text-[var(--color-content-default-tertiary)]
|
||||
${sizeStyles.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>) => {
|
||||
if (disabled) return;
|
||||
|
||||
// Detect if focus came from keyboard (Tab) or mouse (click)
|
||||
// If mouseDown was detected before focus, it's a mouse click (active)
|
||||
// Otherwise, it's keyboard navigation (focus)
|
||||
const method = wasMouseDownRef.current ? "mouse" : "keyboard";
|
||||
const method = wasMouseDownRef.current
|
||||
? "mouse"
|
||||
: isFocusVisible(e.currentTarget)
|
||||
? "keyboard"
|
||||
: null;
|
||||
|
||||
if (shouldAutoManageFocus) {
|
||||
setIsFocused(true);
|
||||
setFocusMethod(method);
|
||||
// Reset mouse down flag after focus is processed
|
||||
wasMouseDownRef.current = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,6 +70,39 @@
|
||||
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 {
|
||||
/* Custom breakpoints */
|
||||
--breakpoint-xsm: 429px;
|
||||
|
||||
@@ -41,6 +41,7 @@ describe("TextArea appearance", () => {
|
||||
const textarea = screen.getByRole("textbox", { name: /notes/i });
|
||||
expect(textarea).toBeInTheDocument();
|
||||
expect(textarea).toHaveClass("border-0");
|
||||
expect(textarea).toHaveClass("appearance-none");
|
||||
});
|
||||
|
||||
it("keeps a programmatic label when the visible header is off", () => {
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
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 "@testing-library/jest-dom/vitest";
|
||||
import TextInput from "../../app/components/controls/TextInput";
|
||||
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>;
|
||||
|
||||
componentTestSuite<TextInputProps>({
|
||||
@@ -82,6 +89,38 @@ describe("TextInput (size tests)", () => {
|
||||
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", () => {
|
||||
const { container } = render(
|
||||
<TextInput
|
||||
|
||||
Reference in new issue
Block a user