react-native-ios-utilities
Version:
Utilities for react-native + iOS and wrappers for using swift together with fabric/paper + JSI
93 lines (92 loc) • 3.5 kB
JavaScript
;
import * as React from 'react';
import { StyleSheet } from 'react-native';
import { RNIDetachedNativeView } from "./RNIDetachedNativeView.js";
import { DEFAULT_DETACHED_SUBVIEW_ENTRY } from "./DetachedSubviewsMap.js";
import { Helpers } from "../../misc/Helpers.js";
import { jsx as _jsx } from "react/jsx-runtime";
export const RNIDetachedView = /*#__PURE__*/React.forwardRef((props, ref) => {
const [viewID, setViewID] = React.useState();
const [reactTag, setReactTag] = React.useState();
const [isDetachedInNative, setIsDetachedInNative] = React.useState(false);
const [detachedSubviewsMap, setDetachedSubviewsMap] = React.useState({});
React.useImperativeHandle(ref, () => ({
getReactTag: () => {
return reactTag;
},
getViewID: () => {
return viewID;
},
getDetachedSubviewsMap: () => {
return detachedSubviewsMap;
},
attachToWindow: async commandArgs => {
if (viewID == null) return;
const module = Helpers.getRNIUtilitiesModule();
setIsDetachedInNative(true);
await module.viewCommandRequest(/* viewID : */viewID, /* commandName: */'attachToWindow', /* commandArgs: */commandArgs);
},
presentInModal: async commandArgs => {
if (viewID == null) return;
const module = Helpers.getRNIUtilitiesModule();
setIsDetachedInNative(true);
await module.viewCommandRequest(/* viewID : */viewID, /* commandName: */'presentInModal', /* commandArgs: */commandArgs);
}
}));
const shouldEnableDebugBackgroundColors = props.shouldEnableDebugBackgroundColors ?? false;
const shouldImmediatelyDetach = props.shouldImmediatelyDetach ?? false;
const isDetached = shouldImmediatelyDetach || isDetachedInNative;
const reactChildrenCount = React.Children.count(props.children);
const children = React.Children.map(props.children, child => {
if (/*#__PURE__*/React.isValidElement(child) && child.type === React.Fragment) {
return child;
}
return /*#__PURE__*/React.cloneElement(child, {
isParentDetached: isDetached,
shouldEnableDebugBackgroundColors,
detachedSubviewsMap
});
});
return /*#__PURE__*/_jsx(RNIDetachedNativeView, {
...props,
style: [isDetached && styles.detachedView, shouldEnableDebugBackgroundColors && styles.detachedViewDebug, props.style],
reactChildrenCount: reactChildrenCount,
onDidSetViewID: event => {
setViewID(event.nativeEvent.viewID);
setReactTag(event.nativeEvent.reactTag);
props.onDidSetViewID?.(event);
event.stopPropagation();
},
onViewDidDetachFromParent: event => {
props.onViewDidDetachFromParent?.(event);
event.stopPropagation();
setIsDetachedInNative(true);
},
onContentViewDidDetach: event => {
props.onContentViewDidDetach?.(event);
event.stopPropagation();
const detachedSubviewViewID = event.nativeEvent.viewID;
const prevEntry = detachedSubviewsMap[detachedSubviewViewID];
setDetachedSubviewsMap({
...detachedSubviewsMap,
[detachedSubviewViewID]: {
...DEFAULT_DETACHED_SUBVIEW_ENTRY,
...prevEntry,
didDetachFromOriginalParent: true
}
});
},
children: children
});
});
const styles = StyleSheet.create({
detachedView: {
position: 'absolute',
pointerEvents: 'none',
opacity: 0
},
detachedViewDebug: {
backgroundColor: 'rgba(255,0,0,0.3)'
}
});
//# sourceMappingURL=RNIDetachedView.js.map