UNPKG

5.93 kBTypeScriptView Raw
1// Type definitions for @reach/router 1.3
2// Project: https://github.com/reach/router
3// Definitions by: A.Mokhtar <https://github.com/xMokAx>
4// Awwit <https://github.com/awwit>
5// wroughtec <https://github.com/wroughtec>
6// O.Jackman <https://github.com/chilledoj>
7// Eyas <https://github.com/Eyas>
8// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9// TypeScript Version: 2.8
10
11import * as React from 'react';
12
13export interface HLocation<S = unknown> {
14 pathname: string;
15 search: string;
16 state: S;
17 hash: string;
18 key?: string | undefined;
19}
20export type WindowLocation<S = unknown> = Window['location'] & HLocation<S>;
21
22export type HistoryActionType = 'PUSH' | 'POP';
23export type HistoryLocation = WindowLocation & { state?: any };
24export interface HistoryListenerParameter {
25 location: HistoryLocation;
26 action: HistoryActionType;
27}
28export type HistoryListener = (parameter: HistoryListenerParameter) => void;
29export type HistoryUnsubscribe = () => void;
30
31export interface History {
32 readonly location: HistoryLocation;
33 readonly transitioning: boolean;
34 listen: (listener: HistoryListener) => HistoryUnsubscribe;
35 navigate: NavigateFn;
36}
37
38export class Router extends React.Component<RouterProps & React.HTMLProps<HTMLDivElement>> {}
39
40export interface RouterProps {
41 basepath?: string | undefined;
42 primary?: boolean | undefined;
43 location?: WindowLocation | undefined;
44 component?: React.ComponentType | string | undefined;
45}
46
47export type RouteComponentProps<TParams = {}> = Partial<TParams> & {
48 path?: string | undefined;
49 default?: boolean | undefined;
50 location?: WindowLocation | undefined;
51 navigate?: NavigateFn | undefined;
52 uri?: string | undefined;
53};
54
55export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
56
57export type AnchorProps = Omit<
58 React.DetailedHTMLProps<React.AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>,
59 'href' // remove href, as it's ignored by the router
60>;
61
62export interface LinkProps<TState> extends AnchorProps {
63 to: string;
64 replace?: boolean | undefined;
65 getProps?: ((props: LinkGetProps) => {}) | undefined;
66 state?: TState | undefined;
67 /** @deprecated If using React >= 16.4, use ref instead. */
68 innerRef?: React.Ref<HTMLAnchorElement> | undefined;
69}
70
71export interface LinkGetProps {
72 isCurrent: boolean;
73 isPartiallyCurrent: boolean;
74 href: string;
75 location: WindowLocation;
76}
77
78export function Link<TState>(
79 // TODO: Define this as ...params: Parameters<Link<TState>> when only TypeScript >= 3.1 support is needed.
80 props: React.PropsWithoutRef<LinkProps<TState>> & React.RefAttributes<HTMLAnchorElement>,
81): ReturnType<Link<TState>>;
82export interface Link<TState>
83 extends React.ForwardRefExoticComponent<
84 React.PropsWithoutRef<LinkProps<TState>> & React.RefAttributes<HTMLAnchorElement>
85 > {}
86
87export interface RedirectProps<TState> {
88 from?: string | undefined;
89 to: string;
90 noThrow?: boolean | undefined;
91 state?: TState | undefined;
92 replace?: boolean | undefined;
93}
94
95export class Redirect<TState> extends React.Component<RouteComponentProps<RedirectProps<TState>>> {}
96
97export interface MatchProps<TParams> {
98 path: string;
99 children: MatchRenderFn<TParams>;
100}
101
102export type MatchRenderFn<TParams> = (props: MatchRenderProps<TParams>) => React.ReactNode;
103
104export interface MatchRenderProps<TParams> {
105 match: null | ({ uri: string; path: string } & TParams);
106 location: WindowLocation;
107 navigate: NavigateFn;
108}
109
110export class Match<TParams> extends React.Component<MatchProps<TParams>> {}
111
112export interface NavigateFn {
113 (to: string, options?: NavigateOptions<{}>): Promise<void>;
114 (to: number, options?: undefined): Promise<void>;
115}
116
117export interface NavigateOptions<TState> {
118 state?: TState | undefined;
119 replace?: boolean | undefined;
120}
121
122export interface LocationProps {
123 children: LocationProviderRenderFn;
124}
125
126export class Location extends React.Component<LocationProps> {}
127
128export interface LocationProviderProps {
129 history?: History | undefined;
130 children?: React.ReactNode | LocationProviderRenderFn | undefined;
131}
132
133export type LocationProviderRenderFn = (context: LocationContext) => React.ReactNode;
134
135export interface LocationContext {
136 location: WindowLocation;
137 navigate: NavigateFn;
138}
139
140export class LocationProvider extends React.Component<LocationProviderProps> {}
141
142export interface ServerLocationProps {
143 url: string;
144}
145
146export class ServerLocation extends React.Component<ServerLocationProps> {}
147
148export const navigate: NavigateFn;
149
150export interface HistorySource {
151 readonly location: WindowLocation;
152 addEventListener(name: string, listener: (event: Event) => void): void;
153 removeEventListener(name: string, listener: (event: Event) => void): void;
154 history: {
155 readonly state: any;
156 pushState(state: any, title: string, uri: string): void;
157 replaceState(state: any, title: string, uri: string): void;
158 };
159}
160
161export function createHistory(source: HistorySource): History;
162
163export function createMemorySource(initialPath: string): HistorySource;
164
165export interface RedirectRequest {
166 uri: string;
167}
168
169export function isRedirect(error: any): error is RedirectRequest;
170
171export function redirectTo(uri: string): void;
172
173export const globalHistory: History;
174
175export function useLocation(): WindowLocation;
176
177export function useNavigate(): NavigateFn;
178
179// TODO: In the next major release update we should update the type parameter default of TPrams from `any` to `{}`,
180// it is currently being keep for backwards compatibility with version 1.3.7 or below.
181export function useParams<TParams extends { [Param in keyof TParams]?: string } = any>(): TParams;
182
183export function useMatch(pathname: string): null | { uri: string; path: string; [param: string]: string };