Skip to content

Commit b8dbead

Browse files
mofopeadegokeclaude
andcommitted
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 <noreply@anthropic.com>
1 parent eaed9a1 commit b8dbead

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

  • apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/variables

‎apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/variables/variables.tsx‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useCallback, useEffect, useMemo, useState } from 'react'
3+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
44
import {
55
Badge,
66
Button,
@@ -223,6 +223,7 @@ interface VariablesProps {
223223
}
224224

225225
export function Variables({ readOnly = false }: VariablesProps) {
226+
const pendingFocusIdRef = useRef<string | null>(null)
226227
const activeWorkflowId = useWorkflowRegistry((s) => s.activeWorkflowId)
227228

228229
const { isOpen, position, width, height, setIsOpen, setPosition, setDimensions } =
@@ -342,14 +343,22 @@ export function Variables({ readOnly = false }: VariablesProps) {
342343

343344
const handleAddVariable = useCallback(() => {
344345
if (!activeWorkflowId || readOnly) return
345-
collaborativeAddVariable({
346+
pendingFocusIdRef.current = collaborativeAddVariable({
346347
name: '',
347348
type: 'plain',
348349
value: '',
349350
workflowId: activeWorkflowId,
350351
})
351352
}, [activeWorkflowId, collaborativeAddVariable, readOnly])
352353

354+
// The new row's input only exists after the next render, so focus it from its ref callback.
355+
const focusIfPending = useCallback((variableId: string, element: HTMLInputElement | null) => {
356+
if (!element || pendingFocusIdRef.current !== variableId) return
357+
pendingFocusIdRef.current = null
358+
element.focus()
359+
element.select()
360+
}, [])
361+
353362
const handleRemoveVariable = useCallback(
354363
(variableId: string) => {
355364
if (readOnly) return
@@ -522,6 +531,7 @@ export function Variables({ readOnly = false }: VariablesProps) {
522531
<div className='flex flex-col gap-1'>
523532
<Label className='text-small'>{STRINGS.labels.name}</Label>
524533
<Input
534+
ref={(element) => focusIfPending(variable.id, element)}
525535
name='name'
526536
autoComplete='off'
527537
value={localNames[variable.id] ?? variable.name}

0 commit comments

Comments
 (0)