one
Version:
One is a React framework for web, iOS, and Android apps.
51 lines (48 loc) • 1.76 kB
JavaScript
import { Activity, Fragment, useState } from "react";
import { useNavigatorContext } from "../views/Navigator.mjs";
import { TabContext } from "./TabContext.mjs";
import { jsx } from "react/jsx-runtime";
function useTabSlot({ detachInactiveScreens = true, renderFn = defaultTabsSlotRender } = {}) {
const { state, descriptorsRef } = useNavigatorContext();
const descriptors = descriptorsRef.current;
const focusedRouteKey = state.routes[state.index].key;
const [loaded, setLoaded] = useState({ [focusedRouteKey]: true });
if (!loaded[focusedRouteKey]) {
setLoaded({
...loaded,
[focusedRouteKey]: true
});
}
return /* @__PURE__ */ jsx(Fragment, { children: state.routes.map((route, index) => {
const descriptor = descriptors[route.key];
return /* @__PURE__ */ jsx(TabContext.Provider, {
value: descriptor.options,
children: renderFn(descriptor, {
index,
isFocused: state.index === index,
loaded: loaded[route.key],
detachInactiveScreens
})
}, descriptor.route.key);
}) });
}
function TabSlot(props) {
return useTabSlot(props);
}
function defaultTabsSlotRender(descriptor, { isFocused, loaded, detachInactiveScreens }) {
const { lazy = true, unmountOnBlur } = descriptor.options;
if ((unmountOnBlur || detachInactiveScreens) && !isFocused) return null;
if (lazy && !loaded && !isFocused) return null;
if (!isFocused) {
return /* @__PURE__ */ jsx(Activity, {
mode: "hidden",
children: descriptor.render()
}, descriptor.route.key);
}
return /* @__PURE__ */ jsx(Fragment, { children: descriptor.render() }, descriptor.route.key);
}
function isTabSlot(child) {
return child.type === TabSlot;
}
export { TabSlot, defaultTabsSlotRender, isTabSlot, useTabSlot };
//# sourceMappingURL=TabSlot.mjs.map