Files
streamyfin/providers/GlobalModalProvider.tsx
Alex Collado 564a593a3a
Some checks failed
🚦 Security & Quality Gate / 🔍 Vulnerable Dependencies (push) Has been cancelled
🚦 Security & Quality Gate / 🚑 Expo Doctor Check (push) Has been cancelled
🏗️ Build Apps / 🤖 Build Android APK (Phone) (push) Has been cancelled
🏗️ Build Apps / 🤖 Build Android APK (TV) (push) Has been cancelled
🏗️ Build Apps / 🍎 Build iOS IPA (Phone) (push) Has been cancelled
🏗️ Build Apps / 🍎 Build iOS IPA (Phone - Unsigned) (push) Has been cancelled
🔒 Lockfile Consistency Check / 🔍 Check bun.lock and package.json consistency (push) Has been cancelled
🛡️ CodeQL Analysis / 🔎 Analyze with CodeQL (actions) (push) Has been cancelled
🛡️ CodeQL Analysis / 🔎 Analyze with CodeQL (javascript-typescript) (push) Has been cancelled
🏷️🔀Merge Conflict Labeler / 🏷️ Labeling Merge Conflicts (push) Has been cancelled
🚦 Security & Quality Gate / 📝 Validate PR Title (push) Has been cancelled
🚦 Security & Quality Gate / 🔍 Lint & Test (check) (push) Has been cancelled
🚦 Security & Quality Gate / 🔍 Lint & Test (format) (push) Has been cancelled
🚦 Security & Quality Gate / 🔍 Lint & Test (lint) (push) Has been cancelled
🚦 Security & Quality Gate / 🔍 Lint & Test (typecheck) (push) Has been cancelled
fix: Close modal when back button is pressed on Android (#1487)
2026-03-12 13:45:17 +01:00

114 lines
2.5 KiB
TypeScript

import type { BottomSheetModal } from "@gorhom/bottom-sheet";
import type React from "react";
import {
createContext,
type ReactNode,
useCallback,
useContext,
useEffect,
useRef,
useState,
} from "react";
import { BackHandler, Platform } from "react-native";
interface ModalOptions {
enableDynamicSizing?: boolean;
snapPoints?: (string | number)[];
enablePanDownToClose?: boolean;
backgroundStyle?: object;
handleIndicatorStyle?: object;
}
interface GlobalModalState {
content: ReactNode | null;
options?: ModalOptions;
}
interface GlobalModalContextType {
showModal: (content: ReactNode, options?: ModalOptions) => void;
hideModal: () => void;
isVisible: boolean;
modalState: GlobalModalState;
modalRef: React.RefObject<BottomSheetModal | null>;
}
const GlobalModalContext = createContext<GlobalModalContextType | undefined>(
undefined,
);
export const useGlobalModal = () => {
const context = useContext(GlobalModalContext);
if (!context) {
throw new Error("useGlobalModal must be used within GlobalModalProvider");
}
return context;
};
interface GlobalModalProviderProps {
children: ReactNode;
}
export const GlobalModalProvider: React.FC<GlobalModalProviderProps> = ({
children,
}) => {
const [modalState, setModalState] = useState<GlobalModalState>({
content: null,
options: undefined,
});
const [isVisible, setIsVisible] = useState(false);
const modalRef = useRef<BottomSheetModal>(null);
const showModal = useCallback(
(content: ReactNode, options?: ModalOptions) => {
setModalState({ content, options });
setIsVisible(true);
},
[],
);
const hideModal = useCallback(() => {
modalRef.current?.dismiss();
setIsVisible(false);
// Clear content after dismiss animation completes
requestAnimationFrame(() => {
setModalState({ content: null, options: undefined });
});
}, []);
useEffect(() => {
if (Platform.OS !== "android") return;
const onBackPress = () => {
if (isVisible) {
hideModal();
return true;
}
return false;
};
const subscription = BackHandler.addEventListener(
"hardwareBackPress",
onBackPress,
);
return () => subscription.remove();
}, [isVisible, hideModal]);
const value = {
showModal,
hideModal,
isVisible,
modalState,
modalRef,
};
return (
<GlobalModalContext.Provider value={value}>
{children}
</GlobalModalContext.Provider>
);
};
export type { GlobalModalContextType, ModalOptions };