# Upload with apiRef

> Read the pad from outside its tree and attach the signature as a PNG file.

A shadcn/ui block from the [@reui](https://reui.io) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/reui/c-signature-pad-12).

- Page, with a live preview: https://shadcn.exchange/r/reui/c-signature-pad-12
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/reui/c-signature-pad-12-light-9740ceec7fce77d9.png
- Registry item: https://reui.io/r/new-york-v4/c-signature-pad-12.json
- Listed: 2026-10-13, source last changed 2026-10-13

## Install

```bash
npx shadcn@latest add https://reui.io/r/new-york-v4/c-signature-pad-12.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- Registry items: `button`, `@reui/signature-pad`

## Files

### `c-signature-pad-12.tsx`

```tsx
"use client"

import { useRef, useState } from "react"
import {
  SignaturePad,
  SignaturePadArea,
  SignaturePadGuide,
  SignaturePadPlaceholder,
  type SignaturePadApi,
} from "@/components/reui/signature-pad"

import { Button } from "@/components/ui/button"

export function Pattern() {
  /* `apiRef` reaches the pad from code outside its tree, such as a form
     library's submit handler. */
  const padRef = useRef<SignaturePadApi>(null)
  const [upload, setUpload] = useState<{ name: string; size: number } | null>(
    null
  )
  const [empty, setEmpty] = useState(true)

  const prepareUpload = async () => {
    const blob = await padRef.current?.toBlob({ scale: 3 })
    if (!blob) return
    const file = new File([blob], "signature.png", { type: "image/png" })
    /* Append `file` to a FormData and POST it to your own endpoint. */
    setUpload({ name: file.name, size: file.size })
  }

  return (
    <div className="mx-auto flex w-full max-w-lg flex-col gap-3">
      <SignaturePad
        apiRef={padRef}
        onValueChange={(strokes) => {
          setEmpty(strokes.length === 0)
          setUpload(null)
        }}
      >
        <SignaturePadArea className="h-44">
          <SignaturePadGuide />
          <SignaturePadPlaceholder />
        </SignaturePadArea>
      </SignaturePad>
      <div className="flex items-center gap-2">
        <Button disabled={empty} onClick={prepareUpload}>
          Attach as PNG
        </Button>
        <Button
          variant="ghost"
          disabled={empty}
          onClick={() => {
            padRef.current?.clear()
            /* This button disables itself, so focus moves to the pad. */
            padRef.current?.focus()
          }}
        >
          Clear
        </Button>
        <p
          role="status"
          className="text-muted-foreground ms-auto truncate text-sm"
        >
          {upload && `${upload.name}, ${(upload.size / 1024).toFixed(1)} KB`}
        </p>
      </div>
    </div>
  )
}
```
