UNPKG

12.3 kBTypeScriptView Raw
1/**
2 * Actions represent the type of change to a location value.
3 *
4 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#action
5 */
6export declare enum Action {
7 /**
8 * A POP indicates a change to an arbitrary index in the history stack, such
9 * as a back or forward navigation. It does not describe the direction of the
10 * navigation, only that the current index changed.
11 *
12 * Note: This is the default action for newly created history objects.
13 */
14 Pop = "POP",
15 /**
16 * A PUSH indicates a new entry being added to the history stack, such as when
17 * a link is clicked and a new page loads. When this happens, all subsequent
18 * entries in the stack are lost.
19 */
20 Push = "PUSH",
21 /**
22 * A REPLACE indicates the entry at the current index in the history stack
23 * being replaced by a new one.
24 */
25 Replace = "REPLACE"
26}
27/**
28 * A URL pathname, beginning with a /.
29 *
30 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.pathname
31 */
32export declare type Pathname = string;
33/**
34 * A URL search string, beginning with a ?.
35 *
36 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.search
37 */
38export declare type Search = string;
39/**
40 * A URL fragment identifier, beginning with a #.
41 *
42 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.hash
43 */
44export declare type Hash = string;
45/**
46 * An object that is used to associate some arbitrary data with a location, but
47 * that does not appear in the URL path.
48 *
49 * @deprecated
50 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.state
51 */
52export declare type State = unknown;
53/**
54 * A unique string associated with a location. May be used to safely store
55 * and retrieve data in some other storage API, like `localStorage`.
56 *
57 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.key
58 */
59export declare type Key = string;
60/**
61 * The pathname, search, and hash values of a URL.
62 */
63export interface Path {
64 /**
65 * A URL pathname, beginning with a /.
66 *
67 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.pathname
68 */
69 pathname: Pathname;
70 /**
71 * A URL search string, beginning with a ?.
72 *
73 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.search
74 */
75 search: Search;
76 /**
77 * A URL fragment identifier, beginning with a #.
78 *
79 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.hash
80 */
81 hash: Hash;
82}
83/**
84 * An entry in a history stack. A location contains information about the
85 * URL path, as well as possibly some arbitrary state and a key.
86 *
87 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location
88 */
89export interface Location extends Path {
90 /**
91 * A value of arbitrary data associated with this location.
92 *
93 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.state
94 */
95 state: unknown;
96 /**
97 * A unique string associated with this location. May be used to safely store
98 * and retrieve data in some other storage API, like `localStorage`.
99 *
100 * Note: This value is always "default" on the initial location.
101 *
102 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.key
103 */
104 key: Key;
105}
106/**
107 * A partial Path object that may be missing some properties.
108 *
109 * @deprecated
110 */
111export declare type PartialPath = Partial<Path>;
112/**
113 * A partial Location object that may be missing some properties.
114 *
115 * @deprecated
116 */
117export declare type PartialLocation = Partial<Location>;
118/**
119 * A change to the current location.
120 */
121export interface Update {
122 /**
123 * The action that triggered the change.
124 */
125 action: Action;
126 /**
127 * The new location.
128 */
129 location: Location;
130}
131/**
132 * A function that receives notifications about location changes.
133 */
134export interface Listener {
135 (update: Update): void;
136}
137/**
138 * A change to the current location that was blocked. May be retried
139 * after obtaining user confirmation.
140 */
141export interface Transition extends Update {
142 /**
143 * Retries the update to the current location.
144 */
145 retry(): void;
146}
147/**
148 * A function that receives transitions when navigation is blocked.
149 */
150export interface Blocker {
151 (tx: Transition): void;
152}
153/**
154 * Describes a location that is the destination of some navigation, either via
155 * `history.push` or `history.replace`. May be either a URL or the pieces of a
156 * URL path.
157 */
158export declare type To = string | Partial<Path>;
159/**
160 * A history is an interface to the navigation stack. The history serves as the
161 * source of truth for the current location, as well as provides a set of
162 * methods that may be used to change it.
163 *
164 * It is similar to the DOM's `window.history` object, but with a smaller, more
165 * focused API.
166 */
167export interface History {
168 /**
169 * The last action that modified the current location. This will always be
170 * Action.Pop when a history instance is first created. This value is mutable.
171 *
172 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.action
173 */
174 readonly action: Action;
175 /**
176 * The current location. This value is mutable.
177 *
178 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.location
179 */
180 readonly location: Location;
181 /**
182 * Returns a valid href for the given `to` value that may be used as
183 * the value of an <a href> attribute.
184 *
185 * @param to - The destination URL
186 *
187 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.createHref
188 */
189 createHref(to: To): string;
190 /**
191 * Pushes a new location onto the history stack, increasing its length by one.
192 * If there were any entries in the stack after the current one, they are
193 * lost.
194 *
195 * @param to - The new URL
196 * @param state - Data to associate with the new location
197 *
198 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.push
199 */
200 push(to: To, state?: any): void;
201 /**
202 * Replaces the current location in the history stack with a new one. The
203 * location that was replaced will no longer be available.
204 *
205 * @param to - The new URL
206 * @param state - Data to associate with the new location
207 *
208 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.replace
209 */
210 replace(to: To, state?: any): void;
211 /**
212 * Navigates `n` entries backward/forward in the history stack relative to the
213 * current index. For example, a "back" navigation would use go(-1).
214 *
215 * @param delta - The delta in the stack index
216 *
217 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.go
218 */
219 go(delta: number): void;
220 /**
221 * Navigates to the previous entry in the stack. Identical to go(-1).
222 *
223 * Warning: if the current location is the first location in the stack, this
224 * will unload the current document.
225 *
226 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.back
227 */
228 back(): void;
229 /**
230 * Navigates to the next entry in the stack. Identical to go(1).
231 *
232 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.forward
233 */
234 forward(): void;
235 /**
236 * Sets up a listener that will be called whenever the current location
237 * changes.
238 *
239 * @param listener - A function that will be called when the location changes
240 * @returns unlisten - A function that may be used to stop listening
241 *
242 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.listen
243 */
244 listen(listener: Listener): () => void;
245 /**
246 * Prevents the current location from changing and sets up a listener that
247 * will be called instead.
248 *
249 * @param blocker - A function that will be called when a transition is blocked
250 * @returns unblock - A function that may be used to stop blocking
251 *
252 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.block
253 */
254 block(blocker: Blocker): () => void;
255}
256/**
257 * A browser history stores the current location in regular URLs in a web
258 * browser environment. This is the standard for most web apps and provides the
259 * cleanest URLs the browser's address bar.
260 *
261 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#browserhistory
262 */
263export interface BrowserHistory extends History {
264}
265/**
266 * A hash history stores the current location in the fragment identifier portion
267 * of the URL in a web browser environment.
268 *
269 * This is ideal for apps that do not control the server for some reason
270 * (because the fragment identifier is never sent to the server), including some
271 * shared hosting environments that do not provide fine-grained controls over
272 * which pages are served at which URLs.
273 *
274 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#hashhistory
275 */
276export interface HashHistory extends History {
277}
278/**
279 * A memory history stores locations in memory. This is useful in stateful
280 * environments where there is no web browser, such as node tests or React
281 * Native.
282 *
283 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#memoryhistory
284 */
285export interface MemoryHistory extends History {
286 readonly index: number;
287}
288export declare type BrowserHistoryOptions = {
289 window?: Window;
290};
291/**
292 * Browser history stores the location in regular URLs. This is the standard for
293 * most web apps, but it requires some configuration on the server to ensure you
294 * serve the same app at multiple URLs.
295 *
296 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createbrowserhistory
297 */
298export declare function createBrowserHistory(options?: BrowserHistoryOptions): BrowserHistory;
299export declare type HashHistoryOptions = {
300 window?: Window;
301};
302/**
303 * Hash history stores the location in window.location.hash. This makes it ideal
304 * for situations where you don't want to send the location to the server for
305 * some reason, either because you do cannot configure it or the URL space is
306 * reserved for something else.
307 *
308 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createhashhistory
309 */
310export declare function createHashHistory(options?: HashHistoryOptions): HashHistory;
311/**
312 * A user-supplied object that describes a location. Used when providing
313 * entries to `createMemoryHistory` via its `initialEntries` option.
314 */
315export declare type InitialEntry = string | Partial<Location>;
316export declare type MemoryHistoryOptions = {
317 initialEntries?: InitialEntry[];
318 initialIndex?: number;
319};
320/**
321 * Memory history stores the current location in memory. It is designed for use
322 * in stateful non-browser environments like tests and React Native.
323 *
324 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#creatememoryhistory
325 */
326export declare function createMemoryHistory(options?: MemoryHistoryOptions): MemoryHistory;
327/**
328 * Creates a string URL path from the given pathname, search, and hash components.
329 *
330 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createpath
331 */
332export declare function createPath({ pathname, search, hash }: Partial<Path>): string;
333/**
334 * Parses a string URL path into its separate pathname, search, and hash components.
335 *
336 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#parsepath
337 */
338export declare function parsePath(path: string): Partial<Path>;