import clsx from "clsx"; import * as React from "react"; import { AwaitProps, Await as RRAwait } from "react-router"; import { SuspenseContext } from "../../contexts"; import { BaseCollection, useCollection, useShallowRender } from "../Collection"; import * as styles from "./styles.module.css"; export function SwitchContainer({ children, ...props }: React.ComponentProps<"div"> & Pick, "resolve">) { const { portal, collection } = useCollection({ children }); return ( <> {portal} ); } function SwitchImpl({ collection, ...props }: React.ComponentProps<"div"> & Pick, "resolve"> & { collection: BaseCollection; }) { const [fallback, children] = React.useMemo(() => { let fallback: React.ReactNode = null, children: React.ReactNode = null; for (const node of collection) { switch (node.type) { case "fallback": fallback = node.props.children; break; case "content": children = node.props.children; break; } } return [fallback, children]; }, [collection]); return ( ); } function SwitchContainerComponent({ fallback, children, resolve, className, preview, ...props }: React.ComponentProps<"div"> & Pick, "resolve"> & { fallback: React.ReactNode; preview?: true; } & Pick, "children">) { const child = React.useMemo(() => { if (resolve instanceof Promise) { return ; } return preview ? fallback : resolve ? typeof children === "function" ? children(resolve) : children : fallback; }, [resolve, children, fallback, preview]); return (
{child}
); } SwitchContainer.Fallback = ({ ref, ...props }: React.ComponentProps<"div">) => { return useShallowRender("fallback", props, ref as any); }; SwitchContainer.Content = ({ ref, ...props }: React.ComponentProps<"div"> & Pick, "children">) => { return useShallowRender("content", props, ref as any); };