|
| 1 | +import clsx from "clsx"; |
| 2 | +import React from "react"; |
| 3 | +import { CardImageBorderRadius, CardImageSize, CardProps } from "./Card.types"; |
| 4 | +import { Button } from "../Button"; |
| 5 | +import { ButtonSize, ButtonType, ButtonVariant } from "../Button/Button.types"; |
| 6 | +import "./Card.scss"; |
| 7 | + |
| 8 | +/** |
| 9 | + * A stylized Code4rena card with 4 variants and optional footer, cta button, and title. |
| 10 | + * Requires an image tag for the top left corner and children for the body. |
| 11 | + * |
| 12 | + * __Available variants:__ |
| 13 | + * - `TRANSPARENT` - makes the background of the card transparent |
| 14 | + * - `OUTLINED` - adds border around the card |
| 15 | + * - `COMPACT` - displays the body below the image to allow the card to be more narrow |
| 16 | + * - `BOTTOM_ALIGNED` - makes the title and body bottom aligned instead of top aligned |
| 17 | + * |
| 18 | + * @param children - Elements to be rendered inside the body of the card, passed in as children. |
| 19 | + * @param className - String of custom classes to extend the default styling of the component. |
| 20 | + * @param variants - Array of style variants to be applied to the rendered component. |
| 21 | + * @param footerDetails - Informational content to be rendered on the left side of the footer. |
| 22 | + * @param footerLinks - Array of anchor elements to be rendered in the right side of the footer. |
| 23 | + * @param imageSize - The size of the image displayed in the top corner. Can be small, medium or large. Defaults to medium |
| 24 | + * @param image - Image element to be rendered in the top left corner |
| 25 | + * @param imageBorderRadius - Border radius of the image. Can be circle, small, medium, or large. Defaults to circle. |
| 26 | + * @param title - The title to be rendered at the top of the card body. Can be string or react node. |
| 27 | + * @param cta - A call to action button rendered in the top right corner. Includes text and an onClick handler. |
| 28 | + */ |
| 29 | +export const Card: React.FC<CardProps> = ({ |
| 30 | + children, |
| 31 | + className, |
| 32 | + variants, |
| 33 | + footerDetails, |
| 34 | + footerLinks, |
| 35 | + imageSize = CardImageSize.MEDIUM, |
| 36 | + image, |
| 37 | + imageBorderRadius = CardImageBorderRadius.CIRCLE, |
| 38 | + title, |
| 39 | + cta, |
| 40 | +}) => { |
| 41 | + return ( |
| 42 | + <div |
| 43 | + className={clsx( |
| 44 | + "card", |
| 45 | + className && className, |
| 46 | + variants && variants.map((variant) => `card--${variant}`) |
| 47 | + )} |
| 48 | + > |
| 49 | + {/* Main section of card */} |
| 50 | + <div className="card__main"> |
| 51 | + <div |
| 52 | + className={`card__image card__image--${imageSize} card__image--radius-${imageBorderRadius}`} |
| 53 | + > |
| 54 | + {image} |
| 55 | + </div> |
| 56 | + {/* body */} |
| 57 | + <div className="card__body"> |
| 58 | + {title && <h2 className="card__title">{title}</h2>} |
| 59 | + {children} |
| 60 | + </div> |
| 61 | + {/* CTA */} |
| 62 | + {cta && ( |
| 63 | + <Button |
| 64 | + className="card__cta" |
| 65 | + onClick={cta.onClick} |
| 66 | + label={cta.text} |
| 67 | + variant={ButtonVariant.SECONDARY} |
| 68 | + type={ButtonType.BUTTON} |
| 69 | + size={ButtonSize.NARROW} |
| 70 | + /> |
| 71 | + )} |
| 72 | + </div> |
| 73 | + {/* Footer */} |
| 74 | + {(footerDetails || footerLinks) && ( |
| 75 | + <footer className="card__footer"> |
| 76 | + {footerDetails && ( |
| 77 | + <p className="card__footer-details">{footerDetails}</p> |
| 78 | + )} |
| 79 | + {footerLinks && ( |
| 80 | + <div className="card__footer-link-wrapper"> |
| 81 | + {footerLinks.map((link) => link)} |
| 82 | + </div> |
| 83 | + )} |
| 84 | + </footer> |
| 85 | + )} |
| 86 | + </div> |
| 87 | + ); |
| 88 | +}; |
0 commit comments