MCPcopy Create free account
hub / github.com/primer/react / getLastElement

Function getLastElement

packages/react/src/TreeView/useRovingTabIndex.ts:184–205  ·  view source on GitHub ↗
(element: HTMLElement)

Source from the content-addressed store, hash-verified

182}
183
184export function getLastElement(element: HTMLElement): HTMLElement | undefined {
185 const root = element.closest('[role=tree]')
186 const items = Array.from(root?.querySelectorAll('[role=treeitem]') || [])
187
188 // If there are no items, return undefined
189 if (items.length === 0) return
190
191 let index = items.length - 1
192 let last = items[index]
193
194 // If last element is nested inside a collapsed subtree, continue iterating
195 while (
196 index > 0 &&
197 last instanceof HTMLElement &&
198 last.parentElement?.closest('[role=treeitem][aria-expanded=false]')
199 ) {
200 index -= 1
201 last = items[index]
202 }
203
204 return last instanceof HTMLElement ? last : undefined
205}
206
207const defaultSize = {
208 height: 32,

Callers 1

getNextFocusableElementFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected