import { ObservableR } from "@slimr/observable/react"; /** * A read-only representation of an Observable containing a state value. */ type ReadonlyObs = Pick, "name" | "use" | "subscribe"> & { /** The current value of the observable. */ readonly val: T; }; /** * Configuration options for the Router instance. */ export interface RouterOptions { /** Optional CSS selector for the scrollable element to restore scroll positions on. */ scrollElSelector?: string; } /** * A basic definition for a route. */ export interface RouteDef { /** Whether the path should be matched exactly. */ exact?: boolean; /** The React component to render when the route matches. */ component: React.FC; /** Optional metadata associated with the route. */ meta?: Record; /** The path mask pattern to match against (e.g. '/user/:id'). */ path: string; /** Whether the route operates as a stack (preserving stack page history). */ isStack?: boolean; } /** * The specialized route definition used for the fallback page when no routes match. */ export interface NotFoundRouterDef { /** The fallback route is never matched exactly. */ exact: false; /** The React component to render for the not-found view. */ component: React.FC; /** Fallback routes cannot have custom metadata. */ meta?: never; /** The path for the fallback route is always '/'. */ path: "/"; /** Fallback routes cannot be stack routes. */ isStack?: never; } /** * Represents a parsed and compiled route. */ export interface Route extends RouteDef { /** Evaluates if a given path matches this route, returning path parameters if it matches. */ isMatch: (path: string) => false | Record; /** The unique key identifying this route in the routing table. */ key: string; /** Resolves the route path using the provided parameters. */ toPath: (urlParams?: Record) => string; /** The parent stack route if this route is nested under one. */ stack?: Route; /** The history stack containing URLs and scroll positions for back-navigation. */ stackHistory?: { href: string; scrollTop: number; }[]; } /** * Maps a set of route definitions to their compiled Route counterparts. */ type RoutesVal> = { [key in keyof T]: Route; }; /** * A Route representation that has successfully matched the current URL, including extracted URL parameters. */ export type RouteMatch = Route & { /** Extracted path and query parameters from the matched URL. */ urlParams?: Record; }; /** * The type of the Router class constructor. */ export type RouterClass = typeof Router; /** * The type of a Router class instance. */ export type RouterInstance = InstanceType; export declare class Router { /** The compiled routes map for the router. */ routes: RoutesVal; /** The CSS selector for the scrollable container. */ private scrollElSelector?; /** * Returns all compiled route objects as an array. */ get routeArray(): Route[]; /** Map storing history state details (route, URL, and scrollTop) by sequence number. */ historyBySeq: Map; /** Internal sequence counter for history navigation entries. */ private _seq; /** The sequence number of the current active history entry. */ private currentSeq; /** The maximum number of entries to keep in historyBySeq before pruning. */ private maxHistoryEntries; /** * Returns the current router state including active route, URL, path, query params, and scroll position. */ get current(): { route: RouteMatch; url: string; path: string; search: string; searchParams: URLSearchParams; scrollTop: number; }; /** The scroll position to apply next, usually when a page load completes. */ private scrollNext; /** Pending setTimeout IDs for scroll restoration triggers. */ private loadTimeouts; /** The private reactive observable storing the current matched route. */ private _route$; /** The private reactive observable storing the current URLSearchParams. */ private _searchParams$; /** * Returns the read-only observable stream for the active matched route. */ get route$(): ReadonlyObs; /** * Returns the read-only observable stream for the active query parameters. */ get searchParams$(): ReadonlyObs; /** * Initializes the Router instance. * * This compiles the provided route definitions (attaching matching logic, parameter extraction, * and nested stack associations), sets up initial reactive state from the current window location, * and hooks history APIs and DOM link clicks to manage navigation dynamically. * * @param routes - The routes configuration mapping keys to RouteDefs. * @param options - Additional options for router behavior (e.g. scroll container configuration). */ constructor(routes: T, options?: RouterOptions); /** * Finds the matching route definitions and parsed parameters for a given URL. * * @param url - The URL to match against the route definitions. * @returns The matched route with merged route parameters. * @throws Error if no matching route is found. */ find: (url: URL) => RouteMatch; /** * Navigates to a route, a route key, or a URL path. * * @param routeOrKeyOrPath - A Route object, a registered route key, or a URL/path string. * @param urlParams - Optional parameters to interpolate into the path or append as a query string. */ goto: (routeOrKeyOrPath: Route | string, urlParams?: Record) => void; /** * Restores the target scroll position after a navigation event. */ onLoad: () => void; /** The original raw history.pushState method bound to the global history. */ static pushStateRaw: typeof history.pushState; /** * Replaces the current history entry with a new route, route key, or URL path. * * @param routeOrKey - A Route object, a registered route key, or a URL/path string. * @param urlParams - Optional parameters to interpolate into the path or append as a query string. */ replace: (routeOrKey: Route | string, urlParams?: Record) => void; /** The original raw history.replaceState method bound to the global history. */ static replaceStateRaw: typeof history.replaceState; /** * Scrolls to the given scroll options, targeting the configured scroll element if specified, * otherwise the window. * * @param options - Standard scroll options specifying scroll behavior and destination. */ scrollTo(options: ScrollToOptions): void; /** * Intercepts history manipulation, navigation events, and anchor clicks to enable single-page routing. */ private hookHistory; /** * Checks if a path matches a given path mask, optionally requiring an exact match. * * @param path - The pathname to test. * @param pathMask - The route path mask (e.g. '/user/:id'). * @param exact - Whether the match must be exact. * @returns An object of route parameters if matched, otherwise false. */ static isMatch: (path: string, pathMask: string, exact?: boolean) => {}; } export {};