Skip to content

Commit 206551a

Browse files
author
Felix Arntz
committed
Vertically align scroll position for horizontal slide view transitions.
1 parent 55c543d commit 206551a

1 file changed

Lines changed: 14 additions & 33 deletions

File tree

src/js/_enqueues/wp/view-transitions.js

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,6 @@ window.wp.viewTransitions.init = ( config ) => {
7878
}
7979
};
8080

81-
/**
82-
* Looks up an element from the list of view transition entries by its view transition name.
83-
*
84-
* @param {Array[]} entries View transition entries as received from `getViewTransitionEntries()`.
85-
* @param {string} vtName View transition name to look up element.
86-
* @return {Element|null} Element found, or null if none is found.
87-
*/
88-
const findElementByViewTransitionName = ( entries, vtName ) => {
89-
for ( const [ element, name ] of entries ) {
90-
if ( ! element ) {
91-
continue;
92-
}
93-
if ( name === vtName ) {
94-
return element;
95-
}
96-
}
97-
return null;
98-
};
99-
10081
/**
10182
* Appends a selector to another selector.
10283
*
@@ -260,15 +241,10 @@ window.wp.viewTransitions.init = ( config ) => {
260241
setTemporaryViewTransitionNames( viewTransitionEntries, event.viewTransition.finished );
261242
const slideViewTransitionName = getViewTransitionNameForSlideAnimation( transitionType );
262243
if ( slideViewTransitionName ) {
263-
const slideElement = findElementByViewTransitionName( viewTransitionEntries, slideViewTransitionName );
264-
if ( slideElement ) {
265-
/*const vPosBefore = slideElement.getBoundingClientRect().top;
266-
const vPosAfter = 373.578125; // Hard-coded based on position in Twenty Twenty-Five.
267-
const correction = vPosAfter - vPosBefore;
268-
slideElement.style.transform = `translateY(${ correction }px)`;*/
269-
const vScrollBefore = window.scrollY;
270-
console.log( 'swap', vScrollBefore );
271-
}
244+
// Consider a scroll offset if defined (e.g. due to fixed navigation bars being in the way).
245+
const scrollYOffset = document.documentElement.style.getPropertyValue( '--wp-scroll-y-offset' );
246+
const currentScrollY = window.scrollY - ( scrollYOffset ? parseInt( scrollYOffset, 10 ) : 0 );
247+
sessionStorage.setItem( 'wpViewTransitionsOldScrollY', currentScrollY );
272248
}
273249
}
274250
}
@@ -302,11 +278,16 @@ window.wp.viewTransitions.init = ( config ) => {
302278
setTemporaryViewTransitionNames( viewTransitionEntries, event.viewTransition.ready );
303279
const slideViewTransitionName = getViewTransitionNameForSlideAnimation( transitionType );
304280
if ( slideViewTransitionName ) {
305-
const slideElement = findElementByViewTransitionName( viewTransitionEntries, slideViewTransitionName );
306-
if ( slideElement ) {
307-
const vScrollAfter = window.scrollY;
308-
console.log( 'reveal', vScrollAfter );
309-
window.scrollTo( 0, 221 ); // Hard-coded as example.
281+
const oldScrollY = sessionStorage.getItem( 'wpViewTransitionsOldScrollY' );
282+
if ( oldScrollY !== null ) {
283+
// Align vertical scroll position.
284+
if ( oldScrollY ) {
285+
window.scrollTo( 0, parseInt( oldScrollY, 10 ) );
286+
}
287+
sessionStorage.removeItem( 'wpViewTransitionsOldScrollY' );
288+
} else {
289+
// Skip view transition to avoid an odd diagonal slide.
290+
event.viewTransition.skipTransition();
310291
}
311292
}
312293
}

0 commit comments

Comments
 (0)