|
17 | 17 | * under the License. |
18 | 18 | */ |
19 | 19 | // Width/height of the custom `::-webkit-scrollbar` applied to the sticky |
20 | | -// body and header sizer in useSticky.tsx. Shared here so the measured |
21 | | -// scrollbar size always matches what actually renders. |
| 20 | +// body and header sizer in useSticky.tsx. |
22 | 21 | export const CUSTOM_SCROLLBAR_SIZE = 8; |
23 | 22 |
|
24 | 23 | let cached: number | undefined; |
| 24 | +let cachedCustom: number | undefined; |
25 | 25 |
|
26 | 26 | const css = (x: TemplateStringsArray) => x.join('\n'); |
27 | 27 |
|
| 28 | +function measureScrollBarSize(probeClassName?: string) { |
| 29 | + const inner = document.createElement('div'); |
| 30 | + const outer = document.createElement('div'); |
| 31 | + // Custom scrollbars are only styleable via a stylesheet rule, since |
| 32 | + // inline styles can't express `::-webkit-scrollbar` pseudo-elements. |
| 33 | + const style = probeClassName ? document.createElement('style') : undefined; |
| 34 | + if (style && probeClassName) { |
| 35 | + style.textContent = `.${probeClassName}::-webkit-scrollbar { width: ${CUSTOM_SCROLLBAR_SIZE}px; height: ${CUSTOM_SCROLLBAR_SIZE}px; }`; |
| 36 | + inner.className = probeClassName; |
| 37 | + document.head.append(style); |
| 38 | + } |
| 39 | + inner.style.cssText = css` |
| 40 | + width: auto; |
| 41 | + height: 100%; |
| 42 | + overflow: scroll; |
| 43 | + `; |
| 44 | + outer.style.cssText = css` |
| 45 | + position: absolute; |
| 46 | + visibility: hidden; |
| 47 | + overflow: hidden; |
| 48 | + width: 100px; |
| 49 | + height: 50px; |
| 50 | + `; |
| 51 | + outer.append(inner); |
| 52 | + document.body.append(outer); |
| 53 | + const size = outer.clientWidth - inner.clientWidth; |
| 54 | + outer.remove(); |
| 55 | + style?.remove(); |
| 56 | + return size; |
| 57 | +} |
| 58 | + |
| 59 | +// Measures the browser/OS native scrollbar width. Used anywhere an |
| 60 | +// un-styled scrollable region's real footprint matters (e.g. the chart's |
| 61 | +// outer resize-twitch guard in TableChart.tsx). |
28 | 62 | export default function getScrollBarSize(forceRefresh = false) { |
29 | 63 | if (typeof document === 'undefined') { |
30 | 64 | return 0; |
31 | 65 | } |
32 | 66 | if (cached === undefined || forceRefresh) { |
33 | | - const probeClassName = 'superset-scrollbar-size-probe'; |
34 | | - const inner = document.createElement('div'); |
35 | | - const outer = document.createElement('div'); |
36 | | - const style = document.createElement('style'); |
37 | | - // Custom scrollbars are only styleable via a stylesheet rule, since |
38 | | - // inline styles can't express `::-webkit-scrollbar` pseudo-elements. |
39 | | - style.textContent = `.${probeClassName}::-webkit-scrollbar { width: ${CUSTOM_SCROLLBAR_SIZE}px; height: ${CUSTOM_SCROLLBAR_SIZE}px; }`; |
40 | | - inner.className = probeClassName; |
41 | | - inner.style.cssText = css` |
42 | | - width: auto; |
43 | | - height: 100%; |
44 | | - overflow: scroll; |
45 | | - `; |
46 | | - outer.style.cssText = css` |
47 | | - position: absolute; |
48 | | - visibility: hidden; |
49 | | - overflow: hidden; |
50 | | - width: 100px; |
51 | | - height: 50px; |
52 | | - `; |
53 | | - outer.append(inner); |
54 | | - document.head.append(style); |
55 | | - document.body.append(outer); |
56 | | - cached = outer.clientWidth - inner.clientWidth; |
57 | | - outer.remove(); |
58 | | - style.remove(); |
| 67 | + cached = measureScrollBarSize(); |
59 | 68 | } |
60 | 69 | return cached; |
61 | 70 | } |
| 71 | + |
| 72 | +// Measures the scrollbar width as it actually renders with the custom |
| 73 | +// `::-webkit-scrollbar` styling applied (see scrollBarStyles in |
| 74 | +// useSticky.tsx). On browsers that don't support `::-webkit-scrollbar` |
| 75 | +// (e.g. Firefox) this equals the native scrollbar width. Only use this for |
| 76 | +// elements that carry that same custom styling. |
| 77 | +export function getCustomScrollBarSize(forceRefresh = false) { |
| 78 | + if (typeof document === 'undefined') { |
| 79 | + return 0; |
| 80 | + } |
| 81 | + if (cachedCustom === undefined || forceRefresh) { |
| 82 | + cachedCustom = measureScrollBarSize('superset-scrollbar-size-probe'); |
| 83 | + } |
| 84 | + return cachedCustom; |
| 85 | +} |
0 commit comments