Skip to main content
Version: 1.0.0

BlogCard Component

The BlogCard component is a reusable React component designed to display a preview of a blog post. It's built for use in Next.js applications and utilizes the contentlayer library for content management.

Importing the Component

To use the BlogCard component in your Next.js application, import it as follows:

import BlogCard from '@/components/BlogCard';

Basic Usage

Here's a basic example of how to use the BlogCard component:

import { Blog } from "contentlayer/generated"

const blogPost: Blog = {
// ... blog post data
}

<BlogCard data={blogPost} />

Props

The BlogCard component accepts a single prop:

| Prop | Type | Description | | ---- | ---- | ----------- | --------------------------------------- | | data | Blog | undefined | An object containing the blog post data |

The Blog type is generated by contentlayer and should include the following properties:

  • slug: The URL slug for the blog post
  • image: The URL of the blog post's featured image
  • title: The title of the blog post
  • description: A short description or excerpt of the blog post
  • date: The publication date of the blog post

Functionality

The BlogCard component provides the following functionality:

  1. Displays a preview of a blog post, including its image, title, description, and publication date.
  2. Links to the full blog post using Next.js's Link component.
  3. Formats the publication date using the date-fns library.

UI Components

The BlogCard component uses the following UI components:

  • Card and CardContent: Wraps the blog post preview content
  • Link: Provides navigation to the full blog post

Styling

The component uses a combination of inline styles and Tailwind CSS classes for styling:

  • The Card component has a custom box shadow applied using inline styles.
  • The CardContent uses Tailwind classes for padding, maximum width, and flex layout.
  • The image container uses Tailwind classes for grouping and z-index.
  • The image itself has rounded corners and a hover effect for the backdrop filter.

Date Formatting

The component uses the date-fns library to format the publication date:

{
format(parseISO(data?.date || ''), 'LLLL d, yyyy');
}

This formats the date in the "Month Day, Year" format (e.g., "June 1, 2023").

Accessibility

To improve accessibility:

  1. Add an aria-label to the Link component to provide more context for screen readers.
  2. Ensure that the contrast ratio between the text and background colors meets WCAG standards.
  3. Add appropriate alt text to the image (currently using the blog post title).

Best Practices

  1. Handle potential undefined data more robustly to prevent errors.
  2. Consider adding fallback content or a skeleton loader for when data is loading or unavailable.
  3. Optimize the image using Next.js's Image component for better performance.

Customization

To customize the BlogCard component:

  1. Modify the Tailwind classes to adjust the layout and styling.
  2. Replace the Card and CardContent components with your own UI components if needed.
  3. Add additional fields from the Blog type if available in your content model.

Example Usage

Here's an example of how you might use the BlogCard component in a blog listing page:

import { allBlogs } from 'contentlayer/generated';
import BlogCard from '@/components/BlogCard';

export default function BlogList() {
return (
<div className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6'>
{allBlogs.map((blog) => (
<BlogCard key={blog.slug} data={blog} />
))}
</div>
);
}

This example demonstrates how to use the BlogCard component to create a responsive grid of blog post previews.


By using the BlogCard component, you can easily display consistent and attractive blog post previews throughout your Next.js application. Remember to provide complete and valid blog post data to ensure the component functions correctly.