55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import {
|
|
createContext,
|
|
useCallback,
|
|
useContext,
|
|
useLayoutEffect,
|
|
useMemo,
|
|
useState,
|
|
type ReactNode,
|
|
} from "react"
|
|
import { useIsFocused } from "expo-router"
|
|
import { useSafeAreaInsets } from "react-native-safe-area-context"
|
|
|
|
type PlayerInsets = { left: number; right: number; bottom: number }
|
|
const TabletPlayerLayout = createContext<{
|
|
insets: PlayerInsets
|
|
setInsets: (insets: PlayerInsets) => void
|
|
} | null>(null)
|
|
|
|
/** Keep one player above the navigator, positioned using the focused screen's safe area. */
|
|
export function TabletPlayerLayoutProvider({
|
|
children,
|
|
}: {
|
|
children: ReactNode
|
|
}) {
|
|
const safeArea = useSafeAreaInsets()
|
|
const [insets, updateInsets] = useState<PlayerInsets>(safeArea)
|
|
const setInsets = useCallback((next: PlayerInsets) => {
|
|
updateInsets((current) =>
|
|
current.left === next.left &&
|
|
current.right === next.right &&
|
|
current.bottom === next.bottom
|
|
? current
|
|
: next
|
|
)
|
|
}, [])
|
|
const value = useMemo(() => ({ insets, setInsets }), [insets, setInsets])
|
|
return <TabletPlayerLayout value={value}>{children}</TabletPlayerLayout>
|
|
}
|
|
|
|
export function TabletPlayerLayoutSync() {
|
|
const focused = useIsFocused()
|
|
const { left, right, bottom } = useSafeAreaInsets()
|
|
const setInsets = useContext(TabletPlayerLayout)?.setInsets
|
|
useLayoutEffect(() => {
|
|
if (focused) setInsets?.({ left, right, bottom })
|
|
}, [focused, left, right, bottom, setInsets])
|
|
return null
|
|
}
|
|
|
|
export function useTabletPlayerInsets() {
|
|
const layout = useContext(TabletPlayerLayout)
|
|
const safeArea = useSafeAreaInsets()
|
|
return layout?.insets ?? safeArea
|
|
}
|