| 6 | * a short final section can't scroll that far. |
| 7 | */ |
| 8 | const activeIndexOf = (headings: (HTMLElement | null)[]): number => { |
| 9 | const root = document.documentElement |
| 10 | if (innerHeight + scrollY >= root.scrollHeight - 4) return headings.length - 1 |
| 11 | const line = parseFloat(getComputedStyle(root).scrollPaddingTop) + 8 |
| 12 | let active = 0 |
| 13 | for (const [i, heading] of headings.entries()) { |
| 14 | if (!heading) continue |
| 15 | if (heading.getBoundingClientRect().top > line) break |
| 16 | active = i |
| 17 | } |
| 18 | return active |
| 19 | } |
| 20 | |
| 21 | /** tracks which of the elements with these ids is being read */ |
| 22 | export const useScrollspy = (ids: readonly string[]): number => { |