fix: Keep style source clicks from focusing the input (#6717)

Clicking a style source or the empty space around it no longer moves
focus to the new source input. Keyboard navigation that focuses the
field container still moves focus into the input.

The field now forwards focus only when the container itself receives
focus. It prevents a mouse click on empty container space from
triggering that focus path. Source buttons keep their own focus and menu
behavior.

- [x] Update focus behavior for source controls and the field container.
- [x] Add a browser regression test; confirmed it failed before the fix
and passes now.
- [x] Run the focused browser tests (14 passed), Builder typecheck,
formatting, and lint.
- [x] Review documentation impact: existing Style panel guidance still
applies; no doc change needed.
- [x] Review fixture impact: no fixture inputs or outputs changed.
- [ ] CI: Builder core jobs and visual regression remain red for the
separate failures below.

CI findings on the latest commit:
- The changed Style sources test passes in the full Builder suite.
Builder app tests, end to end shards, lint, and submodule checks pass.
- Builder core jobs fail in static publish tests with varying timeouts
and assertions under the full suite. The five static publish tests pass
when run alone locally. See the [main
workflow](https://github.com/webstudio-is/webstudio/actions/runs/44212698257).
- Visual regression reports a 440 pixel difference in the Color Picker
Oklch story. This PR does not change that story or its styles. The same
difference appeared on retry. See the [visual
workflow](https://github.com/webstudio-is/webstudio/actions/runs/44212698286).

No migrations or data changes.

Closes #6716
This commit is contained in:
Oleg Isonen authored and GitHub committed 2026-11-18 03:45:00 +01:00
1 parent b9801bb731
commit 02f62eeaf2
2 files changed
+56 -10

No files matched your search

@@ -581,3 +581,48 @@ test("keeps menu arrow keys from toggling the outer combobox", () => {
container.remove();
}
});
test("clicks preserve focus while focusing the container moves focus to the input", async () => {
const container = document.createElement("div");
document.body.appendChild(container);
const root = createRoot(container);
try {
act(() =>
root.render(
<TooltipProvider>
<StyleSourceInput
inputRef={() => {}}
selectedItemSelector={undefined}
value={[
{
id: "token",
label: "Token",
source: "token",
disabled: false,
locked: false,
states: [],
},
]}
/>
</TooltipProvider>
)
);
const input = container.querySelector<HTMLInputElement>("input")!;
const sourceButton = container.querySelector<HTMLButtonElement>(
'[data-id="token"] button'
)!;
act(() => sourceButton.focus());
act(() => sourceButton.click());
expect(document.activeElement).toBe(sourceButton);
const field = input.closest<HTMLElement>('[tabindex="-1"]')!;
await act(async () => userEvent.click(field, { position: { x: 1, y: 1 } }));
expect(document.activeElement).toBe(sourceButton);
act(() => field.focus());
expect(document.activeElement).toBe(input);
} finally {
act(() => root.unmount());
container.remove();
}
});
@@ -42,7 +42,6 @@ import {
type RefObject,
type ReactNode,
useRef,
useCallback,
} from "react";
import { mergeRefs } from "@react-aria/utils";
import {
@@ -201,10 +200,6 @@ const TextFieldBase: ForwardRefRenderFunction<
const { focusWithinProps } = useFocusWithin({ onBlurWithin: onBlur });
const onClickCapture = useCallback(() => {
internalInputRef.current?.focus();
}, [internalInputRef]);
const { stylesByStyleSourceId } = useStore($stylesIndex);
const hasStylesMap = getHasStylesMap(value, stylesByStyleSourceId);
const focusOrder = [
@@ -256,11 +251,17 @@ const TextFieldBase: ForwardRefRenderFunction<
return (
<TextFieldContainer
{...focusWithinProps}
onClickCapture={onClickCapture}
// Setting tabIndex to -1 to allow this element to be focused via JavaScript.
// This is used when we need to hide the caret but want to:
// 1. keep the visual focused state of the component
// 2. keep focus somewhere insisde the component to not trigger some focus-trap logic
onMouseDown={(event) => {
if (event.target === event.currentTarget) {
event.preventDefault();
}
}}
onFocus={(event) => {
if (event.target === event.currentTarget) {
internalInputRef.current?.focus();
}
}}
// Allow keyboard navigation to focus this container and move focus to the input.
tabIndex={-1}
ref={mergeRefs(forwardedRef, containerRef ?? null, sortableRefCallback)}
css={css}