Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SafeAreaProvider } from 'react-native-safe-area-context';

import { UserProvider } from './context/UserContext';
import { HomeScreen } from './screens';
import { ScannerSheet } from './sheets';
import { PersistentWithPortalSheet, ScannerSheet } from './sheets';
import { sharedStyles } from './styles/theme';

export default function App() {
Expand All @@ -30,6 +30,10 @@ export default function App() {
<BottomSheetPersistent id="scanner-sheet">
<ScannerSheet />
</BottomSheetPersistent>
{/* Persistent sheet with nested portal sheet inside */}
<BottomSheetPersistent id="persistent-with-portal">
<PersistentWithPortalSheet />
</BottomSheetPersistent>
</BottomSheetManagerProvider>
</GestureHandlerRootView>
</SafeAreaProvider>
Expand Down
4 changes: 4 additions & 0 deletions example/src/bottom-sheet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ declare module 'react-native-bottom-sheet-stack' {
source: 'home' | 'navigation';
title?: string;
};
'persistent-with-portal': true;
'nested-portal-in-persistent': {
message: string;
};
}
}
12 changes: 12 additions & 0 deletions example/src/screens/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export function HomeScreen() {
const portalSheetControl = useBottomSheetControl('context-portal-sheet');
const portalModeSheetA = useBottomSheetControl('portal-mode-sheet-a');
const scannerControl = useBottomSheetControl('scanner-sheet');
const persistentWithPortalControl = useBottomSheetControl(
'persistent-with-portal'
);

return (
<View style={sharedStyles.container}>
Expand Down Expand Up @@ -125,6 +128,15 @@ export function HomeScreen() {
})
}
/>

<DemoCard
title="Persistent + Nested Portal"
description="Persistent sheet with portal-based sheet defined inside"
color={colors.purple}
onPress={() =>
persistentWithPortalControl.open({ scaleBackground: true })
}
/>
</View>

