feat: always focus the item input when tapping Inicio
This commit is contained in:
@@ -20,6 +20,23 @@ type CreateItemFormProps = {
|
||||
autoFocus?: boolean
|
||||
}
|
||||
|
||||
/** The input carries this id so it can be focused from outside the form (e.g. by the nav). */
|
||||
const NEW_ITEM_INPUT_ID = 'new-item-input'
|
||||
|
||||
/**
|
||||
* Puts the cursor in the "add an item" input when the home page is already the one on
|
||||
* screen (the route does not remount, so nothing else would move the focus). When the
|
||||
* user is navigating *to* the home page this is a no-op: the input takes the focus as
|
||||
* soon as it mounts, through `autoFocus`. Either way, tapping "Inicio" always ends with
|
||||
* the focus in the input — and never with the focus on another page's input.
|
||||
*
|
||||
* @param pathname the route the link was clicked from, so only the home input is focused.
|
||||
*/
|
||||
export function focusNewItemInput(pathname: string): void {
|
||||
if (pathname !== '/') return
|
||||
document.getElementById(NEW_ITEM_INPUT_ID)?.focus()
|
||||
}
|
||||
|
||||
/** Input to add an item to a list. Submits with Enter or the button. */
|
||||
export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProps) {
|
||||
const createItem = useCreateItem(listId)
|
||||
@@ -37,6 +54,7 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
<form onSubmit={onSubmit} className="flex items-start gap-2">
|
||||
<Field className="flex-1" data-invalid={!!form.formState.errors.name}>
|
||||
<Input
|
||||
id={NEW_ITEM_INPUT_ID}
|
||||
aria-label="Nuevo artículo"
|
||||
autoComplete="off"
|
||||
placeholder="Añade un artículo…"
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Link, useRouterState } from '@tanstack/react-router'
|
||||
import type { LucideIcon } from 'lucide-react'
|
||||
import { HomeIcon, ListTodoIcon, SettingsIcon, ShoppingCartIcon } from 'lucide-react'
|
||||
|
||||
import { focusNewItemInput } from '@/components/items/create-item-form'
|
||||
import { NavUser } from '@/components/layout/nav-user'
|
||||
import {
|
||||
Sidebar,
|
||||
@@ -43,7 +44,7 @@ export function AppSidebar() {
|
||||
<SidebarMenu>
|
||||
<SidebarMenuItem>
|
||||
<SidebarMenuButton size="lg" asChild>
|
||||
<Link to="/">
|
||||
<Link to="/" onClick={() => focusNewItemInput(pathname)}>
|
||||
<div className="bg-primary text-primary-foreground flex aspect-square size-8 items-center justify-center rounded-lg text-sm font-semibold">
|
||||
SL
|
||||
</div>
|
||||
@@ -68,7 +69,10 @@ export function AppSidebar() {
|
||||
isActive={isActive(pathname, item.to)}
|
||||
tooltip={item.title}
|
||||
>
|
||||
<Link to={item.to}>
|
||||
<Link
|
||||
to={item.to}
|
||||
onClick={item.to === '/' ? () => focusNewItemInput(pathname) : undefined}
|
||||
>
|
||||
<item.icon />
|
||||
<span>{item.title}</span>
|
||||
</Link>
|
||||
|
||||
74
apps/web/src/components/layout/bottom-nav.tsx
Normal file
74
apps/web/src/components/layout/bottom-nav.tsx
Normal file
@@ -0,0 +1,74 @@
|
||||
import { Link, useRouterState } from '@tanstack/react-router'
|
||||
import { HomeIcon, SettingsIcon, ShoppingCartIcon, UserIcon } from 'lucide-react'
|
||||
|
||||
import { focusNewItemInput } from '@/components/items/create-item-form'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
function isActive(pathname: string, to: string) {
|
||||
if (to === '/') return pathname === '/'
|
||||
return pathname === to || pathname.startsWith(`${to}/`)
|
||||
}
|
||||
|
||||
export function BottomNav() {
|
||||
const pathname = useRouterState({ select: (state) => state.location.pathname })
|
||||
|
||||
return (
|
||||
<footer className="fixed inset-x-0 bottom-0 z-30 border-t bg-background/95 supports-[backdrop-filter]:bg-background/60 backdrop-blur md:hidden pb-[env(safe-area-inset-bottom)]">
|
||||
<nav className="mx-auto flex h-16 max-w-lg items-center justify-around px-2">
|
||||
<Link
|
||||
to="/"
|
||||
onClick={() => focusNewItemInput(pathname)}
|
||||
className={cn(
|
||||
'flex flex-col items-center gap-1 rounded-md px-2 py-1.5 text-xs transition-colors',
|
||||
isActive(pathname, '/')
|
||||
? 'text-foreground'
|
||||
: 'text-muted-foreground hover:text-foreground',
|
||||
)}
|
||||
>
|
||||
<HomeIcon className="size-5" />
|
||||
<span>Inicio</span>
|
||||
</Link>
|
||||
|
||||
<Link
|
||||
to="/lists"
|
||||
className={cn(
|
||||
'flex flex-col items-center gap-1 rounded-md px-2 py-1.5 text-xs transition-colors',
|
||||
isActive(pathname, '/lists')
|
||||
? 'text-foreground'
|
||||
: 'text-muted-foreground hover:text-foreground',
|
||||
)}
|
||||
>
|
||||
<ShoppingCartIcon className="size-5" />
|
||||
<span>Listas</span>
|
||||
</Link>
|
||||
|
||||
<Link
|
||||
to="/settings"
|
||||
className={cn(
|
||||
'flex flex-col items-center gap-1 rounded-md px-2 py-1.5 text-xs transition-colors',
|
||||
isActive(pathname, '/settings')
|
||||
? 'text-foreground'
|
||||
: 'text-muted-foreground hover:text-foreground',
|
||||
)}
|
||||
>
|
||||
<SettingsIcon className="size-5" />
|
||||
<span>Ajustes</span>
|
||||
</Link>
|
||||
|
||||
<Link
|
||||
to="/settings"
|
||||
className={cn(
|
||||
'flex flex-col items-center gap-1 rounded-md px-2 py-1.5 text-xs transition-colors',
|
||||
isActive(pathname, '/settings')
|
||||
? 'text-foreground'
|
||||
: 'text-muted-foreground hover:text-foreground',
|
||||
)}
|
||||
aria-label="Cuenta"
|
||||
>
|
||||
<UserIcon className="size-5" />
|
||||
<span>Cuenta</span>
|
||||
</Link>
|
||||
</nav>
|
||||
</footer>
|
||||
)
|
||||
}
|
||||
@@ -64,7 +64,7 @@ function HomePage() {
|
||||
{primaryList.isPrimary && <Badge>Principal</Badge>}
|
||||
</div>
|
||||
|
||||
<CreateItemForm listId={primaryList.id} />
|
||||
<CreateItemForm listId={primaryList.id} autoFocus />
|
||||
|
||||
<ItemList listId={primaryList.id} />
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user