Optimize HideScrollbar

This commit is contained in:
2023-11-03 11:57:12 +08:00
parent a8167b0741
commit 19f4d38bc2
4 changed files with 31 additions and 53 deletions

View File

@@ -68,14 +68,14 @@
} }
.vertical-scrollbar { .vertical-scrollbar {
padding: 12px 4px; padding: 12px 2px;
height: 100%; height: 100%;
left: 100%; left: 100%;
top: 0; top: 0;
transform: translateX(-100%); transform: translateX(-100%);
.box { .box {
width: 8px; width: 6px;
} }
} }

View File

@@ -11,5 +11,6 @@ body {
.right-panel { .right-panel {
flex: 1; flex: 1;
width: 0;
background-color: constants.$background-color; background-color: constants.$background-color;
} }

View File

@@ -13,7 +13,6 @@ interface HideScrollbarProps
minWidth?: string | number minWidth?: string | number
minHeight?: string | number minHeight?: string | number
scrollbarWidth?: string | number scrollbarWidth?: string | number
animationTransitionTime?: number
autoHideWaitingTime?: number autoHideWaitingTime?: number
} }
@@ -174,9 +173,10 @@ const HideScrollbar = forwardRef<HideScrollbarElement, HideScrollbarProps>((prop
minWidth, minWidth,
minHeight, minHeight,
scrollbarWidth, scrollbarWidth,
animationTransitionTime,
autoHideWaitingTime, autoHideWaitingTime,
children, children,
style,
className,
..._props ..._props
} = props } = props
@@ -412,8 +412,7 @@ const HideScrollbar = forwardRef<HideScrollbarElement, HideScrollbarProps>((prop
} }
useEffect(() => { useEffect(() => {
const windowResizeListener = () => { const resizeObserver = new ResizeObserver(() => {
setTimeout(() => {
setVerticalScrollbarWidth( setVerticalScrollbarWidth(
(rootRef.current?.offsetWidth ?? 0) - (rootRef.current?.clientWidth ?? 0) (rootRef.current?.offsetWidth ?? 0) - (rootRef.current?.clientWidth ?? 0)
) )
@@ -423,8 +422,7 @@ const HideScrollbar = forwardRef<HideScrollbarElement, HideScrollbarProps>((prop
rootRef.current && rootRef.current &&
setVerticalScrollbarLength( setVerticalScrollbarLength(
(rootRef.current.clientHeight / (contentRef.current?.clientHeight ?? 0)) * (rootRef.current.clientHeight / (contentRef.current?.clientHeight ?? 0)) * 100
100
) )
rootRef.current && rootRef.current &&
@@ -432,15 +430,15 @@ const HideScrollbar = forwardRef<HideScrollbarElement, HideScrollbarProps>((prop
(rootRef.current.clientWidth / (contentRef.current?.clientWidth ?? 0)) * 100 (rootRef.current.clientWidth / (contentRef.current?.clientWidth ?? 0)) * 100
) )
refreshLayout() refreshLayout()
}, animationTransitionTime) })
maskRef.current && resizeObserver.observe(maskRef.current)
return windowResizeListener return () => {
maskRef.current && resizeObserver.unobserve(maskRef.current)
} }
setTimeout(() => { }, [])
windowResizeListener()
}, 1000)
window.addEventListener('resize', windowResizeListener())
useEffect(() => {
rootRef.current?.removeEventListener('wheel', wheelListenerRef.current) rootRef.current?.removeEventListener('wheel', wheelListenerRef.current)
if (isPreventAnyScroll) { if (isPreventAnyScroll) {
const handleDefaultWheel = (event: WheelEvent) => { const handleDefaultWheel = (event: WheelEvent) => {
@@ -462,28 +460,7 @@ const HideScrollbar = forwardRef<HideScrollbarElement, HideScrollbarProps>((prop
wheelListenerRef.current = handleDefaultWheel wheelListenerRef.current = handleDefaultWheel
rootRef.current?.addEventListener('wheel', handleDefaultWheel, { passive: false }) rootRef.current?.addEventListener('wheel', handleDefaultWheel, { passive: false })
} }
}, [isPreventAnyScroll, isPreventHorizontalScroll, isPreventScroll, isPreventVerticalScroll])
rootRef.current &&
setVerticalScrollbarLength(
(rootRef.current.clientHeight / (contentRef.current?.clientHeight ?? 0)) * 100
)
rootRef.current &&
setHorizontalScrollbarLength(
(rootRef.current.clientWidth / (contentRef.current?.clientWidth ?? 0)) * 100
)
return () => {
window.removeEventListener('resize', windowResizeListener)
}
}, [
animationTransitionTime,
horizontalScrollbarLength,
isPreventAnyScroll,
isPreventHorizontalScroll,
isPreventScroll,
isPreventVerticalScroll
])
return ( return (
<> <>
@@ -507,13 +484,14 @@ const HideScrollbar = forwardRef<HideScrollbarElement, HideScrollbarProps>((prop
> >
<div <div
ref={rootRef} ref={rootRef}
className={'hide-scrollbar-selection'} className={`hide-scrollbar-selection${className ? ` ${className}` : ''}`}
tabIndex={0} tabIndex={0}
style={{ style={{
width: `calc(${maskRef.current?.clientWidth}px + ${verticalScrollbarWidth}px)`, width: `calc(${maskRef.current?.clientWidth}px + ${verticalScrollbarWidth}px)`,
height: `calc(${maskRef.current?.clientHeight}px + ${horizontalScrollbarWidth}px)`, height: `calc(${maskRef.current?.clientHeight}px + ${horizontalScrollbarWidth}px)`,
touchAction: isPreventAnyScroll ? 'none' : '', touchAction: isPreventAnyScroll ? 'none' : '',
msTouchAction: isPreventAnyScroll ? 'none' : '' msTouchAction: isPreventAnyScroll ? 'none' : '',
...style
}} }}
{..._props} {..._props}
onMouseDown={isPreventAnyScroll ? handleDefaultMouseDown : undefined} onMouseDown={isPreventAnyScroll ? handleDefaultMouseDown : undefined}

View File

@@ -21,7 +21,6 @@ const SidebarScroll = forwardRef<SidebarScrollElement, React.PropsWithChildren>(
<HideScrollbar <HideScrollbar
isShowVerticalScrollbar={true} isShowVerticalScrollbar={true}
scrollbarWidth={2} scrollbarWidth={2}
animationTransitionTime={300}
autoHideWaitingTime={800} autoHideWaitingTime={800}
ref={hideScrollbarRef} ref={hideScrollbarRef}
> >