@@ -26,30 +26,27 @@ window.wp.viewTransitions.init = ( config ) => {
2626 /**
2727 * Gets all view transition entries relevant for a view transition.
2828 *
29- * @param {string } transitionType View transition type (e.g. 'default', 'forwards', 'backwards').
29+ * @param {string } transitionType View transition type (e.g. 'default', 'chronological- forwards', 'chronological- backwards').
3030 * @param {Element } bodyElement The body element.
3131 * @param {Element|null } articleElement The post element relevant for the view transition, if any.
3232 * @return {Array[] } View transition entries with each one containing the element and its view transition name.
3333 */
3434 const getViewTransitionEntries = ( transitionType , bodyElement , articleElement ) => {
35- const isMainSlide = transitionType === 'forwards' || transitionType === 'backwards' ;
36- let foundMainElement = false ;
37- const globalEntries = Object . entries ( config . globalTransitionNames || { } ) . map ( ( [ selector , name ] ) => {
38- const element = bodyElement . querySelector ( selector ) ;
39- if ( name === 'main' && element ) {
40- foundMainElement = true ;
41- }
42- return [ element , name ] ;
43- } ) ;
44- if ( ! articleElement || isMainSlide && foundMainElement ) {
45- return globalEntries ;
46- }
47- return [
48- ...globalEntries ,
49- ...Object . entries ( config . postTransitionNames || { } ) . map ( ( [ selector , name ] ) => {
35+ const globalEntries = config . animations [ transitionType ] . useGlobalTransitionNames ?
36+ Object . entries ( config . globalTransitionNames || { } ) . map ( ( [ selector , name ] ) => {
37+ const element = bodyElement . querySelector ( selector ) ;
38+ return [ element , name ] ;
39+ } ) : [ ] ;
40+
41+ const postEntries = config . animations [ transitionType ] . usePostTransitionNames && articleElement ?
42+ Object . entries ( config . postTransitionNames || { } ) . map ( ( [ selector , name ] ) => {
5043 const element = articleElement . querySelector ( selector ) ;
5144 return [ element , name ] ;
52- } ) ,
45+ } ) : [ ] ;
46+
47+ return [
48+ ...globalEntries ,
49+ ...postEntries ,
5350 ] ;
5451 } ;
5552
@@ -126,14 +123,20 @@ window.wp.viewTransitions.init = ( config ) => {
126123 *
127124 * @param {NavigationHistoryEntry } oldEntry Navigation history entry for the URL navigated from.
128125 * @param {NavigationHistoryEntry } newEntry Navigation history entry for the URL navigated to.
129- * @return {string } View transition type (e.g. 'default', 'forwards', 'backwards').
126+ * @return {string } View transition type (e.g. 'default', 'chronological- forwards', 'chronological- backwards').
130127 */
131128 const determineTransitionType = ( oldEntry , newEntry ) => {
132- if ( ! config . chronologicalSlideInOut ) {
129+ if ( ! oldEntry || ! newEntry ) {
133130 return 'default' ;
134131 }
135132
136- if ( ! oldEntry || ! newEntry ) {
133+ // Use 'default' transition type if all other transition types are disabled.
134+ if (
135+ ! config . animations [ 'chronological-forwards' ] &&
136+ ! config . animations [ 'chronological-backwards' ] &&
137+ ! config . animations [ 'pagination-forwards' ] &&
138+ ! config . animations [ 'pagination-backwards' ]
139+ ) {
137140 return 'default' ;
138141 }
139142
@@ -147,49 +150,61 @@ window.wp.viewTransitions.init = ( config ) => {
147150 return 'default' ;
148151 }
149152
150- // Check if the URLs are for a paginated archive.
151- let oldPageMatches = oldPathname . match ( / \/ p a g e \/ ( \d + ) \/ ? $ / ) ;
152- let newPageMatches = newPathname . match ( / \/ p a g e \/ ( \d + ) \/ ? $ / ) ;
153+ let oldPageMatches = false ;
154+ let newPageMatches = false ;
153155 let prefix = '' ;
154156
157+ // If enabled, check if the URLs are for a chronologically paginated archive.
158+ if ( config . animations [ 'chronological-forwards' ] || config . animations [ 'chronological-backwards' ] ) {
159+ oldPageMatches = oldPathname . match ( / \/ p a g e \/ ( \d + ) \/ ? $ / ) ;
160+ newPageMatches = newPathname . match ( / \/ p a g e \/ ( \d + ) \/ ? $ / ) ;
161+ prefix = 'chronological-' ;
162+ }
163+
155164 // If not, check if the URLs are for a multi-page post.
156- if ( ! oldPageMatches && ! newPageMatches ) {
165+ if ( ! oldPageMatches && ! newPageMatches && ( config . animations [ 'pagination-forwards' ] || config . animations [ 'pagination-backwards' ] ) ) {
157166 oldPageMatches = oldPathname . match ( / \/ ( \d + ) \/ ? $ / ) ;
158167 newPageMatches = newPathname . match ( / \/ ( \d + ) \/ ? $ / ) ;
159- prefix = 'content -' ;
168+ prefix = 'pagination -' ;
160169 }
161170
162171 // If there is a match on at least one of the URLs, compare whether their roots before the page segment match.
163172 if ( oldPageMatches || newPageMatches ) {
164173 const oldPageBase = oldPageMatches ? oldPathname . substring ( 0 , oldPathname . length - oldPageMatches [ 0 ] . length ) : oldPathname . replace ( / \/ $ / , '' ) ;
165174 const newPageBase = newPageMatches ? newPathname . substring ( 0 , newPathname . length - newPageMatches [ 0 ] . length ) : newPathname . replace ( / \/ $ / , '' ) ;
166175 if ( oldPageBase === newPageBase ) { // They belong to the same archive or post.
176+ // Return the appropriate transition type, or 'default' if no particular animation is specified.
167177 if ( oldPageMatches && newPageMatches ) {
168- return Number ( oldPageMatches [ 1 ] ) < Number ( newPageMatches [ 1 ] ) ? `${ prefix } forwards` : `${ prefix } backwards` ;
178+ if ( Number ( oldPageMatches [ 1 ] ) < Number ( newPageMatches [ 1 ] ) ) {
179+ return config . animations [ `${ prefix } forwards` ] ? `${ prefix } forwards` : 'default' ;
180+ }
181+ return config . animations [ `${ prefix } backwards` ] ? `${ prefix } backwards` : 'default' ;
169182 }
170183 if ( newPageMatches && Number ( newPageMatches [ 1 ] ) > 1 ) {
171- return `${ prefix } forwards` ;
184+ return config . animations [ `${ prefix } forwards` ] ? ` ${ prefix } forwards` : 'default' ;
172185 }
173186 if ( oldPageMatches && Number ( oldPageMatches [ 1 ] ) > 1 ) {
174- return `${ prefix } backwards` ;
187+ return config . animations [ `${ prefix } backwards` ] ? ` ${ prefix } backwards` : 'default' ;
175188 }
176189 }
177190 }
178191
179- // Check if the URLs are for content labelled by date (e.g. navigation to previous/next post).
180- const oldDateMatches = oldPathname . match ( / \/ ( \d { 4 } ) \/ ( \d { 2 } ) \/ ( \d { 2 } ) \/ [ ^ \/ ] + \/ ? $ / ) ;
181- const newDateMatches = newPathname . match ( / \/ ( \d { 4 } ) \/ ( \d { 2 } ) \/ ( \d { 2 } ) \/ [ ^ \/ ] + \/ ? $ / ) ;
182- if ( oldDateMatches && newDateMatches ) {
183- const oldPageBase = oldPathname . substring ( 0 , oldPathname . length - oldDateMatches [ 0 ] . length ) ;
184- const newPageBase = newPathname . substring ( 0 , newPathname . length - newDateMatches [ 0 ] . length ) ;
185- if ( oldPageBase === newPageBase ) { // They belong to the same hierarchy.
186- const oldDate = new Date ( parseInt ( oldDateMatches [ 1 ] ) , parseInt ( oldDateMatches [ 2 ] ) - 1 , parseInt ( oldDateMatches [ 3 ] ) ) ;
187- const newDate = new Date ( parseInt ( newDateMatches [ 1 ] ) , parseInt ( newDateMatches [ 2 ] ) - 1 , parseInt ( newDateMatches [ 3 ] ) ) ;
188- if ( oldDate < newDate ) {
189- return 'forwards' ;
190- }
191- if ( oldDate > newDate ) {
192- return 'backwards' ;
192+ // If enabled, check if the URLs are for content labelled by date (e.g. navigation to previous/next post).
193+ if ( config . animations [ 'chronological-forwards' ] || config . animations [ 'chronological-backwards' ] ) {
194+ const oldDateMatches = oldPathname . match ( / \/ ( \d { 4 } ) \/ ( \d { 2 } ) \/ ( \d { 2 } ) \/ [ ^ \/ ] + \/ ? $ / ) ;
195+ const newDateMatches = newPathname . match ( / \/ ( \d { 4 } ) \/ ( \d { 2 } ) \/ ( \d { 2 } ) \/ [ ^ \/ ] + \/ ? $ / ) ;
196+ if ( oldDateMatches && newDateMatches ) {
197+ const oldPageBase = oldPathname . substring ( 0 , oldPathname . length - oldDateMatches [ 0 ] . length ) ;
198+ const newPageBase = newPathname . substring ( 0 , newPathname . length - newDateMatches [ 0 ] . length ) ;
199+ if ( oldPageBase === newPageBase ) { // They belong to the same hierarchy.
200+ const oldDate = new Date ( parseInt ( oldDateMatches [ 1 ] ) , parseInt ( oldDateMatches [ 2 ] ) - 1 , parseInt ( oldDateMatches [ 3 ] ) ) ;
201+ const newDate = new Date ( parseInt ( newDateMatches [ 1 ] ) , parseInt ( newDateMatches [ 2 ] ) - 1 , parseInt ( newDateMatches [ 3 ] ) ) ;
202+ if ( oldDate < newDate ) {
203+ return config . animations [ 'chronological-forwards' ] ? 'chronological-forwards' : 'default' ;
204+ }
205+ if ( oldDate > newDate ) {
206+ return config . animations [ 'chronological-backwards' ] ? 'chronological-backwards' : 'default' ;
207+ }
193208 }
194209 }
195210 }
@@ -204,11 +219,8 @@ window.wp.viewTransitions.init = ( config ) => {
204219 * @return {string|null } View transition name, or null if none is relevant for the transition type.
205220 */
206221 const getViewTransitionNameForSlideAnimation = ( transitionType ) => {
207- if ( transitionType === 'forwards' || transitionType === 'backwards' ) {
208- return 'main' ;
209- }
210- if ( transitionType === 'content-forwards' || transitionType === 'content-backwards' ) {
211- return 'post-content' ;
222+ if ( config . animations [ transitionType ] && config . animations [ transitionType ] . targetName !== '*' ) {
223+ return config . animations [ transitionType ] . targetName ;
212224 }
213225 return null ;
214226 } ;
@@ -239,6 +251,7 @@ window.wp.viewTransitions.init = ( config ) => {
239251 }
240252 if ( viewTransitionEntries ) {
241253 setTemporaryViewTransitionNames ( viewTransitionEntries , event . viewTransition . finished ) ;
254+
242255 const slideViewTransitionName = getViewTransitionNameForSlideAnimation ( transitionType ) ;
243256 if ( slideViewTransitionName ) {
244257 // Consider a scroll offset if defined (e.g. due to fixed navigation bars being in the way).
@@ -276,6 +289,7 @@ window.wp.viewTransitions.init = ( config ) => {
276289 }
277290 if ( viewTransitionEntries ) {
278291 setTemporaryViewTransitionNames ( viewTransitionEntries , event . viewTransition . ready ) ;
292+
279293 const slideViewTransitionName = getViewTransitionNameForSlideAnimation ( transitionType ) ;
280294 if ( slideViewTransitionName ) {
281295 const oldScrollY = sessionStorage . getItem ( 'wpViewTransitionsOldScrollY' ) ;
0 commit comments