Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,27 @@ const ScreenReaderLiveRegion: React.FC<ScreenReaderLiveRegionProps> = ({ liveCon
}

// Use live content if available, otherwise extract from DOM
const rawText = liveContent[id] || getTextFromDOM(id);
const text = cleanUpText(rawText || "A new message");
let text = "";
if (liveContent[id]) {
text = cleanUpText(liveContent[id]);
} else {
const domResult = getTextFromDOM(id);
if (domResult.elementExists) {
// Element exists in DOM but may have no accessible text
// For visible elements without text (like images), provide a generic announcement
text = cleanUpText(domResult.text || "A new message");
} else {
// Element doesn't exist - this is a data-only message that shouldn't be announced
text = "";
}
Comment on lines +66 to +74
}

setLiveMessage({ id, text });
// Only announce if we have meaningful content
if (text.trim()) {
setLiveMessage({ id, text });
} else {
setLiveMessage(null); // Stay silent for data-only messages
}
Comment thread
sushmi21 marked this conversation as resolved.
Comment thread
sushmi21 marked this conversation as resolved.
}, 100);

return () => clearTimeout(timeout);
Expand Down
14 changes: 9 additions & 5 deletions src/webchat-ui/utils/live-region-announcement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,15 @@ export const extractTextForScreenReader = (root: HTMLElement): string => {
* - Using the extractTextForScreenReader function to get the text content
*
* @param id - The message ID to extract text for
* @returns The extracted text or a default message if not found
* @returns An object with the extracted text and whether the element exists
*/
export const getTextFromDOM = (id: string): string => {
export const getTextFromDOM = (id: string): { text: string; elementExists: boolean } => {
const messageElement = document.querySelector(`[data-message-id="${id}"]`);
return messageElement
? extractTextForScreenReader(messageElement as HTMLElement)
: "A new message";
if (!messageElement) {
return { text: "", elementExists: false }; // Element doesn't exist - data-only message
}
Comment on lines +91 to +93

const extractedText = extractTextForScreenReader(messageElement as HTMLElement);
return { text: extractedText, elementExists: true }; // Element exists
};

Loading