|
| 1 | +import { EnvironmentUtils } from "./environment-utils"; |
| 2 | +import { ScrollOptions } from "../interfaces/scroll-options"; |
| 3 | +import { StringUtils } from "./string-utils"; |
| 4 | + |
| 5 | +// ----------------------------------------------------------------------------------------- |
| 6 | +// #region Constants |
| 7 | +// ----------------------------------------------------------------------------------------- |
| 8 | + |
| 9 | +export const DefaultScrollOptions: ScrollOptions = { |
| 10 | + behavior: "auto", |
| 11 | + block: "start", |
| 12 | + inline: "nearest", |
| 13 | +}; |
| 14 | + |
| 15 | +// #endregion Constants |
| 16 | + |
| 17 | +// ----------------------------------------------------------------------------------------- |
| 18 | +// #region Functions |
| 19 | +// ----------------------------------------------------------------------------------------- |
| 20 | + |
| 21 | +/** |
| 22 | + * Attempts to scroll to the element specified by the given ID. |
| 23 | + * In the event of a slow page render, the element may not be immediately available. |
| 24 | + * This method will retry up to 50 times every 100ms to find the element before |
| 25 | + * giving up. |
| 26 | + */ |
| 27 | +const _scrollToElementById = ( |
| 28 | + id: string, |
| 29 | + options: ScrollOptions = DefaultScrollOptions |
| 30 | +) => { |
| 31 | + let retryCount = 0; |
| 32 | + const tryToScroll = () => { |
| 33 | + retryCount += 1; |
| 34 | + |
| 35 | + if (retryCount > 50) { |
| 36 | + EnvironmentUtils.runIfDevelopment(() => |
| 37 | + console.warn( |
| 38 | + `Could not find element with ID ${id} in the page.` |
| 39 | + ) |
| 40 | + ); |
| 41 | + |
| 42 | + // couldn't find element in 50 loops, give up. |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const el = document.getElementById(id); |
| 47 | + if (el != null) { |
| 48 | + el.scrollIntoView(options); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + setTimeout(tryToScroll, 100); |
| 53 | + }; |
| 54 | + |
| 55 | + if (options.initialDelay != null) { |
| 56 | + setTimeout(tryToScroll, options.initialDelay); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + tryToScroll(); |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | + * Attempts to scroll to the element specified in the hash of the current path. |
| 65 | + * In the event of a slow page render, the element may not be immediately available. |
| 66 | + * This method will retry up to 50 times every 100ms to find the element before |
| 67 | + * giving up. |
| 68 | + * |
| 69 | + * Reference: |
| 70 | + * https://stackoverflow.com/a/54042987 |
| 71 | + * https://stackoverflow.com/a/48195222 |
| 72 | + */ |
| 73 | +const _scrollToHash = ( |
| 74 | + location: any, |
| 75 | + options: ScrollOptions = DefaultScrollOptions |
| 76 | +) => { |
| 77 | + if (StringUtils.isEmpty(location.hash)) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const id = location.hash.replace("#", ""); |
| 82 | + _scrollToElementById(id, options); |
| 83 | +}; |
| 84 | + |
| 85 | +// #endregion Functions |
| 86 | + |
| 87 | +// ----------------------------------------------------------------------------------------- |
| 88 | +// #region Exports |
| 89 | +// ----------------------------------------------------------------------------------------- |
| 90 | + |
| 91 | +export const ScrollUtils = { |
| 92 | + scrollToHash: _scrollToHash, |
| 93 | +}; |
| 94 | + |
| 95 | +// #endregion Exports |
0 commit comments