convert to figure/figcaption if caption exists#2056
convert to figure/figcaption if caption exists#2056Ovgodd wants to merge 2 commits intoTypeCellOS:mainfrom
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
Purpose
Improve accessibility and semantic HTML by applying the appropriate structure when captions are present, to comply with WCAG requirement.
issue : 2055
Proposal
figcaptiontagfiguretagfigcaptiontag