Skip to content

Commit

Permalink
chore: useReelCells内のcurrentRefのチェックを最適化する
Browse files Browse the repository at this point in the history
  • Loading branch information
AtsushiM committed Jan 9, 2025
1 parent 631ad61 commit bc46030
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions packages/smarthr-ui/src/components/Table/useReelCells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,41 @@ export const useReelCells = () => {
useEffect(() => {
const currentRef = tableWrapperRef.current

if (!currentRef) {
return () => undefined
}

const handleScroll = () => {
if (currentRef) {
const stickyCells = currentRef.querySelectorAll('.fixedElement') || []
const scrollLeft = currentRef.scrollLeft
const maxScrollLeft = currentRef.scrollWidth - currentRef.clientWidth || 0

stickyCells.forEach((cell) => {
const shouldFix = maxScrollLeft > 0 && scrollLeft < maxScrollLeft

if (shouldFix) {
cell.classList.add('fixed')
setShowShadow(scrollLeft > 0)
} else {
cell.classList.remove('fixed')
setShowShadow(maxScrollLeft === 0 && scrollLeft === 0 ? false : true)
}
})
}
const stickyCells = currentRef.querySelectorAll('.fixedElement') || []
const scrollLeft = currentRef.scrollLeft
const maxScrollLeft = currentRef.scrollWidth - currentRef.clientWidth || 0

stickyCells.forEach((cell) => {
const shouldFix = maxScrollLeft > 0 && scrollLeft < maxScrollLeft

if (shouldFix) {
cell.classList.add('fixed')
setShowShadow(scrollLeft > 0)
} else {
cell.classList.remove('fixed')
setShowShadow(maxScrollLeft === 0 && scrollLeft === 0 ? false : true)
}
})
}

handleScroll()

currentRef?.addEventListener('scroll', handleScroll)
currentRef.addEventListener('scroll', handleScroll)

const observer = new window.ResizeObserver(() => {
handleScroll()
})

if (currentRef) {
observer.observe(currentRef)
}
observer.observe(currentRef)

return () => {
if (currentRef) {
currentRef.removeEventListener('scroll', handleScroll)
observer.unobserve(currentRef)
}
currentRef.removeEventListener('scroll', handleScroll)
observer.unobserve(currentRef)
}
}, [tableWrapperRef, setShowShadow])

Expand Down

0 comments on commit bc46030

Please sign in to comment.