Skip to content

Commit 1cfa125

Browse files
committed
fix(cmdk): clean up search modal input handling
1 parent 1d4d61a commit 1cfa125

File tree

1 file changed

+22
-34
lines changed
  • apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/search-modal

1 file changed

+22
-34
lines changed

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/search-modal/search-modal.tsx

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ export function SearchModal({
7979
const router = useRouter()
8080
const workspaceId = params.workspaceId as string
8181
const inputRef = useRef<HTMLInputElement>(null)
82+
const listRef = useRef<HTMLDivElement>(null)
8283
const [mounted, setMounted] = useState(false)
84+
const [search, setSearch] = useState('')
8385
const openSettingsModal = useSettingsModalStore((state) => state.openModal)
8486
const { config: permissionConfig } = usePermissionConfig()
8587

@@ -142,41 +144,18 @@ export function SearchModal({
142144
)
143145

144146
useEffect(() => {
145-
if (open && inputRef.current) {
146-
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
147-
window.HTMLInputElement.prototype,
148-
'value'
149-
)?.set
150-
if (nativeInputValueSetter) {
151-
nativeInputValueSetter.call(inputRef.current, '')
152-
inputRef.current.dispatchEvent(new Event('input', { bubbles: true }))
153-
}
154-
inputRef.current.focus()
147+
if (open) {
148+
setSearch('')
149+
inputRef.current?.focus()
155150
}
156151
}, [open])
157152

158-
const handleSearchChange = useCallback(() => {
159-
requestAnimationFrame(() => {
160-
const list = document.querySelector('[cmdk-list]')
161-
if (list) {
162-
list.scrollTop = 0
163-
}
164-
})
165-
}, [])
166-
167-
useEffect(() => {
168-
if (!open) return
169-
170-
const handleKeyDown = (e: KeyboardEvent) => {
171-
if (e.key === 'Escape') {
172-
e.preventDefault()
173-
onOpenChange(false)
174-
}
153+
const handleSearchChange = useCallback((value: string) => {
154+
setSearch(value)
155+
if (listRef.current) {
156+
listRef.current.scrollTop = 0
175157
}
176-
177-
document.addEventListener('keydown', handleKeyDown)
178-
return () => document.removeEventListener('keydown', handleKeyDown)
179-
}, [open, onOpenChange])
158+
}, [])
180159

181160
const handleBlockSelect = useCallback(
182161
(block: SearchBlockItem, type: 'block' | 'trigger' | 'tool') => {
@@ -264,7 +243,7 @@ export function SearchModal({
264243
{/* Overlay */}
265244
<div
266245
className={cn(
267-
'fixed inset-0 z-40 bg-[#E4E4E4]/50 backdrop-blur-[0.75px] transition-opacity duration-100 dark:bg-[#0D0D0D]/50',
246+
'fixed inset-0 z-40 bg-[#E4E4E4]/50 transition-opacity duration-100 dark:bg-[#0D0D0D]/50',
268247
open ? 'opacity-100' : 'pointer-events-none opacity-0'
269248
)}
270249
onClick={() => onOpenChange(false)}
@@ -281,16 +260,25 @@ export function SearchModal({
281260
'-translate-x-1/2 fixed top-[15%] left-1/2 z-50 w-[500px] overflow-hidden rounded-[12px] border border-[var(--border)] bg-[var(--surface-4)] shadow-lg',
282261
open ? 'visible opacity-100' : 'invisible opacity-0'
283262
)}
263+
onKeyDown={(e) => {
264+
if (e.key === 'Escape') {
265+
e.preventDefault()
266+
onOpenChange(false)
267+
}
268+
}}
284269
>
285270
<Command label='Search' filter={customFilter}>
286271
<Command.Input
287272
ref={inputRef}
288-
autoFocus
273+
value={search}
289274
onValueChange={handleSearchChange}
290275
placeholder='Search anything...'
291276
className='w-full border-0 border-[var(--border)] border-b bg-transparent px-[12px] py-[10px] font-base text-[15px] text-[var(--text-primary)] placeholder:text-[var(--text-secondary)] focus:outline-none'
292277
/>
293-
<Command.List className='scrollbar-thin scrollbar-thumb-border scrollbar-track-transparent max-h-[400px] overflow-y-auto p-[8px]'>
278+
<Command.List
279+
ref={listRef}
280+
className='scrollbar-thin scrollbar-thumb-border scrollbar-track-transparent max-h-[400px] overflow-y-auto p-[8px]'
281+
>
294282
<Command.Empty className='flex items-center justify-center px-[16px] py-[24px] text-[15px] text-[var(--text-subtle)]'>
295283
No results found.
296284
</Command.Empty>

0 commit comments

Comments
 (0)