UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

183 lines (181 loc) 5.98 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { LinkingContext, useNavigationBuilder } from "@react-navigation/native"; import { Children, Fragment, isValidElement, use, useMemo, useRef } from "react"; import { StyleSheet, View } from "react-native"; import { useRouteInfo } from "../hooks.native.js"; import { resolveHref } from "../link/href.native.js"; import { useContextKey, useRouteNode } from "../router/Route.native.js"; import { shouldLinkExternally } from "../utils/url.native.js"; import { NavigatorContext } from "../views/Navigator.native.js"; import { triggersToScreens, ViewSlot } from "./common.native.js"; 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 { useComponent } from "./useComponent.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 !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function Tabs(props) { var { children, asChild, options, ...rest } = props; var Comp = asChild ? ViewSlot : View; var { 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; var parentTriggerMap = use(TabTriggerMapContext); var routeNode = useRouteNode(); var contextKey = useContextKey(); var linking = use(LinkingContext).options; var routeInfo = useRouteInfo(); if (!routeNode || !linking) { throw new Error("No RouteNode. This is likely a bug in one router."); } var initialRouteName = routeNode.initialRouteName; var { children, triggerMap } = triggersToScreens(triggers, routeNode, linking, initialRouteName, parentTriggerMap, routeInfo, contextKey); var navigatorContext = useNavigationBuilder(ExpoTabRouter, { children, ...rest, triggerMap, id: contextKey, initialRouteName }); var { state, descriptors, navigation, describe, NavigationContent: RNNavigationContent } = navigatorContext; var descriptorsRef = useRef(descriptors); descriptorsRef.current = descriptors; var navigatorContextValue = useMemo(function () { return { state, navigation, contextKey, router: ExpoTabRouter, descriptorsRef }; }, [state, navigation, contextKey, ExpoTabRouter]); var 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] : false; Children.forEach(children, function (child) { if (!child || ! /* @__PURE__ */isValidElement(child) || isTabSlot(child)) { return; } 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; if (child.props.asChild && /* @__PURE__ */isValidElement(_$children) && _$children.props && _type_of(_$children.props) === "object" && "children" in _$children.props) { _$children = _$children.props.children; } return parseTriggersFromChildren(_$children, screenTriggers, isInTabList || isTabList(child)); } if (!isInTabList || !isTabTrigger(child)) { return; } var { href, name } = child.props; if (!href) { if (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) { if (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 }); }); return screenTriggers; } function isFragment(child) { return child.type === Fragment; } var styles = StyleSheet.create({ tabsRoot: { flex: 1 } }); export { Tabs, useTabsWithChildren, useTabsWithTriggers }; //# sourceMappingURL=Tabs.native.js.map