[d565449] | 1 | /**
|
---|
| 2 | * Actions represent the type of change to a location value.
|
---|
| 3 | */
|
---|
| 4 | export declare enum Action {
|
---|
| 5 | /**
|
---|
| 6 | * A POP indicates a change to an arbitrary index in the history stack, such
|
---|
| 7 | * as a back or forward navigation. It does not describe the direction of the
|
---|
| 8 | * navigation, only that the current index changed.
|
---|
| 9 | *
|
---|
| 10 | * Note: This is the default action for newly created history objects.
|
---|
| 11 | */
|
---|
| 12 | Pop = "POP",
|
---|
| 13 | /**
|
---|
| 14 | * A PUSH indicates a new entry being added to the history stack, such as when
|
---|
| 15 | * a link is clicked and a new page loads. When this happens, all subsequent
|
---|
| 16 | * entries in the stack are lost.
|
---|
| 17 | */
|
---|
| 18 | Push = "PUSH",
|
---|
| 19 | /**
|
---|
| 20 | * A REPLACE indicates the entry at the current index in the history stack
|
---|
| 21 | * being replaced by a new one.
|
---|
| 22 | */
|
---|
| 23 | Replace = "REPLACE"
|
---|
| 24 | }
|
---|
| 25 | /**
|
---|
| 26 | * The pathname, search, and hash values of a URL.
|
---|
| 27 | */
|
---|
| 28 | export interface Path {
|
---|
| 29 | /**
|
---|
| 30 | * A URL pathname, beginning with a /.
|
---|
| 31 | */
|
---|
| 32 | pathname: string;
|
---|
| 33 | /**
|
---|
| 34 | * A URL search string, beginning with a ?.
|
---|
| 35 | */
|
---|
| 36 | search: string;
|
---|
| 37 | /**
|
---|
| 38 | * A URL fragment identifier, beginning with a #.
|
---|
| 39 | */
|
---|
| 40 | hash: string;
|
---|
| 41 | }
|
---|
| 42 | /**
|
---|
| 43 | * An entry in a history stack. A location contains information about the
|
---|
| 44 | * URL path, as well as possibly some arbitrary state and a key.
|
---|
| 45 | */
|
---|
| 46 | export interface Location<State = any> extends Path {
|
---|
| 47 | /**
|
---|
| 48 | * A value of arbitrary data associated with this location.
|
---|
| 49 | */
|
---|
| 50 | state: State;
|
---|
| 51 | /**
|
---|
| 52 | * A unique string associated with this location. May be used to safely store
|
---|
| 53 | * and retrieve data in some other storage API, like `localStorage`.
|
---|
| 54 | *
|
---|
| 55 | * Note: This value is always "default" on the initial location.
|
---|
| 56 | */
|
---|
| 57 | key: string;
|
---|
| 58 | }
|
---|
| 59 | /**
|
---|
| 60 | * A change to the current location.
|
---|
| 61 | */
|
---|
| 62 | export interface Update {
|
---|
| 63 | /**
|
---|
| 64 | * The action that triggered the change.
|
---|
| 65 | */
|
---|
| 66 | action: Action;
|
---|
| 67 | /**
|
---|
| 68 | * The new location.
|
---|
| 69 | */
|
---|
| 70 | location: Location;
|
---|
| 71 | /**
|
---|
| 72 | * The delta between this location and the former location in the history stack
|
---|
| 73 | */
|
---|
| 74 | delta: number | null;
|
---|
| 75 | }
|
---|
| 76 | /**
|
---|
| 77 | * A function that receives notifications about location changes.
|
---|
| 78 | */
|
---|
| 79 | export interface Listener {
|
---|
| 80 | (update: Update): void;
|
---|
| 81 | }
|
---|
| 82 | /**
|
---|
| 83 | * Describes a location that is the destination of some navigation, either via
|
---|
| 84 | * `history.push` or `history.replace`. This may be either a URL or the pieces
|
---|
| 85 | * of a URL path.
|
---|
| 86 | */
|
---|
| 87 | export type To = string | Partial<Path>;
|
---|
| 88 | /**
|
---|
| 89 | * A history is an interface to the navigation stack. The history serves as the
|
---|
| 90 | * source of truth for the current location, as well as provides a set of
|
---|
| 91 | * methods that may be used to change it.
|
---|
| 92 | *
|
---|
| 93 | * It is similar to the DOM's `window.history` object, but with a smaller, more
|
---|
| 94 | * focused API.
|
---|
| 95 | */
|
---|
| 96 | export interface History {
|
---|
| 97 | /**
|
---|
| 98 | * The last action that modified the current location. This will always be
|
---|
| 99 | * Action.Pop when a history instance is first created. This value is mutable.
|
---|
| 100 | */
|
---|
| 101 | readonly action: Action;
|
---|
| 102 | /**
|
---|
| 103 | * The current location. This value is mutable.
|
---|
| 104 | */
|
---|
| 105 | readonly location: Location;
|
---|
| 106 | /**
|
---|
| 107 | * Returns a valid href for the given `to` value that may be used as
|
---|
| 108 | * the value of an <a href> attribute.
|
---|
| 109 | *
|
---|
| 110 | * @param to - The destination URL
|
---|
| 111 | */
|
---|
| 112 | createHref(to: To): string;
|
---|
| 113 | /**
|
---|
| 114 | * Returns a URL for the given `to` value
|
---|
| 115 | *
|
---|
| 116 | * @param to - The destination URL
|
---|
| 117 | */
|
---|
| 118 | createURL(to: To): URL;
|
---|
| 119 | /**
|
---|
| 120 | * Encode a location the same way window.history would do (no-op for memory
|
---|
| 121 | * history) so we ensure our PUSH/REPLACE navigations for data routers
|
---|
| 122 | * behave the same as POP
|
---|
| 123 | *
|
---|
| 124 | * @param to Unencoded path
|
---|
| 125 | */
|
---|
| 126 | encodeLocation(to: To): Path;
|
---|
| 127 | /**
|
---|
| 128 | * Pushes a new location onto the history stack, increasing its length by one.
|
---|
| 129 | * If there were any entries in the stack after the current one, they are
|
---|
| 130 | * lost.
|
---|
| 131 | *
|
---|
| 132 | * @param to - The new URL
|
---|
| 133 | * @param state - Data to associate with the new location
|
---|
| 134 | */
|
---|
| 135 | push(to: To, state?: any): void;
|
---|
| 136 | /**
|
---|
| 137 | * Replaces the current location in the history stack with a new one. The
|
---|
| 138 | * location that was replaced will no longer be available.
|
---|
| 139 | *
|
---|
| 140 | * @param to - The new URL
|
---|
| 141 | * @param state - Data to associate with the new location
|
---|
| 142 | */
|
---|
| 143 | replace(to: To, state?: any): void;
|
---|
| 144 | /**
|
---|
| 145 | * Navigates `n` entries backward/forward in the history stack relative to the
|
---|
| 146 | * current index. For example, a "back" navigation would use go(-1).
|
---|
| 147 | *
|
---|
| 148 | * @param delta - The delta in the stack index
|
---|
| 149 | */
|
---|
| 150 | go(delta: number): void;
|
---|
| 151 | /**
|
---|
| 152 | * Sets up a listener that will be called whenever the current location
|
---|
| 153 | * changes.
|
---|
| 154 | *
|
---|
| 155 | * @param listener - A function that will be called when the location changes
|
---|
| 156 | * @returns unlisten - A function that may be used to stop listening
|
---|
| 157 | */
|
---|
| 158 | listen(listener: Listener): () => void;
|
---|
| 159 | }
|
---|
| 160 | /**
|
---|
| 161 | * A user-supplied object that describes a location. Used when providing
|
---|
| 162 | * entries to `createMemoryHistory` via its `initialEntries` option.
|
---|
| 163 | */
|
---|
| 164 | export type InitialEntry = string | Partial<Location>;
|
---|
| 165 | export type MemoryHistoryOptions = {
|
---|
| 166 | initialEntries?: InitialEntry[];
|
---|
| 167 | initialIndex?: number;
|
---|
| 168 | v5Compat?: boolean;
|
---|
| 169 | };
|
---|
| 170 | /**
|
---|
| 171 | * A memory history stores locations in memory. This is useful in stateful
|
---|
| 172 | * environments where there is no web browser, such as node tests or React
|
---|
| 173 | * Native.
|
---|
| 174 | */
|
---|
| 175 | export interface MemoryHistory extends History {
|
---|
| 176 | /**
|
---|
| 177 | * The current index in the history stack.
|
---|
| 178 | */
|
---|
| 179 | readonly index: number;
|
---|
| 180 | }
|
---|
| 181 | /**
|
---|
| 182 | * Memory history stores the current location in memory. It is designed for use
|
---|
| 183 | * in stateful non-browser environments like tests and React Native.
|
---|
| 184 | */
|
---|
| 185 | export declare function createMemoryHistory(options?: MemoryHistoryOptions): MemoryHistory;
|
---|
| 186 | /**
|
---|
| 187 | * A browser history stores the current location in regular URLs in a web
|
---|
| 188 | * browser environment. This is the standard for most web apps and provides the
|
---|
| 189 | * cleanest URLs the browser's address bar.
|
---|
| 190 | *
|
---|
| 191 | * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#browserhistory
|
---|
| 192 | */
|
---|
| 193 | export interface BrowserHistory extends UrlHistory {
|
---|
| 194 | }
|
---|
| 195 | export type BrowserHistoryOptions = UrlHistoryOptions;
|
---|
| 196 | /**
|
---|
| 197 | * Browser history stores the location in regular URLs. This is the standard for
|
---|
| 198 | * most web apps, but it requires some configuration on the server to ensure you
|
---|
| 199 | * serve the same app at multiple URLs.
|
---|
| 200 | *
|
---|
| 201 | * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createbrowserhistory
|
---|
| 202 | */
|
---|
| 203 | export declare function createBrowserHistory(options?: BrowserHistoryOptions): BrowserHistory;
|
---|
| 204 | /**
|
---|
| 205 | * A hash history stores the current location in the fragment identifier portion
|
---|
| 206 | * of the URL in a web browser environment.
|
---|
| 207 | *
|
---|
| 208 | * This is ideal for apps that do not control the server for some reason
|
---|
| 209 | * (because the fragment identifier is never sent to the server), including some
|
---|
| 210 | * shared hosting environments that do not provide fine-grained controls over
|
---|
| 211 | * which pages are served at which URLs.
|
---|
| 212 | *
|
---|
| 213 | * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#hashhistory
|
---|
| 214 | */
|
---|
| 215 | export interface HashHistory extends UrlHistory {
|
---|
| 216 | }
|
---|
| 217 | export type HashHistoryOptions = UrlHistoryOptions;
|
---|
| 218 | /**
|
---|
| 219 | * Hash history stores the location in window.location.hash. This makes it ideal
|
---|
| 220 | * for situations where you don't want to send the location to the server for
|
---|
| 221 | * some reason, either because you do cannot configure it or the URL space is
|
---|
| 222 | * reserved for something else.
|
---|
| 223 | *
|
---|
| 224 | * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createhashhistory
|
---|
| 225 | */
|
---|
| 226 | export declare function createHashHistory(options?: HashHistoryOptions): HashHistory;
|
---|
| 227 | /**
|
---|
| 228 | * @private
|
---|
| 229 | */
|
---|
| 230 | export declare function invariant(value: boolean, message?: string): asserts value;
|
---|
| 231 | export declare function invariant<T>(value: T | null | undefined, message?: string): asserts value is T;
|
---|
| 232 | export declare function warning(cond: any, message: string): void;
|
---|
| 233 | /**
|
---|
| 234 | * Creates a Location object with a unique key from the given Path
|
---|
| 235 | */
|
---|
| 236 | export declare function createLocation(current: string | Location, to: To, state?: any, key?: string): Readonly<Location>;
|
---|
| 237 | /**
|
---|
| 238 | * Creates a string URL path from the given pathname, search, and hash components.
|
---|
| 239 | */
|
---|
| 240 | export declare function createPath({ pathname, search, hash, }: Partial<Path>): string;
|
---|
| 241 | /**
|
---|
| 242 | * Parses a string URL path into its separate pathname, search, and hash components.
|
---|
| 243 | */
|
---|
| 244 | export declare function parsePath(path: string): Partial<Path>;
|
---|
| 245 | export interface UrlHistory extends History {
|
---|
| 246 | }
|
---|
| 247 | export type UrlHistoryOptions = {
|
---|
| 248 | window?: Window;
|
---|
| 249 | v5Compat?: boolean;
|
---|
| 250 | };
|
---|