Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

workspace-pane-divider.tsx

107 lines3.4 KB

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