import { useEntity } from "src/replicache"; import { ImageAltModal } from "./ImageAltModal"; import { EditTiny } from "components/Icons/EditTiny"; import { useState } from "react"; import { useSpring, animated } from "@react-spring/web"; import useMeasure from "react-use-measure"; import { ReadOnlyAltText } from "./ReadOnlyAltText"; // Overlaid on an image; visible on hover (within a `group/image`), while the // containing block is selected, or whenever alt text already exists. Opens the // alt-text modal. Shared by ImageBlock and the image gallery. // Readers (canEdit === false) get only the "ALT" toggle to reveal existing alt // text — the edit/add-alt trigger is hidden. export function ImageAltButton(props: { entityID: string; selected: boolean; canEdit?: boolean; className?: string; }) { let canEdit = props.canEdit ?? true; let hasAlt = !!useEntity(props.entityID, "image/alt")?.data.value; let alt = useEntity(props.entityID, "image/alt")?.data.value; let [showAlt, setShowAlt] = useState(false); let [altRef, { height: altHeight }] = useMeasure(); let altStyle = useSpring({ height: showAlt ? altHeight : 0, opacity: showAlt ? 1 : 0, config: { tension: 450, friction: 30 }, }); // Readers have nothing to interact with when there's no alt text. if (!canEdit && !hasAlt) return null; // Readers get the plain ALT pill + preview, shared with published posts. if (!canEdit) return ; let imageSize = useEntity(props.entityID, "image/max-width")?.data.value; return (
{ let group = e.currentTarget; requestAnimationFrame(() => { if (!group.contains(document.activeElement)) setShowAlt(false); }); }} className={`absolute bottom-1.5 right-1.5 left-1.5 transition-opacity flex flex-col h-fit ${ props.selected || hasAlt ? "opacity-100" : "opacity-0 group-hover/image:opacity-100" } ${props.className || ""}`} >
{canEdit && ( e.preventDefault()} className={`hover:cursor-pointer opaque-container border-2! border-secondary! rounded-md! h-5 text-secondary`} > {hasAlt ? ( ) : (
{imageSize && imageSize < 100 ? "+ ALT" : "ADD ALT"}
)}
} entityID={props.entityID} title={hasAlt ? "Edit Alt Text" : "Add Alt Text"} /> )} {hasAlt && ( )}
{alt}
); }