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}