Skip to content
Mosaic
On this page

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

GuidancePractice
DoPass finalFocus to the remove dialog so focus lands on the next row when it closes.
DoAttach registerTrigger(id) as the ref of the button that opens each row’s actions.
Don’tKeep your own ref map of row buttons to restore focus. Use this hook instead.

Options

PropTypeDescription
idsstring[]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 | nullThe element to focus when no row is left, such as the add button.

Returns

PropTypeDescription
registerTrigger(id: string) => (element: HTMLButtonElement | null) => voidReturns a callback ref that records the trigger button for id.
trigger(id: string) => HTMLButtonElement | nullThe 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 | nullReturns the element to focus after a removal and forgets it, or null when nothing was removed.