Avatar Upload

A profile photo or a logo: upload, crop, change and remove, with initials when there's none.

Profile photo

Shown on your comments, mentions and assigned issues.

Profile photo of Maya Chen

PNG, JPG, WebP or GIF, up to 10 MB

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/avatar-upload

Usage

import type { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { AvatarUpload } from "./ui/uicontext/components/upload/AvatarUpload.ts"
const Message = UiSchema.TaggedEnum({	RequestedPhotoSave: { file: Schema.instanceOf(File) },	RequestedPhotoRemove: {},})// Save hands you the cropped square as a PNG; answer with `outcome` for request "save" or "remove"export const ProfilePhoto = (h: Html.H<typeof Message.Type>, photo: string | null, outcome: Outcome | null) =>	AvatarUpload(h, {		props: { name: "Maya Chen", src: photo, label: "Profile photo", outcome },		onEmit: {			RequestedSave: ({ file }) => Message.RequestedPhotoSave({ file }),			RequestedRemove: () => Message.RequestedPhotoRemove(),		},	})

Examples

A rounded square for a workspace. It takes PNG, JPG or SVG; anything else is turned away with the reason.

Workspace logo

Workspace logo of Northwind Labs

PNG, JPG or SVG, up to 5 MB

Shown in the sidebar and on invitations. Square works best, at least 256 px.

API reference

Props

AvatarUpload props
NameTypeDefaultDescription
namerequiredstring—Whose picture: a person's or a workspace's name, for the initials and the alt text
idstring—A stable id, which picks the initials' color
srcrequiredstring | null—The stored picture's URL, or null for initials
labelrequiredstring—What it is: "Profile photo", "Workspace logo"
shape"circle" | "square"circleA circle for a person, a rounded square for a workspace or an app; circle by default
sizenumber64The picture's size on the page, in px; 64 by default
acceptUploadAccept—What pictures it takes; PNG, JPG, WebP or GIF up to 10 MB by default
outputnumber512The saved file's width and height in px; 512 by default
outcomeOutcome | null—The app's answer to the latest save ("save") or removal ("remove")
disabledReasonstring | null—Why the picture can't be changed now; the buttons hide and this shows
localestring——

Events

AvatarUpload events
EventPayloadDescription
RequestedSave{ file: instanceOf; area: CropArea; original: string }Store this picture: the cropped square, and the area of the original it came from
RequestedRemove{}Take the picture away, back to initials

Source