history.d.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 extends Path {
  47. /**
  48. * A value of arbitrary data associated with this location.
  49. */
  50. state: any;
  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`. May be either a URL or the pieces of a
  85. * URL path.
  86. */
  87. export declare 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 declare type InitialEntry = string | Partial<Location>;
  165. export declare 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 declare 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 declare 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. /**
  233. * Creates a Location object with a unique key from the given Path
  234. */
  235. export declare function createLocation(current: string | Location, to: To, state?: any, key?: string): Readonly<Location>;
  236. /**
  237. * Creates a string URL path from the given pathname, search, and hash components.
  238. */
  239. export declare function createPath({ pathname, search, hash, }: Partial<Path>): string;
  240. /**
  241. * Parses a string URL path into its separate pathname, search, and hash components.
  242. */
  243. export declare function parsePath(path: string): Partial<Path>;
  244. export interface UrlHistory extends History {
  245. }
  246. export declare type UrlHistoryOptions = {
  247. window?: Window;
  248. v5Compat?: boolean;
  249. };