(e: React.MouseEvent, id: string, offset = -80, behavior: ScrollBehavior = "smooth")
| 1 | export function scrollIntoView(e: React.MouseEvent, id: string, offset = -80, behavior: ScrollBehavior = "smooth") { |
| 2 | e.preventDefault() |
| 3 | |
| 4 | const element = document.getElementById(id) |
| 5 | if (!element) return Promise.resolve() |
| 6 | |
| 7 | const targetY = element.getBoundingClientRect().top + window.scrollY + offset |
| 8 | |
| 9 | window.scrollTo({ top: targetY, behavior }) |
| 10 | |
| 11 | if (behavior !== "smooth") { |
| 12 | return Promise.resolve() |
| 13 | } |
| 14 | |
| 15 | const distance = Math.abs(window.scrollY - targetY) |
| 16 | const duration = Math.min(distance / 2, 1000) |
| 17 | |
| 18 | return new Promise((resolve) => { |
| 19 | setTimeout(resolve, duration) |
| 20 | }) |
| 21 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…