Skip to content

Commit

Permalink
fix(core): Update spot on layout changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseLion committed Jul 15, 2024
1 parent d7bdd12 commit 0beb86c
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions package/src/lib/components/attach-step/AttachStep.component.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import React, { ReactElement, ReactNode, RefObject, cloneElement, useContext, useEffect, useRef } from "react";
import { StyleProp, View } from "react-native";
import React, {
ReactElement,
ReactNode,
RefObject,
cloneElement,
useCallback,
useContext,
useEffect,
useRef,
} from "react";
import { LayoutChangeEvent, StyleProp, View } from "react-native";

import { SpotlightTourContext } from "../../SpotlightTour.context";

Expand All @@ -8,6 +17,12 @@ export interface ChildProps<T> {
* A React children, if any.
*/
children?: ReactNode;
/**
* Native components layout change event.
*
* @param event The layout event.
*/
onLayout?: (event: LayoutChangeEvent) => void;
/**
* A React reference.
*/
Expand Down Expand Up @@ -56,14 +71,18 @@ export function AttachStep<T>({ children, fill = false, index }: AttachStepProps

const childRef = useRef<View>(null);

useEffect(() => {
const updateSpot = useCallback((): void => {
if (current === index) {
childRef.current?.measureInWindow((x, y, width, height) => {
changeSpot({ height, width, x, y });
});
}
}, [changeSpot, current, index]);

useEffect(() => {
updateSpot();
}, [updateSpot]);

if (typeof children.type === "function") {
const { style, ...rest } = children.props;
const childStyle = style ?? { };
Expand All @@ -75,6 +94,7 @@ export function AttachStep<T>({ children, fill = false, index }: AttachStepProps
style={{ alignSelf: fill ? "stretch" : "flex-start", ...childStyle }}
collapsable={false}
focusable={false}
onLayout={updateSpot}
>
{cloneElement(
children,
Expand All @@ -87,7 +107,14 @@ export function AttachStep<T>({ children, fill = false, index }: AttachStepProps

return cloneElement(
children,
{ ...children.props, ref: childRef },
{
...children.props,
onLayout: (event: LayoutChangeEvent) => {
updateSpot();
children.props.onLayout?.(event);
},
ref: childRef,
},
children.props?.children,
);
}

0 comments on commit 0beb86c

Please sign in to comment.