Skip to content

feat: 🎸 CopilotStep support edge to custom rect #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/components/CopilotStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ interface Props {
text: string;
children: React.ReactElement<any>;
active?: boolean;
edge?: {
top?: number;
left?: number;
right?: number;
bottom?: number;
};
}

export const CopilotStep = ({
Expand All @@ -17,6 +23,12 @@ export const CopilotStep = ({
text,
children,
active = true,
edge = {
top: 0,
left: 0,
right: 0,
bottom: 0,
},
}: Props) => {
const registeredName = useRef<string | null>(null);
const { registerStep, unregisterStep } = useCopilot();
Expand All @@ -33,11 +45,17 @@ export const CopilotStep = ({
// 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,
y,
width,
height,
x: x - safeEdge.left,
y: y - safeEdge.top,
width: width + safeEdge.left + safeEdge.right,
height: height + safeEdge.top + safeEdge.bottom,
});
});
} else {
Expand Down Expand Up @@ -81,7 +99,7 @@ export const CopilotStep = ({
ref: wrapperRef,
onLayout: () => {}, // Android hack
}),
[]
[],
);

return React.cloneElement(children, { copilot: copilotProps });
Expand Down
24 changes: 20 additions & 4 deletions src/components/style.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { StyleSheet } from "react-native";

export const STEP_NUMBER_RADIUS: number = 14;
export const STEP_NUMBER_DIAMETER: number = STEP_NUMBER_RADIUS * 2;
export const ZINDEX: number = 100;
export const MARGIN: number = 13;
export const OFFSET_WIDTH: number = 4;
export const ARROW_SIZE: number = 6;

export const styles = StyleSheet.create({
let styles = {
container: {
position: "absolute",
left: 0,
Expand Down Expand Up @@ -80,4 +78,22 @@ export const styles = StyleSheet.create({
bottom: 0,
right: 0,
},
});
};

const RNCSetStyle = (stylesToUpdate: {
[K in keyof typeof styles]?: Partial<(typeof styles)[K]>;
}) => {
styles = {
...styles,
...Object.keys(stylesToUpdate).reduce<any>((acc, key) => {
const styleKey = key as keyof typeof styles;
acc[styleKey] = {
...styles[styleKey],
...stylesToUpdate[styleKey],
};
return acc;
}, {}),
};
};

export { styles, RNCSetStyle };
26 changes: 13 additions & 13 deletions src/contexts/CopilotProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
CopilotModal,
type CopilotModalHandle,
} from "../components/CopilotModal";
import { OFFSET_WIDTH } from "../components/style";
import { OFFSET_WIDTH, RNCSetStyle } from "../components/style";
import { useStateWithAwait } from "../hooks/useStateWithAwait";
import { useStepsMap } from "../hooks/useStepsMap";
import { type CopilotOptions, type Step } from "../types";
Expand All @@ -31,7 +31,7 @@ interface CopilotContextType {
currentStep: Step | undefined;
start: (
fromStep?: string,
suppliedScrollView?: ScrollView | null
suppliedScrollView?: ScrollView | null,
) => Promise<void>;
stop: () => Promise<void>;
goToNext: () => Promise<void>;
Expand All @@ -56,6 +56,7 @@ const CopilotContext = createContext<CopilotContextType | undefined>(undefined);
export const CopilotProvider = ({
verticalOffset = 0,
children,
style = {},
...rest
}: PropsWithChildren<CopilotOptions>) => {
const startTries = useRef(0);
Expand All @@ -65,6 +66,8 @@ export const CopilotProvider = ({
const [visible, setVisibility] = useStateWithAwait(false);
const [scrollView, setScrollView] = useState<ScrollView | null>(null);

RNCSetStyle(style);

const {
currentStep,
currentStepNumber,
Expand Down Expand Up @@ -96,7 +99,7 @@ export const CopilotProvider = ({
y: size.y - OFFSET_WIDTH / 2 + verticalOffset,
});
},
[verticalOffset]
[verticalOffset],
);

const setCurrentStep = useCallback(
Expand All @@ -112,7 +115,7 @@ export const CopilotProvider = ({
(_x, y, _w, h) => {
const yOffset = y > 0 ? y - h / 2 : 0;
scrollView.scrollTo({ y: yOffset, animated: false });
}
},
);
}
}
Expand All @@ -123,10 +126,10 @@ export const CopilotProvider = ({
void moveModalToStep(step);
}
},
scrollView != null ? 100 : 0
scrollView != null ? 100 : 0,
);
},
[copilotEvents, moveModalToStep, scrollView, setCurrentStepState]
[copilotEvents, moveModalToStep, scrollView, setCurrentStepState],
);

const start = useCallback(
Expand Down Expand Up @@ -163,7 +166,7 @@ export const CopilotProvider = ({
setCurrentStep,
setVisibility,
steps,
]
],
);

const stop = useCallback(async () => {
Expand All @@ -179,7 +182,7 @@ export const CopilotProvider = ({
async (n: number) => {
await setCurrentStep(getNthStep(n));
},
[getNthStep, setCurrentStep]
[getNthStep, setCurrentStep],
);

const prev = useCallback(async () => {
Expand Down Expand Up @@ -218,16 +221,13 @@ export const CopilotProvider = ({
isLastStep,
currentStepNumber,
totalStepsNumber,
]
],
);

return (
<CopilotContext.Provider value={value}>
<>
<CopilotModal
ref={modal}
{...rest}
/>
<CopilotModal ref={modal} {...rest} />
{children}
</>
</CopilotContext.Provider>
Expand Down
8 changes: 5 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
NativeMethods,
ViewStyle,
} from "react-native";
import type { styles } from "./components/style";

export type WalktroughedComponent = NativeMethods & React.ComponentType<any>;

Expand Down Expand Up @@ -69,15 +70,16 @@ export interface CopilotOptions {
animationDuration?: number;
tooltipComponent?: React.ComponentType<TooltipProps>;
tooltipStyle?: ViewStyle;
stepNumberComponent?: React.ComponentType<any >;
stepNumberComponent?: React.ComponentType<any>;
animated?: boolean;
labels?: Labels;
androidStatusBarVisible?: boolean;
svgMaskPath?: SvgMaskPathFunction;
verticalOffset?: number;
arrowColor?: string;
arrowSize?: number
margin?: number
arrowSize?: number;
margin?: number;
stopOnOutsideClick?: boolean;
backdropColor?: string;
style?: { [K in keyof typeof styles]?: Partial<(typeof styles)[K]> };
}