/
context.ts
150 lines (126 loc) · 3.88 KB
/
context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import * as React from "react";
import type {
TrackedPromise,
History,
Location,
Router,
StaticHandlerContext,
To,
AgnosticRouteMatch,
AgnosticIndexRouteObject,
AgnosticLayoutRouteObject,
AgnosticPathRouteObject,
} from "@remix-run/router";
import type { Action as NavigationType } from "@remix-run/router";
// Create react-specific types from the agnostic types in @remix-run/router to
// export from react-router
export type IndexRouteObject = Omit<AgnosticIndexRouteObject, "children"> & {
children?: undefined;
element?: React.ReactNode | null;
errorElement?: React.ReactNode | null;
};
export type PathRouteObject = Omit<AgnosticPathRouteObject, "children"> & {
children?: RouteObject[];
element?: React.ReactNode | null;
errorElement?: React.ReactNode | null;
};
export type LayoutRouteObject = Omit<AgnosticLayoutRouteObject, "children"> & {
children?: RouteObject[];
element?: React.ReactNode | null;
errorElement?: React.ReactNode | null;
};
export type RouteObject =
| IndexRouteObject
| PathRouteObject
| LayoutRouteObject;
export type DataRouteObject = RouteObject & {
children?: DataRouteObject[];
id: string;
};
export interface RouteMatch<
ParamKey extends string = string,
RouteObjectType extends RouteObject = RouteObject
> extends AgnosticRouteMatch<ParamKey, RouteObjectType> {}
export interface DataRouteMatch extends RouteMatch<string, DataRouteObject> {}
// Contexts for data routers
export const DataStaticRouterContext =
React.createContext<StaticHandlerContext | null>(null);
if (__DEV__) {
DataStaticRouterContext.displayName = "DataStaticRouterContext";
}
export interface DataRouterContextObject extends NavigationContextObject {
router: Router;
}
export const DataRouterContext =
React.createContext<DataRouterContextObject | null>(null);
if (__DEV__) {
DataRouterContext.displayName = "DataRouter";
}
export const DataRouterStateContext = React.createContext<
Router["state"] | null
>(null);
if (__DEV__) {
DataRouterStateContext.displayName = "DataRouterState";
}
export const AwaitContext = React.createContext<TrackedPromise | null>(null);
if (__DEV__) {
AwaitContext.displayName = "Await";
}
export type RelativeRoutingType = "route" | "path";
export interface NavigateOptions {
replace?: boolean;
state?: any;
preventScrollReset?: boolean;
relative?: RelativeRoutingType;
}
/**
* A Navigator is a "location changer"; it's how you get to different locations.
*
* Every history instance conforms to the Navigator interface, but the
* distinction is useful primarily when it comes to the low-level <Router> API
* where both the location and a navigator must be provided separately in order
* to avoid "tearing" that may occur in a suspense-enabled app if the action
* and/or location were to be read directly from the history instance.
*/
export interface Navigator {
createHref: History["createHref"];
go: History["go"];
push(to: To, state?: any, opts?: NavigateOptions): void;
replace(to: To, state?: any, opts?: NavigateOptions): void;
}
interface NavigationContextObject {
basename: string;
navigator: Navigator;
static: boolean;
}
export const NavigationContext = React.createContext<NavigationContextObject>(
null!
);
if (__DEV__) {
NavigationContext.displayName = "Navigation";
}
interface LocationContextObject {
location: Location;
navigationType: NavigationType;
}
export const LocationContext = React.createContext<LocationContextObject>(
null!
);
if (__DEV__) {
LocationContext.displayName = "Location";
}
export interface RouteContextObject {
outlet: React.ReactElement | null;
matches: RouteMatch[];
}
export const RouteContext = React.createContext<RouteContextObject>({
outlet: null,
matches: [],
});
if (__DEV__) {
RouteContext.displayName = "Route";
}
export const RouteErrorContext = React.createContext<any>(null);
if (__DEV__) {
RouteErrorContext.displayName = "RouteError";
}