Stop dark text fields from looking pre-selected when they auto-focus.
This commit is contained in:
1 parent
36112f95d3
commit
6aa06126b5
6 files changed
+111
-22
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user