UNPKG

one

Version:

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

56 lines (55 loc) 2.52 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"; import { isChildOfType } from "../utils/children"; import { Protected } from "../views/Protected"; import { Screen } from "../views/Screen"; 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.js.map