UNPKG

one

Version:

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

78 lines (77 loc) 2.77 kB
import { createNativeStackNavigator } from "@react-navigation/native-stack"; import React, { Children, useMemo } from "react"; import { StackScreen, StackHeader, StackHeaderComponent, StackHeaderSearchBar, appendScreenStackPropsToOptions } from "./stack-utils/index.mjs"; 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; const RNStack = withLayoutContext(NativeStackNavigator); function mapChildren(children) { return Children.toArray(children).map((child, index) => { if (isChildOfType(child, StackScreen)) { const options = appendScreenStackPropsToOptions({}, child.props); const { children: _, ...rest } = child.props; return /* @__PURE__ */jsx(Screen, { ...rest, options }, child.props.name ?? index); } if (isChildOfType(child, Protected)) { return /* @__PURE__ */jsx(Protected, { guard: child.props.guard, children: mapChildren(child.props.children) }, `protected-${index}`); } if (isChildOfType(child, StackHeaderComponent)) { return null; } return child; }).filter(Boolean); } const StackWithComposition = React.forwardRef((props, ref) => { const { children, screenOptions, ...rest } = props; const screenOptionsWithHeader = useMemo(() => { const stackHeader = Children.toArray(children).find(child => isChildOfType(child, StackHeaderComponent)); if (stackHeader && isChildOfType(stackHeader, StackHeaderComponent)) { const headerProps = { children: stackHeader }; if (screenOptions) { if (typeof screenOptions === "function") { return (...args) => { const opts = screenOptions(...args); return appendScreenStackPropsToOptions(opts, headerProps); }; } return appendScreenStackPropsToOptions(screenOptions, headerProps); } return appendScreenStackPropsToOptions({}, headerProps); } return screenOptions; }, [children, screenOptions]); const processedChildren = useMemo(() => mapChildren(children), [children]); return /* @__PURE__ */jsx(RNStack, { ...rest, ref, screenOptions: screenOptionsWithHeader, children: processedChildren }); }); const 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