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
14 changes: 14 additions & 0 deletions src/BottomSheetDefaultIndex.context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createContext, useContext } from 'react';

interface BottomSheetDefaultIndexContextValue {
defaultIndex: number;
}

export const BottomSheetDefaultIndexContext =
createContext<BottomSheetDefaultIndexContextValue | null>(null);

export function useBottomSheetDefaultIndex(): number {
const context = useContext(BottomSheetDefaultIndexContext);
// Default to 0 (open) if no provider - for regular sheets
return context?.defaultIndex ?? 0;
}
9 changes: 3 additions & 6 deletions src/BottomSheetManaged.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type BottomSheetMethods } from '@gorhom/bottom-sheet/lib/typescript/typ
import React from 'react';

import { getAnimatedIndex } from './animatedRegistry';
import { useSheetStatus } from './bottomSheet.store';
import { useBottomSheetDefaultIndex } from './BottomSheetDefaultIndex.context';
import { createSheetEventHandlers } from './bottomSheetCoordinator';
import { useBottomSheetRefContext } from './BottomSheetRef.context';
import { useBottomSheetContext } from './useBottomSheetContext';
Expand All @@ -30,7 +30,6 @@ export const BottomSheetManaged = React.forwardRef<
enablePanDownToClose = true,
backdropComponent = nullBackdrop,
animatedIndex: externalAnimatedIndex,
index: externalIndex,
...props
},
forwardedRef
Expand All @@ -39,9 +38,7 @@ export const BottomSheetManaged = React.forwardRef<
const contextRef = useBottomSheetRefContext();
const ref = contextRef ?? forwardedRef;

const status = useSheetStatus(id);
const shouldBeClosed = status === 'hidden' || status === 'closing';
const index = externalIndex ?? (shouldBeClosed ? -1 : 0);
const defaultIndex = useBottomSheetDefaultIndex();

const animatedIndex = externalAnimatedIndex ?? getAnimatedIndex(id);
const { handleAnimate, handleChange, handleClose } =
Expand Down Expand Up @@ -82,7 +79,7 @@ export const BottomSheetManaged = React.forwardRef<
animationConfigs={config}
ref={ref}
{...props}
index={index}
index={defaultIndex}
animatedIndex={animatedIndex}
onChange={wrappedOnChange}
onClose={wrappedOnClose}
Expand Down
11 changes: 7 additions & 4 deletions src/BottomSheetPersistent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import type { BottomSheetMethods } from '@gorhom/bottom-sheet/lib/typescript/typ
import React, { useEffect, useRef } from 'react';

import { useMount, useSheetExists, 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 { useEvent } from './useEvent';
import { setSheetRef } from './refsMap';
import { useEvent } from './useEvent';

interface BottomSheetPersistentProps {
id: BottomSheetPortalId;
Expand Down Expand Up @@ -49,9 +50,11 @@ export function BottomSheetPersistent({

return (
<BottomSheetPortal id={id}>
<BottomSheetRefContext.Provider value={sheetRef}>
{children}
</BottomSheetRefContext.Provider>
<BottomSheetDefaultIndexContext.Provider value={{ defaultIndex: -1 }}>
<BottomSheetRefContext.Provider value={sheetRef}>
{children}
</BottomSheetRefContext.Provider>
</BottomSheetDefaultIndexContext.Provider>
</BottomSheetPortal>
);
}
4 changes: 1 addition & 3 deletions src/bottomSheetCoordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export function initBottomSheetCoordinator(groupId: string) {

switch (status) {
case 'opening':
requestAnimationFrame(() => {
getSheetRef(id)?.current?.expand();
});
getSheetRef(id)?.current?.expand();
break;
case 'hidden':
if (ref) ref.close();
Expand Down
18 changes: 13 additions & 5 deletions src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export const useBottomSheetStore = create(
mode
);

// Get next portalSession from registry for portal-based sheets
// Registry persists across sheet deletions to ensure unique Portal/PortalHost names
const nextPortalSession = sheet.usePortal
const shouldGetNewPortalSession =
sheet.usePortal && (!existingSheet || !existingSheet.keepMounted);
const nextPortalSession = shouldGetNewPortalSession
? getNextPortalSession(sheet.id)
: undefined;

Expand All @@ -46,7 +46,9 @@ export const useBottomSheetStore = create(
scaleBackground:
sheet.scaleBackground ?? existingSheet.scaleBackground,
params: sheet.params ?? existingSheet.params,
portalSession: nextPortalSession ?? existingSheet.portalSession,
portalSession: existingSheet.keepMounted
? existingSheet.portalSession
: (nextPortalSession ?? existingSheet.portalSession),
}
: { ...sheet, status: 'opening', portalSession: nextPortalSession };

Expand Down Expand Up @@ -144,10 +146,16 @@ export const useBottomSheetStore = create(
set((state) => {
if (state.sheetsById[sheet.id]) return state;

// For portal-based persistent sheets, set initial portalSession
// This session will be reused across open/close cycles
const portalSession = sheet.usePortal
? getNextPortalSession(sheet.id)
: undefined;

return {
sheetsById: {
...state.sheetsById,
[sheet.id]: { ...sheet, status: 'hidden' },
[sheet.id]: { ...sheet, status: 'hidden', portalSession },
},
};
}),
Expand Down
Loading