Open History to see the last commit
1import { useCallback, useMemo, useRef, useState } from "react";
2import { Pressable, StyleSheet, View, type AccessibilityActionEvent } from "react-native";
3import { Gesture, GestureDetector } from "react-native-gesture-handler";
4import { runOnJS } from "react-native-reanimated";
5import { cn } from "../../lib/cn";
6
7const ACCESSIBILITY_RESIZE_STEP = 24;
8
9interface WorkspacePaneDividerProps {
10 readonly accessibilityLabel: string;
11 readonly currentWidth: number;
12 /** 1 when dragging right grows the pane, -1 when dragging left grows it. */
13 readonly resizeDirection: 1 | -1;
14 readonly onResizeStart?: () => void;
15 readonly onResizeBy: (delta: number) => void;
16 readonly onResizeEnd?: () => void;
17}
18
19/** A forgiving divider target for touch, pointer, and VoiceOver users. */
20export function WorkspacePaneDivider(props: WorkspacePaneDividerProps) {
21 const latestProps = useRef(props);
22 latestProps.current = props;
23 const [hovered, setHovered] = useState(false);
24 const [dragging, setDragging] = useState(false);
25 const handleResizeStart = useCallback(() => {
26 setDragging(true);
27 latestProps.current.onResizeStart?.();
28 }, []);
29 const handleResize = useCallback((translationX: number) => {
30 latestProps.current.onResizeBy(translationX * latestProps.current.resizeDirection);
31 }, []);
32 const handleResizeEnd = useCallback(() => {
33 setDragging(false);
34 latestProps.current.onResizeEnd?.();
35 }, []);
36 const resizeGesture = useMemo(
37 () =>
38 Gesture.Pan()
39 .activeOffsetX([-4, 4])
40 .failOffsetY([-24, 24])
41 .onStart(() => {
42 runOnJS(handleResizeStart)();
43 })
44 .onUpdate((event) => {
45 runOnJS(handleResize)(event.translationX);
46 })
47 .onFinalize(() => {
48 runOnJS(handleResizeEnd)();
49 }),
50 [handleResize, handleResizeEnd, handleResizeStart],
51 );
52
53 const handleAccessibilityAction = (event: AccessibilityActionEvent) => {
54 props.onResizeStart?.();
55 if (event.nativeEvent.actionName === "increment") {
56 props.onResizeBy(ACCESSIBILITY_RESIZE_STEP);
57 } else if (event.nativeEvent.actionName === "decrement") {
58 props.onResizeBy(-ACCESSIBILITY_RESIZE_STEP);
59 }
60 props.onResizeEnd?.();
61 };
62
63 return (
64 <GestureDetector gesture={resizeGesture}>
65 <Pressable
66 className="relative z-[100] -mx-[22px] w-11 self-stretch cursor-pointer justify-center"
67 accessibilityActions={[
68 { name: "increment", label: "Make pane wider" },
69 { name: "decrement", label: "Make pane narrower" },
70 ]}
71 accessibilityLabel={props.accessibilityLabel}
72 accessibilityRole="adjustable"
73 accessibilityValue={{
74 now: Math.round(props.currentWidth),
75 text: `${Math.round(props.currentWidth)} points wide`,
76 }}
77 onAccessibilityAction={handleAccessibilityAction}
78 onHoverIn={() => setHovered(true)}
79 onHoverOut={() => setHovered(false)}
80 >
81 <View
82 className={cn(
83 "h-full self-center bg-border opacity-70",
84 hovered || dragging ? "w-0.5 bg-primary opacity-100" : "w-px",
85 )}
86 style={[styles.line, (hovered || dragging) && styles.activeLine]}
87 />
88 </Pressable>
89 </GestureDetector>
90 );
91}
92
93const styles = StyleSheet.create({
94 line: {
95 alignSelf: "center",
96 height: "100%",
97 width: StyleSheet.hairlineWidth,
98 },
99 activeLine: {
100 width: 2,
101 },
102});