Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit f47d3a0

Browse files
committed
Add card component and story
1 parent 8165abe commit f47d3a0

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

components/molecules/Card/Card.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import Button from '@/components/atoms/Button'
2+
import Heading from '@/components/atoms/Heading'
3+
import cn from 'classnames'
4+
import PropTypes from 'prop-types'
5+
import React from 'react'
6+
import styles from './Card.module.css'
7+
8+
/**
9+
* Render the Card component.
10+
*
11+
* @param {object} props Card component props.
12+
* @param {string} props.body Card body text.
13+
* @param {string} props.className Optional classNames.
14+
* @param {string} props.ctaText The text for the cta button.
15+
* @param {string} props.ctaUrl The url for the cta button.
16+
* @param {object} props.image The image object.
17+
* @param {string} props.meta The card metadata string.
18+
* @param {string} props.timestamp The card timestamp.
19+
* @param {string} props.title The card title.
20+
* @return {Element} The Card component.
21+
*/
22+
export default function Card({
23+
body,
24+
className,
25+
ctaText,
26+
ctaUrl,
27+
image,
28+
meta,
29+
timestamp,
30+
title
31+
}) {
32+
return (
33+
<div className={cn(styles.card, className)}>
34+
<div className={styles.image}>
35+
{image && image.sourceUrl && (
36+
<img
37+
draggable="false"
38+
src={image.sourceUrl}
39+
alt={`${image.altText ?? title}`}
40+
loading="lazy"
41+
height={image.height}
42+
width={image.width}
43+
/>
44+
)}
45+
</div>
46+
<div className={styles.content}>
47+
{meta && <p className={styles.meta}>{meta}</p>}
48+
{title && <Heading className={styles.title}>{title}</Heading>}
49+
{body && <p className={styles.body}>{body}</p>}
50+
</div>
51+
<div className={styles.footer}>
52+
{timestamp && (
53+
<p className={styles.timestamp}>
54+
<time>{timestamp}</time>
55+
</p>
56+
)}
57+
{ctaText && ctaUrl && (
58+
<Button
59+
className={styles.button}
60+
url={ctaUrl}
61+
text={ctaText}
62+
type="secondary"
63+
size="md"
64+
/>
65+
)}
66+
</div>
67+
</div>
68+
)
69+
}
70+
71+
Card.propTypes = {
72+
body: PropTypes.string,
73+
className: PropTypes.string,
74+
ctaText: PropTypes.string,
75+
ctaUrl: PropTypes.string,
76+
headingLevel: PropTypes.oneOf(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']),
77+
image: PropTypes.shape({
78+
sourceUrl: PropTypes.string,
79+
altText: PropTypes.string,
80+
height: PropTypes.string,
81+
width: PropTypes.string
82+
}),
83+
meta: PropTypes.string,
84+
title: PropTypes.string,
85+
timestamp: PropTypes.string
86+
}
87+
88+
Card.defaultProps = {
89+
headingLevel: 'h2'
90+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.card {
2+
@apply flex flex-col;
3+
4+
& .image {
5+
@apply mb-2 relative h-0 w-full rounded;
6+
7+
padding-top: 64%; /* Aspect ratio box - https://css-tricks.com/aspect-ratio-boxes */
8+
9+
& img {
10+
@apply absolute top-0 left-0 w-full h-full object-cover rounded;
11+
}
12+
}
13+
14+
& .content {
15+
& .meta {
16+
@apply mb-2 text-sm;
17+
}
18+
19+
& .title {
20+
@apply mb-2 font-bold;
21+
}
22+
23+
& .body {
24+
@apply mb-2;
25+
}
26+
}
27+
28+
& .footer {
29+
@apply mt-auto flex justify-between items-center;
30+
31+
& .timestamp {
32+
@apply text-sm;
33+
}
34+
}
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {Canvas, Meta, Story} from '@storybook/addon-docs/blocks'
2+
import Card from './'
3+
4+
<Meta title="Components/Molecules/Card" component={Card} />
5+
6+
# Card
7+
8+
Use this component to display a card.
9+
10+
<Canvas>
11+
<Story name="Card">
12+
<Card
13+
image={{
14+
sourceUrl:
15+
'https://images.unsplash.com/photo-1610991149688-c1321006bcc1?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1110&q=60'
16+
}}
17+
meta="This is the meta"
18+
title="This is the card title"
19+
body="This is the card body"
20+
timestamp="May 29, 2021"
21+
ctaText="Click Here!"
22+
ctaUrl="https://google.com"
23+
/>
24+
</Story>
25+
</Canvas>

components/molecules/Card/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './Card.js'

0 commit comments

Comments
 (0)