| 1 | import { createContext, use, useState, type ReactNode } from "react"; |
| 2 | import type { NativeLayoutMetrics } from "../lib/reserved-regions"; |
| 3 | import { NativeLayoutObserver } from "./NativeLayoutObserver"; |
| 4 | |
| 5 | const NativeLayoutContext = createContext<NativeLayoutMetrics | null>(null); |
| 6 | // Column reservations come from UIKit's layout, outside the resizing navigator. |
| 7 | export const NativeColumnLayoutMetricsContext = createContext<NativeLayoutMetrics | null>(null); |
| 8 | |
| 9 | export function NativeLayoutMetricsProvider(props: { readonly children: ReactNode }) { |
| 10 | const [metrics, setMetrics] = useState<NativeLayoutMetrics | null>(null); |
| 11 | return ( |
| 12 | <NativeLayoutContext value={metrics}> |
| 13 | <NativeLayoutObserver onChange={setMetrics} /> |
| 14 | {props.children} |
| 15 | </NativeLayoutContext> |
| 16 | ); |
| 17 | } |
| 18 | |
| 19 | export function useNativeLayoutMetrics() { |
| 20 | return use(NativeLayoutContext); |
| 21 | } |
| 22 | |
| 23 | export function useNativeColumnLayoutMetrics() { |
| 24 | return use(NativeColumnLayoutMetricsContext); |
| 25 | } |