diff options
author | dan <dan.abramov@gmail.com> | 2024-11-10 23:03:55 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-10 23:03:55 +0000 |
commit | 9247407c70eff280768e605637be873052ac1b59 (patch) | |
tree | af6ae552122b401d279f0e244aba6aeedaa58d03 /modules | |
parent | 9a10af7117ff2c818a137241a5b4271c2a882f10 (diff) | |
download | voidsky-9247407c70eff280768e605637be873052ac1b59.tar.zst |
Don't render unnecessary <Portal> instances (#6217)
Diffstat (limited to 'modules')
-rw-r--r-- | modules/bottom-sheet/src/BottomSheet.tsx | 25 | ||||
-rw-r--r-- | modules/bottom-sheet/src/BottomSheetNativeComponent.tsx | 80 | ||||
-rw-r--r-- | modules/bottom-sheet/src/BottomSheetPortal.tsx | 2 |
3 files changed, 48 insertions, 59 deletions
diff --git a/modules/bottom-sheet/src/BottomSheet.tsx b/modules/bottom-sheet/src/BottomSheet.tsx index bcc2c42ad..8da2773e9 100644 --- a/modules/bottom-sheet/src/BottomSheet.tsx +++ b/modules/bottom-sheet/src/BottomSheet.tsx @@ -1,24 +1 @@ -import React from 'react' - -import {BottomSheetViewProps} from './BottomSheet.types' -import {BottomSheetNativeComponent} from './BottomSheetNativeComponent' -import {useBottomSheetPortal_INTERNAL} from './BottomSheetPortal' - -export const BottomSheet = React.forwardRef< - BottomSheetNativeComponent, - BottomSheetViewProps ->(function BottomSheet(props, ref) { - const Portal = useBottomSheetPortal_INTERNAL() - - if (__DEV__ && !Portal) { - throw new Error( - 'BottomSheet: You need to wrap your component tree with a <BottomSheetPortalProvider> to use the bottom sheet.', - ) - } - - return ( - <Portal> - <BottomSheetNativeComponent {...props} ref={ref} /> - </Portal> - ) -}) +export {BottomSheetNativeComponent as BottomSheet} from './BottomSheetNativeComponent' diff --git a/modules/bottom-sheet/src/BottomSheetNativeComponent.tsx b/modules/bottom-sheet/src/BottomSheetNativeComponent.tsx index fa2b163bf..acd46ce01 100644 --- a/modules/bottom-sheet/src/BottomSheetNativeComponent.tsx +++ b/modules/bottom-sheet/src/BottomSheetNativeComponent.tsx @@ -11,6 +11,7 @@ import {requireNativeModule, requireNativeViewManager} from 'expo-modules-core' import {BottomSheetState, BottomSheetViewProps} from './BottomSheet.types' import {BottomSheetPortalProvider} from './BottomSheetPortal' +import {Context as PortalContext} from './BottomSheetPortal' const screenHeight = Dimensions.get('screen').height @@ -34,6 +35,8 @@ export class BottomSheetNativeComponent extends React.Component< > { ref = React.createRef<any>() + static contextType = PortalContext + constructor(props: BottomSheetViewProps) { super(props) this.state = { @@ -67,6 +70,17 @@ export class BottomSheetNativeComponent extends React.Component< } render() { + const Portal = this.context as React.ContextType<typeof PortalContext> + if (!Portal) { + throw new Error( + 'BottomSheet: You need to wrap your component tree with a <BottomSheetPortalProvider> to use the bottom sheet.', + ) + } + + if (!this.state.open) { + return null + } + const {children, backgroundColor, ...rest} = this.props const cornerRadius = rest.cornerRadius ?? 0 @@ -83,43 +97,41 @@ export class BottomSheetNativeComponent extends React.Component< } } - if (!this.state.open) { - return null - } - return ( - <NativeView - {...rest} - onStateChange={this.onStateChange} - ref={this.ref} - style={{ - position: 'absolute', - height: screenHeight, - width: '100%', - }} - containerBackgroundColor={backgroundColor}> - <View - style={[ - { - flex: 1, - backgroundColor, - }, - Platform.OS === 'android' && { - borderTopLeftRadius: cornerRadius, - borderTopRightRadius: cornerRadius, - }, - extraStyles, - ]}> + <Portal> + <NativeView + {...rest} + onStateChange={this.onStateChange} + ref={this.ref} + style={{ + position: 'absolute', + height: screenHeight, + width: '100%', + }} + containerBackgroundColor={backgroundColor}> <View - onLayout={e => { - const {height} = e.nativeEvent.layout - this.setState({viewHeight: height}) - this.updateLayout() - }}> - <BottomSheetPortalProvider>{children}</BottomSheetPortalProvider> + style={[ + { + flex: 1, + backgroundColor, + }, + Platform.OS === 'android' && { + borderTopLeftRadius: cornerRadius, + borderTopRightRadius: cornerRadius, + }, + extraStyles, + ]}> + <View + onLayout={e => { + const {height} = e.nativeEvent.layout + this.setState({viewHeight: height}) + this.updateLayout() + }}> + <BottomSheetPortalProvider>{children}</BottomSheetPortalProvider> + </View> </View> - </View> - </NativeView> + </NativeView> + </Portal> ) } } diff --git a/modules/bottom-sheet/src/BottomSheetPortal.tsx b/modules/bottom-sheet/src/BottomSheetPortal.tsx index da14cfa77..4d8ed57ff 100644 --- a/modules/bottom-sheet/src/BottomSheetPortal.tsx +++ b/modules/bottom-sheet/src/BottomSheetPortal.tsx @@ -4,7 +4,7 @@ import {createPortalGroup_INTERNAL} from './lib/Portal' type PortalContext = React.ElementType<{children: React.ReactNode}> -const Context = React.createContext({} as PortalContext) +export const Context = React.createContext({} as PortalContext) export const useBottomSheetPortal_INTERNAL = () => React.useContext(Context) |