one
Version:
One is a React framework for web, iOS, and Android apps.
108 lines (105 loc) • 4.05 kB
JavaScript
import { jsx } from "react/jsx-runtime";
import * as React from "react";
import { NativeSafeAreaProvider } from "./NativeSafeAreaProvider.native.js";
import { getWindowFrame } from "./getWindowFrame.native.js";
import { initialWindowMetrics } from "./InitialWindow.native.js";
import { feedTamaguiSafeArea } from "./tamaguiSafeArea.native.js";
var isDev = process.env.NODE_ENV !== "production";
var SafeAreaInsetsContext = /* @__PURE__ */ React.createContext(null);
if (isDev) {
SafeAreaInsetsContext.displayName = "SafeAreaInsetsContext";
}
var SafeAreaFrameContext = /* @__PURE__ */ React.createContext(null);
if (isDev) {
SafeAreaFrameContext.displayName = "SafeAreaFrameContext";
}
var ZERO_INSETS = {
top: 0,
bottom: 0,
left: 0,
right: 0
};
function SafeAreaProvider(param) {
var { children, initialMetrics, initialSafeAreaInsets, ...others } = param;
var _ref, _ref1, _ref2, _ref3, _ref4;
var parentInsets = useParentSafeAreaInsets();
var parentFrame = useParentSafeAreaFrame();
var isRoot = parentInsets === null;
var [insets, setInsets] = React.useState((_ref = (_ref1 = (_ref2 = initialMetrics === null || initialMetrics === void 0 ? void 0 : initialMetrics.insets) !== null && _ref2 !== void 0 ? _ref2 : initialSafeAreaInsets) !== null && _ref1 !== void 0 ? _ref1 : parentInsets) !== null && _ref !== void 0 ? _ref : ZERO_INSETS);
var [frame, setFrame] = React.useState((_ref3 = (_ref4 = initialMetrics === null || initialMetrics === void 0 ? void 0 : initialMetrics.frame) !== null && _ref4 !== void 0 ? _ref4 : parentFrame) !== null && _ref3 !== void 0 ? _ref3 : getWindowFrame());
var onInsetsChange = React.useCallback(function(event) {
var { nativeEvent: { frame: nextFrame, insets: nextInsets } } = event;
if (isRoot && nextFrame) {
feedTamaguiSafeArea({
insets: nextInsets,
frame: nextFrame
}, tamaguiHooks);
}
setFrame(function(curFrame) {
if (nextFrame && (nextFrame.height !== curFrame.height || nextFrame.width !== curFrame.width || nextFrame.x !== curFrame.x || nextFrame.y !== curFrame.y)) {
return nextFrame;
}
return curFrame;
});
setInsets(function(curInsets) {
if (!curInsets || nextInsets.bottom !== curInsets.bottom || nextInsets.left !== curInsets.left || nextInsets.right !== curInsets.right || nextInsets.top !== curInsets.top) {
return nextInsets;
}
return curInsets;
});
}, [isRoot]);
return /* @__PURE__ */ jsx(NativeSafeAreaProvider, {
onInsetsChange,
...others,
children: /* @__PURE__ */ jsx(SafeAreaFrameContext.Provider, {
value: frame,
children: /* @__PURE__ */ jsx(SafeAreaInsetsContext.Provider, {
value: insets,
children
})
})
});
}
function useParentSafeAreaInsets() {
return React.useContext(SafeAreaInsetsContext);
}
function useParentSafeAreaFrame() {
return React.useContext(SafeAreaFrameContext);
}
var NO_INSETS_ERROR = "No safe area value available. Make sure you are rendering `<SafeAreaProvider>` at the top of your app.";
function useSafeAreaInsets() {
var insets = React.useContext(SafeAreaInsetsContext);
if (insets == null) {
throw new Error(NO_INSETS_ERROR);
}
return insets;
}
function useSafeAreaFrame() {
var frame = React.useContext(SafeAreaFrameContext);
if (frame == null) {
throw new Error(NO_INSETS_ERROR);
}
return frame;
}
function withSafeAreaInsets(WrappedComponent) {
return /* @__PURE__ */ React.forwardRef(function(props, ref) {
var insets = useSafeAreaInsets();
return /* @__PURE__ */ jsx(WrappedComponent, {
...props,
insets,
ref
});
});
}
var tamaguiHooks = {
useSafeAreaInsets,
useSafeAreaFrame
};
feedTamaguiSafeArea(initialWindowMetrics, tamaguiHooks);
function useSafeArea() {
return useSafeAreaInsets();
}
var SafeAreaConsumer = SafeAreaInsetsContext.Consumer;
var SafeAreaContext = SafeAreaInsetsContext;
export { SafeAreaConsumer, SafeAreaContext, SafeAreaFrameContext, SafeAreaInsetsContext, SafeAreaProvider, useSafeArea, useSafeAreaFrame, useSafeAreaInsets, withSafeAreaInsets };
//# sourceMappingURL=SafeAreaContext.native.js.map