convert to figure/figcaption if caption exists#2056
Conversation
|
@Ovgodd is attempting to deploy a commit to the TypeCell Team on Vercel. A member of the Team first needs to authorize it. |
| const { dom } = createFigureWithCaption(image, block.props.caption); | ||
| // Enhance figure with explicit role and aria-label | ||
| dom.setAttribute("role", "img"); | ||
| dom.setAttribute("aria-label", `Image: ${block.props.caption}`); | ||
| return { dom }; |
There was a problem hiding this comment.
Why do this here instead of fixing it in createFigureWithCaption?
There was a problem hiding this comment.
I removed it,role="img" isn’t needed on figure since it already has proper semantics for screen readers.
| // Accessibility for exported HTML: prefer caption as alt when present | ||
| if (block.props.caption) { | ||
| image.alt = block.props.caption; | ||
| } else { | ||
| image.alt = ""; | ||
| image.setAttribute("role", "presentation"); | ||
| image.setAttribute("aria-hidden", "true"); | ||
| } |
There was a problem hiding this comment.
Missing block.props.name, why not just:
| // Accessibility for exported HTML: prefer caption as alt when present | |
| if (block.props.caption) { | |
| image.alt = block.props.caption; | |
| } else { | |
| image.alt = ""; | |
| image.setAttribute("role", "presentation"); | |
| image.setAttribute("aria-hidden", "true"); | |
| } | |
| image.alt = block.props.name || block.props.caption || ""; | |
| if (!image.alt) { | |
| image.setAttribute("role", "presentation"); | |
| image.setAttribute("aria-hidden", "true"); | |
| } |
| // Accessibility: set alt/aria based on presence of caption per RGAA | ||
| const accessibleImageWithCaption = () => { | ||
| image.alt = block.props.caption; | ||
| image.removeAttribute("aria-hidden"); | ||
| image.removeAttribute("role"); | ||
| image.setAttribute("tabindex", "0"); | ||
| }; | ||
|
|
||
| const accessibleImage = () => { | ||
| image.alt = ""; | ||
| image.setAttribute("role", "presentation"); | ||
| image.setAttribute("aria-hidden", "true"); | ||
| image.setAttribute("tabindex", "-1"); | ||
| }; | ||
|
|
||
| block.props.caption ? accessibleImageWithCaption() : accessibleImage(); |
There was a problem hiding this comment.
Same as below, just fallback when there is no alt, and this doesn't have the block.props.name
| // Special accessible structure for images with captions: use figure/figcaption | ||
| const shouldUseFigure = | ||
| element && | ||
| block.type === "image" && | ||
| block.props.caption && | ||
| block.props.showPreview !== false; | ||
| if (shouldUseFigure) { | ||
| const figure = document.createElement("figure"); | ||
| figure.className = wrapper.className; | ||
|
|
||
| // Move preview element inside the figure | ||
| figure.appendChild(element.dom); | ||
|
|
||
| // Create figcaption instead of a paragraph | ||
| const figcaption = document.createElement("figcaption"); | ||
| figcaption.className = "bn-file-caption"; | ||
| figcaption.textContent = block.props.caption; | ||
| figure.appendChild(figcaption); | ||
|
|
||
| // ARIA enhancements for screen readers | ||
| figure.setAttribute("role", "img"); | ||
| figure.setAttribute("aria-label", `Image: ${block.props.caption}`); | ||
|
|
||
| return { dom: figure, destroy: element.destroy }; | ||
| } |
There was a problem hiding this comment.
I would prefer this as attributes rather than figure figcaption, this makes the code more difficult to reason about, and don't buy that it is any more accessible
Closes #2055. Supersedes #2056. When a file/image/video/audio block has a caption, render the wrapper as <figure> with a <figcaption> instead of a <div>+<p>. This matches the WCAG-recommended semantic for caption-content association and removes the need for ad-hoc ARIA fallbacks. Image alt text logic is also tightened: - caption present -> alt="" (the figcaption is the accessible name; this avoids screen readers double-announcing the caption) - no caption, name present -> alt={name} - neither -> alt="" (decorative; aria-hidden was dropped because it would have removed unintentionally-unlabeled images from the accessibility tree entirely) Co-Authored-By: Cyril G <c.gromoff@gmail.com> Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Purpose
Improve accessibility and semantic HTML by applying the appropriate structure when captions are present, to comply with WCAG requirement.
issue : 2055
Proposal
figcaptiontagfiguretagfigcaptiontag