feat: improving buttons on gallery slider

This commit is contained in:
Hydra 2024-05-13 02:02:55 +01:00
parent cabec999ba
commit ff20d531c2
No known key found for this signature in database
5 changed files with 127 additions and 71 deletions

View file

@ -96,7 +96,9 @@
"dont_show_it_again": "Don't show it again", "dont_show_it_again": "Don't show it again",
"copy_to_clipboard": "Copy", "copy_to_clipboard": "Copy",
"copied_to_clipboard": "Copied", "copied_to_clipboard": "Copied",
"got_it": "Got it" "got_it": "Got it",
"previous_screenshot": "Previous screenshot",
"next_screenshot": "Next screenshot"
}, },
"activation": { "activation": {
"title": "Activate Hydra", "title": "Activate Hydra",

View file

@ -92,7 +92,9 @@
"dont_show_it_again": "Não mostrar novamente", "dont_show_it_again": "Não mostrar novamente",
"copy_to_clipboard": "Copiar", "copy_to_clipboard": "Copiar",
"copied_to_clipboard": "Copiado", "copied_to_clipboard": "Copiado",
"got_it": "Entendi" "got_it": "Entendi",
"previous_screenshot": "Captura de tela anterior",
"next_screenshot": "Próxima captura de tela"
}, },
"activation": { "activation": {
"title": "Ativação", "title": "Ativação",

View file

@ -24,6 +24,7 @@ export function Modal({
}: ModalProps) { }: ModalProps) {
const [isClosing, setIsClosing] = useState(false); const [isClosing, setIsClosing] = useState(false);
const modalContentRef = useRef<HTMLDivElement | null>(null); const modalContentRef = useRef<HTMLDivElement | null>(null);
const titleRef = useRef<HTMLHeadingElement | null>(null);
const { t } = useTranslation("modal"); const { t } = useTranslation("modal");
@ -52,6 +53,8 @@ export function Modal({
useEffect(() => { useEffect(() => {
if (visible) { if (visible) {
if (titleRef.current) titleRef.current.focus();
const onKeyDown = (e: KeyboardEvent) => { const onKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape" && isTopMostModal()) { if (e.key === "Escape" && isTopMostModal()) {
handleCloseClick(); handleCloseClick();
@ -94,8 +97,10 @@ export function Modal({
> >
<div className={styles.modalHeader}> <div className={styles.modalHeader}>
<div style={{ display: "flex", gap: 4, flexDirection: "column" }}> <div style={{ display: "flex", gap: 4, flexDirection: "column" }}>
<h3>{title}</h3> <h3 ref={titleRef} tabIndex={0}>
{description && <p>{description}</p>} {title}
</h3>
{description && <p tabIndex={0}>{description}</p>}
</div> </div>
<button <button

View file

@ -1,3 +1,4 @@
import { recipe } from "@vanilla-extract/recipes";
import { SPACING_UNIT, vars } from "../../theme.css"; import { SPACING_UNIT, vars } from "../../theme.css";
import { style } from "@vanilla-extract/css"; import { style } from "@vanilla-extract/css";
@ -13,7 +14,7 @@ export const gallerySliderMedia = style({
width: "100%", width: "100%",
height: "100%", height: "100%",
display: "block", display: "block",
flexShrink: 0, flexShrink: "0",
flexGrow: "0", flexGrow: "0",
transition: "translate 0.3s ease-in-out", transition: "translate 0.3s ease-in-out",
borderRadius: "4px", borderRadius: "4px",
@ -54,43 +55,70 @@ export const gallerySliderPreview = style({
}, },
}); });
export const gallerySliderMediaPreview = style({ export const gallerySliderMediaPreview = recipe({
cursor: "pointer", base: {
width: "20%", cursor: "pointer",
height: "20%", width: "20%",
display: "block", height: "20%",
flexShrink: 0, display: "block",
flexGrow: 0, flexShrink: "0",
opacity: 0.3, flexGrow: "0",
transition: "translate 0.3s ease-in-out, opacity 0.2s ease", opacity: "0.3",
borderRadius: "4px", transition: "translate 0.3s ease-in-out, opacity 0.2s ease",
border: `solid 1px ${vars.color.border}`, borderRadius: "4px",
":hover": { border: `solid 1px ${vars.color.border}`,
opacity: "1", ":hover": {
opacity: "0.8",
},
},
variants: {
active: {
true: {
opacity: "1",
},
},
}, },
}); });
export const gallerySliderMediaPreviewActive = style({ export const gallerySliderButton = recipe({
opacity: 1, base: {
}); position: "absolute",
alignSelf: "center",
export const gallerySliderButton = style({ cursor: "pointer",
all: "unset", backgroundColor: "rgba(0, 0, 0, 0.4)",
display: "block", transition: "all 0.2s ease-in-out",
position: "absolute", borderRadius: "50%",
height: "15%", color: vars.color.muted,
top: "50%", width: "48px",
transform: "translateY(-50%)", height: "48px",
padding: "1rem", ":hover": {
cursor: "pointer", backgroundColor: "rgba(0, 0, 0, 0.6)",
transition: "background-color 100ms ease-in-out", },
":hover": { ":active": {
backgroundColor: "rgb(0, 0, 0, 0.2)", transform: "scale(0.95)",
},
},
variants: {
direction: {
left: {
left: "0",
marginLeft: `${SPACING_UNIT}px`,
transform: `translateX(${-(48 + SPACING_UNIT)}px)`,
},
right: {
right: "0",
marginRight: `${SPACING_UNIT}px`,
transform: `translateX(${48 + SPACING_UNIT}px)`,
},
},
visible: {
true: {
transform: "translateX(0)",
opacity: "1",
},
false: {
opacity: "0",
},
},
}, },
}); });
export const gallerySliderIcons = style({
fill: vars.color.muted,
width: "2rem",
height: "2rem",
});

View file

@ -1,7 +1,10 @@
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
import { ShopDetails, SteamMovies, SteamScreenshot } from "@types";
import { ChevronRightIcon, ChevronLeftIcon } from "@primer/octicons-react"; import { ChevronRightIcon, ChevronLeftIcon } from "@primer/octicons-react";
import type { ShopDetails, SteamMovies, SteamScreenshot } from "@types";
import * as styles from "./gallery-slider.css"; import * as styles from "./gallery-slider.css";
import { useTranslation } from "react-i18next";
export interface GallerySliderProps { export interface GallerySliderProps {
gameDetails: ShopDetails | null; gameDetails: ShopDetails | null;
@ -12,6 +15,8 @@ export function GallerySlider({ gameDetails }: GallerySliderProps) {
const mediaContainerRef = useRef<HTMLDivElement>(null); const mediaContainerRef = useRef<HTMLDivElement>(null);
const currentVideoRef = useRef<HTMLVideoElement | null>(null); const currentVideoRef = useRef<HTMLVideoElement | null>(null);
const { t } = useTranslation("game_details");
const hasScreenshots = gameDetails && gameDetails.screenshots.length; const hasScreenshots = gameDetails && gameDetails.screenshots.length;
const hasMovies = gameDetails && gameDetails.movies?.length; const hasMovies = gameDetails && gameDetails.movies?.length;
@ -30,7 +35,7 @@ export function GallerySlider({ gameDetails }: GallerySliderProps) {
}); });
const [mediaIndex, setMediaIndex] = useState<number>(0); const [mediaIndex, setMediaIndex] = useState<number>(0);
const [arrowShow, setArrowShow] = useState(false); const [showArrows, setShowArrows] = useState(false);
const showNextImage = () => { const showNextImage = () => {
setMediaIndex((index: number) => { setMediaIndex((index: number) => {
@ -54,18 +59,18 @@ export function GallerySlider({ gameDetails }: GallerySliderProps) {
useEffect(() => { useEffect(() => {
if (currentVideoRef.current) { if (currentVideoRef.current) {
currentVideoRef.current.pause() currentVideoRef.current.pause();
} }
if (hasMovies && mediaContainerRef.current) { if (hasMovies && mediaContainerRef.current) {
mediaContainerRef.current.childNodes.forEach((node, index) => { mediaContainerRef.current.childNodes.forEach((node, index) => {
if (index == mediaIndex && node instanceof HTMLVideoElement) { if (index == mediaIndex && node instanceof HTMLVideoElement) {
node.play() node.play();
currentVideoRef.current = node currentVideoRef.current = node;
} }
}) });
} }
}, [hasMovies, mediaContainerRef, mediaIndex]) }, [hasMovies, mediaContainerRef, mediaIndex]);
useEffect(() => { useEffect(() => {
if (scrollContainerRef.current) { if (scrollContainerRef.current) {
@ -82,8 +87,8 @@ export function GallerySlider({ gameDetails }: GallerySliderProps) {
{hasScreenshots && ( {hasScreenshots && (
<div className={styles.gallerySliderContainer}> <div className={styles.gallerySliderContainer}>
<div <div
onMouseEnter={() => setArrowShow(true)} onMouseEnter={() => setShowArrows(true)}
onMouseLeave={() => setArrowShow(false)} onMouseLeave={() => setShowArrows(false)}
className={styles.gallerySliderAnimationContainer} className={styles.gallerySliderAnimationContainer}
ref={mediaContainerRef} ref={mediaContainerRef}
> >
@ -97,6 +102,7 @@ export function GallerySlider({ gameDetails }: GallerySliderProps) {
style={{ translate: `${-100 * mediaIndex}%` }} style={{ translate: `${-100 * mediaIndex}%` }}
loop loop
muted muted
tabIndex={-1}
> >
<source src={video.mp4.max.replace("http", "https")} /> <source src={video.mp4.max.replace("http", "https")} />
</video> </video>
@ -110,30 +116,36 @@ export function GallerySlider({ gameDetails }: GallerySliderProps) {
className={styles.gallerySliderMedia} className={styles.gallerySliderMedia}
src={image.path_full} src={image.path_full}
style={{ translate: `${-100 * mediaIndex}%` }} style={{ translate: `${-100 * mediaIndex}%` }}
tabIndex={-1}
/> />
) )
)} )}
{arrowShow && (
<>
<button
onClick={showPrevImage}
type="button"
className={styles.gallerySliderButton}
style={{ left: 0 }}
>
<ChevronLeftIcon className={styles.gallerySliderIcons} />
</button>
<button <button
onClick={showNextImage} onClick={showPrevImage}
type="button" type="button"
className={styles.gallerySliderButton} className={styles.gallerySliderButton({
style={{ right: 0 }} visible: showArrows,
> direction: "left",
<ChevronRightIcon className={styles.gallerySliderIcons} /> })}
</button> aria-label={t("previous_screenshot")}
</> tabIndex={0}
)} >
<ChevronLeftIcon size={36} />
</button>
<button
onClick={showNextImage}
type="button"
className={styles.gallerySliderButton({
visible: showArrows,
direction: "right",
})}
aria-label={t("next_screenshot")}
tabIndex={0}
>
<ChevronRightIcon size={36} />
</button>
</div> </div>
<div className={styles.gallerySliderPreview} ref={scrollContainerRef}> <div className={styles.gallerySliderPreview} ref={scrollContainerRef}>
@ -144,7 +156,9 @@ export function GallerySlider({ gameDetails }: GallerySliderProps) {
loading="lazy" loading="lazy"
onClick={() => setMediaIndex(i)} onClick={() => setMediaIndex(i)}
src={video.thumbnail} src={video.thumbnail}
className={`${styles.gallerySliderMediaPreview} ${mediaIndex === i ? styles.gallerySliderMediaPreviewActive : ""}`} className={styles.gallerySliderMediaPreview({
active: mediaIndex === i,
})}
/> />
))} ))}
{gameDetails.screenshots && {gameDetails.screenshots &&
@ -158,7 +172,12 @@ export function GallerySlider({ gameDetails }: GallerySliderProps) {
i + (gameDetails.movies ? gameDetails.movies.length : 0) i + (gameDetails.movies ? gameDetails.movies.length : 0)
) )
} }
className={`${styles.gallerySliderMediaPreview} ${mediaIndex === i + (gameDetails.movies ? gameDetails.movies.length : 0) ? styles.gallerySliderMediaPreviewActive : ""}`} className={styles.gallerySliderMediaPreview({
active:
mediaIndex ===
i +
(gameDetails.movies ? gameDetails.movies.length : 0),
})}
src={image.path_full} src={image.path_full}
/> />
) )