useListRemovalFocus
Moves focus after a list row is removed: to the trigger of the row that took the removed row's place, or the previous one at the end of the list, or fallback() when the list is empty.
Use for
- moving focus after a list row is removed
- restoring focus from a remove dialog to the next row instead of the page body
Practices
| Guidance | Practice |
|---|---|
| Do | Pass finalFocus to the remove dialog so focus lands on the next row when it closes. |
| Do | Attach registerTrigger(id) as the ref of the button that opens each row’s actions. |
| Don’t | Keep your own ref map of row buttons to restore focus. Use this hook instead. |
Options
| Prop | Type | Description |
|---|---|---|
ids | string[] | The row ids in rendered order, before the removal. |
onRemove | (id: string) => void | Promise<void> | Removes the row. When unset, remove does nothing. |
fallback | () => HTMLElement | null | The element to focus when no row is left, such as the add button. |
Returns
| Prop | Type | Description |
|---|---|---|
registerTrigger | (id: string) => (element: HTMLButtonElement | null) => void | Returns a callback ref that records the trigger button for id. |
trigger | (id: string) => HTMLButtonElement | null | The registered trigger for id, or null. |
remove | (id: string) => Promise<void> | Awaits onRemove(id), then remembers the row so finalFocus can pick its successor. Remembers nothing if onRemove rejects. |
finalFocus | () => HTMLElement | null | Returns the element to focus after a removal and forgets it, or null when nothing was removed. |
Related
useConfirmController- usePendingAction