(target: Element)
| 256 | } |
| 257 | |
| 258 | function scrollIntoView(target: Element) { |
| 259 | let root = document.scrollingElement || document.documentElement; |
| 260 | let nextTarget: Element | null = target; |
| 261 | while (nextTarget && nextTarget !== root && nextTarget.isConnected) { |
| 262 | // Find the parent scrollable element and adjust the scroll position if the target is not already in view. |
| 263 | let scrollable = getScrollParent(nextTarget); |
| 264 | if ( |
| 265 | scrollable !== document.documentElement && |
| 266 | scrollable !== document.body && |
| 267 | scrollable !== nextTarget |
| 268 | ) { |
| 269 | let scrollableRect = scrollable.getBoundingClientRect(); |
| 270 | let targetRect = nextTarget.getBoundingClientRect(); |
| 271 | if ( |
| 272 | targetRect.top < scrollableRect.top || |
| 273 | targetRect.bottom > scrollableRect.top + nextTarget.clientHeight |
| 274 | ) { |
| 275 | let bottom = scrollableRect.bottom; |
| 276 | if (visualViewport) { |
| 277 | bottom = Math.min(bottom, visualViewport.offsetTop + visualViewport.height); |
| 278 | } |
| 279 | |
| 280 | // Center within the viewport. |
| 281 | let adjustment = |
| 282 | targetRect.top - |
| 283 | scrollableRect.top - |
| 284 | ((bottom - scrollableRect.top) / 2 - targetRect.height / 2); |
| 285 | scrollable.scrollTo({ |
| 286 | // Clamp to the valid range to prevent over-scrolling. |
| 287 | top: Math.max( |
| 288 | 0, |
| 289 | Math.min( |
| 290 | scrollable.scrollHeight - scrollable.clientHeight, |
| 291 | scrollable.scrollTop + adjustment |
| 292 | ) |
| 293 | ), |
| 294 | behavior: 'smooth' |
| 295 | }); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | nextTarget = scrollable.parentElement; |
| 300 | } |
| 301 | } |
no test coverage detected