Skip to content

Commit 670215c

Browse files
author
Felix Arntz
committed
Explore view transitions in Twenty Fifteen for post title and featured image specifically.
1 parent d2630e0 commit 670215c

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@view-transition {
2+
navigation: auto;
3+
}

src/wp-content/themes/twentyfifteen/functions.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,21 @@ function twentyfifteen_scripts() {
474474
'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
475475
)
476476
);
477+
478+
wp_enqueue_style(
479+
'twentyfifteen-view-transitions',
480+
get_template_directory_uri() . '/css/view-transitions.css',
481+
array(),
482+
'20250115'
483+
);
484+
485+
// This script must be loaded prior to rendering, i.e. not in the footer and not deferred or async.
486+
wp_enqueue_script(
487+
'twentyfifteen-view-transitions',
488+
get_template_directory_uri() . '/js/view-transitions.js',
489+
array(),
490+
'20250115'
491+
);
477492
}
478493
add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );
479494

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
if ( !! window.navigation && 'CSSViewTransitionRule' in window ) {
2+
const getParentElement = ( element, selector ) => {
3+
element = element.parentElement;
4+
while ( element && element !== document ) {
5+
if ( ! selector || element.matches( selector ) ) {
6+
return element;
7+
}
8+
}
9+
return null;
10+
};
11+
12+
const setTemporaryViewTransitionNames = async ( entries, vtPromise ) => {
13+
for ( const [ element, name ] of entries ) {
14+
if ( ! element ) {
15+
continue;
16+
}
17+
element.style.viewTransitionName = name;
18+
}
19+
20+
await vtPromise;
21+
22+
for ( const [ element, name ] of entries ) {
23+
if ( ! element ) {
24+
continue;
25+
}
26+
element.style.viewTransitionName = '';
27+
}
28+
};
29+
30+
window.addEventListener( 'pageswap', ( e ) => {
31+
if ( e.viewTransition ) {
32+
if ( document.body.classList.contains( 'single' ) ) {
33+
const article = document.querySelectorAll( 'article.post' );
34+
if ( article.length !== 1 ) {
35+
return;
36+
}
37+
38+
setTemporaryViewTransitionNames( [
39+
[ article[ 0 ].querySelector( '.entry-title' ), 'post-title' ],
40+
[ article[ 0 ].querySelector( '.post-thumbnail' ), 'post-thumbnail' ],
41+
], e.viewTransition.finished );
42+
} else if ( document.body.classList.contains( 'home' ) || document.body.classList.contains( 'archive' ) ) {
43+
const articleLink = document.querySelector( 'article.post a[href="' + e.activation.entry.url + '"]' );
44+
if ( ! articleLink ) {
45+
return;
46+
}
47+
48+
const article = getParentElement( articleLink, 'article.post' );
49+
50+
setTemporaryViewTransitionNames( [
51+
[ article.querySelector( '.entry-title' ), 'post-title' ],
52+
[ article.querySelector( '.post-thumbnail' ), 'post-thumbnail' ],
53+
], e.viewTransition.finished );
54+
}
55+
}
56+
} );
57+
58+
window.addEventListener( 'pagereveal', ( e ) => {
59+
if ( ! window.navigation.activation.from ) {
60+
return;
61+
}
62+
63+
if ( e.viewTransition ) {
64+
if ( document.body.classList.contains( 'single' ) ) {
65+
const article = document.querySelectorAll( 'article.post' );
66+
if ( article.length !== 1 ) {
67+
return;
68+
}
69+
70+
setTemporaryViewTransitionNames( [
71+
[ article[ 0 ].querySelector( '.entry-title' ), 'post-title' ],
72+
[ article[ 0 ].querySelector( '.post-thumbnail' ), 'post-thumbnail' ],
73+
], e.viewTransition.ready );
74+
} else if ( document.body.classList.contains( 'home' ) || document.body.classList.contains( 'archive' ) ) {
75+
const articleLink = document.querySelector( 'article.post a[href="' + window.navigation.activation.from.url + '"]' );
76+
if ( ! articleLink ) {
77+
return;
78+
}
79+
80+
const article = getParentElement( articleLink, 'article.post' );
81+
82+
setTemporaryViewTransitionNames( [
83+
[ article.querySelector( '.entry-title' ), 'post-title' ],
84+
[ article.querySelector( '.post-thumbnail' ), 'post-thumbnail' ],
85+
], e.viewTransition.ready );
86+
}
87+
}
88+
} );
89+
} else {
90+
window.console.warn( 'View transitions not loaded as the browser is lacking support.' );
91+
}

0 commit comments

Comments
 (0)