File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ ' preact-iso ' : patch
3+ ---
4+
5+ Scroll cross-route URL fragments into view after the destination route commits.
Original file line number Diff line number Diff line change @@ -23,6 +23,22 @@ function isInScope(href) {
2323 ) ;
2424}
2525
26+ /** @param {string } url */
27+ function scrollToUrl ( url ) {
28+ const hash = new URL ( url , location . origin ) . hash ;
29+ if ( hash ) {
30+ try {
31+ const target = document . getElementById ( decodeURIComponent ( hash . slice ( 1 ) ) ) ;
32+ if ( target ) {
33+ target . scrollIntoView ( ) ;
34+ return ;
35+ }
36+ } catch { }
37+ }
38+
39+ scrollTo ( 0 , 0 ) ;
40+ }
41+
2642/**
2743 * @param {string } state
2844 * @param {MouseEvent | PopStateEvent | { url: string, replace?: boolean } } action
@@ -274,7 +290,7 @@ export function Router(props) {
274290
275291 // The route is loaded and rendered.
276292 if ( prevRoute . current !== path ) {
277- if ( wasPush ) scrollTo ( 0 , 0 ) ;
293+ if ( wasPush ) scrollToUrl ( url ) ;
278294 if ( props . onRouteChange ) props . onRouteChange ( url ) ;
279295
280296 prevRoute . current = path ;
You can’t perform that action at this time.
0 commit comments