UNPKG

one

Version:

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

66 lines (65 loc) 2.69 kB
import { createNativeStackNavigator } from "@react-navigation/native-stack"; import React, { Children, useMemo } from "react"; import { StackScreen, StackHeader, StackHeaderComponent, StackHeaderSearchBar, appendScreenStackPropsToOptions } from "./stack-utils"; import { withLayoutContext } from "./withLayoutContext.mjs"; import { isChildOfType } from "../utils/children.mjs"; import { Protected } from "../views/Protected.mjs"; import { Screen } from "../views/Screen.mjs"; import { jsx } from "react/jsx-runtime"; const NativeStackNavigator = createNativeStackNavigator().Navigator, RNStack = withLayoutContext(NativeStackNavigator); function mapChildren(children) { return Children.toArray(children).map((child, index) => { if (isChildOfType(child, StackScreen)) { const options = appendScreenStackPropsToOptions({}, child.props), { children: _, ...rest } = child.props; return /* @__PURE__ */jsx(Screen, { ...rest, options }, child.props.name ?? index); } return isChildOfType(child, Protected) ? /* @__PURE__ */jsx(Protected, { guard: child.props.guard, children: mapChildren(child.props.children) }, `protected-${index}`) : isChildOfType(child, StackHeaderComponent) ? null : child; }).filter(Boolean); } const StackWithComposition = React.forwardRef((props, ref) => { const { children, screenOptions, ...rest } = props, screenOptionsWithHeader = useMemo(() => { const stackHeader = Children.toArray(children).find(child => isChildOfType(child, StackHeaderComponent)); if (stackHeader && isChildOfType(stackHeader, StackHeaderComponent)) { const headerProps = { children: stackHeader }; return screenOptions ? typeof screenOptions == "function" ? (...args) => { const opts = screenOptions(...args); return appendScreenStackPropsToOptions(opts, headerProps); } : appendScreenStackPropsToOptions(screenOptions, headerProps) : appendScreenStackPropsToOptions({}, headerProps); } return screenOptions; }, [children, screenOptions]), processedChildren = useMemo(() => mapChildren(children), [children]); return /* @__PURE__ */jsx(RNStack, { ...rest, ref, screenOptions: screenOptionsWithHeader, children: processedChildren }); }), Stack = Object.assign(StackWithComposition, { Screen: StackScreen, Header: StackHeader, Protected, SearchBar: StackHeaderSearchBar }); var Stack_default = Stack; export { Stack, Stack_default as default }; //# sourceMappingURL=Stack.mjs.map