From b8dbeada82be80b220a47c9d840b2966d01537ba Mon Sep 17 00:00:00 2001 From: Adegoke Mofopefoluwa Daniel <110726038+mofopeadegoke@users.noreply.github.com> Date: Sun, 15 Nov 2026 04:26:36 +0200 Subject: [PATCH] improvement(variables): focus the new variable's name field after adding it Clicking "Add new variable" left focus on the + button, so typing went nowhere and keyboard users had to hunt for the new row. Focus and select the new variable's Name input once it mounts, so the first keystroke replaces the default name. Only the client that clicked + moves focus; variables added by collaborators don't. Fixes #10064 Co-Authored-By: Claude Opus 5.5 --- .../components/variables/variables.tsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/variables/variables.tsx b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/variables/variables.tsx index 4955fa91961..c35d32dac8e 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/variables/variables.tsx +++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/variables/variables.tsx @@ -1,6 +1,6 @@ 'use client' -import { useCallback, useEffect, useMemo, useState } from 'react' +import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { Badge, Button, @@ -223,6 +223,7 @@ interface VariablesProps { } export function Variables({ readOnly = false }: VariablesProps) { + const pendingFocusIdRef = useRef(null) const activeWorkflowId = useWorkflowRegistry((s) => s.activeWorkflowId) const { isOpen, position, width, height, setIsOpen, setPosition, setDimensions } = @@ -342,7 +343,7 @@ export function Variables({ readOnly = false }: VariablesProps) { const handleAddVariable = useCallback(() => { if (!activeWorkflowId || readOnly) return - collaborativeAddVariable({ + pendingFocusIdRef.current = collaborativeAddVariable({ name: '', type: 'plain', value: '', @@ -350,6 +351,14 @@ export function Variables({ readOnly = false }: VariablesProps) { }) }, [activeWorkflowId, collaborativeAddVariable, readOnly]) + // The new row's input only exists after the next render, so focus it from its ref callback. + const focusIfPending = useCallback((variableId: string, element: HTMLInputElement | null) => { + if (!element || pendingFocusIdRef.current !== variableId) return + pendingFocusIdRef.current = null + element.focus() + element.select() + }, []) + const handleRemoveVariable = useCallback( (variableId: string) => { if (readOnly) return @@ -522,6 +531,7 @@ export function Variables({ readOnly = false }: VariablesProps) {
focusIfPending(variable.id, element)} name='name' autoComplete='off' value={localNames[variable.id] ?? variable.name}