Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/vtbag/website. The @vtbag website. ⭐️ Please star to support this work! ⭐️
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151---
---
<span class="media-container"> <span class="media"><slot /></span> <dialog class="media"> <span><slot /></span> <slot name="caption" /> </dialog></span>
<portal-gate to="end:head"> <script> import { mayStartViewTransition } from "@vtbag/utensil-drawer/may-start-view-transition"; document.querySelectorAll<HTMLSpanElement>("span.media").forEach((e) => { // e.parentElement!.style.viewTransitionName = `media-${idx}`; // e.parentElement!.style.viewTransitionClass = "nonewanim"; const dialog = e.nextElementSibling as HTMLDialogElement; const content = dialog.firstElementChild as HTMLSpanElement;
e.addEventListener("click", () => { const path = (current: HTMLElement | SVGSVGElement) => { const path = []; while (current !== e) { path.unshift([...current.parentElement!.children].indexOf(current)); current = current.parentElement!; } return path; };
const walk = (current: HTMLElement, path: number[]) => { for (const i of path) { current = current.children[i] as HTMLElement; } return current; };
const smallMedia = [ ...e.querySelectorAll< HTMLImageElement | HTMLVideoElement | SVGSVGElement >("img,video,svg"), ].find( (e) => (!(e instanceof SVGSVGElement) && e.offsetHeight > 0 && e.offsetWidth > 0) || (e instanceof SVGSVGElement && e.height.baseVal.value > 0 && e.width.baseVal.value > 0) ); if (!smallMedia) return;
smallMedia.style.viewTransitionName = "media"; const aspectRatio = (smallMedia instanceof HTMLImageElement && smallMedia.naturalWidth / smallMedia.naturalHeight) || (smallMedia instanceof HTMLVideoElement && smallMedia.videoWidth / smallMedia.videoHeight) || (smallMedia instanceof SVGSVGElement && smallMedia.width.baseVal.value / smallMedia.height.baseVal.value) || 1; const viewportAspectRatio = window.innerWidth / window.innerHeight;
const bigMedia = walk(content, path(smallMedia))!;
if (aspectRatio > viewportAspectRatio) { // Scale width, set height to auto bigMedia.style.width = "80vw"; bigMedia.style.height = "auto"; } else { // Scale height, set width to auto bigMedia.style.width = "auto"; bigMedia.style.height = "80vh"; } bigMedia.style.marginInline = "auto"; //@ts-ignore bigMedia.style.viewTransitionClass = //@ts-ignore smallMedia.style.viewTransitionClass = "media";
mayStartViewTransition( { update: () => { dialog.style.viewTransitionName = `dialog`; smallMedia.style.viewTransitionName = "none"; bigMedia.style.viewTransitionName = "media"; dialog.showModal(); }, types: ["media"], }, { useTypesPolyfill: "auto" } ); dialog.addEventListener( "click", () => { mayStartViewTransition({ update: () => { bigMedia.style.viewTransitionName = "none"; smallMedia.style.viewTransitionName = "media"; dialog.close(); }, types: ["media"], }, {useTypesPolyfill: 'auto'}).finished.finally(() => { smallMedia.style.viewTransitionName = dialog.style.viewTransitionName = "none"; }); }, { once: true } ); }); }); </script></portal-gate>
<style is:global> span.media { cursor: zoom-in; } dialog { margin: auto; &::backdrop { view-transition-name: dialog-backdrop; backdrop-filter: blur(5px); } } span.media, dialog.media span:not(.dark\:sl-hidden):not(.light\:sl-hidden) { display: inline-block; } dialog.media { border: dotted 1px gray; border-radius: 1em; box-shadow: 0 0 1em #8888; } ::view-transition-group(dialog-backdrop) { animation-duration: 500ms; } ::view-transition-group(dialog) { z-index: 1; } ::view-transition-group(.media) { z-index: 2; } ::view-transition-new(.media), ::view-transition-old(.media), ::view-transition-new(.nonewanim) { animation-name: none; }</style>