Skip to content
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

Experiment with cross-document view transitions in Twenty Fifteen theme #8026

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/wp-content/themes/twentyfifteen/css/view-transitions.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@view-transition {
navigation: auto;
}

::view-transition-group(*) {
animation-duration: 1s;
}

@keyframes slide-in-from-left {
from {
translate: -100vw 0;
}
}

@keyframes slide-in-from-right {
from {
translate: 100vw 0;
}
}

@keyframes slide-out-to-left {
to {
translate: -100vw 0;
}
}

@keyframes slide-out-to-right {
to {
translate: 100vw 0;
}
}

html:active-view-transition-type(forwards, backwards) {
:root {
view-transition-name: none;
}
main {
view-transition-name: main;
}
}

html:active-view-transition-type(forwards) {
&::view-transition-old(main) {
animation-name: slide-out-to-left;
}
&::view-transition-new(main) {
animation-name: slide-in-from-right;
}
}

html:active-view-transition-type(backwards) {
&::view-transition-old(main) {
animation-name: slide-out-to-right;
}
&::view-transition-new(main) {
animation-name: slide-in-from-left;
}
}
18 changes: 18 additions & 0 deletions src/wp-content/themes/twentyfifteen/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,24 @@ function twentyfifteen_scripts() {
'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
)
);

wp_enqueue_style(
'twentyfifteen-view-transitions',
get_template_directory_uri() . '/css/view-transitions.css',
array(),
'20241219'
);

wp_enqueue_script(
'twentyfifteen-view-transitions',
get_template_directory_uri() . '/js/view-transitions.js',
array(),
'20241219',
array(
'in_footer' => false,
'strategy' => 'defer',
)
);
}
add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );

Expand Down
61 changes: 61 additions & 0 deletions src/wp-content/themes/twentyfifteen/js/view-transitions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// TODO: Check for `!! window.navigation && 'CSSViewTransitionRule' in window`.

const determineTransitionType = ( oldNavigationEntry, newNavigationEntry ) => {
if ( ! oldNavigationEntry || ! newNavigationEntry ) {
return 'unknown';
}

const currentURL = new URL( oldNavigationEntry.url );
const destinationURL = new URL( newNavigationEntry.url );

const currentPathname = currentURL.pathname;
const destinationPathname = destinationURL.pathname;

if ( currentPathname !== destinationPathname ) {
// If post URLs start with a date, use that to determine "order".
const currentDateMatches = currentPathname.match( /^\/(\d{4})\/(\d{2})\/(\d{2})\// );
const destinationDateMatches = destinationPathname.match( /^\/(\d{4})\/(\d{2})\/(\d{2})\// );
console.log( currentPathname );
console.log( destinationPathname );
if ( currentDateMatches && destinationDateMatches ) {
const currentDate = new Date( parseInt( currentDateMatches[ 1 ] ), parseInt( currentDateMatches[ 2 ] ) - 1, parseInt( currentDateMatches[ 3 ] ) );
const destinationDate = new Date( parseInt( destinationDateMatches[ 1 ] ), parseInt( destinationDateMatches[ 2 ] ) - 1, parseInt( destinationDateMatches[ 3 ] ) );
if ( currentDate < destinationDate ) {
return 'forwards';
}
if ( currentDate > destinationDate ) {
return 'backwards';
}
return 'unknown';
}

// Otherwise, check URL "hierarchy".
if ( destinationPathname.startsWith( currentPathname ) ) {
return 'forwards';
}
if ( currentPathname.startsWith( destinationPathname ) ) {
return 'backwards';
}
}

return 'unknown';
};

window.addEventListener( 'pageswap', async ( e ) => {
if ( e.viewTransition ) {
const transitionType = determineTransitionType( e.activation.from, e.activation.entry );

console.log( `pageSwap: ${ transitionType }` );
e.viewTransition.types.add( transitionType );
}
} );

window.addEventListener( 'pagereveal', async ( e ) => {
console.log( 'pageRevealEvent', e );
Comment on lines +53 to +54
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, the pagereveal event is not fired in most of my page loads. I "randomly" see it fired, but rarely - no idea where that comes from.

I added the pageRevealEvent console log to ensure it has nothing to do with the e.viewTransition check below, so it's indeed the event itself that is not fired.

🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only see it fired when using the back/forward buttons in the browser.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is that the scripts that adds the event listener is called too late. It has to be added before the first rendering opportunity.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also seeing that MDN doesn't talk much about this, mind filing an issue?
pagereveal event listeners need to be added in either:

  1. A classic parser-blocking script at the head (not a module, not async, not defer)
  2. An async script in the head with blocking=render, either module or classic.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @bramus, perhaps the cross-document view-transition official chromium docs should mention this if they don't already.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the ping @noamr. It’s not mentioned in our docs either (but all demos do use it that way). I remember being bitten by this myself, so would indeed be good to have some extra notices around this.

if ( e.viewTransition ) {
const transitionType = determineTransitionType( navigation.activation.from, navigation.activation.entry );

console.log( `pageReveal: ${ transitionType }` );
e.viewTransition.types.add( transitionType );
}
} );
Loading