fix: auto focus new dir name input on create dir
Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
@@ -353,11 +353,17 @@ function NewItemRow() {
|
||||
}),
|
||||
})
|
||||
|
||||
// Auto-focus the input when newItemKind changes to a truthy value
|
||||
useEffect(() => {
|
||||
if (newItemKind) {
|
||||
setTimeout(() => {
|
||||
inputRef.current?.focus()
|
||||
}, 1)
|
||||
if (newItemKind && inputRef.current) {
|
||||
// Use requestAnimationFrame to ensure the component is fully rendered
|
||||
// and the dropdown has completed its close cycle
|
||||
requestAnimationFrame(() => {
|
||||
if (inputRef.current) {
|
||||
inputRef.current.focus()
|
||||
inputRef.current.select() // Also select the default text for better UX
|
||||
}
|
||||
})
|
||||
}
|
||||
}, [newItemKind])
|
||||
|
||||
@@ -389,9 +395,9 @@ function NewItemRow() {
|
||||
<TableCell className="p-0">
|
||||
<div className="flex items-center gap-2 px-2 py-1 h-full">
|
||||
{isPending ? (
|
||||
<LoadingSpinner className="size-6" />
|
||||
<LoadingSpinner className="size-4" />
|
||||
) : (
|
||||
<DirectoryIcon />
|
||||
<DirectoryIcon className="size-4" />
|
||||
)}
|
||||
<form
|
||||
className="w-full"
|
||||
|
@@ -6,7 +6,7 @@ import type {
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { Link } from "@tanstack/react-router"
|
||||
import { useMutation as useConvexMutation } from "convex/react"
|
||||
import { useAtom, useSetAtom } from "jotai"
|
||||
import { useAtom, useAtomValue, useSetAtom } from "jotai"
|
||||
import {
|
||||
ChevronDownIcon,
|
||||
Loader2Icon,
|
||||
@@ -195,11 +195,19 @@ function UploadFileButton() {
|
||||
|
||||
function NewDirectoryItemDropdown() {
|
||||
const setNewItemKind = useSetAtom(newItemKindAtom)
|
||||
const newItemKind = useAtomValue(newItemKindAtom)
|
||||
|
||||
const addNewDirectory = () => {
|
||||
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 (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
@@ -209,7 +217,7 @@ function NewDirectoryItemDropdown() {
|
||||
<ChevronDownIcon className="pl-1 size-4 shrink-0" />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent>
|
||||
<DropdownMenuContent onCloseAutoFocus={handleCloseAutoFocus}>
|
||||
<DropdownMenuItem>
|
||||
<TextFileIcon />
|
||||
Text file
|
||||
|
Reference in New Issue
Block a user