{/* Features */}
Expand Down
212 changes: 212 additions & 0 deletions example/src/sheets/PersistentWithNestedPortal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import type { BottomSheetMethods } from '@gorhom/bottom-sheet/lib/typescript/types';
import { forwardRef, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import {
BottomSheetPortal,
useBottomSheetContext,
useBottomSheetControl,
} from 'react-native-bottom-sheet-stack';

import { Badge, Button, SecondaryButton, Sheet } from '../components';
import { colors, sharedStyles } from '../styles/theme';

/**
* Nested portal sheet content - defined inside the persistent sheet
* to demonstrate portal sheets can be declared within other sheets
*/
const NestedPortalSheetContent = forwardRef<BottomSheetMethods>((_, ref) => {
const { close, params } =
useBottomSheetContext<'nested-portal-in-persistent'>();
const [counter, setCounter] = useState(0);

return (
<Sheet ref={ref} enableDynamicSizing>
<View style={styles.badgeRow}>
<Badge label="Portal" color={colors.warning} />
<Badge label="Nested" color={colors.purple} />
</View>
<Text style={sharedStyles.h1}>Nested Portal Sheet</Text>
<Text style={sharedStyles.text}>
This portal-based sheet is defined inside the persistent sheet. It has
access to the same React context as its parent.
</Text>

{params?.message && (
<View style={styles.paramBox}>
<Text style={styles.paramLabel}>Message from parent:</Text>
<Text style={styles.paramValue}>{params.message}</Text>
</View>
)}

<View style={styles.counterBox}>
<Text style={styles.counterLabel}>Local counter:</Text>
<Text style={styles.counterValue}>{counter}</Text>
</View>

<View style={styles.actions}>
<Button
title="Increment Counter"
onPress={() => setCounter((c) => c + 1)}
/>
<SecondaryButton title="Close" onPress={close} />
</View>

<View style={styles.infoBox}>
<Text style={styles.infoText}>
Note: This sheet's state resets on close because it's a portal sheet
(not persistent). The parent persistent sheet keeps its state.
</Text>
</View>
</Sheet>
);
});

NestedPortalSheetContent.displayName = 'NestedPortalSheetContent';

/**
* Persistent sheet that contains a portal-based sheet definition inside
*/
export const PersistentWithPortalSheet = forwardRef<BottomSheetMethods>(
(_, ref) => {
const { close } = useBottomSheetContext<'persistent-with-portal'>();
const nestedPortalControl = useBottomSheetControl(
'nested-portal-in-persistent'
);
const [openCount, setOpenCount] = useState(0);

const handleOpenNestedPortal = () => {
setOpenCount((c) => c + 1);
nestedPortalControl.open({
scaleBackground: true,
mode: 'push',
params: {
message: `Opened ${openCount + 1} time(s) from persistent sheet`,
},
});
};

return (
<>
{/* Portal sheet defined inside the persistent sheet */}
<BottomSheetPortal id="nested-portal-in-persistent">
<NestedPortalSheetContent />
</BottomSheetPortal>

<Sheet ref={ref} enableDynamicSizing>
<View style={styles.badgeRow}>
<Badge label="Persistent" color={colors.cyan} />
<Badge label="Has Nested Portal" color={colors.purple} />
</View>
<Text style={sharedStyles.h1}>Persistent + Portal Demo</Text>
<Text style={sharedStyles.text}>
This persistent sheet contains a portal-based sheet definition
inside. The persistent sheet keeps its state across open/close
cycles, while the nested portal sheet resets.
</Text>

<View style={styles.stateBox}>
<Text style={styles.stateLabel}>Nested portal opened:</Text>
<Text style={styles.stateValue}>{openCount} time(s)</Text>
</View>

<View style={styles.actions}>
<Button
title="Open Nested Portal Sheet"
onPress={handleOpenNestedPortal}
/>
<SecondaryButton title="Close" onPress={close} />
</View>

<View style={styles.infoBox}>
<Text style={styles.infoText}>
Close this sheet and reopen it - the "opened count" persists
because this is a persistent sheet. The nested portal sheet's
counter will reset each time it opens.
</Text>
</View>
</Sheet>
</>
);
}
);

PersistentWithPortalSheet.displayName = 'PersistentWithPortalSheet';

const styles = StyleSheet.create({
badgeRow: {
flexDirection: 'row',
gap: 8,
},
actions: {
gap: 12,
marginTop: 16,
},
paramBox: {
backgroundColor: colors.primaryDark,
borderRadius: 12,
padding: 14,
marginTop: 16,
},
paramLabel: {
color: colors.textSecondary,
fontSize: 12,
marginBottom: 4,
},
paramValue: {
color: colors.primary,
fontSize: 16,
fontWeight: '600',
},
counterBox: {
backgroundColor: colors.background,
borderRadius: 12,
padding: 14,
marginTop: 12,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
},
counterLabel: {
color: colors.textSecondary,
fontSize: 14,
},
counterValue: {
color: colors.warning,
fontSize: 24,
fontWeight: '700',
},
stateBox: {
backgroundColor: colors.background,
borderRadius: 12,
padding: 14,
marginTop: 16,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
borderWidth: 1,
borderColor: colors.border,
},
stateLabel: {
color: colors.textSecondary,
fontSize: 14,
},
stateValue: {
color: colors.cyan,
fontSize: 20,
fontWeight: '700',
},
infoBox: {
backgroundColor: colors.background,
borderRadius: 12,
padding: 14,
marginTop: 16,
borderWidth: 1,
borderColor: colors.border,
},
infoText: {
color: colors.textSecondary,
fontSize: 13,
lineHeight: 18,
textAlign: 'center',
},
});
1 change: 1 addition & 0 deletions example/src/sheets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export {
export { HeavySheet } from './DynamicContentSheet';
export { SheetA, SheetB, SheetC, SheetD } from './NavigationSheets';
export { NestedSheet1, NestedSheet2, NestedSheet3 } from './NestedScaleSheets';
export { PersistentWithPortalSheet } from './PersistentWithNestedPortal';
export { PortalModeSheetA, PortalModeSheetB } from './PortalModeSheets';
export { ScannerSheet } from './ScannerSheet';
28 changes: 18 additions & 10 deletions src/BottomSheetPersistent.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import type { BottomSheetMethods } from '@gorhom/bottom-sheet/lib/typescript/types';
import React, { useEffect, useRef } from 'react';

import { useMount, useSheetExists, useUnmount } from './bottomSheet.store';
import {
useMount,
useSheetExists,
useSheetPortalSession,
useUnmount,
} from './bottomSheet.store';
import { BottomSheetDefaultIndexContext } from './BottomSheetDefaultIndex.context';
import { useMaybeBottomSheetManagerContext } from './BottomSheetManager.provider';
import { BottomSheetPortal } from './BottomSheetPortal';
import { BottomSheetRefContext } from './BottomSheetRef.context';
import type { BottomSheetPortalId } from './portal.types';
import { setSheetRef } from './refsMap';
import { useEvent } from './useEvent';
import { Portal } from 'react-native-teleport';
import { BottomSheetContext } from './BottomSheet.context';

interface BottomSheetPersistentProps {
id: BottomSheetPortalId;
Expand All @@ -23,7 +29,7 @@ export function BottomSheetPersistent({
const mount = useMount();
const unmount = useUnmount();
const sheetExists = useSheetExists(id);

const portalSession = useSheetPortalSession(id);
const sheetRef = useRef<BottomSheetMethods>(null);
const groupId = bottomSheetManagerContext?.groupId || 'default';

Expand All @@ -49,12 +55,14 @@ export function BottomSheetPersistent({
}

return (
<BottomSheetPortal id={id}>
<BottomSheetDefaultIndexContext.Provider value={{ defaultIndex: -1 }}>
<BottomSheetRefContext.Provider value={sheetRef}>
{children}
</BottomSheetRefContext.Provider>
</BottomSheetDefaultIndexContext.Provider>
</BottomSheetPortal>
<Portal hostName={`bottomsheet-${id}-${portalSession}`}>
<BottomSheetContext.Provider value={{ id }}>
<BottomSheetDefaultIndexContext.Provider value={{ defaultIndex: -1 }}>
<BottomSheetRefContext.Provider value={sheetRef}>
{children}
</BottomSheetRefContext.Provider>
</BottomSheetDefaultIndexContext.Provider>
</BottomSheetContext.Provider>
</Portal>
);
}
19 changes: 10 additions & 9 deletions src/BottomSheetPortal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import React from 'react';
import { Portal } from 'react-native-teleport';

import { BottomSheetContext } from './BottomSheet.context';
import { useSheetUsePortal, useSheetPortalSession } from './bottomSheet.store';
import { BottomSheetDefaultIndexContext } from './BottomSheetDefaultIndex.context';
import { BottomSheetRefContext } from './BottomSheetRef.context';
import { useSheetPortalSession } from './bottomSheet.store';
import type { BottomSheetPortalId } from './portal.types';
import { getSheetRef } from './refsMap';

Expand All @@ -14,22 +16,21 @@ interface BottomSheetPortalProps {
}

export function BottomSheetPortal({ id, children }: BottomSheetPortalProps) {
const usePortal = useSheetUsePortal(id);
const portalSession = useSheetPortalSession(id);
const ref = getSheetRef(id);

if (!usePortal || portalSession === undefined) {
if (!portalSession || !ref) {
return null;
}

const ref = getSheetRef(id);
const childWithRef = React.cloneElement(children, {
ref,
} as { ref: typeof ref });

return (
<Portal hostName={`bottomsheet-${id}-${portalSession}`}>
<BottomSheetContext.Provider value={{ id }}>
{childWithRef}
<BottomSheetDefaultIndexContext.Provider value={{ defaultIndex: 0 }}>
<BottomSheetRefContext.Provider value={ref}>
{children}
</BottomSheetRefContext.Provider>
</BottomSheetDefaultIndexContext.Provider>
</BottomSheetContext.Provider>
</Portal>
);
Expand Down
Loading