one
Version:
One is a new React Framework that makes Vite serve both native and web.
163 lines (161 loc) • 5.88 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { LinkingContext, useNavigationBuilder } from "@react-navigation/native";
import { Children, Fragment, isValidElement, use, useMemo } from "react";
import { StyleSheet, View } from "react-native";
import { TabTriggerMapContext } from "./TabContext.native.js";
import { isTabList } from "./TabList.native.js";
import { ExpoTabRouter } from "./TabRouter.native.js";
import { isTabSlot } from "./TabSlot.native.js";
import { isTabTrigger } from "./TabTrigger.native.js";
import { ViewSlot, triggersToScreens } from "./common.native.js";
import { useComponent } from "./useComponent.native.js";
import { useRouteNode, useContextKey } from "../router/Route.native.js";
import { useRouteInfo } from "../hooks.native.js";
import { resolveHref } from "../link/href.native.js";
import { shouldLinkExternally } from "../utils/url.native.js";
import { NavigatorContext } from "../views/Navigator.native.js";
export * from "./TabContext.native.js";
export * from "./TabList.native.js";
export * from "./TabSlot.native.js";
export * from "./TabTrigger.native.js";
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol < "u" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function Tabs(props) {
var {
children,
asChild,
options,
...rest
} = props,
Comp = asChild ? ViewSlot : View,
{
NavigationContent
} = useTabsWithChildren({
// asChild adds an extra layer, so we need to process the child's children
children: asChild && /* @__PURE__ */isValidElement(children) && children.props && _type_of(children.props) === "object" && "children" in children.props ? children.props.children : children,
...options
});
return /* @__PURE__ */_jsx(Comp, {
style: styles.tabsRoot,
...rest,
children: /* @__PURE__ */_jsx(NavigationContent, {
children
})
});
}
function useTabsWithChildren(options) {
var {
children,
...rest
} = options;
return useTabsWithTriggers({
triggers: parseTriggersFromChildren(children),
...rest
});
}
function useTabsWithTriggers(options) {
var {
triggers,
...rest
} = options,
parentTriggerMap = use(TabTriggerMapContext),
routeNode = useRouteNode(),
contextKey = useContextKey(),
linking = use(LinkingContext).options,
routeInfo = useRouteInfo();
if (!routeNode || !linking) throw new Error("No RouteNode. This is likely a bug in one router.");
var initialRouteName = routeNode.initialRouteName,
{
children,
triggerMap
} = triggersToScreens(triggers, routeNode, linking, initialRouteName, parentTriggerMap, routeInfo, contextKey),
navigatorContext = useNavigationBuilder(ExpoTabRouter, {
children,
...rest,
triggerMap,
id: contextKey,
initialRouteName
}),
{
state,
descriptors,
navigation,
describe,
NavigationContent: RNNavigationContent
} = navigatorContext,
navigatorContextValue = useMemo(function () {
return {
...navigatorContext,
contextKey,
router: ExpoTabRouter
};
}, [navigatorContext, contextKey, ExpoTabRouter]),
NavigationContent = useComponent(function (children2) {
return /* @__PURE__ */_jsx(TabTriggerMapContext.Provider, {
value: triggerMap,
children: /* @__PURE__ */_jsx(NavigatorContext.Provider, {
value: navigatorContextValue,
children: /* @__PURE__ */_jsx(RNNavigationContent, {
children: children2
})
})
});
});
return {
state,
descriptors,
navigation,
NavigationContent,
describe
};
}
function parseTriggersFromChildren(children) {
var screenTriggers = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [],
isInTabList = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : !1;
return Children.forEach(children, function (child) {
if (!(!child || ! /* @__PURE__ */isValidElement(child) || isTabSlot(child))) {
if (isFragment(child) && typeof child.props.children != "function") return parseTriggersFromChildren(child.props.children, screenTriggers, isInTabList || isTabList(child));
if (isTabList(child) && typeof child.props.children != "function") {
var _$children = child.props.children;
return child.props.asChild && /* @__PURE__ */isValidElement(_$children) && _$children.props && _type_of(_$children.props) === "object" && "children" in _$children.props && (_$children = _$children.props.children), parseTriggersFromChildren(_$children, screenTriggers, isInTabList || isTabList(child));
}
if (!(!isInTabList || !isTabTrigger(child))) {
var {
href,
name
} = child.props;
if (!href) {
process.env.NODE_ENV === "development" && console.warn(`<TabTrigger name={${name}}> does not have a 'href' prop. TabTriggers within a <TabList /> are required to have an href.`);
return;
}
var resolvedHref = resolveHref(href);
if (shouldLinkExternally(resolvedHref)) return screenTriggers.push({
type: "external",
name,
href: resolvedHref
});
if (!name) {
process.env.NODE_ENV === "development" && console.warn("<TabTrigger> does not have a 'name' prop. TabTriggers within a <TabList /> are required to have a name.");
return;
}
return screenTriggers.push({
type: "internal",
href: resolvedHref,
name
});
}
}
}), screenTriggers;
}
function isFragment(child) {
return child.type === Fragment;
}
var styles = StyleSheet.create({
tabsRoot: {
flex: 1
}
});
export { Tabs, useTabsWithChildren, useTabsWithTriggers };
//# sourceMappingURL=Tabs.native.js.map