|
| 1 | +/* |
| 2 | +
|
| 3 | + MIT License |
| 4 | +
|
| 5 | + Copyright (c) 2020 Looker Data Sciences, Inc. |
| 6 | +
|
| 7 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + of this software and associated documentation files (the "Software"), to deal |
| 9 | + in the Software without restriction, including without limitation the rights |
| 10 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + copies of the Software, and to permit persons to whom the Software is |
| 12 | + furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | + The above copyright notice and this permission notice shall be included in all |
| 15 | + copies or substantial portions of the Software. |
| 16 | +
|
| 17 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + SOFTWARE. |
| 24 | +
|
| 25 | + */ |
| 26 | + |
| 27 | +import pick from 'lodash/pick' |
| 28 | + |
| 29 | +export interface ScrollLockMap { |
| 30 | + [key: string]: HTMLElement |
| 31 | +} |
| 32 | + |
| 33 | +function getScrollBarWidth() { |
| 34 | + // Add a hidden scrolling div to the page to get the width of a scrollbar |
| 35 | + const scrollDiv = document.createElement('div') |
| 36 | + scrollDiv.style.width = '99px' |
| 37 | + scrollDiv.style.height = '99px' |
| 38 | + scrollDiv.style.position = 'absolute' |
| 39 | + scrollDiv.style.top = '-9999px' |
| 40 | + scrollDiv.style.overflow = 'scroll' |
| 41 | + |
| 42 | + document.body.appendChild(scrollDiv) |
| 43 | + const scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth |
| 44 | + document.body.removeChild(scrollDiv) |
| 45 | + |
| 46 | + return scrollbarWidth |
| 47 | +} |
| 48 | + |
| 49 | +// For the purposes of scroll lock, we're only interested in |
| 50 | +// the overflow and paddingRight properties |
| 51 | +type BodyStyle = Pick<CSSStyleDeclaration, 'overflow' | 'paddingRight'> | null |
| 52 | + |
| 53 | +function getBodyCurrentStyle(): BodyStyle { |
| 54 | + return document !== undefined |
| 55 | + ? pick(document.body.style, ['overflow', 'paddingRight']) |
| 56 | + : null |
| 57 | +} |
| 58 | + |
| 59 | +function setBodyStyles() { |
| 60 | + if (document !== undefined) { |
| 61 | + // Is there a vertical scrollbar? |
| 62 | + if (window.innerWidth > document.documentElement.clientWidth) { |
| 63 | + const scrollbarWidth = getScrollBarWidth() |
| 64 | + const curPaddingRight = window |
| 65 | + .getComputedStyle(document.body) |
| 66 | + .getPropertyValue('padding-right') |
| 67 | + if (curPaddingRight.indexOf('calc') === -1) { |
| 68 | + document.body.style.paddingRight = `calc(${curPaddingRight} + ${scrollbarWidth}px)` |
| 69 | + } |
| 70 | + } |
| 71 | + document.body.style.overflow = 'hidden' |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +function resetBodyStyles(previousStyle: BodyStyle) { |
| 76 | + if (previousStyle) { |
| 77 | + document.body.style.paddingRight = previousStyle.paddingRight |
| 78 | + document.body.style.overflow = previousStyle.overflow |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +export function getActiveScrollLock(lockMap: ScrollLockMap) { |
| 83 | + // Sort the elements according to dom position and return the last |
| 84 | + // which we assume to be stacked on top since all components using Portal |
| 85 | + // share a single zIndexFloor and use dom order to determine stacking |
| 86 | + const elements = Object.values(lockMap) |
| 87 | + if (elements.length === 0) return null |
| 88 | + |
| 89 | + const sortedElements = elements.sort((elementA, elementB) => { |
| 90 | + const relationship = elementA.compareDocumentPosition(elementB) |
| 91 | + return relationship > 3 ? 1 : -1 |
| 92 | + }) |
| 93 | + return sortedElements[0] || null |
| 94 | +} |
| 95 | + |
| 96 | +export function activateScrollLock(element: HTMLElement) { |
| 97 | + let scrollTop = window.scrollY |
| 98 | + let scrollTarget: EventTarget | HTMLElement | null = document |
| 99 | + |
| 100 | + // Handler for scroll event is needed since body overflow: hidden |
| 101 | + // won't stop other scroll-able elements from scrolling |
| 102 | + function stopScroll(e: Event) { |
| 103 | + if (e.target !== null && e.target !== scrollTarget) { |
| 104 | + // If the user has started scrolling in a new scroll-able element |
| 105 | + // we need to update the stored scroll top position |
| 106 | + scrollTarget = e.target |
| 107 | + scrollTop = |
| 108 | + scrollTarget instanceof Element |
| 109 | + ? scrollTarget.scrollTop |
| 110 | + : window.scrollY |
| 111 | + } |
| 112 | + if ( |
| 113 | + scrollTarget instanceof Element && |
| 114 | + !(element && element.contains(scrollTarget)) |
| 115 | + ) { |
| 116 | + scrollTarget.scrollTop = scrollTop |
| 117 | + } else if (scrollTarget === document) { |
| 118 | + // Scroll event can't actually be prevented so instead we |
| 119 | + // scroll the window back to the stored position |
| 120 | + window.scrollTo({ top: scrollTop }) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + const previousStyle = getBodyCurrentStyle() |
| 125 | + |
| 126 | + setBodyStyles() |
| 127 | + window.addEventListener('scroll', stopScroll, true) |
| 128 | + |
| 129 | + return () => { |
| 130 | + window.removeEventListener('scroll', stopScroll, true) |
| 131 | + resetBodyStyles(previousStyle) |
| 132 | + } |
| 133 | +} |
0 commit comments