Command Palette

Search for a command to run...

AI Badges/Generate Badge

Generate Badge

Hook and utilities for generating badge images from SVG, with PNG/JPEG export support

Open in v0Open in
Guillermo Rauch - TEAM Badge#001GUILLERMORAUCHCEO AT VERCELTEAMMEMBER

Overview

The Generate Badge component provides a React hook and button component for converting SVG badges into downloadable PNG/JPEG images. Perfect for letting users download their badges for sharing on social media.

Installation

npx shadcn@latest add "https://tryelements.dev/r/generate-badge"

Usage

import { AIBadge } from "@/components/elements/ai-badge";
import { useGenerateBadge, GenerateBadgeButton } from "@/components/elements/generate-badge";

export function DownloadableBadge() {
  const badge = useGenerateBadge();

  return (
    <div className="flex flex-col gap-4">
      <AIBadge
        ref={badge.badgeRef}
        profilePictureUrl="https://example.com/avatar.jpg"
        badgeNumber="#001"
        firstName="John"
        lastName="Doe"
        role="Developer"
      />
      <GenerateBadgeButton
        badge={badge}
        filename="my-badge"
        options={{ scale: 2 }}
      >
        Download Badge
      </GenerateBadgeButton>
    </div>
  );
}

Hook API

useGenerateBadge

const {
  badgeRef,          // Ref to attach to SVG element
  generateAndDownload, // Download badge as image
  generateBlob,      // Get badge as Blob
  generateDataUrl,   // Get badge as data URL
  isGenerating,      // Loading state
  error,             // Error state
} = useGenerateBadge();

Options

OptionTypeDefaultDescription
scalenumber1Scale factor for output image
format"png" | "jpeg""png"Output format
qualitynumber0.9JPEG quality (0-1)
backgroundColorstring-Background color for JPEG

Button Props

PropTypeDefaultDescription
badgeUseGenerateBadgeReturn-The hook return value
filenamestring"badge"Filename for download
optionsGenerateBadgeOptions-Generation options
loadingContentReact.ReactNode"Generating..."Loading state content

Advanced Usage

Manual Download

const badge = useGenerateBadge();

const handleDownload = async () => {
  await badge.generateAndDownload("my-badge", {
    scale: 2,
    format: "png",
  });
};

Get as Blob (for Upload)

const badge = useGenerateBadge();

const handleUpload = async () => {
  const blob = await badge.generateBlob({ scale: 2 });
  if (blob) {
    const formData = new FormData();
    formData.append("badge", blob, "badge.png");
    await fetch("/api/upload", {
      method: "POST",
      body: formData,
    });
  }
};

Get as Data URL

const badge = useGenerateBadge();

const handleShare = async () => {
  const dataUrl = await badge.generateDataUrl({ scale: 2 });
  if (dataUrl && navigator.share) {
    await navigator.share({
      files: [await fetch(dataUrl).then(r => r.blob())],
    });
  }
};