@tanstack/react-router 1.88.0 → 1.89.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-router",
3
- "version": "1.88.0",
3
+ "version": "1.89.0",
4
4
  "description": "Modern and scalable routing for React applications",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -15,6 +15,7 @@ import type {
15
15
  export interface CommitLocationOptions {
16
16
  replace?: boolean
17
17
  resetScroll?: boolean
18
+ hashScrollIntoView?: boolean | ScrollIntoViewOptions
18
19
  viewTransition?: boolean | ViewTransitionOptions
19
20
  /**
20
21
  * @deprecated All navigations use React transitions under the hood now
@@ -143,10 +143,13 @@ export function Transitioner() {
143
143
  }))
144
144
 
145
145
  if (typeof document !== 'undefined' && (document as any).querySelector) {
146
- if (router.state.location.hash !== '') {
146
+ const hashScrollIntoViewOptions =
147
+ router.state.location.state.__hashScrollIntoViewOptions ?? true
148
+
149
+ if (hashScrollIntoViewOptions && router.state.location.hash !== '') {
147
150
  const el = document.getElementById(router.state.location.hash)
148
151
  if (el) {
149
- el.scrollIntoView()
152
+ el.scrollIntoView(hashScrollIntoViewOptions)
150
153
  }
151
154
  }
152
155
  }
package/src/history.ts CHANGED
@@ -4,5 +4,6 @@ declare module '@tanstack/history' {
4
4
  interface HistoryState {
5
5
  __tempLocation?: HistoryLocation
6
6
  __tempKey?: string
7
+ __hashScrollIntoViewOptions?: boolean | ScrollIntoViewOptions
7
8
  }
8
9
  }
package/src/link.tsx CHANGED
@@ -210,6 +210,10 @@ export type NavigateOptions<
210
210
  > = ToOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & NavigateOptionProps
211
211
 
212
212
  export interface NavigateOptionProps {
213
+ // if set to `true`, the router will scroll the element with an id matching the hash into view with default ScrollIntoViewOptions.
214
+ // if set to `false`, the router will not scroll the element with an id matching the hash into view.
215
+ // if set to `ScrollIntoViewOptions`, the router will scroll the element with an id matching the hash into view with the provided options.
216
+ hashScrollIntoView?: boolean | ScrollIntoViewOptions
213
217
  // `replace` is a boolean that determines whether the navigation should replace the current history entry or push a new one.
214
218
  replace?: boolean
215
219
  resetScroll?: boolean
@@ -609,6 +613,7 @@ export function useLinkProps<
609
613
  to,
610
614
  preload: userPreload,
611
615
  preloadDelay: userPreloadDelay,
616
+ hashScrollIntoView,
612
617
  replace,
613
618
  startTransition,
614
619
  resetScroll,
@@ -806,6 +811,7 @@ export function useLinkProps<
806
811
  ...options,
807
812
  replace,
808
813
  resetScroll,
814
+ hashScrollIntoView,
809
815
  startTransition,
810
816
  viewTransition,
811
817
  ignoreBlocker,
package/src/router.ts CHANGED
@@ -313,6 +313,14 @@ export interface RouterOptions<
313
313
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultviewtransition-property)
314
314
  */
315
315
  defaultViewTransition?: boolean | ViewTransitionOptions
316
+ /**
317
+ * The default `hashScrollIntoView` a route should use if no hashScrollIntoView is provided while navigating
318
+ *
319
+ * See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView) for more information on `ScrollIntoViewOptions`.
320
+ *
321
+ * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaulthashscrollintoview-property)
322
+ */
323
+ defaultHashScrollIntoView?: boolean | ScrollIntoViewOptions
316
324
  /**
317
325
  * @default 'fuzzy'
318
326
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#notfoundmode-property)
@@ -1809,7 +1817,7 @@ export class Router<
1809
1817
  this.load()
1810
1818
  } else {
1811
1819
  // eslint-disable-next-line prefer-const
1812
- let { maskedLocation, ...nextHistory } = next
1820
+ let { maskedLocation, hashScrollIntoView, ...nextHistory } = next
1813
1821
 
1814
1822
  if (maskedLocation) {
1815
1823
  nextHistory = {
@@ -1839,6 +1847,9 @@ export class Router<
1839
1847
  }
1840
1848
  }
1841
1849
 
1850
+ nextHistory.state.__hashScrollIntoViewOptions =
1851
+ hashScrollIntoView ?? this.options.defaultHashScrollIntoView ?? true
1852
+
1842
1853
  this.shouldViewTransition = viewTransition
1843
1854
 
1844
1855
  this.history[next.replace ? 'replace' : 'push'](
@@ -1860,6 +1871,7 @@ export class Router<
1860
1871
  buildAndCommitLocation = ({
1861
1872
  replace,
1862
1873
  resetScroll,
1874
+ hashScrollIntoView,
1863
1875
  viewTransition,
1864
1876
  ignoreBlocker,
1865
1877
  href,
@@ -1882,6 +1894,7 @@ export class Router<
1882
1894
  viewTransition,
1883
1895
  replace,
1884
1896
  resetScroll,
1897
+ hashScrollIntoView,
1885
1898
  ignoreBlocker,
1886
1899
  })
1887
1900
  }