Skip to content

Commit 8545b4f

Browse files
committed
Add type declarations for view transitions JS code.
1 parent b6a7d94 commit 8545b4f

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

plugins/view-transitions/js/types.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export type ViewTransitionsConfig = {
2+
postSelector?: string;
3+
globalTransitionNames?: Record< string, string >;
4+
postTransitionNames?: Record< string, string >;
5+
};
6+
7+
export type InitViewTransitionsFunction = (
8+
config: ViewTransitionsConfig
9+
) => void;
10+
11+
export type ExtendedWindow = Window &
12+
typeof globalThis & {
13+
plvtInitViewTransitions?: InitViewTransitionsFunction;
14+
navigation?: {
15+
activation: NavigationActivation;
16+
};
17+
};
18+
19+
export type PageSwapListenerFunction = ( event: PageSwapEvent ) => void;
20+
export type PageRevealListenerFunction = ( event: PageRevealEvent ) => void;

plugins/view-transitions/js/view-transitions.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1+
/**
2+
* @typedef {import("./types.ts").ExtendedWindow} ExtendedWindow
3+
* @typedef {import("./types.ts").ViewTransitionsConfig} ViewTransitionsConfig
4+
* @typedef {import("./types.ts").InitViewTransitionsFunction} InitViewTransitionsFunction
5+
* @typedef {import("./types.ts").PageSwapListenerFunction} PageSwapListenerFunction
6+
* @typedef {import("./types.ts").PageRevealListenerFunction} PageRevealListenerFunction
7+
*/
8+
9+
/**
10+
* Window reference to reduce size when script is minified.
11+
*
12+
* @type {ExtendedWindow}
13+
*/
14+
const win = window;
15+
116
/**
217
* Initializes view transitions for the current URL.
318
*
4-
* @param {Object} config The view transitions configuration.
5-
* @param {string} config.postSelector General selector for post elements in the DOM.
6-
* @param {Object} config.globalTransitionNames Map of selectors for global elements (queried relative to 'body')
7-
* and their view transition names.
8-
* @param {Object} config.postTransitionNames Map of selectors for post elements (queried relative to an element
9-
* identified by config.postSelector) and their view transition names.
19+
* @type {InitViewTransitionsFunction}
20+
* @param {ViewTransitionsConfig} config - The view transitions configuration.
1021
*/
11-
window.plvtInitViewTransitions = ( config ) => {
12-
if ( ! window.navigation || ! ( 'CSSViewTransitionRule' in window ) ) {
13-
window.console.warn(
22+
win.plvtInitViewTransitions = ( config ) => {
23+
if ( ! win.navigation || ! ( 'CSSViewTransitionRule' in win ) ) {
24+
win.console.warn(
1425
'View transitions not loaded as the browser is lacking support.'
1526
);
1627
return;
@@ -121,9 +132,10 @@ window.plvtInitViewTransitions = ( config ) => {
121132
/**
122133
* Customizes view transition behavior on the URL that is being navigated from.
123134
*
124-
* @param {Event} event Event fired as the previous URL is about to unload.
135+
* @type {PageSwapListenerFunction}
136+
* @param {PageSwapEvent} event - Event fired as the previous URL is about to unload.
125137
*/
126-
window.addEventListener( 'pageswap', ( event ) => {
138+
win.addEventListener( 'pageswap', ( event ) => {
127139
if ( event.viewTransition ) {
128140
let viewTransitionEntries;
129141
if ( document.body.classList.contains( 'single' ) ) {
@@ -152,9 +164,10 @@ window.plvtInitViewTransitions = ( config ) => {
152164
/**
153165
* Customizes view transition behavior on the URL that is being navigated to.
154166
*
155-
* @param {Event} event Event fired as the new URL being navigated to is loaded.
167+
* @type {PageRevealListenerFunction}
168+
* @param {PageRevealEvent} event - Event fired as the new URL being navigated to is loaded.
156169
*/
157-
window.addEventListener( 'pagereveal', ( event ) => {
170+
win.addEventListener( 'pagereveal', ( event ) => {
158171
if ( event.viewTransition ) {
159172
let viewTransitionEntries;
160173
if ( document.body.classList.contains( 'single' ) ) {
@@ -168,10 +181,8 @@ window.plvtInitViewTransitions = ( config ) => {
168181
) {
169182
viewTransitionEntries = getViewTransitionEntries(
170183
document.body,
171-
window.navigation.activation.from
172-
? getArticleForUrl(
173-
window.navigation.activation.from.url
174-
)
184+
win.navigation.activation.from
185+
? getArticleForUrl( win.navigation.activation.from.url )
175186
: null
176187
);
177188
}

0 commit comments

Comments
 (0)