import { Button, cn, Text } from "@cloudflare/kumo"; import { Crosshair, X } from "@phosphor-icons/react"; import { type KeyboardEvent, type PointerEvent, useId, useRef } from "react"; export interface FocalPoint { /** 0 (left) to 1 (right). */ x: number; /** 0 (top) to 1 (bottom). */ y: number; } export interface FocalPointPickerLabels { /** The area's accessible name ("Focal point"). */ title: string; /** How to use it, under the picture ("Click the subject, or use the arrow keys"). */ hint: string; /** Where it is now, spoken and shown ("Left 30%, top 40%"); `undefined` → `unset`. */ position: (p: FocalPoint) => string; /** No point set yet ("Centre: crops keep the middle"). */ unset: string; /** The button that removes it ("Clear focal point"). */ clear: string; /** The crop previews' caption ("How crops look"). */ previews: string; } export interface FocalPointPickerProps { src: string; /** The picture's own size, for the frame's ratio. */ width: number; height: number; value: FocalPoint | null; onChange: (value: FocalPoint | null) => void; labels: FocalPointPickerLabels; disabled?: boolean; className?: string; "data-testid"?: string; } const STEP = 0.05; const clamp = (v: number) => Math.round(Math.min(1, Math.max(0, v)) * 1000) / 1000; const pct = (v: number) => `${Math.round(v * 1000) / 10}%`; /** * Where an image's subject is (RFC-0028 §1): cover crops of the picture keep this point in view. * Click or tap the subject, or move the point with the arrow keys (Shift for bigger steps, Home * for the centre). Three crop previews (wide, square, tall) show what each frame keeps. Kumo has * no point picker, so it lives here. */ export function FocalPointPicker({ src, width, height, value, onChange, labels, disabled = false, className, "data-testid": testId, }: FocalPointPickerProps) { const area = useRef(null); const hintId = useId(); const point = value ?? { x: 0.5, y: 0.5 }; // Key repeats can arrive before the parent re-renders: steps build on the last point sent. const latest = useRef(point); latest.current = point; const send = (next: FocalPoint) => { latest.current = next; onChange(next); }; const spoken = value ? labels.position(value) : labels.unset; const pick = (e: PointerEvent) => { if (disabled || !area.current) return; const r = area.current.getBoundingClientRect(); if (r.width === 0 || r.height === 0) return; send({ x: clamp((e.clientX - r.left) / r.width), y: clamp((e.clientY - r.top) / r.height), }); }; const keys = (e: KeyboardEvent) => { if (disabled) return; const step = e.shiftKey ? STEP * 4 : STEP; const move: Record = { ArrowLeft: [-step, 0], ArrowRight: [step, 0], ArrowUp: [0, -step], ArrowDown: [0, step], }; if (e.key === "Home") { e.preventDefault(); send({ x: 0.5, y: 0.5 }); return; } const d = move[e.key]; if (!d) return; e.preventDefault(); const from = latest.current; send({ x: clamp(from.x + d[0]), y: clamp(from.y + d[1]) }); }; return (
{value && ( )}
{labels.hint} · {spoken}
{labels.previews}
{(["16 / 9", "1 / 1", "3 / 4"] as const).map((ratio) => ( ))}
); }