Created
April 17, 2025 13:58
-
-
Save megasmack/cc17398e8e8437bb60b4cdc958996eda to your computer and use it in GitHub Desktop.
A startViewTransition wrapper method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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