generated from Clarkkkk/vite-vanilla-ts-lib-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ViewTransitionsPlugin and startViewTransition
- Loading branch information
Showing
3 changed files
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
import "./style.css"; | ||
|
||
export { sum } from "./sum"; | ||
export { ViewTransitionsPlugin } from './plugin' | ||
export { startViewTransition } from './transition' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { Plugin } from 'vue' | ||
|
||
export function ViewTransitionsPlugin(): Plugin { | ||
return { | ||
install(app) { | ||
app.directive('view-transition-name', { | ||
beforeMount(el, binding) { | ||
el.style.viewTransitionName = binding.value | ||
} | ||
}) | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
interface ViewTransition { | ||
ready: Promise<void> | ||
finished: Promise<void> | ||
skipTransition: () => void | ||
} | ||
|
||
export async function startViewTransition(): Promise<ViewTransition> { | ||
const viewTransition = {} as ViewTransition | ||
// @ts-expect-error startViewTransition is not existed on document yet | ||
if (document.startViewTransition) { | ||
await new Promise<void>((resolve) => { | ||
// @ts-expect-error startViewTransition is not existed on document yet | ||
const nativeViewTransition = document.startViewTransition(() => { | ||
resolve() | ||
}) | ||
viewTransition.ready = nativeViewTransition.ready | ||
viewTransition.finished = nativeViewTransition.finished | ||
viewTransition.skipTransition = | ||
nativeViewTransition.skipTransition.bind(nativeViewTransition) | ||
}) | ||
} else { | ||
console.error( | ||
"This browser doesn't support View Transitions Api, please check: https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API#browser_compatibility" | ||
) | ||
} | ||
return viewTransition | ||
} |