Skip to content

Commit

Permalink
feat: ViewTransitionsPlugin and startViewTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
Clarkkkk committed Jun 20, 2023
1 parent ffc1d32 commit df1ec5f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/index.ts
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'
13 changes: 13 additions & 0 deletions src/plugin.ts
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
}
})
}
}
}
27 changes: 27 additions & 0 deletions src/transition.ts
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
}

0 comments on commit df1ec5f

Please sign in to comment.