-
-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathCopilotStep.tsx
More file actions
106 lines (97 loc) · 2.44 KB
/
Copy pathCopilotStep.tsx
File metadata and controls
106 lines (97 loc) · 2.44 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React, { useEffect, useMemo, useRef } from "react";
import { type NativeMethods } from "react-native";
import { useCopilot } from "../contexts/CopilotProvider";
interface Props {
name: string;
order: number;
text: string;
children: React.ReactElement<any>;
active?: boolean;
edge?: {
top?: number;
left?: number;
right?: number;
bottom?: number;
};
}
export const CopilotStep = ({
name,
order,
text,
children,
active = true,
edge = {
top: 0,
left: 0,
right: 0,
bottom: 0,
},
}: Props) => {
const registeredName = useRef<string | null>(null);
const { registerStep, unregisterStep } = useCopilot();
const wrapperRef = React.useRef<NativeMethods | null>(null);
const measure = async () => {
return await new Promise<{
x: number;
y: number;
width: number;
height: number;
}>((resolve) => {
const measure = () => {
// Wait until the wrapper element appears
if (wrapperRef.current != null && "measure" in wrapperRef.current) {
wrapperRef.current.measure((_ox, _oy, width, height, x, y) => {
const safeEdge = {
top: edge?.top ?? 0,
left: edge?.left ?? 0,
right: edge?.right ?? 0,
bottom: edge?.bottom ?? 0,
};
resolve({
x: x - safeEdge.left,
y: y - safeEdge.top,
width: width + safeEdge.left + safeEdge.right,
height: height + safeEdge.top + safeEdge.bottom,
});
});
} else {
requestAnimationFrame(measure);
}
};
measure();
});
};
useEffect(() => {
if (active) {
if (registeredName.current && registeredName.current !== name) {
unregisterStep(registeredName.current);
}
registerStep({
name,
text,
order,
measure,
wrapperRef,
visible: true,
});
registeredName.current = name;
}
}, [name, order, text, registerStep, unregisterStep, active]);
useEffect(() => {
if (active) {
return () => {
if (registeredName.current) {
unregisterStep(registeredName.current);
}
};
}
}, [name, unregisterStep, active]);
const copilotProps = useMemo(
() => ({
ref: wrapperRef,
onLayout: () => {}, // Android hack
}),
[],
);
return React.cloneElement(children, { copilot: copilotProps });
};