UNPKG

cell-router

Version:

Web Component Router based on WebCell & MobX

78 lines (76 loc) 2.81 kB
import { URLData } from "web-utility"; import { JsxProps, VNode } from "dom-renderer"; import { ClassComponent, FC, WebCell, WebCellProps, ComponentProps, ComponentType } from "web-cell"; export enum RouterMode { hash = "#", history = "/" } export class History { baseURL: string; delimiter: RouterMode; accessor path: string; accessor oldPath: string; constructor(baseURL?: string, delimiter?: RouterMode); protected restore: () => void; push(path?: string): string; static dataOf(path: string): URLData<unknown>; match(pattern: string, path?: string): { [key: string]: string; }; static getTitle(root: HTMLElement): string; titleOf(path?: string): string; handleLink(event: Event, link: HTMLAnchorElement): void; handleForm: (event: Event, form: HTMLFormElement) => void; } export type IncludeText<Raw extends string, Sub extends string> = { [K in Raw]: K extends `${string}${Sub}${string}` ? K : never; }[Raw]; export interface PageProps extends JsxProps<HTMLElement> { path: string; history: History; [key: string]: any; } export interface Route { path: string; component: FC<PageProps> | ClassComponent; } export interface CellRoute extends WebCell<Route> { } export class CellRoute extends HTMLElement implements WebCell<Route> { accessor path: string; component: Route['component']; } export interface CellRouterProps extends WebCellProps { history?: History; routes?: Route[]; } export interface CellRouter extends WebCell<CellRouterProps> { } export class CellRouter extends HTMLElement implements WebCell<CellRouterProps> { #private; accessor history: History | undefined; accessor routes: Route[]; mountedCallback(): void; handleSlotChange: ({ currentTarget }: Event) => void; renderChildren(): Promise<void>; render(): VNode; } export interface RouterOptions<LinkTagName extends string, LinkTagProps extends { href?: string; }> { mode?: keyof typeof RouterMode; linkTags?: Record<LinkTagName, ComponentType<LinkTagProps>>; } export interface LinkProps extends WebCellProps<HTMLAnchorElement> { to: string; } export type FormProps = JsxProps<HTMLFormElement>; export function createRouter<LinkTagName extends string, LinkTagProps extends { href?: string; }>({ mode, linkTags }?: RouterOptions<LinkTagName, LinkTagProps>): { [K in keyof Record<LinkTagName, ComponentType<LinkTagProps>>]: FC<ComponentProps<Record<LinkTagName, ComponentType<LinkTagProps>>[K]>>; } & { Router: ({ routes, ...props }: CellRouterProps) => VNode; Route: ({ path, ...props }: Route) => VNode; Link: ({ to, children, ...props }: LinkProps) => VNode; Form: ({ action, children, ...props }: FormProps) => VNode; }; //# sourceMappingURL=index.d.ts.map