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
30 changes: 30 additions & 0 deletions src/__tests__/accessibility/announcements.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { render, screen, act } from '@testing-library/react';
import { ScreenReaderAnnouncer } from '@/components/accessibility/ScreenReaderAnnouncer';
import { announceToScreenReader } from '@/lib/accessibility/announcements';

describe('Accessible Announcements for Wallet & Escrow Updates', () => {
it('renders polite and assertive live regions correctly', () => {
render(<ScreenReaderAnnouncer />);

act(() => {
announceToScreenReader('Escrow milestone funded successfully', 'polite');
});

const politeRegion = screen.getByText('Escrow milestone funded successfully');
expect(politeRegion).toBeInTheDocument();
expect(politeRegion.closest('div')).toHaveAttribute('aria-live', 'polite');
});

it('handles assertive error announcements for failed transactions', () => {
render(<ScreenReaderAnnouncer />);

act(() => {
announceToScreenReader('Wallet transaction failed', 'assertive');
});

const assertiveRegion = screen.getByText('Wallet transaction failed');
expect(assertiveRegion).toBeInTheDocument();
expect(assertiveRegion.closest('div')).toHaveAttribute('aria-live', 'assertive');
});
});
31 changes: 31 additions & 0 deletions src/components/accessibility/ScreenReaderAnnouncer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use client';

import React from 'react';
import { useScreenReaderAnnouncer } from '@/lib/accessibility/announcements';

export function ScreenReaderAnnouncer() {
const announcement = useScreenReaderAnnouncer();

if (!announcement) return null;

return (
<>
<div
aria-live="polite"
aria-atomic="true"
className="sr-only"
style={{ position: 'absolute', width: 1, height: 1, padding: 0, margin: -1, overflow: 'hidden', clip: 'rect(0, 0, 0, 0)', whiteSpace: 'nowrap', border: 0 }}
>
{announcement.priority === 'polite' ? announcement.message : ''}
</div>
<div
aria-live="assertive"
aria-atomic="true"
className="sr-only"
style={{ position: 'absolute', width: 1, height: 1, padding: 0, margin: -1, overflow: 'hidden', clip: 'rect(0, 0, 0, 0)', whiteSpace: 'nowrap', border: 0 }}
>
{announcement.priority === 'assertive' ? announcement.message : ''}
</div>
</>
);
}
36 changes: 36 additions & 0 deletions src/lib/accessibility/announcements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useEffect, useState } from 'react';

export type AnnouncementPriority = 'polite' | 'assertive';

export interface Announcement {
id: string;
message: string;
priority: AnnouncementPriority;
}

let globalAnnouncerListener: ((announcement: Announcement) => void) | null = null;

export function announceToScreenReader(message: string, priority: AnnouncementPriority = 'polite') {
if (globalAnnouncerListener) {
globalAnnouncerListener({
id: Math.random().toString(36).substring(2, 9),
message,
priority,
});
}
}

export function useScreenReaderAnnouncer() {
const [currentAnnouncement, setCurrentAnnouncement] = useState<Announcement | null>(null);

useEffect(() => {
globalAnnouncerListener = (announcement) => {
setCurrentAnnouncement(announcement);
};
return () => {
globalAnnouncerListener = null;
};
}, []);

return currentAnnouncement;
}
Loading