UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

1 lines • 5.88 kB
{"version":3,"file":"SafeAreaContext.js","names":[],"sources":["esm/safe-area-context/SafeAreaContext.js"],"sourcesContent":["import * as React from \"react\";\nimport { NativeSafeAreaProvider } from \"./NativeSafeAreaProvider\";\nimport { getWindowFrame } from \"./getWindowFrame\";\nimport { initialWindowMetrics } from \"./InitialWindow\";\nimport { feedTamaguiSafeArea } from \"./tamaguiSafeArea\";\nimport { jsx } from \"react/jsx-runtime\";\nconst isDev = process.env.NODE_ENV !== \"production\";\nconst SafeAreaInsetsContext = React.createContext(null);\nif (isDev) {\n SafeAreaInsetsContext.displayName = \"SafeAreaInsetsContext\";\n}\nconst SafeAreaFrameContext = React.createContext(null);\nif (isDev) {\n SafeAreaFrameContext.displayName = \"SafeAreaFrameContext\";\n}\nconst ZERO_INSETS = { top: 0, bottom: 0, left: 0, right: 0 };\nfunction SafeAreaProvider({\n children,\n initialMetrics,\n initialSafeAreaInsets,\n ...others\n}) {\n const parentInsets = useParentSafeAreaInsets();\n const parentFrame = useParentSafeAreaFrame();\n const isRoot = parentInsets === null;\n const [insets, setInsets] = React.useState(\n initialMetrics?.insets ?? initialSafeAreaInsets ?? parentInsets ?? ZERO_INSETS\n );\n const [frame, setFrame] = React.useState(\n initialMetrics?.frame ?? parentFrame ?? // backwards compat so we render anyway if we don't have frame.\n getWindowFrame()\n );\n const onInsetsChange = React.useCallback(\n (event) => {\n const {\n nativeEvent: { frame: nextFrame, insets: nextInsets }\n } = event;\n if (isRoot && nextFrame) {\n feedTamaguiSafeArea({ insets: nextInsets, frame: nextFrame }, tamaguiHooks);\n }\n setFrame((curFrame) => {\n if (\n // Backwards compat with old native code that won't send frame.\n nextFrame && (nextFrame.height !== curFrame.height || nextFrame.width !== curFrame.width || nextFrame.x !== curFrame.x || nextFrame.y !== curFrame.y)\n ) {\n return nextFrame;\n }\n return curFrame;\n });\n setInsets((curInsets) => {\n if (!curInsets || nextInsets.bottom !== curInsets.bottom || nextInsets.left !== curInsets.left || nextInsets.right !== curInsets.right || nextInsets.top !== curInsets.top) {\n return nextInsets;\n }\n return curInsets;\n });\n },\n [isRoot]\n );\n return /* @__PURE__ */ jsx(NativeSafeAreaProvider, { onInsetsChange, ...others, children: /* @__PURE__ */ jsx(SafeAreaFrameContext.Provider, { value: frame, children: /* @__PURE__ */ jsx(SafeAreaInsetsContext.Provider, { value: insets, children }) }) });\n}\nfunction useParentSafeAreaInsets() {\n return React.useContext(SafeAreaInsetsContext);\n}\nfunction useParentSafeAreaFrame() {\n return React.useContext(SafeAreaFrameContext);\n}\nconst NO_INSETS_ERROR = \"No safe area value available. Make sure you are rendering `<SafeAreaProvider>` at the top of your app.\";\nfunction useSafeAreaInsets() {\n const insets = React.useContext(SafeAreaInsetsContext);\n if (insets == null) {\n throw new Error(NO_INSETS_ERROR);\n }\n return insets;\n}\nfunction useSafeAreaFrame() {\n const frame = React.useContext(SafeAreaFrameContext);\n if (frame == null) {\n throw new Error(NO_INSETS_ERROR);\n }\n return frame;\n}\nfunction withSafeAreaInsets(WrappedComponent) {\n return React.forwardRef((props, ref) => {\n const insets = useSafeAreaInsets();\n return /* @__PURE__ */ jsx(WrappedComponent, { ...props, insets, ref });\n });\n}\nconst tamaguiHooks = { useSafeAreaInsets, useSafeAreaFrame };\nfeedTamaguiSafeArea(initialWindowMetrics, tamaguiHooks);\nfunction useSafeArea() {\n return useSafeAreaInsets();\n}\nconst SafeAreaConsumer = SafeAreaInsetsContext.Consumer;\nconst SafeAreaContext = SafeAreaInsetsContext;\nexport {\n SafeAreaConsumer,\n SafeAreaContext,\n SafeAreaFrameContext,\n SafeAreaInsetsContext,\n SafeAreaProvider,\n useSafeArea,\n useSafeAreaFrame,\n useSafeAreaInsets,\n withSafeAreaInsets\n};\n//# sourceMappingURL=SafeAreaContext.js.map\n"],"mappings":";;;;;;;;AAMA,MAAM,QAAQ,QAAQ,IAAI,aAAa;AACvC,MAAM,wBAAwB,MAAM,cAAc,IAAI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