React Integration

A complete React guide to installing and using Leafpad Blogs with React Router, dynamic routes, blog cards, and individual post rendering. Fast setup for any Re


Leafpad Blogs gives you a plug-and-play way to add a full blog system posts, HTML rendering, TOC, cards, and more directly inside your React application with zero dependencies and fully customizable UI.

This guide walks you through a full quick setup for React, including routing, fetching, and rendering.

1. Install the Package

Leafpad is extremely lightweight and has zero external dependencies.

# Using npm
npm install @leafpad/blogs

# Using pnpm
pnpm add @leafpad/blogs

# Using yarn
yarn add @leafpad/blogs

2. Add Required Styles

Leafpad ships with minimal CSS: height, spacing, and font styling.
These are optional but recommended for a clean baseline.

In your main React entry point (e.g., src/index.js, src/main.jsx) or inside the /blogs route:

import '@leafpad/blogs/src/styles/style.css';

You may override or remove the file later React styling, Tailwind, or CSS Modules work fine. We recommend to keep it initially to get a look and feel. If you don't like you can always customize it.


3. Display All Blog Cards

This is your main /blogs page, which lists all your blog posts using the pre-built card layout.

Below is an example using React Router:

src/pages/BlogsPage.jsx

import { useEffect, useState } from "react";
import { BlogPostFormat, BlogsService } from "@leafpad/blogs";

const blogsApi = new BlogsService("my-awesome-blog");

export default function BlogsPage() {
  const [posts, setPosts] = useState([]);

  useEffect(() => {
    async function load() {
      const { posts } = await blogsApi.fetchPosts({
        includeHtml: true,
      });
      setPosts(posts);
    }
    load();
  }, []);

  return (
    <div
      dangerouslySetInnerHTML={{
        __html: BlogPostFormat.blogCards({
          posts,
          urlPrefix: "/blogs",
        }),
      }}
    />
  );
}

You can use any routing library as use wish

urlPrefix: Incase if you wish to host your blogs under a different url you can pass the same here.

Routing file

import { BrowserRouter, Routes, Route } from "react-router-dom";
import BlogsPage from "./pages/BlogsPage";
import BlogIndividualPage from "./pages/BlogIndividualPage";

export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/blogs" element={<BlogsPage />} />
        <Route path="/blogs/:slug" element={<BlogIndividualPage />} />
      </Routes>
    </BrowserRouter>
  );
}

4. Individual Blog Page

Renders a single blog post at /blogs/:slug.

src/pages/BlogIndividualPage.jsx

import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { BlogPostFormat, BlogsService, BlogUtils } from "@leafpad/blogs";

const blogsApi = new BlogsService("my-awesome-blog");

export default function BlogIndividualPage() {
  const { slug } = useParams();
  const [post, setPost] = useState(null);

  useEffect(() => {
    async function load() {
      const result = await blogsApi.fetchBlog(slug, { includeHtml: true });
      setPost(result);
      // add scroll listener for automatic TOC highlighting
      const {destroy} = BlogUtils.attachTocListener('toc')
      return destroy
    }
    return load();
  }, [slug]);

  if (!post) return <div>Loading...</div>;

  return (
    <div className="leafpad-blog-wrapper">
      <div
        dangerouslySetInnerHTML={{
          __html: BlogPostFormat.blogPostWithToc({ post }),
        }}
      />
    </div>
  );
}

Published with LeafPad