-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: scrollbar measure should consider
scrollbar-color
`::webkit-sc…
…rollbar` mixing (#507) * docs: update demo * chore: fix style shaking * test: update testcase * chore: comment
- Loading branch information
Showing
3 changed files
with
142 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,104 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import { removeCSS, updateCSS } from './Dom/dynamicCSS'; | ||
|
||
let cached: number; | ||
type ScrollBarSize = { width: number; height: number }; | ||
|
||
export default function getScrollBarSize(fresh?: boolean) { | ||
if (typeof document === 'undefined') { | ||
return 0; | ||
} | ||
type ExtendCSSStyleDeclaration = CSSStyleDeclaration & { | ||
scrollbarColor?: string; | ||
scrollbarWidth?: string; | ||
}; | ||
|
||
if (fresh || cached === undefined) { | ||
const inner = document.createElement('div'); | ||
inner.style.width = '100%'; | ||
inner.style.height = '200px'; | ||
let cached: ScrollBarSize; | ||
|
||
const outer = document.createElement('div'); | ||
const outerStyle = outer.style; | ||
function measureScrollbarSize(ele?: HTMLElement): ScrollBarSize { | ||
const randomId = `rc-scrollbar-measure-${Math.random() | ||
.toString(36) | ||
.substring(7)}`; | ||
const measureEle = document.createElement('div'); | ||
measureEle.id = randomId; | ||
|
||
outerStyle.position = 'absolute'; | ||
outerStyle.top = '0'; | ||
outerStyle.left = '0'; | ||
outerStyle.pointerEvents = 'none'; | ||
outerStyle.visibility = 'hidden'; | ||
outerStyle.width = '200px'; | ||
outerStyle.height = '150px'; | ||
outerStyle.overflow = 'hidden'; | ||
// Create Style | ||
const measureStyle: ExtendCSSStyleDeclaration = measureEle.style; | ||
measureStyle.position = 'absolute'; | ||
measureStyle.left = '0'; | ||
measureStyle.top = '0'; | ||
measureStyle.width = '100px'; | ||
measureStyle.height = '100px'; | ||
measureStyle.overflow = 'scroll'; | ||
|
||
outer.appendChild(inner); | ||
// Clone Style if needed | ||
let fallbackWidth: number; | ||
let fallbackHeight: number; | ||
if (ele) { | ||
const targetStyle: ExtendCSSStyleDeclaration = getComputedStyle(ele); | ||
measureStyle.scrollbarColor = targetStyle.scrollbarColor; | ||
measureStyle.scrollbarWidth = targetStyle.scrollbarWidth; | ||
|
||
document.body.appendChild(outer); | ||
// Set Webkit style | ||
const webkitScrollbarStyle = getComputedStyle(ele, '::-webkit-scrollbar'); | ||
|
||
const widthContained = inner.offsetWidth; | ||
outer.style.overflow = 'scroll'; | ||
let widthScroll = inner.offsetWidth; | ||
// Try wrap to handle CSP case | ||
try { | ||
updateCSS( | ||
` | ||
#${randomId}::-webkit-scrollbar { | ||
width: ${webkitScrollbarStyle.width}; | ||
height: ${webkitScrollbarStyle.height}; | ||
} | ||
`, | ||
randomId, | ||
); | ||
} catch (e) { | ||
// Can't wrap, just log error | ||
console.error(e); | ||
|
||
if (widthContained === widthScroll) { | ||
widthScroll = outer.clientWidth; | ||
// Get from style directly | ||
fallbackWidth = parseInt(webkitScrollbarStyle.width, 10); | ||
fallbackHeight = parseInt(webkitScrollbarStyle.height, 10); | ||
} | ||
} | ||
|
||
document.body.removeChild(outer); | ||
document.body.appendChild(measureEle); | ||
|
||
cached = widthContained - widthScroll; | ||
} | ||
return cached; | ||
// Measure. Get fallback style if provided | ||
const scrollWidth = | ||
ele && fallbackWidth && !isNaN(fallbackWidth) | ||
? fallbackWidth | ||
: measureEle.offsetWidth - measureEle.clientWidth; | ||
const scrollHeight = | ||
ele && fallbackHeight && !isNaN(fallbackHeight) | ||
? fallbackHeight | ||
: measureEle.offsetHeight - measureEle.clientHeight; | ||
|
||
// Clean up | ||
document.body.removeChild(measureEle); | ||
removeCSS(randomId); | ||
|
||
return { | ||
width: scrollWidth, | ||
height: scrollHeight, | ||
}; | ||
} | ||
|
||
function ensureSize(str: string) { | ||
const match = str.match(/^(.*)px$/); | ||
const value = Number(match?.[1]); | ||
return Number.isNaN(value) ? getScrollBarSize() : value; | ||
export default function getScrollBarSize(fresh?: boolean): number { | ||
if (typeof document === 'undefined') { | ||
return 0; | ||
} | ||
|
||
if (fresh || cached === undefined) { | ||
cached = measureScrollbarSize(); | ||
} | ||
return cached.width; | ||
} | ||
|
||
export function getTargetScrollBarSize(target: HTMLElement) { | ||
if (typeof document === 'undefined' || !target || !(target instanceof Element)) { | ||
if ( | ||
typeof document === 'undefined' || | ||
!target || | ||
!(target instanceof Element) | ||
) { | ||
return { width: 0, height: 0 }; | ||
} | ||
|
||
const { width, height } = getComputedStyle(target, '::-webkit-scrollbar'); | ||
return { | ||
width: ensureSize(width), | ||
height: ensureSize(height), | ||
}; | ||
return measureScrollbarSize(target); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters