UNPKG

@vaadin/hilla-file-router

Version:

Hilla file-based router

66 lines 2.23 kB
import { createElement } from "react"; import { getHandleFlag, isIndexRoute, isOptionalRoute, isWildcardRoute, RouteHandleFlag } from "./utils.js"; /** * Creates fallback routes for handling unmatched paths and index routes. * * @param component - The React component to render for the fallback routes * @param config - Optional view configuration to attach to the route handles * * @returns A tuple of fallback routes. */ export function createFallbackRoutes(component, config) { return [{ path: "*", element: createElement(component), handle: config }, { index: true, element: createElement(component), handle: config }]; } /** * Creates a route transformer that adds fallback routes to handle unmatched * paths. * * This transformer adds two types of fallback routes: * - A wildcard route (`path: '*'`) that renders the specified fallback * component for any unmatched path. * - An index fallback route (`index: true`) that renders the fallback component * for the empty path. * * The transformer logic determines which fallback to add based on the existing * child routes: * - If a wildcard child route already defined, only the index fallback is * added. * - If an index or optional child route exists, only the wildcard fallback is * added. * - Otherwise, both fallback routes are added. * * @param component - The React component to render as the fallback. * @param config - A view configuration of the fallback route if any. * @returns A route transformer function. */ export default function createFallbackTransformer([notFoundFallback, indexFallback]) { return ({ original, override, children, dupe }) => { if (original && !getHandleFlag(original, RouteHandleFlag.IGNORE_FALLBACK) && !dupe) { if (!children) { return original; } let fallback; if (children.some((route) => isWildcardRoute(route))) { fallback = [indexFallback]; } else if (children.some((route) => isIndexRoute(route) || isOptionalRoute(route))) { fallback = [notFoundFallback]; } else { fallback = [notFoundFallback, indexFallback]; } return { ...original, children: [...children, ...fallback] }; } return override; }; } //# sourceMappingURL=./createFallbackTransformer.js.map