Skip to content

Commit fefbc81

Browse files
committed
perf: have dedicated hook for layout calculations
1 parent 93ce153 commit fefbc81

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

package/src/components/UIComponents/PortalWhileClosingView.tsx

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
createClosingPortalLayoutRegistrationId,
1212
setClosingPortalLayout,
1313
useShouldTeleportToClosingPortal,
14-
useOverlayController,
14+
useHasActiveId,
1515
} from '../../state-store';
1616

1717
type PortalWhileClosingViewProps = {
@@ -32,49 +32,6 @@ type PortalWhileClosingViewProps = {
3232
portalName: string;
3333
};
3434

35-
const useSyncingApi = (portalHostName: string, registrationId: string) => {
36-
const containerRef = useRef<View | null>(null);
37-
const placeholderLayout = useSharedValue({ h: 0, w: 0 });
38-
const insets = useSafeAreaInsets();
39-
const { id } = useOverlayController();
40-
41-
const syncPortalLayout = useStableCallback(() => {
42-
if (!id) {
43-
return;
44-
}
45-
46-
containerRef.current?.measureInWindow((x, y, width, height) => {
47-
const absolute = {
48-
x,
49-
y: y + (Platform.OS === 'android' ? insets.top : 0),
50-
};
51-
52-
if (!width || !height) {
53-
return;
54-
}
55-
56-
placeholderLayout.value = { h: height, w: width };
57-
58-
setClosingPortalLayout(portalHostName, registrationId, {
59-
...absolute,
60-
h: height,
61-
w: width,
62-
});
63-
});
64-
});
65-
66-
useEffect(() => {
67-
if (id) {
68-
syncPortalLayout();
69-
}
70-
}, [insets.bottom, id, syncPortalLayout]);
71-
72-
return useMemo(
73-
() => ({ syncPortalLayout, containerRef, placeholderLayout }),
74-
[placeholderLayout, syncPortalLayout],
75-
);
76-
};
77-
7835
/**
7936
* Keeps wrapped UI above the message overlay during close animation by teleporting it to a closing portal host.
8037
*
@@ -153,3 +110,46 @@ export const PortalWhileClosingView = ({
153110
</>
154111
);
155112
};
113+
114+
const useSyncingApi = (portalHostName: string, registrationId: string) => {
115+
const containerRef = useRef<View | null>(null);
116+
const placeholderLayout = useSharedValue({ h: 0, w: 0 });
117+
const insets = useSafeAreaInsets();
118+
const hasActiveId = useHasActiveId();
119+
120+
const syncPortalLayout = useStableCallback(() => {
121+
if (!hasActiveId) {
122+
return;
123+
}
124+
125+
containerRef.current?.measureInWindow((x, y, width, height) => {
126+
const absolute = {
127+
x,
128+
y: y + (Platform.OS === 'android' ? insets.top : 0),
129+
};
130+
131+
if (!width || !height) {
132+
return;
133+
}
134+
135+
placeholderLayout.value = { h: height, w: width };
136+
137+
setClosingPortalLayout(portalHostName, registrationId, {
138+
...absolute,
139+
h: height,
140+
w: width,
141+
});
142+
});
143+
});
144+
145+
useEffect(() => {
146+
if (hasActiveId) {
147+
syncPortalLayout();
148+
}
149+
}, [insets.bottom, hasActiveId, syncPortalLayout]);
150+
151+
return useMemo(
152+
() => ({ syncPortalLayout, containerRef, placeholderLayout }),
153+
[placeholderLayout, syncPortalLayout],
154+
);
155+
};

package/src/state-store/message-overlay-store.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,11 @@ export const useIsOverlayActive = (id: string) => {
342342

343343
return useStateStore(overlayStore, messageOverlaySelector);
344344
};
345+
346+
export const activeIdSelector = (nextState: OverlayState) => ({ id: nextState.id });
347+
348+
export const useHasActiveId = () => {
349+
const { id } = useStateStore(overlayStore, activeIdSelector);
350+
351+
return !!id;
352+
};

0 commit comments

Comments
 (0)