@vtbag/utensil-drawer
Version:
Pull out just what you need to craft seamless transitions. The Utensil Drawer holds reusable functions to help you build websites with view transitions. It is a bit sparse right now, but like the one in your kitchen, it is bound to fill up over time.
36 lines (35 loc) • 1.04 kB
JavaScript
export function createViewTransitionSurrogate(update) {
let res;
let rej;
let readyReject;
const updateCallbackDone = new Promise((resolve, reject) => {
res = resolve;
rej = reject;
});
let updateFailure = undefined;
requestAnimationFrame(async () => {
try {
await update();
res();
}
catch (e) {
updateFailure = e;
rej(e);
}
});
const ready = new Promise((res, rej) => {
readyReject = rej;
updateCallbackDone.then(() => res(), //rej('Browser does not support View Transition API'),
(_) => res() //rej(err)
);
});
const done = (res, rej) => updateFailure ? rej(updateFailure) : res();
const finished = new Promise((res, rej) => updateCallbackDone.finally(() => ready.then(() => done(res, rej), (e) => done(res, rej))));
return {
updateCallbackDone,
ready,
finished,
skipTransition: () => { },
types: new Set(),
};
}