@wbe/low-router-preact
Version:
<h1 align="center" style="text-align:center">low-router preact</h1> <p align="center"> <img alt="npm" src="https://img.shields.io/npm/v/@wbe/low-router-preact"> <img alt="npm bundle size" src="https://img.shields.io/bundlephobia/minzip/%40wbe%2Flow-router
224 lines (210 loc) • 6.47 kB
TypeScript
import * as preact from 'preact';
import { Route as Route$1, LowRouter, RouteContext as RouteContext$1, RouterOptions, HistoryAPI, RouteParams, RouteProps } from '@wbe/low-router';
import * as preact_compat from 'preact/compat';
type AnchorHTMLAttributes<T extends EventTarget> = preact_compat.AnchorHTMLAttributes<T> | preact.JSX.HTMLAttributes<T>;
type PropsWithChildren<T> = T & {
children?: preact.ComponentChildren;
};
type ReactElement = preact_compat.ReactElement;
type Locale<T = any> = {
code: T | string;
name?: string;
default?: boolean;
};
declare class I18n<T = any> {
#private;
locales: Locale<T>[];
currentLocale: Locale<T>;
defaultLocale: Locale<T>;
browserLocale: Locale<T>;
defaultLocaleInUrl: boolean;
base: string;
staticLocation: string;
constructor(locales: Locale<T>[], options?: Partial<{
base: string;
defaultLocaleInUrl: boolean;
staticLocation: string;
}>);
/**
* Set new locale to URL
* Use _fullUrl of last router instance (and not path), to manage locale as needed
*
* ex:
* -> /base/locale/path (with locale)
* -> /base/new-locale/path (with new locale)
* -> /base/path (without locale)
*
*
* Push new URL in history
* @param toLocale
* @param forcePageReload
* @param routeContext
*/
setLocale(toLocale: Locale<T> | string, forcePageReload?: boolean, routeContext?: RouteContext): void;
/**
* Redirect to browser locale
* @param forcePageReload
*/
redirectToBrowserLocale(forcePageReload?: boolean): void;
/**
* Redirect to default locale if no locale is set
* @param forcePageReload
*/
redirectToDefaultLocale(forcePageReload?: boolean): void;
/**
* Current locale is default locale
*/
isDefaultLocaleCode(code?: string | T): boolean;
/**
* Show locale in URL
*/
showLocaleInUrl(): boolean;
/**
* Add :lang param on path
* @param pPath
* @param showLocale
*/
patchLangParam(pPath: string, showLocale?: boolean): string;
/**
* Add Locale to Routes
* Patch all first level routes with ":lang" param
* { path: "/foo" } -> { path: "/:lang/foo" }
* @param routes
* @param showLocaleInUrl
*/
addLocaleParamToRoutes(routes: Route$1[], showLocaleInUrl?: boolean): Route$1[];
}
type InitialStaticProps = Record<string, Record<string, any>>;
/**
* get static props from url
*
* - create a router instance
* - resolve the url
* - request getStaticProps from the route
*
* @param url
* @param router
* @param i18n
*/
declare function getStaticPropsFromUrl(url: string, router: LowRouter, i18n?: I18n): Promise<InitialStaticProps>;
interface IRouterContext {
prevContext: RouteContext$1;
currentContext: RouteContext$1;
router: LowRouter;
base: string;
routes: Route$1[];
options: Partial<RouterOptions>;
history: HistoryAPI | any;
counter: number;
staticLocation: string;
i18n: I18n;
}
declare const RouterContext: preact.Context<IRouterContext>;
interface IRouters {
router: LowRouter;
base: string;
routes: Route$1[];
history: any;
currentContext: RouteContext$1;
staticLocation: string;
initialStaticProps: InitialStaticProps;
staticPropsCache: Record<string, Record<string, any>>;
isFirstRoute: boolean;
routeCounter: number;
i18n: I18n;
}
declare let ROUTERS: IRouters;
/**
* LowReactRouter
* A Single react/preact router instance
*
*/
declare function LowReactRouter(props: {
router: LowRouter;
children: ReactElement | ReactElement[];
history?: HistoryAPI | any;
staticLocation?: string;
initialStaticProps?: InitialStaticProps;
i18n?: I18n;
}): preact.VNode<{
value: IRouterContext;
children?: preact.ComponentChildren;
}>;
interface RouteRef {
playIn: () => Promise<void>;
playOut: () => Promise<void>;
root: HTMLElement;
name: string;
routeId?: number;
}
interface StackTransitionsParams {
prev: RouteRef;
current: RouteRef;
unmountPrev: () => void;
}
interface Props {
transitions?: (T: StackTransitionsParams) => Promise<void>;
clampRoutesRender?: boolean;
}
/**
* Stack
* @param transitions
* @param clampRoutesRender
*/
declare function Stack({ transitions, clampRoutesRender }: Props): preact.VNode<preact.ClassAttributes<HTMLElement> & {
className: string;
}>;
type TAnchorWithoutHref = Omit<AnchorHTMLAttributes<HTMLAnchorElement>, "href">;
interface ILinkProps extends PropsWithChildren<TAnchorWithoutHref> {
to: string | {
name: string;
params?: RouteParams;
};
onClick?: () => void;
className?: string;
children: any;
}
declare const ForwardLink: preact.FunctionalComponent<preact_compat.PropsWithoutRef<ILinkProps> & {
ref?: preact.Ref<any>;
}>;
/**
* Shortcut to create a router instance passed to <Router> component
* @param from Route name of the component where the router is created
* @param id Unique id for the router instance
*/
declare const useCreateRouter: ({ from, id }: {
from: string;
id: number | string;
}) => LowRouter;
declare const useRouter: () => IRouterContext;
/**
* return a compile pathname (or url) by route name
* ex:
* /base/fr/path-fr/ -> /base/en/path-en/
* const composedUrl = composeUrlByRouteName(routeContext.route?.name, { lang: toLocale.key })
*
* @param name
* @param params
* @param routes
* @param base
* @param i18n
*/
declare function composeUrlByRouteName(name: string, params: RouteParams, routes?: Route[], i18n?: I18n<any>, base?: string): string;
declare function setLocation(to: string | {
name: string;
params?: RouteParams;
}, i18n?: I18n<any>, base?: string, history?: any): void;
declare module "@wbe/low-router" {
export interface Route {
path?: string
name: string
getStaticProps?: (context: RouteContext, locale: Locale) => Promise<any>
translations?: Record<string, string>
_props?: RouteProps
}
export interface RouteContext {
routeId: number
route: Route
}
}
export { I18n, type IRouterContext, type InitialStaticProps, ForwardLink as Link, type Locale, ROUTERS, LowReactRouter as Router, RouterContext, Stack, type StackTransitionsParams, composeUrlByRouteName, getStaticPropsFromUrl, setLocation, useCreateRouter, useRouter };