-
Notifications
You must be signed in to change notification settings - Fork 138
Add support for dynamic view transition names for global elements, and certain post elements to View Transitions plugin #1999
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
Conversation
…d certain post elements to View Transitions plugin.
| * identified by config.postSelector) and their view transition names. | ||
| */ | ||
| window.plvtInitViewTransitions = ( config ) => { | ||
| if ( ! window.navigation || ! ( 'CSSViewTransitionRule' in window ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@swissspidy I get TypeScript warnings locally here, like navigation not being a valid property on window, etc. The same in regards to the various (valid) properties accessed on the event objects. Do you have any advice for how to fix them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, looks like they show here too: https://github.com/WordPress/performance/actions/runs/14766566981/job/41459069785?pr=1999
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Navigation API is still relatively new and only supported by Chromium-based browsers. TypeScript by default only ships such types if the feature is supported by at least 2 different browser engines.
Until that happens, you can use the community-maintained navigation-api-types package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that, but it looks like that package doesn't properly support the latest specification either. It doesn't support e.g. the activation property of https://developer.mozilla.org/en-US/docs/Web/API/Navigation, which is what's needed here. So using this package doesn't resolve the issue, for now I ended up manually typing it on a custom ExtendedWindow type.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| /** | ||
| * Customizes view transition behavior on the URL that is being navigated from. | ||
| * | ||
| * @param {Event} event Event fired as the previous URL is about to unload. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| * @param {Event} event Event fired as the previous URL is about to unload. | |
| * @param {PageSwapEvent} event Event fired as the previous URL is about to unload. |
That should help with those errors below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had it like that originally actually, but that would give me other errors about PageSwapEvent / PageRevealEvent being undefined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made several updates in 8545b4f, which resolves all errors except those related to PageSwapEvent and PageRevealEvent.
I am confused why these warnings still appear because:
- Both
PageSwapEventandPageRevealEventare declared in TypeScript's owndomlibrary. I verified that they're there in the version we currently use. - The errors all refer to
Event(instead of the above two), which I'm puzzled why they do that, even though the JS and TS code refers toPageSwapEventandPageRevealEvent.
Let me know if you have any ideas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm looking at 8545b4f, instead of adding such a ExtendedWindow type, you should extend the Window interface. Like so:
declare global {
interface Window {
plvtInitViewTransitions?: InitViewTransitionsFunction;
navigation?: {
activation: NavigationActivation;
};
}
}There's no need for this const win = window; part (and it doesn't affect minification anyway, so the comment there is incorrect).
The PageSwapEvent errors I can look at tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks, fixed in eaf9060. I took the comment from Optimization Detective's detect.js, which assigns const win = window with that comment, so I thought that had a purpose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case I would follow #1999 (comment) for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks, fixed in eaf9060. I took the comment from Optimization Detective's
detect.js, which assignsconst win = windowwith that comment, so I thought that had a purpose.
For this line:
| const win = window; |
The purpose was indeed to facilitate minification, since the window global symbol won't be used in favor of win which can be further reduced during minification to something like w.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK minifiers automatically do that kind of stuff. I'd be very surprised if one needs to help them like that 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried eliminating win in favor of reusing window and the minification used in this repo doesn't introduce any minified alias:
if (0 === window.innerWidth || 0 === window.innerHeight) return void y("Window must have non-zero dimensions for URL Metric collection.");Otherwise, when minified it is passing through the symbol as-is:
if (0 === win.innerWidth || 0 === win.innerHeight) return void y("Window must have non-zero dimensions for URL Metric collection.");I'm also surprised that the existing minifier isn't doing something smarter by reducing win to w since it is a local variable in the module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding the ESLint problem, I applied the suggested code from @ShyamGadde, that's indeed better. Everything passes now 👍
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #1999 +/- ##
==========================================
- Coverage 72.72% 72.47% -0.25%
==========================================
Files 87 88 +1
Lines 7127 7100 -27
==========================================
- Hits 5183 5146 -37
- Misses 1944 1954 +10
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Co-authored-by: Shyamsundar Gadde <[email protected]>
| * | ||
| * @global array<string, mixed> $_wp_theme_features Theme support features added and their arguments. | ||
| */ | ||
| function plvt_sanitize_view_transitions_theme_support(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idea: Instead of having plvt_sanitize_view_transitions_theme_support() sanitize the global variable and then override $_wp_theme_features with the sanitized value, what if there was instead a plvt_get_view_transitions_theme_support() which always returned the sanitized value? Then there would be no need to set plvt_sanitize_view_transitions_theme_support() at init action, and there wouldn't be a risk that a theme made changes to the theme support after the init action happened.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea, though I personally think the risk is low enough to ignore it in favor of having a more Core-like API, relying on the actual functions that should be used for this and "patching" the lack of integrated sanitization via a hook like this.
If later there are reports of problems with this, we could reassess the relevance.
| wp_add_inline_style( 'wp-view-transitions', $stylesheet ); | ||
| wp_enqueue_style( 'wp-view-transitions' ); | ||
|
|
||
| $theme_support = get_theme_support( 'view-transitions' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per above, this could be replaced with:
| $theme_support = get_theme_support( 'view-transitions' ); | |
| $theme_support = plvt_get_view_transitions_theme_support(); |
| ( ! is_array( $theme_support['global-transition-names'] ) || count( $theme_support['global-transition-names'] ) === 0 ) && | ||
| ( ! is_array( $theme_support['post-transition-names'] ) || count( $theme_support['post-transition-names'] ) === 0 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the is_array() checks if the theme support value has been sanitized, right?
| ( ! is_array( $theme_support['global-transition-names'] ) || count( $theme_support['global-transition-names'] ) === 0 ) && | |
| ( ! is_array( $theme_support['post-transition-names'] ) || count( $theme_support['post-transition-names'] ) === 0 ) | |
| count( $theme_support['global-transition-names'] ) === 0 && | |
| count( $theme_support['post-transition-names'] ) === 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really, but since the data comes from a global variable, I think it's better to use defensive coding.
Summary
See #1997.
Relevant technical choices
header,main), others for post specific elements like post titles and post featured images.view-transitionstheme support. The defaults cover both block themes (standardized via Core blocks) and classic themes (via common conventions and partially Core CSS classes), and should work for the majority of themes.