|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +/** The possible ways the browser may handle the horizontal scroll axis in RTL languages. */ |
| 10 | +export enum RtlScrollAxisType { |
| 11 | + /** |
| 12 | + * scrollLeft is 0 when scrolled all the way left and (scrollWidth - clientWidth) when scrolled |
| 13 | + * all the way right. |
| 14 | + */ |
| 15 | + NORMAL, |
| 16 | + /** |
| 17 | + * scrollLeft is -(scrollWidth - clientWidth) when scrolled all the way left and 0 when scrolled |
| 18 | + * all the way right. |
| 19 | + */ |
| 20 | + NEGATED, |
| 21 | + /** |
| 22 | + * scrollLeft is (scrollWidth - clientWidth) when scrolled all the way left and 0 when scrolled |
| 23 | + * all the way right. |
| 24 | + */ |
| 25 | + INVERTED |
| 26 | +} |
| 27 | + |
| 28 | +/** Cached result of the way the browser handles the horizontal scroll axis in RTL mode. */ |
| 29 | +let rtlScrollAxisType: RtlScrollAxisType; |
| 30 | + |
| 31 | +/** Check whether the browser supports scroll behaviors. */ |
| 32 | +export function supportsScrollBehavior(): boolean { |
| 33 | + return !!(document && document.documentElement && document.documentElement.style && |
| 34 | + 'scrollBehavior' in document.documentElement.style); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Checks the type of RTL scroll axis used by this browser. As of time of writing, Chrome is NORMAL, |
| 39 | + * Firefox & Safari are NEGATED, and IE & Edge are INVERTED. |
| 40 | + */ |
| 41 | +export function getRtlScrollAxisType(): RtlScrollAxisType { |
| 42 | + // We can't check unless we're on the browser. Just assume 'normal' if we're not. |
| 43 | + if (typeof document !== 'object' || !document) { |
| 44 | + return RtlScrollAxisType.NORMAL; |
| 45 | + } |
| 46 | + |
| 47 | + if (!rtlScrollAxisType) { |
| 48 | + // Create a 1px wide scrolling container and a 2px wide content element. |
| 49 | + const scrollContainer = document.createElement('div'); |
| 50 | + scrollContainer.dir = 'rtl'; |
| 51 | + scrollContainer.style.height = '1px'; |
| 52 | + scrollContainer.style.width = '1px'; |
| 53 | + scrollContainer.style.overflow = 'auto'; |
| 54 | + scrollContainer.style.visibility = 'hidden'; |
| 55 | + scrollContainer.style.pointerEvents = 'none'; |
| 56 | + scrollContainer.style.position = 'absolute'; |
| 57 | + |
| 58 | + const content = document.createElement('div'); |
| 59 | + content.style.width = '2px'; |
| 60 | + content.style.height = '1px'; |
| 61 | + |
| 62 | + scrollContainer.appendChild(content); |
| 63 | + document.body.appendChild(scrollContainer); |
| 64 | + |
| 65 | + rtlScrollAxisType = RtlScrollAxisType.NORMAL; |
| 66 | + |
| 67 | + // The viewport starts scrolled all the way to the right in RTL mode. If we are in a NORMAL |
| 68 | + // browser this would mean that the scrollLeft should be 1. If it's zero instead we know we're |
| 69 | + // dealing with one of the other two types of browsers. |
| 70 | + if (scrollContainer.scrollLeft == 0) { |
| 71 | + // In a NEGATED browser the scrollLeft is always somewhere in [-maxScrollAmount, 0]. For an |
| 72 | + // INVERTED browser it is always somewhere in [0, maxScrollAmount]. We can determine which by |
| 73 | + // setting to the scrollLeft to 1. This is past the max for a NEGATED browser, so it will |
| 74 | + // return 0 when we read it again. |
| 75 | + scrollContainer.scrollLeft = 1; |
| 76 | + rtlScrollAxisType = |
| 77 | + scrollContainer.scrollLeft == 0 ? RtlScrollAxisType.NEGATED : RtlScrollAxisType.INVERTED; |
| 78 | + } |
| 79 | + |
| 80 | + document.body.removeChild(scrollContainer); |
| 81 | + } |
| 82 | + return rtlScrollAxisType; |
| 83 | +} |
0 commit comments