one
Version:
One is a new React Framework that makes Vite serve both native and web.
199 lines • 7.29 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var Tabs_exports = {};
__export(Tabs_exports, {
Tabs: () => Tabs,
useTabsWithChildren: () => useTabsWithChildren,
useTabsWithTriggers: () => useTabsWithTriggers
});
module.exports = __toCommonJS(Tabs_exports);
var import_native = require("@react-navigation/native");
var import_react = require("react");
var import_react_native = require("react-native-web");
var import_hooks = require("../hooks.cjs");
var import_href = require("../link/href.cjs");
var import_Route = require("../router/Route.cjs");
var import_url = require("../utils/url.cjs");
var import_Navigator = require("../views/Navigator.cjs");
var import_common = require("./common.cjs");
var import_TabContext = require("./TabContext.cjs");
var import_TabList = require("./TabList.cjs");
var import_TabRouter = require("./TabRouter.cjs");
var import_TabSlot = require("./TabSlot.cjs");
var import_TabTrigger = require("./TabTrigger.cjs");
var import_useComponent = require("./useComponent.cjs");
__reExport(Tabs_exports, require("./TabContext.cjs"), module.exports);
__reExport(Tabs_exports, require("./TabList.cjs"), module.exports);
__reExport(Tabs_exports, require("./TabSlot.cjs"), module.exports);
__reExport(Tabs_exports, require("./TabTrigger.cjs"), module.exports);
var import_jsx_runtime = require("react/jsx-runtime");
function Tabs(props) {
const {
children,
asChild,
options,
...rest
} = props;
const Comp = asChild ? import_common.ViewSlot : import_react_native.View;
const {
NavigationContent
} = useTabsWithChildren({
// asChild adds an extra layer, so we need to process the child's children
children: asChild && (0, import_react.isValidElement)(children) && children.props && typeof children.props === "object" && "children" in children.props ? children.props.children : children,
...options
});
return /* @__PURE__ */(0, import_jsx_runtime.jsx)(Comp, {
style: styles.tabsRoot,
...rest,
children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(NavigationContent, {
children
})
});
}
function useTabsWithChildren(options) {
const {
children,
...rest
} = options;
return useTabsWithTriggers({
triggers: parseTriggersFromChildren(children),
...rest
});
}
function useTabsWithTriggers(options) {
const {
triggers,
...rest
} = options;
const parentTriggerMap = (0, import_react.use)(import_TabContext.TabTriggerMapContext);
const routeNode = (0, import_Route.useRouteNode)();
const contextKey = (0, import_Route.useContextKey)();
const linking = (0, import_react.use)(import_native.LinkingContext).options;
const routeInfo = (0, import_hooks.useRouteInfo)();
if (!routeNode || !linking) {
throw new Error("No RouteNode. This is likely a bug in one router.");
}
const initialRouteName = routeNode.initialRouteName;
const {
children,
triggerMap
} = (0, import_common.triggersToScreens)(triggers, routeNode, linking, initialRouteName, parentTriggerMap, routeInfo, contextKey);
const navigatorContext = (0, import_native.useNavigationBuilder)(import_TabRouter.ExpoTabRouter, {
children,
...rest,
triggerMap,
id: contextKey,
initialRouteName
});
const {
state,
descriptors,
navigation,
describe,
NavigationContent: RNNavigationContent
} = navigatorContext;
const descriptorsRef = (0, import_react.useRef)(descriptors);
descriptorsRef.current = descriptors;
const navigatorContextValue = (0, import_react.useMemo)(() => ({
state,
navigation,
contextKey,
router: import_TabRouter.ExpoTabRouter,
descriptorsRef
}), [state, navigation, contextKey, import_TabRouter.ExpoTabRouter]);
const NavigationContent = (0, import_useComponent.useComponent)(children2 => /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_TabContext.TabTriggerMapContext.Provider, {
value: triggerMap,
children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_Navigator.NavigatorContext.Provider, {
value: navigatorContextValue,
children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(RNNavigationContent, {
children: children2
})
})
}));
return {
state,
descriptors,
navigation,
NavigationContent,
describe
};
}
function parseTriggersFromChildren(children, screenTriggers = [], isInTabList = false) {
import_react.Children.forEach(children, child => {
if (!child || !(0, import_react.isValidElement)(child) || (0, import_TabSlot.isTabSlot)(child)) {
return;
}
if (isFragment(child) && typeof child.props.children !== "function") {
return parseTriggersFromChildren(child.props.children, screenTriggers, isInTabList || (0, import_TabList.isTabList)(child));
}
if ((0, import_TabList.isTabList)(child) && typeof child.props.children !== "function") {
let children2 = child.props.children;
if (child.props.asChild && (0, import_react.isValidElement)(children2) && children2.props && typeof children2.props === "object" && "children" in children2.props) {
children2 = children2.props.children;
}
return parseTriggersFromChildren(children2, screenTriggers, isInTabList || (0, import_TabList.isTabList)(child));
}
if (!isInTabList || !(0, import_TabTrigger.isTabTrigger)(child)) {
return;
}
const {
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;
}
const resolvedHref = (0, import_href.resolveHref)(href);
if ((0, import_url.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 === import_react.Fragment;
}
const styles = import_react_native.StyleSheet.create({
tabsRoot: {
flex: 1
}
});