one
Version:
One is a React framework for web, iOS, and Android apps.
105 lines (103 loc) • 3.47 kB
JavaScript
function resolveSafeAreaEdgeModes(edges) {
const all = [
"top",
"right",
"bottom",
"left"
];
if (edges == null) {
return {
top: "additive",
right: "additive",
bottom: "additive",
left: "additive"
};
}
if (Array.isArray(edges)) {
return Object.fromEntries(all.map((edge) => [edge, edges.includes(edge) ? "additive" : "off"]));
}
return Object.fromEntries(all.map((edge) => {
const mode = edges[edge];
return [edge, mode === "maximum" || mode === "additive" || mode === "off" ? mode : "off"];
}));
}
function flattenStyle(style, resolveStyle) {
if (!style) return {};
if (resolveStyle) {
const resolved = resolveStyle(style);
if (!resolved || typeof resolved !== "object" || Array.isArray(resolved)) {
return flattenStyle(resolved);
}
return resolved;
}
if (Array.isArray(style)) {
return Object.assign({}, ...style.map((entry) => flattenStyle(entry)));
}
if (typeof style === "number") {
throw new Error("buildSafeAreaInsetStyle cannot read a registered StyleSheet ID without resolveStyle; pass StyleSheet.flatten");
}
return typeof style === "object" ? style : {};
}
function readBaseInset(style, mode, edge) {
const capitalized = edge.charAt(0).toUpperCase() + edge.slice(1);
const exact = style[`${mode}${capitalized}`];
if (typeof exact === "number") return exact;
const axis = edge === "top" || edge === "bottom" ? style[`${mode}Vertical`] : style[`${mode}Horizontal`];
if (typeof axis === "number") return axis;
const base = style[mode];
return typeof base === "number" ? base : 0;
}
function buildSafeAreaInsetStyle(options) {
const mode = options.mode === "margin" ? "margin" : "padding";
const flattened = flattenStyle(options.style, options.resolveStyle);
const edgeModes = resolveSafeAreaEdgeModes(options.edges);
const output = {};
for (const edge of [
"top",
"right",
"bottom",
"left"
]) {
const base = readBaseInset(flattened, mode, edge);
const inset = options.insets[edge];
const edgeMode = edgeModes[edge];
const value = edgeMode === "off" ? base : edgeMode === "maximum" ? Math.max(base, inset) : base + inset;
output[`${mode}${edge.charAt(0).toUpperCase() + edge.slice(1)}`] = value;
}
return output;
}
function keyboardSafeBottom(systemBottom, stableBottom) {
return Math.min(systemBottom, stableBottom);
}
function resolveOverlappingInsets(options) {
const { windowInsets, windowWidth, windowHeight, visibleLeft, visibleTop, viewWidth, viewHeight } = options;
return {
top: Math.max(windowInsets.top - visibleTop, 0),
right: Math.max(Math.min(visibleLeft + viewWidth - windowWidth, 0) + windowInsets.right, 0),
bottom: Math.max(Math.min(visibleTop + viewHeight - windowHeight, 0) + windowInsets.bottom, 0),
left: Math.max(windowInsets.left - visibleLeft, 0)
};
}
function providerEventToMetrics(payload) {
for (const [key, value] of Object.entries(payload)) {
if (typeof value !== "number" || !Number.isFinite(value)) {
throw new Error(`OneNativeSafeAreaProvider emitted non-finite ${key}`);
}
}
return {
insets: {
top: payload.insetTop,
right: payload.insetRight,
bottom: payload.insetBottom,
left: payload.insetLeft
},
frame: {
x: payload.frameX,
y: payload.frameY,
width: payload.frameWidth,
height: payload.frameHeight
}
};
}
export { buildSafeAreaInsetStyle, keyboardSafeBottom, providerEventToMetrics, resolveOverlappingInsets, resolveSafeAreaEdgeModes };
//# sourceMappingURL=insets.mjs.map