-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview-splitter-bar.tsx
More file actions
49 lines (39 loc) · 1.39 KB
/
view-splitter-bar.tsx
File metadata and controls
49 lines (39 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import React from 'react';
import { useMergedRefs } from './hooks';
import { PropsWithChildren, RefForwardingComponent, MoveEvent } from './types';
export type ViewSplitterBarProps = PropsWithChildren & {
onMoveStart: (event: MoveEvent) => void
}
const ViewSplitterBar: RefForwardingComponent<'div', ViewSplitterBarProps> = React.forwardRef<HTMLElement, ViewSplitterBarProps>(({
as: Component = 'div',
onMoveStart,
className = "",
children,
...props
}: ViewSplitterBarProps, ref) => {
const barRef = React.useRef<HTMLElement>(null);
const mergedRefs = useMergedRefs(ref, barRef);
const onMoveStartRef = React.useRef(onMoveStart);
React.useEffect(() => {
onMoveStartRef.current = onMoveStart;
}, [onMoveStart]);
React.useEffect(() => {
const bar = barRef.current;
const eventHandler = onMoveStartRef.current;
if (bar) {
bar.addEventListener('mousedown', eventHandler, { passive: false });
bar.addEventListener('touchstart', eventHandler, { passive: false });
}
return () => {
if (bar) {
bar.removeEventListener('mousedown', eventHandler);
bar.removeEventListener('touchstart', eventHandler);
}
}
}, []);
return <Component ref={mergedRefs} className={`view-splitter-bar ${className}`.trim()} {...props}>
{children}
</Component>;
});
ViewSplitterBar.displayName = "ViewSplitterBar";
export default ViewSplitterBar;