Skip to content
Closed
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
4 changes: 2 additions & 2 deletions core/block_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,7 @@ export class BlockSvg
newLoc?: Coordinate,
) {
if (isCanceled) {
aria.announceDynamicAriaState('Canceled movement');
aria.announceDynamicAriaState('Canceled movement.');
return;
}
if (!isMoving) return;
Expand All @@ -2036,7 +2036,7 @@ export class BlockSvg

// If the block is currently being moved, announce the new block label so that the user understands where it is now.
// TODO: Figure out how much recomputeAriaTreeItemDetailsRecursively needs to anticipate position if it won't be reannounced, and how much of that context should be included in the liveannouncement.
aria.announceDynamicAriaState(announcementContext.join(' '));
aria.announceDynamicAriaState(announcementContext.join(' ') + '.');
} else if (newLoc) {
// The block is being freely dragged.
aria.announceDynamicAriaState(
Expand Down
4 changes: 2 additions & 2 deletions core/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ export function inject(
);

// See: https://stackoverflow.com/a/48590836 for a reference.
const ariaAnnouncementSpan = document.createElement('span');
const ariaAnnouncementSpan = document.createElement('div');
ariaAnnouncementSpan.id = 'blocklyAriaAnnounce';
dom.addClass(ariaAnnouncementSpan, 'hiddenForAria');
aria.setState(ariaAnnouncementSpan, aria.State.LIVE, 'assertive');
aria.setState(ariaAnnouncementSpan, aria.State.LIVE, 'polite');
subContainer.appendChild(ariaAnnouncementSpan);

return workspace;
Expand Down
8 changes: 5 additions & 3 deletions core/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ export class Toast {
workspace.getInjectionDiv().appendChild(toast);
toast.dataset.toastId = options.id;
toast.className = CLASS_NAME;
aria.setRole(toast, aria.Role.STATUS);
aria.setState(toast, aria.State.LIVE, assertiveness);

const messageElement = toast.appendChild(document.createElement('div'));
messageElement.className = MESSAGE_CLASS_NAME;
messageElement.innerText = message;
Expand Down Expand Up @@ -157,6 +154,11 @@ export class Toast {
toast.addEventListener('mouseleave', setToastTimeout);
setToastTimeout();

aria.announceDynamicAriaState(message, {
assertiveness,
role: aria.Role.STATUS,
});

return toast;
}

Expand Down
33 changes: 29 additions & 4 deletions core/utils/aria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ export function getState(element: Element, stateName: State): string | null {
return element.getAttribute(attrStateName);
}

let ariaAnnounceTimeout: ReturnType<typeof setTimeout>;
let ariaAnnounceHtml = '';
let addBreakingSpace = false;

/**
* Assertively requests that the specified text be read to the user if a screen
* reader is currently active.
Expand All @@ -217,10 +221,31 @@ export function getState(element: Element, stateName: State): string | null {
*
* @param text The text to read to the user.
*/
export function announceDynamicAriaState(text: string) {
const ariaAnnouncementSpan = document.getElementById('blocklyAriaAnnounce');
if (!ariaAnnouncementSpan) {
export function announceDynamicAriaState(
text: string,
options: {
assertiveness: string;
role: Role | null;
} = {
assertiveness: 'polite',
role: null,
},
) {
const ariaAnnouncementContainer = document.getElementById(
'blocklyAriaAnnounce',
);
if (!ariaAnnouncementContainer) {
throw new Error('Expected element with id blocklyAriaAnnounce to exist.');
}
ariaAnnouncementSpan.innerHTML = text;
const {assertiveness, role} = options;
ariaAnnouncementContainer.innerHTML = '';
setState(ariaAnnouncementContainer, State.LIVE, assertiveness);
ariaAnnounceHtml += `<p>${text}${addBreakingSpace ? '&nbsp;' : ''}</p>`;
addBreakingSpace = !addBreakingSpace;
clearTimeout(ariaAnnounceTimeout);
ariaAnnounceTimeout = setTimeout(() => {
setRole(ariaAnnouncementContainer, role);
ariaAnnouncementContainer.innerHTML = ariaAnnounceHtml;
ariaAnnounceHtml = '';
}, 10);
}
Loading