WIP: tree-slider
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
4fe1ddfbd2
commit
cc3a3429fd
@ -0,0 +1,66 @@
|
||||
import {ReactNode, useCallback, useEffect, useRef, useState} from "react";
|
||||
|
||||
export interface SlideLayoutProps {
|
||||
children: ReactNode[2]
|
||||
}
|
||||
|
||||
export default function CurtainLayout({children}: SlideLayoutProps) {
|
||||
|
||||
const [rightWidth, setRightWidth] = useState(80)
|
||||
const curtainRef = useRef<HTMLDivElement>(null)
|
||||
const sliderRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const resize = useCallback((e: MouseEvent) => {
|
||||
const sliderPosX = e.clientX
|
||||
const curtainWidth = curtainRef.current!.getBoundingClientRect().width
|
||||
|
||||
setRightWidth((sliderPosX / curtainWidth) * 100)
|
||||
}, [curtainRef, setRightWidth])
|
||||
|
||||
const [resizing, setResizing] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
const curtain = curtainRef.current!
|
||||
const slider = sliderRef.current!
|
||||
|
||||
if (resizing) {
|
||||
const handleMouseUp = () => setResizing(false)
|
||||
|
||||
curtain.addEventListener('mousemove', resize)
|
||||
curtain.addEventListener('mouseup', handleMouseUp)
|
||||
return () => {
|
||||
curtain.removeEventListener('mousemove', resize)
|
||||
curtain.removeEventListener('mouseup', handleMouseUp)
|
||||
}
|
||||
}
|
||||
|
||||
const handleMouseDown = () => setResizing(true)
|
||||
|
||||
slider.addEventListener('mousedown', handleMouseDown)
|
||||
|
||||
return () => {
|
||||
slider.removeEventListener('mousedown', handleMouseDown)
|
||||
}
|
||||
}, [sliderRef, curtainRef, resizing, setResizing])
|
||||
|
||||
return (
|
||||
<div className={"curtain"} ref={curtainRef} style={{display: "flex"}}>
|
||||
<div className={"curtain-left"} style={{width: `${rightWidth}%`}}>
|
||||
{children[0]}
|
||||
</div>
|
||||
<div ref={sliderRef}
|
||||
style={{
|
||||
width: 2,
|
||||
height: "100%",
|
||||
backgroundColor: "grey",
|
||||
cursor: "col-resize"
|
||||
}}>
|
||||
</div>
|
||||
|
||||
<div className={"curtain-right"} style={{width: `${100 - rightWidth}%`}}>
|
||||
{children[1]}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
)
|
||||
}
|
Loading…
Reference in new issue