Skip to content

Instantly share code, notes, and snippets.

@megasmack
Created April 17, 2025 13:58
Show Gist options
  • Select an option

  • Save megasmack/cc17398e8e8437bb60b4cdc958996eda to your computer and use it in GitHub Desktop.

Select an option

Save megasmack/cc17398e8e8437bb60b4cdc958996eda to your computer and use it in GitHub Desktop.
A startViewTransition wrapper method.
/**
* Wraps the startViewTransition to check avialability and reduced motion settings.
* Usage: await viewTransition(() => {this.showElement = true});
* @param {Function} callback - callback to function that triggers the animation.
* @returns {Promise<unknown>} - Finish transition.
*/
export const viewTransition = (callback) => {
return new Promise((resolve) => {
const mediaQuery = window.matchMedia('(prefers-reduced-motion)');
const isReducedMotion = mediaQuery.matches;
if (!document.startViewTransition || isReducedMotion) {
callback();
resolve();
} else {
const transition = document.startViewTransition(() => {
callback();
});
transition.finished.then(resolve);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment