style: update hero-carousel styling

This commit is contained in:
Prayag Prajapati committed 2024-06-08 17:52:05 +05:30
1 parent badc2b622f
commit 326b093210
27 files changed
+750 -382

No files matched your search

+171
View File
@@ -0,0 +1,171 @@
import useDebounce from "@/utils/hooks/useDebounce";
import React, {
type ReactNode,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import { useSpring } from "@react-spring/web";
import { debounce } from "lodash";
import "./style.scss";
import { Button, IconButton } from "@mui/material";
enum Direction {
RIGHT = 0,
LEFT = 1,
}
const Slider = ({
content,
currentSlide,
}: { content: ReactNode[]; currentSlide: number }) => {
const containerRef = useRef<HTMLDivElement>(null);
const [scrollPos, setScrollPos] = useState({ isStart: true, isEnd: false });
const handleScroll = useCallback(() => {
const scrollWidth = containerRef.current?.scrollWidth ?? 0;
const clientWidth =
containerRef.current?.getBoundingClientRect().width ?? 0;
const scrolledPostion = containerRef.current?.scrollLeft ?? 0;
if (content.length === 0) {
setScrollPos({ isStart: true, isEnd: true });
} else if (clientWidth >= scrollWidth) {
setScrollPos({ isStart: true, isEnd: true });
} else if (
scrolledPostion >=
(containerRef.current?.scrollWidth ?? 0) - clientWidth
) {
setScrollPos({ isStart: false, isEnd: true });
} else if (scrolledPostion > 0) {
setScrollPos({ isStart: false, isEnd: false });
} else {
setScrollPos({ isStart: true, isEnd: false });
}
}, [content]);
const debouncedScroll = useCallback(
() => debounce(handleScroll, 50),
[handleScroll],
);
useEffect(() => {
const handleResize = () => {
debouncedScroll();
};
window.addEventListener("resize", handleResize, { passive: true });
return () => window.removeEventListener("resize", handleResize);
}, [debouncedScroll]);
useEffect(() => {
handleScroll();
}, [handleScroll, content]);
const onScroll = () => {
debouncedScroll();
};
const [, setX] = useSpring(() => ({
from: { x: 0 },
to: { x: 0 },
onChange: (results) => {
if (containerRef.current) {
containerRef.current.scrollLeft = results.value.x;
}
},
}));
const slide = (direction: Direction) => {
const clientWidth =
containerRef.current?.getBoundingClientRect().width ?? 0;
const cardWidth =
containerRef.current?.firstElementChild?.getBoundingClientRect().width ??
0;
const scrollPosition = containerRef.current?.scrollLeft ?? 0;
const visibleItems = Math.floor(clientWidth / cardWidth);
const scrollOffset = scrollPosition % cardWidth;
if (direction === Direction.LEFT) {
const newX = Math.max(
scrollPosition - scrollOffset - visibleItems * cardWidth,
0,
);
setX.start({
from: { x: scrollPosition },
to: { x: newX },
onChange: (results) => {
if (containerRef.current) {
containerRef.current.scrollLeft = results.value.x;
}
},
reset: true,
config: { friction: 60, tension: 1000, velocity: 2 },
});
if (newX === 0) {
setScrollPos({ isStart: true, isEnd: false });
} else {
setScrollPos({ isStart: false, isEnd: false });
}
} else if (direction === Direction.RIGHT) {
const newX = Math.min(
scrollPosition - scrollOffset + visibleItems * cardWidth,
containerRef.current?.scrollWidth ?? 0 - clientWidth,
);
setX.start({
from: { x: scrollPosition },
to: { x: newX },
onChange: (results) => {
if (containerRef.current) {
containerRef.current.scrollLeft = results.value.x;
}
},
reset: true,
config: { friction: 60, tension: 500, velocity: 2 },
});
if (newX >= (containerRef.current?.scrollWidth ?? 0) - clientWidth) {
setScrollPos({ isStart: false, isEnd: true });
} else {
setScrollPos({ isStart: false, isEnd: false });
}
}
};
return (
<div className="slider">
<IconButton
className="slider-button left"
onClick={() => slide(Direction.LEFT)}
disabled={scrollPos.isStart}
variant="contained"
>
<span className="material-symbols-rounded">chevron_left</span>
</IconButton>
<div className="slider-track" ref={containerRef} onScroll={onScroll}>
{content?.map((item, index) => (
<div
key={index}
className={
currentSlide === index
? "slider-track-item active"
: "slider-track-item"
}
>
{item}
</div>
))}
</div>
<IconButton
className="slider-button right"
onClick={() => slide(Direction.RIGHT)}
disabled={scrollPos.isEnd}
variant="contained"
>
<span className="material-symbols-rounded">chevron_right</span>
</IconButton>
</div>
);
};
export default Slider;
+31
View File
@@ -0,0 +1,31 @@
.slider {
position: relative;
&-track {
overflow-y: hidden;
overflow-x: auto;
white-space: nowrap;
position: relative;
overscroll-behavior-x: contain;
margin: 0.5em 1em;
&-item {
display: inline-block;
// align-self: top;
padding: 0.5em 0;
}
}
&-button {
position: absolute !important;
bottom: 1.3em;
&.left {
left: -1.2em;
}
&.right {
right: -1.2em;
}
}
}
+1
View File
@@ -103,6 +103,7 @@ export const AppBar = () => {
return usr.data;
},
enabled: display,
throwOnError: true,
});
const libraries = useQuery({
queryKey: ["libraries"],
+11 -11
View File
@@ -11,6 +11,7 @@ import Typography from "@mui/material/Typography";
import {
type BaseItemDto,
BaseItemKind,
type ImageType,
} from "@jellyfin/sdk/lib/generated-client";
import LikeButton from "../buttons/likeButton";
import MarkPlayedButton from "../buttons/markPlayedButton";
@@ -49,22 +50,21 @@ export const Card = ({
overrideIcon,
}: {
item: BaseItemDto;
cardTitle: string | null;
cardCaption: string | null;
imageType: string;
cardTitle: string | undefined | null;
cardCaption?: string | null | number;
imageType: ImageType;
cardType: string;
queryKey: [];
userId: string;
seriesId: string | null;
hideText: boolean;
onClick: () => null | null;
disableOverlay: boolean;
overrideIcon: any;
queryKey?: string[];
userId?: string;
seriesId?: string | null;
hideText?: boolean;
onClick?: () => void;
disableOverlay?: boolean;
overrideIcon?: any;
}) => {
const api = useApiInContext((s) => s.api);
const navigate = useNavigate();
const defaultOnClick = () => {
switch (item.Type) {
case BaseItemKind.BoxSet:
navigate({ to: "/boxset/$id", params: { id: item.Id } });
+2 -1
View File
@@ -10,6 +10,7 @@ import { Blurhash } from "react-blurhash";
import "./card.scss";
import { getRuntimeCompact } from "@/utils/date/time";
import { useApiInContext } from "@/utils/store/api";
import { useNavigate, useRouteContext } from "@tanstack/react-router";
import LikeButton from "../buttons/likeButton";
import MarkPlayedButton from "../buttons/markPlayedButton";
@@ -43,7 +44,7 @@ export const EpisodeCard = ({
disableRunTime = false,
onClick,
}) => {
const api = useRouteContext({ from: "/" }).api;
const api = useApiInContext((s) => s.api);
const navigate = useNavigate();
const defaultOnClick = () => {
navigate({ to: `/episode/${item.Id}` });
+48 -21
View File
@@ -1,45 +1,72 @@
.carousel {
position: relative;
height: 70vh;
height: 64vh;
width: calc(100% + 2*$page-margin);
left: -$page-margin;
margin-bottom: 3.2em;
.slider {
position: absolute;
bottom: 0;
max-width: 35%;
right: $page-margin;
}
&-button {
position: absolute !important;
top: 50%;
transform: translateY(-50%);
z-index: 100;
&.right {
left: calc(100% + 0.5em);
right: 0.5em;
}
&.left {
right: calc(100% + 0.5em);
left: 0.5em;
}
}
&-indicator {
height: 3px;
width: 100%;
border-radius: 10px;
transition: background $transition-time-fast,
height $transition-time-fast;
background: rgb(255 255 255 / 0.5);
aspect-ratio: 1.77777;
overflow: hidden;
border-radius: 2px;
padding: 2px;
transition: opacity $transition-time-fast;
opacity: 0.5;
width: auto;
&:hover {
cursor: pointer;
height: 6px;
background: rgb(255 255 255 / 0.8) !important;
opacity: 0.8;
}
&-container {
display: flex;
position: absolute;
bottom: -1.5em;
left: 10px;
right: 10px;
gap: 1em;
align-items: center;
justify-content: center;
}
&.active {
height: 6px;
background: white !important;
}
}
&-image {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 4px;
}
&-icon {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
background: rgb(155 155 155 / 0.4);
border-radius: 4px;
}
&.active {
opacity: 1;
.material-symbols-rounded {
--fill: 1;
}
}
}
}
@@ -5,46 +5,60 @@ import IconButton from "@mui/material/IconButton";
import { useCarouselStore } from "../../utils/store/carousel";
import "./carousel.scss";
import { useApiInContext } from "@/utils/store/api";
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
import CarouselSlide from "../carouselSlide";
import ReactMultiCarousel from "react-multi-carousel";
import Slider from "../Slider";
import { getTypeIcon } from "../utils/iconsCollection";
const swipeConfidenceThreshold = 8000;
const swipePower = (offset, velocity) => {
return Math.abs(offset) * velocity;
};
const Carousel = ({ content, onChange }) => {
const Carousel = ({
content,
onChange,
}: {
content: BaseItemDto[];
onChange: () => void;
}) => {
const [currentSlide, setCurrentSlide] = useState(0);
const [setDirection, dir] = useCarouselStore((state) => [
state.setDirection,
state.direction,
]);
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
useEffect(() => {
onChange(currentSlide);
}, [content[currentSlide]?.key]);
}, [content[currentSlide]?.Id]);
const api = useApiInContext((s) => s.api);
const responsive = {
superLargeDesktop: {
// the naming can be any, depends on you.
breakpoint: { max: 4000, min: 3000 },
items: 6,
},
desktop: {
breakpoint: { max: 3000, min: 1024 },
items: 6,
},
tablet: {
breakpoint: { max: 1024, min: 464 },
items: 4,
},
mobile: {
breakpoint: { max: 464, min: 0 },
items: 1,
},
};
return (
<div className="carousel">
<IconButton
className="carousel-button left"
onClick={() => {
if (currentSlide === 0) {
setDirection("left");
setCurrentSlide(content.length - 1);
}
if (currentSlide > 0) {
setDirection("left");
setCurrentSlide((init) => init - 1);
}
}}
sx={{
background: " rgb(200 200 200 / 0.05)",
}}
// disabled={currentSlide == 0}
>
<div className="material-symbols-rounded">arrow_left</div>
</IconButton>
<AnimatePresence mode="sync">
<motion.div
key={currentSlide}
@@ -77,35 +91,31 @@ const Carousel = ({ content, onChange }) => {
position: "absolute",
}}
>
{content[currentSlide]}
<CarouselSlide
item={content[currentSlide]}
key={content[currentSlide].Id}
/>
</motion.div>
</AnimatePresence>
<IconButton
className="carousel-button right"
onClick={() => {
if (currentSlide === content.length - 1) {
setDirection("right");
setCurrentSlide(0);
} else if (currentSlide < content.length - 1) {
setDirection("right");
setCurrentSlide((init) => init + 1);
}
}}
// disabled={currentSlide == content.length - 1}
sx={{
background: " rgb(200 200 200 / 0.05)",
}}
>
<div className="material-symbols-rounded">arrow_right</div>
</IconButton>
<div className="carousel-indicator-container">
{content.map((item, index) => (
<div
<Slider
currentSlide={currentSlide}
content={content.map((item, index) => (
<motion.div
className={
currentSlide === index
? "carousel-indicator active"
: "carousel-indicator"
}
layout
initial={{
height: "4.6em",
}}
whileHover={{
height: "5.5em",
}}
transition={{
duration: 0.05,
}}
key={item.Id}
onClick={() => {
if (currentSlide > index) {
@@ -115,9 +125,24 @@ const Carousel = ({ content, onChange }) => {
}
setCurrentSlide(index);
}}
/>
>
{item.ImageTags?.Primary ? (
<img
src={api.getItemImageUrl(item.Id, "Thumb", {
tag: item.ImageTags.Primary,
fillWidth: 300,
})}
alt={item.Name ?? "item-image"}
className="carousel-indicator-image"
/>
) : (
<div className="carousel-indicator-icon">
{getTypeIcon(item.Type)}
</div>
)}
</motion.div>
))}
</div>
/>
</div>
);
};
+77 -75
View File
@@ -14,12 +14,15 @@ import { yellow } from "@mui/material/colors";
import { endsAt, getRuntime } from "@/utils/date/time";
import { green, red } from "@mui/material/colors";
import { useQuery } from "@tanstack/react-query";
import { useNavigate, useRouteContext } from "@tanstack/react-router";
import { useNavigate } from "@tanstack/react-router";
import { getTypeIcon } from "../utils/iconsCollection";
import { useApiInContext } from "@/utils/store/api";
import { useCarouselStore } from "@/utils/store/carousel";
import { BaseItemKind } from "@jellyfin/sdk/lib/generated-client";
import {
type BaseItemDto,
BaseItemKind,
} from "@jellyfin/sdk/lib/generated-client";
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
import LikeButton from "../buttons/likeButton";
import MarkPlayedButton from "../buttons/markPlayedButton";
@@ -41,7 +44,7 @@ const availableSpecialRoutes = [
* @param {Props}
* @returns {React.Component}
*/
const CarouselSlide = ({ item }) => {
const CarouselSlide = ({ item }: { item: BaseItemDto }) => {
const api = useApiInContext((s) => s.api);
const navigate = useNavigate();
@@ -55,19 +58,31 @@ const CarouselSlide = ({ item }) => {
});
const handleMoreInfo = () => {
if (availableSpecialRoutes.includes(item.Type)) {
navigate({
to: `/${item.Type.toLocaleLowerCase()}/$id`,
params: { id: item.Id },
});
} else if (!!item.Role || item.Type === BaseItemKind.Person) {
navigate({ to: "/person/$id", params: { id: item.Id } });
} else if (item.Type === BaseItemKind.MusicArtist) {
navigate({ to: "/artist/$id", params: { id: item.Id } });
} else if (item.Type === BaseItemKind.Episode) {
navigate({ to: "/episode/$id", params: { id: item.Id } });
} else {
navigate({ to: "/item/$id", params: { id: item.Id } });
switch (item.Type) {
case BaseItemKind.BoxSet:
navigate({ to: "/boxset/$id", params: { id: item.Id } });
break;
case BaseItemKind.Episode:
navigate({ to: "/episode/$id", params: { id: item.Id } });
break;
case BaseItemKind.MusicAlbum:
navigate({ to: "/album/$id", params: { id: item.Id } });
break;
case BaseItemKind.MusicArtist:
navigate({ to: "/artist/$id", params: { id: item.Id } });
break;
case BaseItemKind.Person:
navigate({ to: "/person/$id", params: { id: item.Id } });
break;
case BaseItemKind.Series:
navigate({ to: "/series/$id", params: { id: item.Id } });
break;
case BaseItemKind.Playlist:
navigate({ to: "/playlist/$id", params: { id: item.Id } });
break;
default:
navigate({ to: "/item/$id", params: { id: item.Id } });
break;
}
};
@@ -78,50 +93,33 @@ const CarouselSlide = ({ item }) => {
className="hero-carousel-slide"
sx={{
background: "transparent",
px: 3,
// px: 3,
}}
elevation={10}
elevation={0}
>
<div className="hero-carousel-background-container">
{!!item.ImageBlurHashes.Backdrop && (
<>
{Object.keys(item.ImageBlurHashes.Backdrop).length !== 0 && (
<Blurhash
hash={
item.ImageBlurHashes.Backdrop[
Object.keys(item.ImageBlurHashes.Backdrop)[0]
]
}
// hash="LEHV6nWB2yk8pyo0adR*.7kCMdnj"
width="256"
height="179.2"
resolutionX={64}
resolutionY={45}
className="hero-carousel-background-blurhash"
punch={1.4}
/>
)}
<img
alt={item.Name}
className="hero-carousel-background-image"
src={
item.ParentBackdropItemId
? `${api.basePath}/Items/${item.ParentBackdropItemId}/Images/Backdrop?quality=80`
: `${api.basePath}/Items/${item.Id}/Images/Backdrop?quality=80&fillHeight=1400`
}
style={{
opacity: 0,
}}
onLoad={(e) => {
e.target.style.opacity = 1;
}}
loading="eager"
/>
</>
{item.BackdropImageTags?.length ? (
<img
alt={item.Name}
className="hero-carousel-background-image"
src={
item.ParentBackdropItemId
? `${api.basePath}/Items/${item.ParentBackdropItemId}/Images/Backdrop?quality=80`
: `${api.basePath}/Items/${item.Id}/Images/Backdrop?quality=80&fillHeight=1400`
}
style={{
opacity: 0,
}}
onLoad={(e) => {
e.target.style.opacity = 1;
}}
loading="eager"
/>
) : (
<div className="hero-carousel-background-icon-container">
{getTypeIcon(item.Type)}
</div>
)}
<div className="hero-carousel-background-icon-container">
{getTypeIcon(item.Type)}
</div>
</div>
<div className="hero-carousel-detail">
<Typography
@@ -207,10 +205,14 @@ const CarouselSlide = ({ item }) => {
justifyItems="flex-start"
alignItems="center"
>
<Typography style={{ opacity: "0.8" }} variant="subtitle1">
{item.ProductionYear ? item.ProductionYear : "Unknown"}
</Typography>
<Chip variant="filled" label={item.OfficialRating ?? "Not Rated"} />
{item.PremiereDate && (
<Typography style={{ opacity: "0.8" }} variant="subtitle2">
{item.ProductionYear ?? ""}
</Typography>
)}
{item.OfficialRating && (
<Chip variant="filled" size="small" label={item.OfficialRating} />
)}
{item.CommunityRating && (
<div
@@ -222,12 +224,10 @@ const CarouselSlide = ({ item }) => {
className="hero-carousel-info-rating"
>
<div
className="material-symbols-rounded "
className="material-symbols-rounded fill"
style={{
// fontSize: "2.2em",
color: yellow[400],
fontVariationSettings:
'"FILL" 1, "wght" 300, "GRAD" 25, "opsz" 40',
}}
>
star
@@ -236,13 +236,13 @@ const CarouselSlide = ({ item }) => {
style={{
opacity: "0.8",
}}
variant="subtitle1"
variant="subtitle2"
>
{Math.round(item.CommunityRating * 10) / 10}
</Typography>
</div>
)}
{Boolean(item.CriticRating) && (
{item.CriticRating && (
<div
style={{
display: "flex",
@@ -252,11 +252,9 @@ const CarouselSlide = ({ item }) => {
className="hero-carousel-info-rating"
>
<div
className="material-symbols-rounded "
className="material-symbols-rounded fill"
style={{
color: item.CriticRating > 50 ? green[400] : red[400],
fontVariationSettings:
'"FILL" 1, "wght" 300, "GRAD" 25, "opsz" 40',
}}
>
{item.CriticRating > 50 ? "thumb_up" : "thumb_down"}
@@ -265,23 +263,26 @@ const CarouselSlide = ({ item }) => {
style={{
opacity: "0.8",
}}
variant="subtitle1"
variant="subtitle2"
>
{item.CriticRating}
</Typography>
</div>
)}
{!!item.RunTimeTicks && (
<Typography style={{ opacity: "0.8" }} variant="subtitle1">
{item.RunTimeTicks && (
<Typography style={{ opacity: "0.8" }} variant="subtitle2">
{getRuntime(item.RunTimeTicks)}
</Typography>
)}
{!!item.RunTimeTicks && (
<Typography style={{ opacity: "0.8" }} variant="subtitle1">
{endsAt(item.RunTimeTicks)}
{item.RunTimeTicks && (
<Typography style={{ opacity: "0.8" }} variant="subtitle2">
{endsAt(item.RunTimeTicks - item.UserData.PlaybackPositionTicks)}
</Typography>
)}
<Typography variant="subtitle2" style={{ opacity: 0.8 }}>
{item.Genres?.slice(0, 4).join(" / ")}
</Typography>
</Stack>
<Typography
component={motion.div}
@@ -308,7 +309,6 @@ const CarouselSlide = ({ item }) => {
duration: 0.25,
ease: "easeInOut",
}}
variant="subtitle1"
className="hero-carousel-text"
sx={{
display: "-webkit-box",
@@ -319,6 +319,8 @@ const CarouselSlide = ({ item }) => {
WebkitLineClamp: "4",
WebkitBoxOrient: "vertical",
}}
variant="subtitle2"
fontWeight={300}
>
{item.Overview}
</Typography>
@@ -5,15 +5,19 @@ import { CardScroller } from "../../cardScroller/cardScroller";
import { CardsSkeleton } from "../../skeleton/cards";
import { useApiInContext } from "@/utils/store/api";
import { BaseItemKind } from "@jellyfin/sdk/lib/generated-client";
import {
type BaseItemDto,
BaseItemKind,
} from "@jellyfin/sdk/lib/generated-client";
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
import { useRouteContext } from "@tanstack/react-router";
/**
* @description Latest Media Section
*/
export const LatestMediaSection = ({ latestMediaLib }) => {
export const LatestMediaSection = ({
latestMediaLib,
}: { latestMediaLib: BaseItemDto }) => {
const api = useApiInContext((s) => s.api);
const user = useQuery({
queryKey: ["user"],
@@ -23,27 +27,27 @@ export const LatestMediaSection = ({ latestMediaLib }) => {
},
networkMode: "always",
});
const fetchLatestMedia = async (library) => {
const fetchLatestMedia = async (library: BaseItemDto) => {
const media = await getUserLibraryApi(api).getLatestMedia({
userId: user.data.Id,
parentId: library,
userId: user.data?.Id,
parentId: library.Id,
limit: 16,
fields: ["PrimaryImageAspectRatio"],
});
return media.data;
};
const data = useQuery({
queryKey: ["homeSection, latestMedia", latestMediaLib],
queryFn: () => fetchLatestMedia(latestMediaLib[0]),
queryKey: ["home", "latestMedia", latestMediaLib.Id],
queryFn: () => fetchLatestMedia(latestMediaLib),
enabled: !!user.data,
refetchOnMount: true
refetchOnMount: true,
});
if (data.isPending) {
return <CardsSkeleton />;
}
if (data.isSuccess && data.data.length >= 1) {
return (
<CardScroller displayCards={7} title={`Latest ${latestMediaLib[1]}`}>
<CardScroller displayCards={7} title={`Latest ${latestMediaLib.Name}`}>
{data.data.map((item) => {
return (
<Card
@@ -75,8 +79,8 @@ export const LatestMediaSection = ({ latestMediaLib }) => {
? "square"
: "portrait"
}
queryKey={["homeSection, latestMedia", latestMediaLib]}
userId={user.data.Id}
queryKey={["home", "latestMedia", latestMediaLib.Id]}
userId={user.data?.Id}
imageBlurhash={
!!item.ImageBlurHashes?.Primary &&
item.ImageBlurHashes?.Primary[
+4 -2
View File
@@ -28,7 +28,7 @@ import React, { useEffect, useState } from "react";
import logo from "@/assets/logo.png";
import { createApi, jellyfin, useApiInContext } from "@/utils/store/api";
import { jellyfin, useApiInContext } from "@/utils/store/api";
import { useCentralStore } from "@/utils/store/central";
import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
@@ -64,7 +64,9 @@ const Settings = () => {
state.dialogOpen,
state.tabValue,
]);
const api = useApiInContext((s) => s.api);
const api = useApiInContext((s) => s.api);
const createApi = useApiInContext((s) => s.createApi);
const systemInfo = useQuery({
queryKey: ["about", "systemInfo"],