Slim carousel page weight, with stable per-instance slide IDs
Build and Deploy to Web Server / deploy (push) Failing after 19m10s
Build and Deploy to Web Server / deploy (push) Failing after 19m10s
This commit is contained in:
4 files changed
+18
-13
No files matched your search
@@ -6,6 +6,7 @@ import { shuffleArray } from "@lib/utils";
|
|||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
images: Array<ImageMetadata>;
|
images: Array<ImageMetadata>;
|
||||||
|
id?: string;
|
||||||
class: string;
|
class: string;
|
||||||
altText?: string | ((index: number) => string);
|
altText?: string | ((index: number) => string);
|
||||||
interval?: number;
|
interval?: number;
|
||||||
@@ -23,6 +24,7 @@ interface Props {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
images,
|
images,
|
||||||
|
id = "carousel",
|
||||||
class: className,
|
class: className,
|
||||||
altText = null,
|
altText = null,
|
||||||
interval = 5000,
|
interval = 5000,
|
||||||
@@ -38,8 +40,6 @@ const {
|
|||||||
shuffle = false
|
shuffle = false
|
||||||
} = Astro.props as Props;
|
} = Astro.props as Props;
|
||||||
|
|
||||||
const IDs: string[] = [];
|
|
||||||
|
|
||||||
const imagesArray = shuffle ? shuffleArray(images) : images;
|
const imagesArray = shuffle ? shuffleArray(images) : images;
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -50,12 +50,9 @@ const imagesArray = shuffle ? shuffleArray(images) : images;
|
|||||||
|
|
||||||
{
|
{
|
||||||
imagesArray.map((image, index) => {
|
imagesArray.map((image, index) => {
|
||||||
const ID = `image-carousel-${Math.random().toString(36)}`;
|
|
||||||
IDs.push(ID);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Image
|
<Image
|
||||||
data-id={`${ID}`}
|
data-id={`${id}-${index}`}
|
||||||
class:list={[
|
class:list={[
|
||||||
"absolute !h-full w-full object-cover object-center transition-opacity",
|
"absolute !h-full w-full object-cover object-center transition-opacity",
|
||||||
transitionStyle,
|
transitionStyle,
|
||||||
@@ -89,16 +86,19 @@ const imagesArray = shuffle ? shuffleArray(images) : images;
|
|||||||
<slot />
|
<slot />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script define:vars={{ imagesArray, interval, IDs }}>
|
<script
|
||||||
|
define:vars={{ imageCount: imagesArray.length, carouselId: id, interval }}
|
||||||
|
>
|
||||||
let currentIndex = 0;
|
let currentIndex = 0;
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
const total = imagesArray.length;
|
|
||||||
const current = document.querySelectorAll(
|
const current = document.querySelectorAll(
|
||||||
`[data-id="${IDs[currentIndex]}"]`
|
`[data-id="${carouselId}-${currentIndex}"]`
|
||||||
|
)[0];
|
||||||
|
const nextIndex = (currentIndex + 1) % imageCount;
|
||||||
|
const next = document.querySelectorAll(
|
||||||
|
`[data-id="${carouselId}-${nextIndex}"]`
|
||||||
)[0];
|
)[0];
|
||||||
const nextIndex = (currentIndex + 1) % total;
|
|
||||||
const next = document.querySelectorAll(`[data-id="${IDs[nextIndex]}"]`)[0];
|
|
||||||
|
|
||||||
if (!current || !next) return;
|
if (!current || !next) return;
|
||||||
|
|
||||||
@@ -109,9 +109,9 @@ const imagesArray = shuffle ? shuffleArray(images) : images;
|
|||||||
next.classList.remove("opacity-0", "hidden");
|
next.classList.remove("opacity-0", "hidden");
|
||||||
next.setAttribute("aria-hidden", "false");
|
next.setAttribute("aria-hidden", "false");
|
||||||
|
|
||||||
const preloadIndex = (nextIndex + 1) % total;
|
const preloadIndex = (nextIndex + 1) % imageCount;
|
||||||
const preloadImage = document.querySelectorAll(
|
const preloadImage = document.querySelectorAll(
|
||||||
`[data-id="${IDs[preloadIndex]}"]`
|
`[data-id="${carouselId}-${preloadIndex}"]`
|
||||||
)[0];
|
)[0];
|
||||||
if (preloadImage) {
|
if (preloadImage) {
|
||||||
preloadImage.classList.add("opacity-0");
|
preloadImage.classList.add("opacity-0");
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ const heroImagesArray = await convertEagerImagesImportGlobToArray(heroImages);
|
|||||||
<MainLayout title="Oops! 404" navbarDisplay="transparent">
|
<MainLayout title="Oops! 404" navbarDisplay="transparent">
|
||||||
<div class="absolute inset-0 h-full w-full bg-black">
|
<div class="absolute inset-0 h-full w-full bg-black">
|
||||||
<ImageCarousel
|
<ImageCarousel
|
||||||
|
id="not-found"
|
||||||
images={heroImagesArray}
|
images={heroImagesArray}
|
||||||
class="h-full w-full"
|
class="h-full w-full"
|
||||||
foreground={true}
|
foreground={true}
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ const tracks = (
|
|||||||
<div class="h-screen w-full flex-row">
|
<div class="h-screen w-full flex-row">
|
||||||
<div class="absolute inset-0 h-full w-full bg-black">
|
<div class="absolute inset-0 h-full w-full bg-black">
|
||||||
<ImageCarousel
|
<ImageCarousel
|
||||||
|
id="hero"
|
||||||
images={heroImagesArray}
|
images={heroImagesArray}
|
||||||
class="h-full w-full"
|
class="h-full w-full"
|
||||||
foreground={true}
|
foreground={true}
|
||||||
@@ -108,6 +109,7 @@ const tracks = (
|
|||||||
<section id="cta" class="text-white">
|
<section id="cta" class="text-white">
|
||||||
<div class="flex h-48 w-full items-center justify-center">
|
<div class="flex h-48 w-full items-center justify-center">
|
||||||
<ImageCarousel
|
<ImageCarousel
|
||||||
|
id="cta"
|
||||||
images={imagesForCTAArray}
|
images={imagesForCTAArray}
|
||||||
class="h-full w-full"
|
class="h-full w-full"
|
||||||
foreground={true}
|
foreground={true}
|
||||||
@@ -192,6 +194,7 @@ const tracks = (
|
|||||||
<section id="fillerUntilServicesIsComplete" class="text-white">
|
<section id="fillerUntilServicesIsComplete" class="text-white">
|
||||||
<div class="flex h-96 w-full items-center justify-center">
|
<div class="flex h-96 w-full items-center justify-center">
|
||||||
<ImageCarousel
|
<ImageCarousel
|
||||||
|
id="filler"
|
||||||
images={imagesForCTAArray}
|
images={imagesForCTAArray}
|
||||||
class="h-full w-full"
|
class="h-full w-full"
|
||||||
foreground={true}
|
foreground={true}
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ import P from "@components/MDX/P.astro";
|
|||||||
<section id="title" class="text-white">
|
<section id="title" class="text-white">
|
||||||
<div class="flex h-100 w-full items-center justify-center">
|
<div class="flex h-100 w-full items-center justify-center">
|
||||||
<ImageCarousel
|
<ImageCarousel
|
||||||
|
id="project-hero"
|
||||||
images={images}
|
images={images}
|
||||||
class="h-full w-full"
|
class="h-full w-full"
|
||||||
foreground={true}
|
foreground={true}
|
||||||
|
|||||||
Reference in new issue
Block a user