UNPKG

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