1"use client";
2
3import {
4 type ComponentProps,
5 createContext,
6 type HTMLAttributes,
7 type ReactNode,
8 useContext,
9 useEffect,
10 useState,
11} from "react";
12import { PaymentIcon } from "react-svg-credit-card-payment-icons";
13import { cn } from "@/lib/utils";
14
15const useSupportsHover = () => {
16 const [supportsHover, setSupportsHover] = useState(false);
17
18 useEffect(() => {
19 const mql = window.matchMedia("(hover: hover)");
20 const handler = (e: MediaQueryListEvent) => setSupportsHover(e.matches);
21
22 setSupportsHover(mql.matches);
23 mql.addEventListener("change", handler);
24
25 return () => mql.removeEventListener("change", handler);
26 }, []);
27
28 return supportsHover;
29};
30
31export type CreditCardProps = HTMLAttributes<HTMLDivElement>;
32
33export const CreditCard = ({ className, ...props }: CreditCardProps) => (
34 <div
35 className={cn(
36 "group/kibo-credit-card perspective-distant aspect-[8560/5398] w-full max-w-96 text-white",
37 "@container",
38 className
39 )}
40 {...props}
41 />
42);
43
44const CreditCardFlipContext = createContext(false);
45
46export type CreditCardFlipperProps = HTMLAttributes<HTMLDivElement>;
47
48export const CreditCardFlipper = ({
49 className,
50 children,
51 ...props
52}: CreditCardFlipperProps & { children?: ReactNode }) => {
53 const supportsHover = useSupportsHover();
54 const [isFlipped, setIsFlipped] = useState(false);
55
56 const handleClick = () => {
57 if (!supportsHover) {
58 setIsFlipped((prev) => !prev);
59 }
60 };
61
62 return (
63 <CreditCardFlipContext.Provider value={true}>
64 {/* biome-ignore lint/nursery/noStaticElementInteractions: tap to flip for touch devices */}
65 <div
66 aria-label="Flip credit card"
67 className={cn(
68 "h-full w-full",
69 "@xs:rounded-2xl rounded-lg",
70 "transform-3d transition duration-700 ease-in-out",
71 supportsHover &&
72 "group-hover/kibo-credit-card:-rotate-y-180 group-hover/kibo-credit-card:shadow-lg",
73 !supportsHover && isFlipped && "-rotate-y-180 shadow-lg",
74 className
75 )}
76 onClick={handleClick}
77 {...props}
78 >
79 {children}
80 </div>
81 </CreditCardFlipContext.Provider>
82 );
83};
84
85export type CreditCardNameProps = HTMLAttributes<HTMLParagraphElement>;
86
87export const CreditCardName = ({
88 className,
89 style,
90 ...props
91}: CreditCardNameProps) => (
92 <p
93 className={cn("font-semibold uppercase", className)}
94 style={{
95 lineHeight: "100%",
96 ...style,
97 }}
98 {...props}
99 />
100);
101
102export type CreditCardChipProps = HTMLAttributes<SVGSVGElement>;
103
104export const CreditCardChip = ({
105 className,
106 children,
107 ...props
108}: CreditCardChipProps) =>
109 children ? (
110 <div
111 className={cn(
112 "-translate-y-1/2 absolute top-1/2 left-0 w-1/6 shrink-0 rounded-[18%]",
113 className
114 )}
115 >
116 {children}
117 </div>
118 ) : (
119 <svg
120 className={cn(
121 "-translate-y-1/2 absolute top-1/2 left-0 w-1/6 shrink-0 rounded-[18%]",
122 className
123 )}
124 enableBackground="new 0 0 110 92"
125 viewBox="0 0 110 92"
126 xmlns="http://www.w3.org/2000/svg"
127 {...props}
128 >
129 <title>Chip</title>
130 <path
131 d="M1 13A12 12 0 0 1 13 1h84a12 12 0 0 1 12 12v66a12 12 0 0 1-12 12H13A12 12 0 0 1 1 79V13Z"
132 fill="url(#kibo-credit-card-chip-gradient)"
133 />
134 <path
135 d="M108 71.5H83.65L70.53 60.87A21.41 21.41 0 0 1 56 67.47V90h41a11 11 0 0 0 11-11v-7.5ZM76.48 47a21.38 21.38 0 0 1-4.63 12.36l12.5 10.14H108V47H76.48ZM2 69.5h24.14l12.02-10.12A21.38 21.38 0 0 1 33.52 47H2v22.5Zm53-43c-5.85 0-11.1 2.57-14.68 6.66A19.4 19.4 0 0 0 35.5 46a19.4 19.4 0 0 0 4.82 12.84A19.43 19.43 0 0 0 55 65.5c5.85 0 11.1-2.57 14.68-6.66A19.4 19.4 0 0 0 74.5 46a19.4 19.4 0 0 0-4.82-12.84A19.43 19.43 0 0 0 55 26.5Zm16.85 6.14A21.38 21.38 0 0 1 76.48 45H108V22.5H84.35l-12.5 10.14ZM2 45h31.52a21.38 21.38 0 0 1 4.64-12.38L26.14 22.5H2V45Zm0 34a11 11 0 0 0 11 11h41V67.47a21.41 21.41 0 0 1-14.52-6.59L27.14 71.26l-.27.24H2V79Zm106-66A11 11 0 0 0 97 2H56v22.52c5.7.27 10.83 2.74 14.53 6.61L83.65 20.5H108V13ZM2 20.5h24.87l.27.24 12.34 10.38A21.41 21.41 0 0 1 54 24.52V2H13A11 11 0 0 0 2 13v7.5ZM110 79a13 13 0 0 1-13 13H13A13 13 0 0 1 0 79V13A13 13 0 0 1 13 0h84a13 13 0 0 1 13 13v66Z"
136 fill="#000"
137 />
138 <defs>
139 <linearGradient
140 gradientUnits="userSpaceOnUse"
141 id="kibo-credit-card-chip-gradient"
142 x1="1"
143 x2="112.7"
144 y1="46"
145 y2="78.12"
146 >
147 <stop stopColor="#EDE5A6" />
148 <stop offset="1" stopColor="#CFA255" />
149 </linearGradient>
150 </defs>
151 </svg>
152 );
153
154export type CreditCardLogoProps = HTMLAttributes<HTMLDivElement>;
155
156export const CreditCardLogo = ({
157 className,
158 ...props
159}: CreditCardLogoProps) => (
160 <div
161 className={cn("absolute top-0 right-0 size-1/6", className)}
162 {...props}
163 />
164);
165
166export type CreditCardFrontProps = HTMLAttributes<HTMLDivElement> & {
167 safeArea?: number;
168};
169
170export const CreditCardFront = ({
171 className,
172 safeArea = 20,
173 children,
174 ...props
175}: CreditCardFrontProps) => (
176 <div
177 className={cn(
178 "backface-hidden absolute inset-0 flex overflow-hidden bg-foreground/90",
179 "@xs:rounded-2xl rounded-lg",
180 className
181 )}
182 {...props}
183 >
184 <div
185 className="relative flex-1"
186 style={{
187 margin: `${safeArea}px`,
188 }}
189 >
190 {children}
191 </div>
192 </div>
193);
194
195export type CreditCardServiceProviderProps = ComponentProps<typeof PaymentIcon>;
196
197export const CreditCardServiceProvider = ({
198 className,
199 children,
200 type = "Visa",
201 ...props
202}: CreditCardServiceProviderProps) => {
203 if (children) {
204 return (
205 <div
206 className={cn(
207 "absolute right-0 bottom-0",
208 "max-h-1/3 max-w-1/3",
209 className
210 )}
211 >
212 {children}
213 </div>
214 );
215 }
216
217 return (
218 <PaymentIcon
219 className={cn(
220 "absolute right-0 bottom-0",
221 "max-h-1/3 max-w-1/3",
222 className
223 )}
224 type={type}
225 {...props}
226 />
227 );
228};
229
230export type CreditCardMagStripeProps = HTMLAttributes<HTMLDivElement>;
231
232export type CreditCardBackContextValue = {
233 safeArea: number;
234};
235
236const CreditCardBackContext = createContext<CreditCardBackContextValue>({
237 safeArea: 20,
238});
239
240export type CreditCardBackProps = HTMLAttributes<HTMLDivElement> & {
241 safeArea?: number;
242};
243
244export const CreditCardBack = ({
245 safeArea = 16,
246 children,
247 className,
248 ...props
249}: CreditCardBackProps) => {
250 const isInsideFlipper = useContext(CreditCardFlipContext);
251
252 return (
253 <CreditCardBackContext.Provider value={{ safeArea }}>
254 <div
255 className={cn(
256 "backface-hidden absolute inset-0 flex overflow-hidden bg-foreground/90",
257 "@xs:rounded-2xl rounded-lg",
258 isInsideFlipper && "rotate-y-180",
259 className
260 )}
261 {...props}
262 >
263 <div
264 className="relative flex-1"
265 style={{
266 margin: `${safeArea}px`,
267 }}
268 >
269 {children}
270 </div>
271 </div>
272 </CreditCardBackContext.Provider>
273 );
274};
275
276export const CreditCardMagStripe = ({
277 className,
278 ...props
279}: CreditCardMagStripeProps) => {
280 const context = useContext(CreditCardBackContext);
281
282 return (
283 <div
284 className={cn(
285 "-translate-x-1/2 absolute top-[3%] left-1/2 h-1/4 bg-gray-900",
286 className
287 )}
288 style={{
289 width: `calc(100% + 2 * ${context.safeArea}px)`,
290 }}
291 {...props}
292 />
293 );
294};
295
296export type CreditCardNumberProps = HTMLAttributes<HTMLParagraphElement>;
297
298export const CreditCardNumber = ({
299 className,
300 children,
301 style,
302 ...props
303}: CreditCardNumberProps) => (
304 <p
305 className={cn("font-mono", "@xs:text-2xl", className)}
306 style={{
307 lineHeight: "100%",
308 ...style,
309 }}
310 {...props}
311 >
312 {children}
313 </p>
314);
315
316export type CreditCardExpiryProps = HTMLAttributes<HTMLParagraphElement>;
317
318export const CreditCardExpiry = ({
319 className,
320 style,
321 ...props
322}: CreditCardExpiryProps) => (
323 <p
324 className={cn("font-mono", className)}
325 style={{
326 lineHeight: "100%",
327 ...style,
328 }}
329 {...props}
330 />
331);
332
333export type CreditCardCvvProps = HTMLAttributes<HTMLParagraphElement>;
334
335export const CreditCardCvv = ({
336 className,
337 style,
338 ...props
339}: CreditCardCvvProps) => (
340 <p
341 className={cn("font-mono", className)}
342 style={{
343 lineHeight: "100%",
344 ...style,
345 }}
346 {...props}
347 />
348);
349