Skip to content

Commit

Permalink
chore: return a width in px instead of a boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikThePendric committed Dec 11, 2024
1 parent adc9a95 commit ae4cba4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/components/DashboardContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import React, {
createContext,
useContext,
} from 'react'
import { detectOsScrollbarWidth } from '../modules/detectOsScrollbarWidth.js'
import classes from './styles/DashboardContainer.module.css'

const HasScrollbarContext = createContext(undefined)
const ContainerScrollbarWidthContext = createContext(0)
const osScrollbarWidth = detectOsScrollbarWidth()

const DashboardContainer = ({ children, covered }) => {
const [hasScrollbar, setHasScrollbar] = useState(false)
const [scrollbarWidth, setScrollbarWidth] = useState(0)
const containerRef = useRef(null)
const contentWrapRef = useRef(null)

Expand All @@ -26,7 +28,9 @@ const DashboardContainer = ({ children, covered }) => {
* a scrollbar on the outer scroll-container */
const isNarrowerThanWindow = window.innerWidth > el.offsetWidth
const hasInnerScrollbar = el.scrollHeight > el.clientHeight
setHasScrollbar(isNarrowerThanWindow || hasInnerScrollbar)
setScrollbarWidth(
isNarrowerThanWindow || hasInnerScrollbar ? osScrollbarWidth : 0
)
})
resizeObserver.observe(contentWrapRef.current)

Expand All @@ -35,6 +39,7 @@ const DashboardContainer = ({ children, covered }) => {
}
}, [])

console.log(scrollbarWidth, osScrollbarWidth)
return (
<div
className={cx(
Expand All @@ -46,9 +51,9 @@ const DashboardContainer = ({ children, covered }) => {
data-test="inner-scroll-container"
>
<div ref={contentWrapRef} className={classes.contentWrap}>
<HasScrollbarContext.Provider value={hasScrollbar}>
<ContainerScrollbarWidthContext.Provider value={scrollbarWidth}>
{children}
</HasScrollbarContext.Provider>
</ContainerScrollbarWidthContext.Provider>
</div>
</div>
)
Expand All @@ -59,5 +64,6 @@ DashboardContainer.propTypes = {
covered: PropTypes.bool,
}

export const useContainerHasScrollbar = () => useContext(HasScrollbarContext)
export const useContainerScrollbarWidth = () =>
useContext(ContainerScrollbarWidthContext)
export default DashboardContainer
28 changes: 28 additions & 0 deletions src/modules/detectOsScrollbarWidth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const className = '__horizontal-scrollbar-height-test__'
const styles = `
.${className} {
position: absolute;
top: -9999px;
width: 100px;
height: 100px;
overflow-y: scroll;
}
`

export function detectOsScrollbarWidth() {
const style = document.createElement('style')
style.innerHTML = styles

const el = document.createElement('div')
el.classList.add(className)

document.body.appendChild(style)
document.body.appendChild(el)

const scrollbarWidth = el.offsetWidth - el.clientWidth

document.body.removeChild(style)
document.body.removeChild(el)

return scrollbarWidth
}

0 comments on commit ae4cba4

Please sign in to comment.