|
| 1 | +/* globals getComputedStyle */ |
| 2 | + |
| 3 | +/** |
| 4 | + * Use these functions instead of `Element::scrollIntoView()` and |
| 5 | + * `Element::scrollIntoViewIfNeeded()`! |
| 6 | + * |
| 7 | + * We've had a recurring issue in Nuclide (e.g. T20028138) where the UI would shift, leaving part of |
| 8 | + * the workspace element offscreen and a blank area in the window. This was caused by called to the |
| 9 | + * native `scrollIntoView()` and `scrollIntoViewIfNeeded()` which, according to the spec, has two |
| 10 | + * potentially surprising behaviors: |
| 11 | + * |
| 12 | + * 1. [It scrolls every scrollable ancestor (not just the closest)][1], where |
| 13 | + * 2. "scrollable" is [explicitly defined][2] to include elements with `overflow: hidden` |
| 14 | + * |
| 15 | + * This is surprising because `overflow: hidden` is typically used to make elements *not |
| 16 | + * scrollable*. |
| 17 | + * |
| 18 | + * Once the `overflow: hidden` element is scrolled, the user has no way to return it to its original |
| 19 | + * position (as it has no scrollbars). |
| 20 | + * |
| 21 | + * Note that this API doesn't support smooth scrolling. If that becomes necessary, we'll need to |
| 22 | + * come up with a better fix. |
| 23 | + * |
| 24 | + * It's tempting to assume that using `scrollIntoViewIfNeeded()` would fix this issue, however, if |
| 25 | + * the window is small enough so that no amount of scrolling the desired scrollable element would |
| 26 | + * ever reveal the element you're trying to, the browser will keep scrolling ancestors. |
| 27 | + * |
| 28 | + * [1]: https://drafts.csswg.org/cssom-view/#element-scrolling-members |
| 29 | + * [2]: https://drafts.csswg.org/cssom-view/#scrolling-box |
| 30 | + */ |
| 31 | + |
| 32 | +export function scrollIntoView(el: Element, alignToTop?: boolean): void { |
| 33 | + const scrollTops = getScrollTops(el) |
| 34 | + el.scrollIntoView(alignToTop) |
| 35 | + restoreOverflowHiddenScrollTops(scrollTops) |
| 36 | +} |
| 37 | + |
| 38 | +export function scrollIntoViewIfNeeded(el: Element, center?: boolean): void { |
| 39 | + const scrollTops = getScrollTops(el) |
| 40 | + el?.scrollIntoViewIfNeeded(center) ?? el.scrollIntoView(center) |
| 41 | + restoreOverflowHiddenScrollTops(scrollTops) |
| 42 | +} |
| 43 | + |
| 44 | +function getScrollTops(el_: Element): Map<Element, number> { |
| 45 | + let el: Element | null = el_ |
| 46 | + const scrollTops = new Map() |
| 47 | + while (el != null) { |
| 48 | + scrollTops.set(el, el.scrollTop) |
| 49 | + el = el.parentElement |
| 50 | + } |
| 51 | + return scrollTops |
| 52 | +} |
| 53 | + |
| 54 | +function restoreOverflowHiddenScrollTops(scrollTops: Map<Element, number>): void { |
| 55 | + scrollTops.forEach((scrollTop, el) => { |
| 56 | + if (el.scrollTop !== scrollTop && isOverflowHidden(el)) { |
| 57 | + el.scrollTop = scrollTop |
| 58 | + } |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +function isOverflowHidden(el: HTMLElement | SVGElement | Element): boolean { |
| 63 | + //@ts-ignore |
| 64 | + const overflowStyle = el?.style.overflow |
| 65 | + const overflow = overflowStyle ?? getComputedStyle(el).overflow |
| 66 | + return overflow === "hidden" |
| 67 | +} |
0 commit comments