Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | 27x 27x 1x 1x 1x 1x 2x 2x 2x 2x | import { ChevronLeft, ChevronRight, GripVertical, Play, Star, Trash2 } from "lucide-react";
import type { DragEvent } from "react";
import { getImageUrl, type Media } from "../../lib/api";
import { cn } from "../../lib/utils";
import { Checkbox } from "../ui/checkbox";
/**
* One photo tile — `project-detail-photos.html`, `.tile` in the drawer.
*
* The three marks printed on a tile are the grip, the ordinal and the COVER
* code, all on the `--cover` ground with `--on-cover` ink (the mockup's
* `.tile-grip` / `.tile-ord` / `.thumb-code`). That pair is an overlay, so its
* ink deliberately does NOT flip with the theme — `dark-mode-contrast.test.ts`
* records the measured ratio for it.
*
* WHAT IS NEW
* - A **selection checkbox**, because the drawer's bulk actions need one. The
* selected state is marked by the page ink (`--foreground-default` ring),
* never by index red: selection is state, and index red is never state.
* - **◀ ▶ nudge buttons**. Reorder used to be drag-only, which is unusable
* from a keyboard and hard on a touch screen. The drag is still there; these
* are the accessible path to the same operation.
*/
type MediaItemProps = {
item: Media;
index: number;
isDragging: boolean;
isDragOver: boolean;
onDragStart: (e: DragEvent<HTMLDivElement>, mediaId: number) => void;
onDragEnd: (e: DragEvent<HTMLDivElement>) => void;
onDragOver: (e: DragEvent<HTMLDivElement>, mediaId: number) => void;
onDrop: (e: DragEvent<HTMLDivElement>, targetMediaId: number) => void;
onClick: () => void;
onSetCover: (mediaId: number) => void;
onDelete: (mediaId: number) => void;
/** Selection, for the drawer's bulk actions. Omit to render no checkbox. */
isSelected?: boolean;
onToggleSelect?: (mediaId: number) => void;
/** Keyboard-reachable reorder. Omit to render no nudge controls. */
onMoveEarlier?: (mediaId: number) => void;
onMoveLater?: (mediaId: number) => void;
/** Disables ◀ on the first tile and ▶ on the last. */
isFirst?: boolean;
isLast?: boolean;
};
export function MediaItem({
item,
index,
isDragging,
isDragOver,
onDragStart,
onDragEnd,
onDragOver,
onDrop,
onClick,
onSetCover,
onDelete,
isSelected,
onToggleSelect,
onMoveEarlier,
onMoveLater,
isFirst,
isLast,
}: MediaItemProps) {
const describe = item.caption || `photo ${index + 1}`;
return (
<li
className={cn(
"relative list-none transition-transform",
isDragOver && "scale-105",
isDragging && "opacity-50",
)}
draggable
onDragStart={(e) =>
onDragStart(e as unknown as DragEvent<HTMLDivElement>, item.id)
}
onDragEnd={(e) => onDragEnd(e as unknown as DragEvent<HTMLDivElement>)}
onDragOver={(e) =>
onDragOver(e as unknown as DragEvent<HTMLDivElement>, item.id)
}
onDrop={(e) => onDrop(e as unknown as DragEvent<HTMLDivElement>, item.id)}
>
{/* The −3px lift means "pick-up-able", and this tile is draggable —
the one place it is allowed. */}
<div className="group relative block transition-transform hover:-translate-y-[3px]">
<button
type="button"
aria-label={`View ${describe}`}
onClick={onClick}
className={cn(
"block aspect-square w-full cursor-grab overflow-hidden rounded-sm bg-background-muted",
"shadow-[inset_0_0_0_1px_var(--border-default)]",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500",
// Selected is marked by the page ink, not by index red.
isSelected &&
"shadow-[inset_0_0_0_2px_var(--foreground-default)]",
)}
>
{item.mediaType === "video" ? (
<span className="relative block h-full w-full">
{item.thumbnailKey ? (
<img
src={getImageUrl(item.thumbnailKey)}
alt={item.caption || "Video thumbnail"}
className="h-full w-full object-cover"
draggable={false}
loading="lazy"
/>
) : (
<span className="flex h-full w-full items-center justify-center bg-background-subtle">
<Play className="size-6 text-foreground-subtle" />
</span>
)}
{item.durationSeconds ? (
<span className="absolute right-1 bottom-1 rounded-sm bg-cover px-1 text-xs tabular-nums text-on-cover">
{Math.floor(item.durationSeconds / 60)}:
{String(item.durationSeconds % 60).padStart(2, "0")}
</span>
) : null}
</span>
) : (
<img
src={getImageUrl(item.storageKey)}
alt={item.caption || "Media"}
className="h-full w-full object-cover"
draggable={false}
loading="lazy"
/>
)}
</button>
{/* The grip: this tile can be picked up. */}
<span
aria-hidden="true"
className="pointer-events-none absolute top-0.5 left-0.5 grid size-4.5 place-items-center rounded-sm bg-cover text-on-cover"
>
<GripVertical className="size-3" />
</span>
{/* The ordinal, right-aligned and tabular. */}
<span className="pointer-events-none absolute top-0.5 right-0.5 grid h-4.5 min-w-4.5 place-items-center rounded-sm bg-cover px-1 text-xs font-semibold tabular-nums text-on-cover">
{index + 1}
</span>
{item.isCover && (
<span className="pointer-events-none absolute bottom-1 left-1 rounded-sm bg-cover px-1 text-xs font-semibold uppercase tracking-wider text-on-cover">
Cover
</span>
)}
{onToggleSelect && (
<span className="absolute top-5.5 left-0.5">
<Checkbox
checked={Boolean(isSelected)}
onChange={() => onToggleSelect(item.id)}
aria-label={`Select ${describe}`}
/>
</span>
)}
{/* Per-tile actions. Visible on hover and on keyboard focus — an
action that only appears on hover is unreachable by keyboard. */}
<span className="absolute top-0.5 right-6 flex gap-0.5 opacity-0 transition-opacity group-hover:opacity-100 group-focus-within:opacity-100">
{item.mediaType === "image" && !item.isCover && (
<button
type="button"
onClick={(e) => {
e.stopPropagation();
onSetCover(item.id);
}}
title="Set as cover"
className="grid size-4.5 place-items-center rounded-sm bg-cover text-on-cover"
>
<Star className="size-3" />
<span className="sr-only">Set {describe} as the cover</span>
</button>
)}
<button
type="button"
onClick={(e) => {
e.stopPropagation();
onDelete(item.id);
}}
title="Delete"
className="grid size-4.5 place-items-center rounded-sm bg-error text-foreground-inverse"
>
<Trash2 className="size-3" />
<span className="sr-only">Delete {describe}</span>
</button>
</span>
</div>
{(onMoveEarlier || onMoveLater) && (
<div className="mt-1 flex items-center gap-0.5">
{item.isCover && (
<span className="text-xs font-semibold uppercase tracking-wider text-foreground-subtle">
Cover
</span>
)}
<span className="ml-auto flex gap-0.5">
{onMoveEarlier && (
<button
type="button"
disabled={isFirst}
onClick={() => onMoveEarlier(item.id)}
title="Move earlier"
className="grid size-6 place-items-center rounded-sm text-foreground-muted hover:bg-foreground-default/7 disabled:opacity-50"
>
<ChevronLeft aria-hidden="true" className="size-3.5" />
<span className="sr-only">Move {describe} earlier</span>
</button>
)}
{onMoveLater && (
<button
type="button"
disabled={isLast}
onClick={() => onMoveLater(item.id)}
title="Move later"
className="grid size-6 place-items-center rounded-sm text-foreground-muted hover:bg-foreground-default/7 disabled:opacity-50"
>
<ChevronRight aria-hidden="true" className="size-3.5" />
<span className="sr-only">Move {describe} later</span>
</button>
)}
</span>
</div>
)}
</li>
);
}
|