UNPKG

12.6 kBTypeScriptView Raw
1import { StoryData } from './utils.js';
2export { DEEPLY_EQUAL, buildArgsParam, deepDiff, getMatch, parsePath, queryFromLocation, queryFromString, stringifyQuery } from './utils.js';
3import * as React from 'react';
4import React__default, { ReactNode, ReactElement } from 'react';
5
6/**
7 * Actions represent the type of change to a location value.
8 *
9 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#action
10 */
11declare enum Action {
12 /**
13 * A POP indicates a change to an arbitrary index in the history stack, such
14 * as a back or forward navigation. It does not describe the direction of the
15 * navigation, only that the current index changed.
16 *
17 * Note: This is the default action for newly created history objects.
18 */
19 Pop = "POP",
20 /**
21 * A PUSH indicates a new entry being added to the history stack, such as when
22 * a link is clicked and a new page loads. When this happens, all subsequent
23 * entries in the stack are lost.
24 */
25 Push = "PUSH",
26 /**
27 * A REPLACE indicates the entry at the current index in the history stack
28 * being replaced by a new one.
29 */
30 Replace = "REPLACE"
31}
32/**
33 * A URL pathname, beginning with a /.
34 *
35 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.pathname
36 */
37declare type Pathname = string;
38/**
39 * A URL search string, beginning with a ?.
40 *
41 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.search
42 */
43declare type Search = string;
44/**
45 * A URL fragment identifier, beginning with a #.
46 *
47 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.hash
48 */
49declare type Hash = string;
50/**
51 * A unique string associated with a location. May be used to safely store
52 * and retrieve data in some other storage API, like `localStorage`.
53 *
54 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.key
55 */
56declare type Key = string;
57/**
58 * The pathname, search, and hash values of a URL.
59 */
60interface Path {
61 /**
62 * A URL pathname, beginning with a /.
63 *
64 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.pathname
65 */
66 pathname: Pathname;
67 /**
68 * A URL search string, beginning with a ?.
69 *
70 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.search
71 */
72 search: Search;
73 /**
74 * A URL fragment identifier, beginning with a #.
75 *
76 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.hash
77 */
78 hash: Hash;
79}
80/**
81 * An entry in a history stack. A location contains information about the
82 * URL path, as well as possibly some arbitrary state and a key.
83 *
84 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location
85 */
86interface Location$1 extends Path {
87 /**
88 * A value of arbitrary data associated with this location.
89 *
90 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.state
91 */
92 state: unknown;
93 /**
94 * A unique string associated with this location. May be used to safely store
95 * and retrieve data in some other storage API, like `localStorage`.
96 *
97 * Note: This value is always "default" on the initial location.
98 *
99 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.key
100 */
101 key: Key;
102}
103/**
104 * A change to the current location.
105 */
106interface Update {
107 /**
108 * The action that triggered the change.
109 */
110 action: Action;
111 /**
112 * The new location.
113 */
114 location: Location$1;
115}
116/**
117 * A function that receives notifications about location changes.
118 */
119interface Listener {
120 (update: Update): void;
121}
122/**
123 * A change to the current location that was blocked. May be retried
124 * after obtaining user confirmation.
125 */
126interface Transition extends Update {
127 /**
128 * Retries the update to the current location.
129 */
130 retry(): void;
131}
132/**
133 * A function that receives transitions when navigation is blocked.
134 */
135interface Blocker {
136 (tx: Transition): void;
137}
138/**
139 * Describes a location that is the destination of some navigation, either via
140 * `history.push` or `history.replace`. May be either a URL or the pieces of a
141 * URL path.
142 */
143declare type To = string | Partial<Path>;
144/**
145 * A history is an interface to the navigation stack. The history serves as the
146 * source of truth for the current location, as well as provides a set of
147 * methods that may be used to change it.
148 *
149 * It is similar to the DOM's `window.history` object, but with a smaller, more
150 * focused API.
151 */
152interface History {
153 /**
154 * The last action that modified the current location. This will always be
155 * Action.Pop when a history instance is first created. This value is mutable.
156 *
157 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.action
158 */
159 readonly action: Action;
160 /**
161 * The current location. This value is mutable.
162 *
163 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.location
164 */
165 readonly location: Location$1;
166 /**
167 * Returns a valid href for the given `to` value that may be used as
168 * the value of an <a href> attribute.
169 *
170 * @param to - The destination URL
171 *
172 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.createHref
173 */
174 createHref(to: To): string;
175 /**
176 * Pushes a new location onto the history stack, increasing its length by one.
177 * If there were any entries in the stack after the current one, they are
178 * lost.
179 *
180 * @param to - The new URL
181 * @param state - Data to associate with the new location
182 *
183 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.push
184 */
185 push(to: To, state?: any): void;
186 /**
187 * Replaces the current location in the history stack with a new one. The
188 * location that was replaced will no longer be available.
189 *
190 * @param to - The new URL
191 * @param state - Data to associate with the new location
192 *
193 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.replace
194 */
195 replace(to: To, state?: any): void;
196 /**
197 * Navigates `n` entries backward/forward in the history stack relative to the
198 * current index. For example, a "back" navigation would use go(-1).
199 *
200 * @param delta - The delta in the stack index
201 *
202 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.go
203 */
204 go(delta: number): void;
205 /**
206 * Navigates to the previous entry in the stack. Identical to go(-1).
207 *
208 * Warning: if the current location is the first location in the stack, this
209 * will unload the current document.
210 *
211 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.back
212 */
213 back(): void;
214 /**
215 * Navigates to the next entry in the stack. Identical to go(1).
216 *
217 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.forward
218 */
219 forward(): void;
220 /**
221 * Sets up a listener that will be called whenever the current location
222 * changes.
223 *
224 * @param listener - A function that will be called when the location changes
225 * @returns unlisten - A function that may be used to stop listening
226 *
227 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.listen
228 */
229 listen(listener: Listener): () => void;
230 /**
231 * Prevents the current location from changing and sets up a listener that
232 * will be called instead.
233 *
234 * @param blocker - A function that will be called when a transition is blocked
235 * @returns unblock - A function that may be used to stop blocking
236 *
237 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.block
238 */
239 block(blocker: Blocker): () => void;
240}
241
242/**
243 * A Navigator is a "location changer"; it's how you get to different locations.
244 *
245 * Every history instance conforms to the Navigator interface, but the
246 * distinction is useful primarily when it comes to the low-level <Router> API
247 * where both the location and a navigator must be provided separately in order
248 * to avoid "tearing" that may occur in a suspense-enabled app if the action
249 * and/or location were to be read directly from the history instance.
250 */
251declare type Navigator = Omit<History, "action" | "location" | "back" | "forward" | "listen" | "block">;
252interface RouterProps {
253 basename?: string;
254 children?: React.ReactNode;
255 location: Partial<Location$1> | string;
256 navigationType?: Action;
257 navigator: Navigator;
258 static?: boolean;
259}
260/**
261 * Provides location context for the rest of the app.
262 *
263 * Note: You usually won't render a <Router> directly. Instead, you'll render a
264 * router that is more specific to your environment such as a <BrowserRouter>
265 * in web browsers or a <StaticRouter> for server rendering.
266 *
267 * @see https://reactrouter.com/docs/en/v6/api#router
268 */
269declare function Router({ basename: basenameProp, children, location: locationProp, navigationType, navigator, static: staticProp }: RouterProps): React.ReactElement | null;
270interface NavigateOptions$1 {
271 replace?: boolean;
272 state?: any;
273}
274
275interface BrowserRouterProps {
276 basename?: string;
277 children?: React.ReactNode;
278 window?: Window;
279}
280/**
281 * A <Router> for use in web browsers. Provides the cleanest URLs.
282 */
283declare function BrowserRouter({ basename, children, window }: BrowserRouterProps): JSX.Element;
284
285interface Other extends StoryData {
286 path: string;
287 singleStory?: boolean;
288}
289type NavigateOptions = NavigateOptions$1 & {
290 plain?: boolean;
291};
292type NavigateFunction = (to: To | number, options?: NavigateOptions) => void;
293type RouterData = {
294 location: Partial<Location$1>;
295 navigate: NavigateFunction;
296} & Other;
297type RenderData = Pick<RouterData, 'location'> & Other;
298interface LinkProps {
299 to: string;
300 children: ReactNode;
301}
302
303interface MatchingData {
304 match: null | {
305 path: string;
306 };
307}
308interface LocationProps {
309 children: (renderData: RenderData) => any;
310}
311interface MatchPropsStartsWith {
312 path: string;
313 startsWith: boolean;
314 children: (matchingData: MatchingData) => ReactNode;
315}
316interface MatchPropsDefault {
317 path: RegExp;
318 startsWith: false;
319 children: (matchingData: MatchingData) => ReactNode;
320}
321interface RoutePropsStartsWith {
322 path: string;
323 startsWith?: boolean;
324 children: ReactNode;
325}
326interface RoutePropsDefault {
327 path: RegExp;
328 startsWith?: false;
329 children: ReactNode;
330}
331declare const useNavigate: () => (to: To | number, { plain, ...options }?: NavigateOptions) => void;
332/**
333 * A component that will navigate to a new location/path when clicked
334 */
335declare const Link: {
336 ({ to, children, ...rest }: LinkProps): React__default.JSX.Element;
337 displayName: string;
338};
339/**
340 * A render-prop component where children is called with a location
341 * and will be called whenever it changes when it changes
342 */
343declare const Location: {
344 ({ children }: LocationProps): React__default.JSX.Element;
345 displayName: string;
346};
347/**
348 * A render-prop component for rendering when a certain path is hit.
349 * It's immensely similar to `Location` but it receives an addition data property: `match`.
350 * match has a truthy value when the path is hit.
351 */
352declare function Match(props: MatchPropsStartsWith): ReactElement;
353declare function Match(props: MatchPropsDefault): ReactElement;
354declare namespace Match {
355 var displayName: string;
356}
357/**
358 * A component to conditionally render children based on matching a target path
359 */
360declare function Route(props: RoutePropsDefault): ReactElement;
361declare function Route(props: RoutePropsStartsWith): ReactElement;
362declare namespace Route {
363 var displayName: string;
364}
365
366declare const LocationProvider: typeof BrowserRouter;
367declare const BaseLocationProvider: typeof Router;
368
369export { BaseLocationProvider, Link, LinkProps, Location, LocationProvider, Match, NavigateFunction, NavigateOptions, Other, RenderData, Route, RouterData, StoryData, useNavigate };