-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explore view transitions in Twenty Fifteen for post title and featured image specifically #8131
base: trunk
Are you sure you want to change the base?
Changes from 1 commit
670215c
761369c
868f960
30b15f8
22eeb83
a2520a4
ab9b25b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@view-transition { | ||
navigation: auto; | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,91 @@ | ||||||||
if ( !! window.navigation && 'CSSViewTransitionRule' in window ) { | ||||||||
const getParentElement = ( element, selector ) => { | ||||||||
element = element.parentElement; | ||||||||
while ( element && element !== document ) { | ||||||||
if ( ! selector || element.matches( selector ) ) { | ||||||||
return element; | ||||||||
} | ||||||||
} | ||||||||
return null; | ||||||||
}; | ||||||||
|
||||||||
const setTemporaryViewTransitionNames = async ( entries, vtPromise ) => { | ||||||||
for ( const [ element, name ] of entries ) { | ||||||||
if ( ! element ) { | ||||||||
continue; | ||||||||
} | ||||||||
element.style.viewTransitionName = name; | ||||||||
} | ||||||||
|
||||||||
await vtPromise; | ||||||||
|
||||||||
for ( const [ element, name ] of entries ) { | ||||||||
if ( ! element ) { | ||||||||
continue; | ||||||||
} | ||||||||
element.style.viewTransitionName = ''; | ||||||||
} | ||||||||
}; | ||||||||
|
||||||||
window.addEventListener( 'pageswap', ( e ) => { | ||||||||
if ( e.viewTransition ) { | ||||||||
if ( document.body.classList.contains( 'single' ) ) { | ||||||||
const article = document.querySelectorAll( 'article.post' ); | ||||||||
if ( article.length !== 1 ) { | ||||||||
return; | ||||||||
} | ||||||||
|
||||||||
setTemporaryViewTransitionNames( [ | ||||||||
[ article[ 0 ].querySelector( '.entry-title' ), 'post-title' ], | ||||||||
[ article[ 0 ].querySelector( '.post-thumbnail' ), 'post-thumbnail' ], | ||||||||
], e.viewTransition.finished ); | ||||||||
} else if ( document.body.classList.contains( 'home' ) || document.body.classList.contains( 'archive' ) ) { | ||||||||
const articleLink = document.querySelector( 'article.post a[href="' + e.activation.entry.url + '"]' ); | ||||||||
if ( ! articleLink ) { | ||||||||
return; | ||||||||
} | ||||||||
Comment on lines
+33
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bramus I debugged the issue further and got as far as figuring out that it, for some reason, comes down to this:
I still don't understand why that's happening, for two reasons:
|
||||||||
|
||||||||
const article = getParentElement( articleLink, 'article.post' ); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this code throws when there is no
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch! That said, this is not what caused the problem, it wasn't actually happening. Still a good safeguard to have in any case. |
||||||||
|
||||||||
setTemporaryViewTransitionNames( [ | ||||||||
[ article.querySelector( '.entry-title' ), 'post-title' ], | ||||||||
[ article.querySelector( '.post-thumbnail' ), 'post-thumbnail' ], | ||||||||
], e.viewTransition.finished ); | ||||||||
} | ||||||||
} | ||||||||
} ); | ||||||||
|
||||||||
window.addEventListener( 'pagereveal', ( e ) => { | ||||||||
if ( ! window.navigation.activation.from ) { | ||||||||
return; | ||||||||
} | ||||||||
|
||||||||
if ( e.viewTransition ) { | ||||||||
if ( document.body.classList.contains( 'single' ) ) { | ||||||||
const article = document.querySelectorAll( 'article.post' ); | ||||||||
if ( article.length !== 1 ) { | ||||||||
return; | ||||||||
} | ||||||||
|
||||||||
setTemporaryViewTransitionNames( [ | ||||||||
[ article[ 0 ].querySelector( '.entry-title' ), 'post-title' ], | ||||||||
[ article[ 0 ].querySelector( '.post-thumbnail' ), 'post-thumbnail' ], | ||||||||
], e.viewTransition.ready ); | ||||||||
} else if ( document.body.classList.contains( 'home' ) || document.body.classList.contains( 'archive' ) ) { | ||||||||
const articleLink = document.querySelector( 'article.post a[href="' + window.navigation.activation.from.url + '"]' ); | ||||||||
if ( ! articleLink ) { | ||||||||
return; | ||||||||
} | ||||||||
|
||||||||
const article = getParentElement( articleLink, 'article.post' ); | ||||||||
|
||||||||
setTemporaryViewTransitionNames( [ | ||||||||
[ article.querySelector( '.entry-title' ), 'post-title' ], | ||||||||
[ article.querySelector( '.post-thumbnail' ), 'post-thumbnail' ], | ||||||||
], e.viewTransition.ready ); | ||||||||
} | ||||||||
} | ||||||||
} ); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @noamr @bramus Maybe you can help me out here: With this current logic, whenever I click on a link to open a WordPress post that does not have a featured image ( Maybe it's just a simple logic flaw, but I'm not sure what's missing. My |
||||||||
} else { | ||||||||
window.console.warn( 'View transitions not loaded as the browser is lacking support.' ); | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@felixarntz: I’ve narrowed the problem you are having down to this
getClosestParent
function: it gets stuck in a loop becauseelement
remains the same while inside loop.The fix:
Furthermore I think you could replace the functionality entirely with
Element.closest()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yikes - of course it had to be an oversight like this 🤦
Thanks! Great point on using
closest
, I've updated this in ab9b25b - works like a charm now!