|
| 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