Choose the part of a picture to keep: drag, pinch, scroll or arrow keys, in a fixed frame or a circle.
Loading…
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { SegmentedControl } from "./ui/uicontext/components/SegmentedControl.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"import { CropArea, roundArea } from "./ui/uicontext/components/upload/crop.ts"import { ImageCrop } from "./ui/uicontext/components/upload/ImageCrop.ts"import { landscape } from "./uploads.ts"const shapes = [ { value: "16:9", label: "Cover" }, { value: "1:1", label: "Square" }, { value: "circle", label: "Circle" },] as constconst Message = UiSchema.TaggedEnum({ RequestedCoverCrop: { area: CropArea }, PickedShape: { shape: Schema.String },})const styles = stylex.create({ panel: { display: "grid", gap: space.lg, inlineSize: "min(100%, 34rem)" }, readout: { margin: 0, color: color.muted, fontSize: font.sm, fontVariantNumeric: "tabular-nums" },})const size = { width: 1600, height: 900 }export const CoverImage = Html.component({ name: "CoverImage", model: Schema.Struct({ area: Schema.NullOr(CropArea), shape: Schema.String }), message: Message,}).make({ init: () => ({ area: null, shape: "16:9" }), update: { RequestedCoverCrop: ($, { area }) => $.evolve({ area }), // a new frame starts centered again PickedShape: ($, { shape }) => $.evolve({ shape, area: null }), }, view: (h, model) => { const kept = model.area === null ? null : roundArea(size, model.area) return h.div.attrs(sx(styles.panel)).children( SegmentedControl(h, { label: "Frame", value: model.shape, options: shapes.map((s) => ({ value: s.value, label: s.label })), onPick: (shape) => Message.PickedShape({ shape }), }), ImageCrop(h, { key: model.shape, props: { src: landscape, label: "Cover image", value: model.area, aspect: model.shape === "16:9" ? 16 / 9 : 1, shape: model.shape === "circle" ? "circle" : "rect", }, onEmit: { RequestedCrop: ({ area }) => Message.RequestedCoverCrop({ area }) }, }), h.p.attrs({ ...sx(styles.readout) }).children( kept === null ? "Loading…" : `Keeps ${kept.width} × ${kept.height} px from (${kept.x}, ${kept.y})`, ), ) },})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/image-cropUsage
import { ImageCrop } from "./ui/uicontext/components/upload/ImageCrop.ts"import { CropArea } from "./ui/uicontext/components/upload/crop.ts"const Message = UiSchema.TaggedEnum({ CroppedCover: { area: CropArea } })// The crop is yours: keep `area` (in the image's pixels) and pass it back as `value`export const CoverCrop = (h: Html.H<typeof Message.Type>, src: string, area: CropArea | null) => ImageCrop(h, { props: { src, label: "Cover image", value: area, aspect: 16 / 9 }, onEmit: { RequestedCrop: ({ area }) => Message.CroppedCover({ area }) }, })API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
srcrequired | string | — | The picture: a URL, or an object URL for a file the person just chose |
labelrequired | string | — | What's being cropped, as the control's name: "Profile photo" |
valuerequired | CropArea | null | — | The area kept, in the image's pixels; null to start centered |
aspect | number | 1 | The frame's width ÷ height; 1 by default |
shape | "rect" | "circle" | — | A circle mask for a profile photo (its aspect is 1) |
maxZoom | number | 4 | How far in it can go, from the whole picture; 4 by default |
disabled | boolean | — | Shows the crop and takes no changes |
Events
| Event | Payload | Description |
|---|---|---|
RequestedCrop | { area: CropArea } | The area to keep, in the image's pixels |