Skip to content

Commit 79031e9

Browse files
committed
Added an HD Button to Toggle Image Quality for Loading Times
1 parent 46519f0 commit 79031e9

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

src/app/globals.css

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,26 @@ img {
612612
cursor: pointer;
613613
}
614614

615+
.lightbox-hd {
616+
position: absolute;
617+
top: 28px;
618+
left: 28px;
619+
height: 36px;
620+
padding: 0 14px;
621+
border-radius: 999px;
622+
border: 1px solid var(--line);
623+
background: rgba(10, 10, 10, 0.6);
624+
color: var(--ink);
625+
font-size: 0.62rem;
626+
letter-spacing: 0.2em;
627+
text-transform: uppercase;
628+
}
629+
630+
.lightbox-hd.is-active {
631+
background: rgba(245, 242, 238, 0.12);
632+
border-color: rgba(245, 242, 238, 0.4);
633+
}
634+
615635
.lightbox-button {
616636
top: 50%;
617637
transform: translateY(-50%);
@@ -665,6 +685,14 @@ img {
665685
.lightbox-button.next {
666686
right: 16px;
667687
}
688+
689+
.lightbox-hd {
690+
top: 16px;
691+
left: 16px;
692+
height: 32px;
693+
padding: 0 12px;
694+
font-size: 0.58rem;
695+
}
668696
}
669697

670698
@media (max-width: 900px) and (orientation: landscape) {
@@ -713,6 +741,14 @@ img {
713741
top: 12px;
714742
right: 12px;
715743
}
744+
745+
.lightbox-hd {
746+
top: 12px;
747+
left: 12px;
748+
height: 30px;
749+
padding: 0 10px;
750+
font-size: 0.54rem;
751+
}
716752
}
717753

718754
@media (prefers-reduced-motion: reduce) {

src/app/page.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export default function Home() {
5353
const [categoriesOpen, setCategoriesOpen] = useState(false);
5454
const [panelFloating, setPanelFloating] = useState(false);
5555
const [lightbox, setLightbox] = useState<LightboxState | null>(null);
56+
const [isHd, setIsHd] = useState(false);
5657
const galleryRef = useRef<HTMLDivElement | null>(null);
5758
const scrollState = useRef({ target: 0, frame: 0 });
5859

@@ -84,6 +85,7 @@ export default function Home() {
8485
setLightbox(null);
8586
}
8687
if (event.key === "ArrowRight") {
88+
setIsHd(false);
8789
setLightbox((current) =>
8890
current
8991
? {
@@ -94,6 +96,7 @@ export default function Home() {
9496
);
9597
}
9698
if (event.key === "ArrowLeft") {
99+
setIsHd(false);
97100
setLightbox((current) =>
98101
current
99102
? {
@@ -190,10 +193,12 @@ export default function Home() {
190193
}, []);
191194

192195
const openLightbox = (items: Photo[], index: number) => {
196+
setIsHd(false);
193197
setLightbox({ items, index });
194198
};
195199

196200
const goNext = () => {
201+
setIsHd(false);
197202
setLightbox((current) =>
198203
current
199204
? { ...current, index: (current.index + 1) % current.items.length }
@@ -202,6 +207,7 @@ export default function Home() {
202207
};
203208

204209
const goPrev = () => {
210+
setIsHd(false);
205211
setLightbox((current) =>
206212
current
207213
? {
@@ -306,6 +312,14 @@ export default function Home() {
306312

307313
{lightbox && (
308314
<div className="lightbox" role="dialog" aria-modal="true">
315+
<button
316+
className={`lightbox-hd${isHd ? " is-active" : ""}`}
317+
type="button"
318+
aria-pressed={isHd}
319+
onClick={() => setIsHd((current) => !current)}
320+
>
321+
HD
322+
</button>
309323
<button
310324
className="lightbox-close"
311325
type="button"
@@ -332,9 +346,17 @@ export default function Home() {
332346
</button>
333347
<figure>
334348
<img
335-
src={responsiveSrc(lightbox.items[lightbox.index].src)}
336-
srcSet={responsiveSrcSet(lightbox.items[lightbox.index].src)}
337-
sizes="(max-width: 900px) 96vw, 80vw"
349+
src={
350+
isHd
351+
? withBasePath(lightbox.items[lightbox.index].src)
352+
: responsiveSrc(lightbox.items[lightbox.index].src)
353+
}
354+
srcSet={
355+
isHd
356+
? undefined
357+
: responsiveSrcSet(lightbox.items[lightbox.index].src)
358+
}
359+
sizes={isHd ? undefined : "(max-width: 900px) 96vw, 80vw"}
338360
loading="eager"
339361
decoding="async"
340362
/>

src/components/CategoryGallery.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default function CategoryGallery({
3131
}: CategoryGalleryProps) {
3232
const [lightbox, setLightbox] = useState<LightboxState | null>(null);
3333
const [switchOpen, setSwitchOpen] = useState(false);
34+
const [isHd, setIsHd] = useState(false);
3435

3536
const preloadImage = (src: string) => {
3637
if (typeof window === "undefined") return;
@@ -55,13 +56,15 @@ export default function CategoryGallery({
5556
setLightbox(null);
5657
}
5758
if (event.key === "ArrowRight") {
59+
setIsHd(false);
5860
setLightbox((current) =>
5961
current
6062
? { index: (current.index + 1) % images.length }
6163
: current
6264
);
6365
}
6466
if (event.key === "ArrowLeft") {
67+
setIsHd(false);
6568
setLightbox((current) =>
6669
current
6770
? { index: (current.index - 1 + images.length) % images.length }
@@ -86,12 +89,14 @@ export default function CategoryGallery({
8689
}, [images, lightbox]);
8790

8891
const goNext = () => {
92+
setIsHd(false);
8993
setLightbox((current) =>
9094
current ? { index: (current.index + 1) % images.length } : current
9195
);
9296
};
9397

9498
const goPrev = () => {
99+
setIsHd(false);
95100
setLightbox((current) =>
96101
current
97102
? { index: (current.index - 1 + images.length) % images.length }
@@ -145,14 +150,25 @@ export default function CategoryGallery({
145150
<button
146151
type="button"
147152
aria-label={`Open ${image.alt}`}
148-
onClick={() => setLightbox({ index })}
153+
onClick={() => {
154+
setIsHd(false);
155+
setLightbox({ index });
156+
}}
149157
/>
150158
</article>
151159
))}
152160
</div>
153161

154162
{lightbox && (
155163
<div className="lightbox" role="dialog" aria-modal="true">
164+
<button
165+
className={`lightbox-hd${isHd ? " is-active" : ""}`}
166+
type="button"
167+
aria-pressed={isHd}
168+
onClick={() => setIsHd((current) => !current)}
169+
>
170+
HD
171+
</button>
156172
<button
157173
className="lightbox-close"
158174
type="button"
@@ -179,9 +195,15 @@ export default function CategoryGallery({
179195
</button>
180196
<figure>
181197
<img
182-
src={responsiveSrc(images[lightbox.index].src)}
183-
srcSet={responsiveSrcSet(images[lightbox.index].src)}
184-
sizes="(max-width: 900px) 96vw, 80vw"
198+
src={
199+
isHd
200+
? withBasePath(images[lightbox.index].src)
201+
: responsiveSrc(images[lightbox.index].src)
202+
}
203+
srcSet={
204+
isHd ? undefined : responsiveSrcSet(images[lightbox.index].src)
205+
}
206+
sizes={isHd ? undefined : "(max-width: 900px) 96vw, 80vw"}
185207
alt={images[lightbox.index].alt}
186208
loading="eager"
187209
decoding="async"

0 commit comments

Comments
 (0)