Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-alt-text-behavior.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

Adjusted captions so the alt text on an image is set when captions are hidden, and is otherwise blank, to improve screen reader behavior.
1 change: 1 addition & 0 deletions src/app/components/RenderMessageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ function RenderMessageContentInternal({
return renderCaptionedAttachment(
<MImage
content={content as Record<string, never> & { msgtype: MsgType.Image }}
suppressInlineImageAlt={captionPosition !== CaptionPosition.Hidden}
renderImageContent={(imageProps) => (
<ImageContent
{...imageProps}
Expand Down
14 changes: 12 additions & 2 deletions src/app/components/message/MsgTypeRenderers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ export function MNotice({

type RenderImageContentProps = {
body: string;
imageAlt?: string;
filename?: string;
info?: IImageInfo & IThumbnailContent;
mimeType?: string;
Expand All @@ -396,13 +397,21 @@ type MImageProps = {
content: IImageContent;
renderImageContent: (props: RenderImageContentProps) => ReactNode;
outlined?: boolean;
suppressInlineImageAlt?: boolean;
};
export function MImage({ content, renderImageContent, outlined }: MImageProps) {
export function MImage({
content,
renderImageContent,
outlined,
suppressInlineImageAlt,
}: MImageProps) {
const imgInfo = content?.info;
const mxcUrl = content.file?.url ?? content.url;
if (typeof mxcUrl !== 'string') {
return <BrokenContent body={content.body ?? content.filename} />;
}
const description =
(typeof content.body === 'string' && content.body.trim()) || content.filename || 'Image';
const MAX_SIZE = 400;
const imgW = imgInfo?.w ?? MAX_SIZE;
const imgH = imgInfo?.h ?? MAX_SIZE;
Expand All @@ -426,7 +435,8 @@ export function MImage({ content, renderImageContent, outlined }: MImageProps) {
}}
>
{renderImageContent({
body: content.filename || 'Image',
body: description,
...(suppressInlineImageAlt ? { imageAlt: '' } : {}),
info: imgInfo,
mimeType: imgInfo?.mimetype,
url: mxcUrl,
Expand Down
4 changes: 3 additions & 1 deletion src/app/components/message/content/ImageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
};
export type ImageContentProps = {
body: string;
imageAlt?: string;
mimeType?: string;
url: string;
info?: IImageInfo;
Expand All @@ -91,6 +92,7 @@
className,
style,
body,
imageAlt,
mimeType,
url,
info,
Expand Down Expand Up @@ -160,9 +162,9 @@
if (!mediaUrl || cancelled) return;
setViewerFullSrc(mediaUrl);
})();
return () => {
cancelled = true;
};

Check warning on line 167 in src/app/components/message/content/ImageContent.tsx

View workflow job for this annotation

GitHub Actions / Lint

typescript-eslint(consistent-return)

Function expected no return value.
}, [viewer, matrixThumbnailMaxEdge, encInfo, url, mx, useAuthentication]);

const handleLoad = () => {
Expand Down Expand Up @@ -200,7 +202,7 @@
: isContained
? { minHeight: containedReserveStrip ? toRem(stripMin) : undefined }
: hasDimensions
? { aspectRatio: `${info!.w} / ${info!.h}` }

Check warning on line 205 in src/app/components/message/content/ImageContent.tsx

View workflow job for this annotation

GitHub Actions / Lint

typescript-eslint(no-unnecessary-type-assertion)

This assertion is unnecessary since it does not change the type of the expression.

Check warning on line 205 in src/app/components/message/content/ImageContent.tsx

View workflow job for this annotation

GitHub Actions / Lint

typescript-eslint(no-unnecessary-type-assertion)

This assertion is unnecessary since it does not change the type of the expression.
: { minHeight: '150px' };

const fillPreviewSlotStyle = fillsSlot
Expand Down Expand Up @@ -283,7 +285,7 @@
style={{ width: '100%' }}
>
{renderImage({
alt: body,
alt: imageAlt !== undefined ? imageAlt : body,
title: body,
src: srcState.data,
onLoad: handleLoad,
Expand Down
Loading