Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions components/_partials/components-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,22 @@ const gifDuration = (buffer) => {
return total;
};

// Quarto rewrites `src` to a relative path but leaves `data-*` site-root
// absolute, which misses the /py prefix in production. Resolve them against
// the site root Quarto records in every page.
const siteRoot = new URL(
document.querySelector('meta[name="quarto:offset"]')?.content ?? "./",
document.baseURI,
);
const fromSiteRoot = (path) => new URL(path.replace(/^\//, ""), siteRoot).href;

document.querySelectorAll(".component-list-card").forEach((card) => {
const image = card.querySelector(".component-list-preview");
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");

if (!image) return;
const staticSrc = fromSiteRoot(image.dataset.staticSrc);
const animatedSrc = fromSiteRoot(image.dataset.animatedSrc);

// Fetch the GIF once, then give it a fresh blob URL on every hover: a new URL
// always plays from frame 0 (re-setting a cached GIF's src does not restart
Expand All @@ -54,7 +65,7 @@ document.querySelectorAll(".component-list-card").forEach((card) => {
};

const loadGif = () =>
(gif ??= fetch(image.dataset.animatedSrc)
(gif ??= fetch(animatedSrc)
.then((response) => (response.ok ? download(response) : Promise.reject()))
.then(async (blob) => {
const duration = gifDuration(await blob.arrayBuffer());
Expand All @@ -64,7 +75,7 @@ document.querySelectorAll(".component-list-card").forEach((card) => {

const showPoster = () => {
previewing = false;
image.src = image.dataset.staticSrc;
image.src = staticSrc;
card.classList.remove("is-previewing", "is-playing");
if (blobUrl) URL.revokeObjectURL(blobUrl);
blobUrl = undefined;
Expand Down Expand Up @@ -98,7 +109,7 @@ document.querySelectorAll(".component-list-card").forEach((card) => {
if (blobUrl && image.src === blobUrl) card.classList.add("is-playing");
});
image.addEventListener("error", () => {
if (image.src !== new URL(image.dataset.staticSrc, document.baseURI).href) {
if (image.src !== staticSrc) {
showPoster();
}
});
Expand Down
Loading