This project is still a work in progress.

payload bricks
Fields

Icon

Icon with inline SVG support

Installation

npx shadcn@latest add https://payload-bricks.bblank.dev/r/icon-field.json

Usage

example-config.ts
import { iconField } from "@/fields/icon"
import type { Block } from "payload"

export const ExampleBlock: Block = {
  slug: "example",
  interfaceName: "Example",
  fields: [
    // your other fields
    iconField,
  ],
}
example-block.tsx
import type { Example } from "@/payload-types"
import Image from "next/image"

export function ExampleBlock(props: Example) {
  const icon = props.icon
  if (!icon) return null

  if (icon.useSvgRaw && icon.svgRaw) {
    return <span aria-hidden dangerouslySetInnerHTML={{ __html: icon.svgRaw }} />
  }

  if (typeof icon.iconFile === "object" && icon.iconFile?.url) {
    return (
      <Image
        src={icon.iconFile.url}
        alt=""
        className="size-8"
        width={32}
        height={32}
      />
    )
  }

  return null
}

Field structure

nametype
iconFileupload
useSvgRawcheckbox
svgRawtextarea
svgSourceIdtexthidden

How SVG imports work

When an SVG is selected from the media collection, a field hook fetches the file, sanitizes its markup, and stores it in svgRaw. The hidden field svgSourceId prevents another fetch until the selected file changes. Non-SVG uploads stay as file references. svgRaw also allows to directly paste an SVG as field content.

On this page