Skip to content

Commit

Permalink
feat: improve hasScrollbars performace
Browse files Browse the repository at this point in the history
domUtils.hasScrollbars  add cache to improve performance
Fixes  ##116
  • Loading branch information
鏌忔剼 authored and bubkoo committed May 7, 2020
1 parent 9986647 commit c782a38
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions packages/x6/src/dom/util/style.ts
Expand Up @@ -28,11 +28,29 @@ export function getComputedStyle(elem: Element, name?: string) {
return computed
}

export function hasScrollbars(container: HTMLElement) {
interface Container extends HTMLElement {
cacheTime?: number
hasScrollbars?: boolean
}

export function hasScrollbars(
container: Container,
cache: boolean = true,
): boolean {
const now = Date.now()
if (cache) {
const { hasScrollbars, cacheTime = 0 } = container
const queryGap = Date.now() - cacheTime
if ((hasScrollbars === false || hasScrollbars === true) && queryGap < 500) {
return hasScrollbars
}
}
const style = getComputedStyle(container)
return (
const hasScrollbars =
style != null && (style.overflow === 'scroll' || style.overflow === 'auto')
)
container.cacheTime = now
container.hasScrollbars = hasScrollbars
return hasScrollbars
}

/**
Expand Down

0 comments on commit c782a38

Please sign in to comment.