fix: auto focus new dir name input on create dir

Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
2025-09-21 15:45:40 +00:00
parent de8a53d7a8
commit bc1f655aab
2 changed files with 22 additions and 8 deletions

View File

@@ -353,11 +353,17 @@ function NewItemRow() {
}), }),
}) })
// Auto-focus the input when newItemKind changes to a truthy value
useEffect(() => { useEffect(() => {
if (newItemKind) { if (newItemKind && inputRef.current) {
setTimeout(() => { // Use requestAnimationFrame to ensure the component is fully rendered
inputRef.current?.focus() // and the dropdown has completed its close cycle
}, 1) requestAnimationFrame(() => {
if (inputRef.current) {
inputRef.current.focus()
inputRef.current.select() // Also select the default text for better UX
}
})
} }
}, [newItemKind]) }, [newItemKind])
@@ -389,9 +395,9 @@ function NewItemRow() {
<TableCell className="p-0"> <TableCell className="p-0">
<div className="flex items-center gap-2 px-2 py-1 h-full"> <div className="flex items-center gap-2 px-2 py-1 h-full">
{isPending ? ( {isPending ? (
<LoadingSpinner className="size-6" /> <LoadingSpinner className="size-4" />
) : ( ) : (
<DirectoryIcon /> <DirectoryIcon className="size-4" />
)} )}
<form <form
className="w-full" className="w-full"

View File

@@ -6,7 +6,7 @@ import type {
import { useMutation } from "@tanstack/react-query" import { useMutation } from "@tanstack/react-query"
import { Link } from "@tanstack/react-router" import { Link } from "@tanstack/react-router"
import { useMutation as useConvexMutation } from "convex/react" import { useMutation as useConvexMutation } from "convex/react"
import { useAtom, useSetAtom } from "jotai" import { useAtom, useAtomValue, useSetAtom } from "jotai"
import { import {
ChevronDownIcon, ChevronDownIcon,
Loader2Icon, Loader2Icon,
@@ -195,11 +195,19 @@ function UploadFileButton() {
function NewDirectoryItemDropdown() { function NewDirectoryItemDropdown() {
const setNewItemKind = useSetAtom(newItemKindAtom) const setNewItemKind = useSetAtom(newItemKindAtom)
const newItemKind = useAtomValue(newItemKindAtom)
const addNewDirectory = () => { const addNewDirectory = () => {
setNewItemKind("directory") setNewItemKind("directory")
} }
const handleCloseAutoFocus = (event: Event) => {
// If we just created a new item, prevent the dropdown from restoring focus to the trigger
if (newItemKind) {
event.preventDefault()
}
}
return ( return (
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger asChild> <DropdownMenuTrigger asChild>
@@ -209,7 +217,7 @@ function NewDirectoryItemDropdown() {
<ChevronDownIcon className="pl-1 size-4 shrink-0" /> <ChevronDownIcon className="pl-1 size-4 shrink-0" />
</Button> </Button>
</DropdownMenuTrigger> </DropdownMenuTrigger>
<DropdownMenuContent> <DropdownMenuContent onCloseAutoFocus={handleCloseAutoFocus}>
<DropdownMenuItem> <DropdownMenuItem>
<TextFileIcon /> <TextFileIcon />
Text file Text file