-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import React, { | ||
FC, | ||
ReactNode, | ||
useRef, | ||
useState, | ||
useEffect, | ||
useCallback, | ||
} from "react"; | ||
import styled from "styled-components"; | ||
|
||
const Container = styled.div` | ||
display: flex; | ||
`; | ||
|
||
const DIVIDER_WIDTH = 4; | ||
|
||
const Divider = styled.div` | ||
width: ${DIVIDER_WIDTH}px; | ||
height: 20rem; | ||
cursor: col-resize; | ||
`; | ||
|
||
interface IProps { | ||
className?: string; | ||
leftChild: (width: number) => ReactNode; | ||
rightChild: (width: number) => ReactNode; | ||
} | ||
|
||
const Draggable: FC<IProps> = ({ className = "", leftChild, rightChild }) => { | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
const dividerRef = useRef<HTMLDivElement>(null); | ||
const [width, setWidth] = useState(0); | ||
const [containerRect, setContainerRect] = useState<DOMRect | null>(null); | ||
|
||
useEffect(() => { | ||
const containerEl = containerRef.current; | ||
if (containerEl) { | ||
const fullWidth = containerEl.clientWidth; | ||
const containerRect = containerEl.getBoundingClientRect(); | ||
setContainerRect(containerRect); | ||
setWidth(fullWidth / 2); | ||
} | ||
}, []); | ||
const keepDragging = useCallback( | ||
(event: MouseEvent) => { | ||
const { clientX } = event; | ||
console.log("keep dragging", clientX); | ||
if (containerRect) { | ||
setWidth(clientX - containerRect.left); | ||
} | ||
}, | ||
[containerRect] | ||
); | ||
const stopDrag = useCallback(() => { | ||
console.log("stop dragging"); | ||
document.removeEventListener("mousemove", keepDragging); | ||
document.removeEventListener("mouseup", stopDrag); | ||
}, [keepDragging]); | ||
const startDrag = useCallback(() => { | ||
document.addEventListener("mousemove", keepDragging); | ||
document.addEventListener("mouseup", stopDrag); | ||
}, [keepDragging, stopDrag]); | ||
useEffect(() => { | ||
const dividerEl = dividerRef.current; | ||
if (dividerEl) { | ||
dividerEl.addEventListener("mousedown", startDrag); | ||
} | ||
return () => { | ||
if (dividerEl) { | ||
dividerEl.removeEventListener("mousedown", startDrag); | ||
} | ||
}; | ||
}, [startDrag]); | ||
return ( | ||
<Container className={className} ref={containerRef}> | ||
{leftChild(width)} | ||
<Divider ref={dividerRef} /> | ||
{containerRect && rightChild(containerRect.width - width - DIVIDER_WIDTH)} | ||
</Container> | ||
); | ||
}; | ||
|
||
export default Draggable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters