Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add loader to images #2141

Merged
merged 1 commit into from
Nov 9, 2024
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
65 changes: 59 additions & 6 deletions apps/passport-client/new-components/shared/TicketCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Property } from "csstype";
import { forwardRef } from "react";
import styled from "styled-components";
import { forwardRef, useState } from "react";
import styled, { keyframes } from "styled-components";
import { Typography } from "./Typography";

export const TicketCardHeight = 300;
Expand Down Expand Up @@ -33,15 +33,56 @@ const TicketCardContainer = styled.div<{
0px 4px 6px -1px rgba(0, 0, 0, 0.1);
`;

const TicketCardImage = styled.div<{ src?: string }>`
${({ src }): string | undefined =>
src ? `background: url(${src});` : undefined}
const TicketCardImage = styled.img<{ src?: string }>`
background-size: cover;
background-position: 50% 50%;
width: 100%;
height: 100%;
`;

const shimmer = keyframes`
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(100%);
}
`;

const LoaderContainer = styled.div`
height: 100%;
display: flex;
justify-content: center;
align-items: center;

/* Skeleton styling */
.skeleton {
width: 100%;
height: 100%;
background-color: #e0e0e0;
position: relative;
overflow: hidden;
border-radius: 8px;

&::before {
content: "";
display: block;
position: absolute;
top: 0;
left: 0;
width: 200%;
height: 100%;
background-image: linear-gradient(
90deg,
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 0.4) 50%,
/* Increased opacity for more contrast */ rgba(255, 255, 255, 0) 100%
);
animation: ${shimmer} 2s infinite linear;
}
}
`;

const TicketCardImageContainer = styled.div`
position: relative;
width: 100%;
Expand Down Expand Up @@ -72,14 +113,26 @@ export const TicketCard = forwardRef<HTMLDivElement, TicketCardProps>(
{ imgSource, title, address, ticketCount, cardColor, ticketWidth },
ref
): JSX.Element => {
const [imageLoading, setImageLoading] = useState(true);
return (
<TicketCardContainer
$width={ticketWidth || 300}
ref={ref}
$borderColor={CARD_COLORS[cardColor]}
>
<TicketCardImageContainer>
<TicketCardImage src={imgSource} />
{imageLoading && (
<LoaderContainer>
<div className="skeleton"></div>
</LoaderContainer>
)}
<TicketCardImage
style={{ opacity: imageLoading ? 0 : 1 }}
onLoad={() => {
setImageLoading(false);
}}
src={imgSource}
/>
</TicketCardImageContainer>
<TicketCardDetails>
<Typography fontSize={18} fontWeight={800}>
Expand Down
Loading