From 02f62eeaf2a97a2cdd4dd8dd5cfb5acf67dcab5e Mon Sep 17 00:00:00 2001 From: Oleg Isonen Date: Wed, 18 Nov 2026 02:45:00 +0000 Subject: [PATCH] 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 --- .../style-source/style-source-input.test.tsx | 45 +++++++++++++++++++ .../style-source/style-source-input.tsx | 21 ++++----- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.test.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.test.tsx index 6c0374d1a8..3699262f3d 100644 --- a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.test.tsx +++ b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.test.tsx @@ -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( + + {}} + selectedItemSelector={undefined} + value={[ + { + id: "token", + label: "Token", + source: "token", + disabled: false, + locked: false, + states: [], + }, + ]} + /> + + ) + ); + const input = container.querySelector("input")!; + const sourceButton = container.querySelector( + '[data-id="token"] button' + )!; + act(() => sourceButton.focus()); + act(() => sourceButton.click()); + expect(document.activeElement).toBe(sourceButton); + + const field = input.closest('[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(); + } +}); diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx index f6652eb8cc..07fd2ab753 100644 --- a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx +++ b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx @@ -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 ( { + 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}