React

The @freeplaceholder/react package provides type-safe <Placeholder> and <Avatar> components for React.

Installation

bash
npm install https://github.com/freeplaceholder/react

Placeholder Component

tsx
import { Placeholder } from "@freeplaceholder/react";

function Hero() {
  return (
    <Placeholder
      width={800}
      height={400}
      text="Hero Banner"
      format="webp"
      bg="3b82f6"
      textColor="ffffff"
      fontWeight="bold"
      rounded="lg"
    />
  );
}

Placeholder Props

PropTypeDescription
widthnumberImage width in px (required)
heightnumberImage height in px (required)
formatImageFormatOutput format: svg, png, jpg, webp
bgstringBackground hex color
gradientGradientDirectionto-t, to-tr, to-r, to-br, to-b, to-bl, to-l, to-tl, radial
fromstringGradient start color — hex without #
viastringGradient middle color — hex without #
tostringGradient end color — hex without #
textColorstringText hex color
textstringCustom text overlay
textSizenumberFont size in px
textAlignTextAlignleft, center, right
textTransformTextTransformuppercase, lowercase, capitalize, none
textDecorationTextDecorationunderline, overline, line-through, none
letterSpacing`LetterSpacing \string`tighter, tight, wide, wider, widest, or px
fontWeightFontWeightthin, light, normal, medium, semibold, bold, extrabold, black
opacitynumber0–100
blurnumberGaussian blur 0–100 px
brightnessnumber0–200 (100 = normal)
contrastnumber0–200 (100 = normal)
hueRotatenumber0–360 degrees
invertbooleanInvert colors
saturatenumber0–200 (100 = normal)
sepiabooleanSepia tone
grayscalebooleanGrayscale filter
bordernumberBorder width in px
borderColorstringBorder hex color
borderStyleBorderStylesolid, dashed, dotted, double, none
rounded`Rounded \number`sm, md, lg, xl, 2xl, 3xl, full, or px
lazybooleanEnable lazy loading (default: true)

All standard <img> HTML attributes are also supported.

Avatar Component

tsx
import { Avatar } from "@freeplaceholder/react";

function UserCard({ user }) {
  return (
    <Avatar
      name={user.name}
      size={64}
      format="png"
      fontWeight="bold"
      border={2}
      borderColor="ffffff"
    />
  );
}

Avatar Props

PropTypeDescription
namestringName to derive initials from (required)
sizenumberSquare dimensions in px (default: 128)
formatImageFormatOutput format
bgstringBackground hex color
gradientGradientDirectionto-t, to-tr, to-r, to-br, to-b, to-bl, to-l, to-tl, radial
fromstringGradient start color — hex without #
viastringGradient middle color — hex without #
tostringGradient end color — hex without #
textColorstringText hex color
textDecorationTextDecorationunderline, overline, line-through, none
letterSpacing`LetterSpacing \string`Letter spacing
fontWeightFontWeightFont weight
opacitynumber0–100
blurnumberGaussian blur 0–100 px
brightnessnumber0–200
contrastnumber0–200
hueRotatenumber0–360 degrees
invertbooleanInvert colors
saturatenumber0–200
sepiabooleanSepia tone
grayscalebooleanGrayscale filter
bordernumberBorder width in px
borderColorstringBorder hex color
borderStyleBorderStyleBorder style
rounded`Rounded \number`Border radius
lazybooleanEnable lazy loading (default: true)

URL Helpers

If you need just the URL without a component:

tsx
import { avatarUrl, placeholderUrl, snippetUrl } from "@freeplaceholder/react";

const src = placeholderUrl({ width: 600, height: 400, bg: "3b82f6" });
const avatar = avatarUrl({ name: "John Doe", size: 256, format: "png" });
const og = snippetUrl({ title: "Hello", subtitle: "World", cta: "Learn more", format: "png" });

TypeScript

All types are exported for use in your own components:

tsx
import type {
  PlaceholderComponentProps,
  AvatarComponentProps,
  FontWeight,
  BorderStyle,
  TextAlign,
} from "@freeplaceholder/react";

Overriding Base URL

tsx
import { configure } from "@freeplaceholder/react";

configure({ baseUrl: "https://my-custom-instance.com" });