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.
PNG, JPG, WebP or GIF, up to 10 MB
import { Command, UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Effect, Schema } from "effect"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"import { AvatarUpload } from "./ui/uicontext/components/upload/AvatarUpload.ts"import { portrait } from "./uploads.ts"const Message = UiSchema.TaggedEnum({ RequestedPhotoSave: { file: Schema.instanceOf(File) }, RequestedPhotoRemove: {}, SucceededStorePhoto: { src: Schema.NullOr(Schema.String), request: Schema.String },})// stands in for your file storage: a second to store the picture, which comes back as a URLconst StorePhoto = Command.make("profile/StorePhoto", { args: { file: Schema.NullOr(Schema.instanceOf(File)) } }, ({ file }) => Effect.sleep("1 second").pipe(Effect.as(Message.SucceededStorePhoto({ src: file === null ? null : URL.createObjectURL(file), request: file === null ? "remove" : "save", }))))const styles = stylex.create({ card: { display: "grid", gap: space.lg, inlineSize: "min(100%, 30rem)", padding: space.xl, borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: radius.xl, backgroundColor: color.panel, }, title: { margin: 0, fontSize: font.base, fontWeight: 600 }, note: { margin: 0, color: color.muted, fontSize: font.sm },})export const ProfilePhoto = Html.component({ name: "ProfilePhoto", model: Schema.Struct({ src: Schema.NullOr(Schema.String), outcome: Schema.NullOr(Outcome) }), message: Message,}).make({ init: () => ({ src: portrait, outcome: null }), update: { RequestedPhotoSave: ($, { file }) => $.evolve({ outcome: { _tag: "Pending", request: "save" } }).run(StorePhoto.make({ file })), RequestedPhotoRemove: ($) => $.evolve({ outcome: { _tag: "Pending", request: "remove" } }).run(StorePhoto.make({ file: null })), SucceededStorePhoto: ($, { src, request }) => $.evolve({ src, outcome: { _tag: "Accepted", request } }), }, view: (h, model) => h.section.attrs(sx(styles.card)).children( h.div.children( h.h3.attrs(sx(styles.title)).children("Profile photo"), h.p.attrs(sx(styles.note)).children("Shown on your comments, mentions and assigned issues."), ), AvatarUpload(h, { props: { name: "Maya Chen", id: "maya", src: model.src, label: "Profile photo", size: 72, outcome: model.outcome }, onEmit: { RequestedSave: ({ file }) => Message.RequestedPhotoSave({ file }), RequestedRemove: () => Message.RequestedPhotoRemove(), }, }), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/avatar-uploadUsage
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 workspace logo
A rounded square for a workspace. It takes PNG, JPG or SVG; anything else is turned away with the reason.
Workspace logo
PNG, JPG or SVG, up to 5 MB
Shown in the sidebar and on invitations. Square works best, at least 256 px.
import { Command, UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Effect, Schema } from "effect"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"import { AvatarUpload } from "./ui/uicontext/components/upload/AvatarUpload.ts"import { workspaceLogo } from "./uploads.ts"const Message = UiSchema.TaggedEnum({ RequestedLogoSave: { file: Schema.instanceOf(File) }, RequestedLogoRemove: {}, CompletedStoreLogo: { src: Schema.NullOr(Schema.String), outcome: Outcome },})// stands in for your server: a moment to store the logo, which comes back as a URLconst StoreLogo = Command.make("workspace/StoreLogo", { args: { file: Schema.NullOr(Schema.instanceOf(File)) } }, ({ file }) => Effect.sleep("800 millis").pipe(Effect.as( file === null ? Message.CompletedStoreLogo({ src: null, outcome: { _tag: "Accepted", request: "remove" } }) : Message.CompletedStoreLogo({ src: URL.createObjectURL(file), outcome: { _tag: "Accepted", request: "save" } }), )))const styles = stylex.create({ row: { display: "grid", gap: space.md, inlineSize: "min(100%, 30rem)" }, label: { margin: 0, fontSize: font.sm, fontWeight: 500 }, note: { margin: 0, color: color.muted, fontSize: font.sm },})export const WorkspaceLogo = Html.component({ name: "WorkspaceLogo", model: Schema.Struct({ src: Schema.NullOr(Schema.String), outcome: Schema.NullOr(Outcome) }), message: Message,}).make({ init: () => ({ src: workspaceLogo, outcome: null }), update: { RequestedLogoSave: ($, { file }) => $.evolve({ outcome: { _tag: "Pending", request: "save" } }).run(StoreLogo.make({ file })), RequestedLogoRemove: ($) => $.evolve({ outcome: { _tag: "Pending", request: "remove" } }).run(StoreLogo.make({ file: null })), // a refused save keeps the logo there was CompletedStoreLogo: ($, { src, outcome }) => $.evolve({ src: outcome._tag === "Refused" ? $.model.src : src, outcome }), }, view: (h, model) => h.div.attrs(sx(styles.row)).children( h.p.attrs(sx(styles.label)).children("Workspace logo"), AvatarUpload(h, { props: { name: "Northwind Labs", src: model.src, label: "Workspace logo", shape: "square", size: 64, accept: { types: ["image/png", "image/jpeg", "image/svg+xml"], typeLabel: "PNG, JPG or SVG", typeNoun: "an image we can show", maxSize: 5 * 1024 * 1024 }, outcome: model.outcome, }, onEmit: { RequestedSave: ({ file }) => Message.RequestedLogoSave({ file }), RequestedRemove: () => Message.RequestedLogoRemove(), }, }), h.p.attrs(sx(styles.note)).children("Shown in the sidebar and on invitations. Square works best, at least 256 px."), ),})API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
namerequired | string | — | Whose picture: a person's or a workspace's name, for the initials and the alt text |
id | string | — | A stable id, which picks the initials' color |
srcrequired | string | null | — | The stored picture's URL, or null for initials |
labelrequired | string | — | What it is: "Profile photo", "Workspace logo" |
shape | "circle" | "square" | circle | A circle for a person, a rounded square for a workspace or an app; circle by default |
size | number | 64 | The picture's size on the page, in px; 64 by default |
accept | UploadAccept | — | What pictures it takes; PNG, JPG, WebP or GIF up to 10 MB by default |
output | number | 512 | The saved file's width and height in px; 512 by default |
outcome | Outcome | null | — | The app's answer to the latest save ("save") or removal ("remove") |
disabledReason | string | null | — | Why the picture can't be changed now; the buttons hide and this shows |
locale | string | — | — |
Events
| Event | Payload | Description |
|---|---|---|
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 |