-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from agneym/feature/draggable
Draggable Borders for Sections
- Loading branch information
Showing
10 changed files
with
147 additions
and
39 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,43 @@ | ||
import React, { FC, ReactNode, useRef, useContext } from "react"; | ||
import styled, { ThemeContext } from "styled-components"; | ||
|
||
import useDrag from "./useDrag"; | ||
|
||
const Container = styled.div` | ||
display: flex; | ||
align-items: stretch; | ||
`; | ||
|
||
const Divider = styled.div` | ||
width: ${props => props.theme.divider.width}px; | ||
cursor: col-resize; | ||
background-color: ${props => props.theme.divider.background}; | ||
`; | ||
|
||
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 themeContext = useContext(ThemeContext); | ||
|
||
const { leftWidth, rightWidth } = useDrag({ | ||
containerRef, | ||
dividerRef, | ||
dividerWidth: themeContext.divider.width, | ||
}); | ||
|
||
return ( | ||
<Container className={className} ref={containerRef}> | ||
{leftChild(leftWidth)} | ||
<Divider ref={dividerRef} /> | ||
{rightChild(rightWidth)} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useState, useEffect, useCallback, RefObject } from "react"; | ||
|
||
interface IProps { | ||
containerRef: RefObject<HTMLDivElement | null>; | ||
dividerRef: RefObject<HTMLDivElement | null>; | ||
dividerWidth: number; | ||
} | ||
|
||
function useDrag({ containerRef, dividerRef, dividerWidth }: IProps) { | ||
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; | ||
if (containerRect) { | ||
setWidth(clientX - containerRect.left); | ||
} | ||
}, | ||
[containerRect] | ||
); | ||
const stopDrag = useCallback(() => { | ||
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 { | ||
leftWidth: width, | ||
rightWidth: containerRect ? containerRect.width - width - dividerWidth : 0, | ||
}; | ||
} | ||
|
||
export default useDrag; |
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
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
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 |
---|---|---|
@@ -1,28 +1,8 @@ | ||
import "styled-components"; | ||
|
||
import theme from "../utils/theme"; | ||
|
||
declare module "styled-components" { | ||
export interface DefaultTheme { | ||
container: { | ||
border: string; | ||
minHeight: string; | ||
}; | ||
error: { | ||
background: string; | ||
color: string; | ||
}; | ||
console: { | ||
background: string; | ||
}; | ||
tabs: { | ||
tabHeader: { | ||
borderBottom: string; | ||
}; | ||
tabPanel: { | ||
phoneHeight: string; | ||
}; | ||
selectedTab: { | ||
borderBottom: string; | ||
}; | ||
}; | ||
} | ||
type Theme = typeof theme; | ||
export interface DefaultTheme extends Theme {} | ||
} |
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