import { createContext, memo, type ReactNode, useContext } from "react"; import { StyleSheet, View, type StyleProp, type ViewStyle } from "react-native"; const RetainedPanelActiveContext = createContext(true); export function useRetainedPanelActive(): boolean { return useContext(RetainedPanelActiveContext); } interface RetainedPanelProps { active: boolean; children: ReactNode; style?: StyleProp; testID?: string; } interface RetainedPanelActivityProps { active: boolean; children: ReactNode; } export function RetainedPanelActivity({ active, children }: RetainedPanelActivityProps) { const parentActive = useRetainedPanelActive(); return ( {children} ); } /** * Keeps expensive panel state mounted without letting an inactive panel render * on screen. The stable, non-collapsible native root is intentional: retained * panels must not detach or reparent their native descendants when visibility * changes. */ export const RetainedPanel = memo(function RetainedPanel({ active, children, style, testID, }: RetainedPanelProps) { const visibleStyle = StyleSheet.compose(styles.root, style); const panelStyle = active ? visibleStyle : StyleSheet.compose(visibleStyle, styles.hidden); return ( {children} ); }); const styles = StyleSheet.create({ root: { flex: 1, }, hidden: { display: "none", }, });