-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(react-headless-components): use AriaLiveAnnouncer for Toast component and update tests #36261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dmytrokirpa
wants to merge
2
commits into
microsoft:master
Choose a base branch
from
dmytrokirpa:feat/headless-toast-followup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+205
−77
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-headless-components-preview-0c06bf3c-fc53-48f0-8c19-5d507ad82db8.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "patch", | ||
| "comment": "feat: use AriaLiveAnnouncer for Toast component and update tests", | ||
| "packageName": "@fluentui/react-headless-components-preview", | ||
| "email": "dmytrokirpa@microsoft.com", | ||
| "dependentChangeType": "patch" | ||
| } | ||
159 changes: 159 additions & 0 deletions
159
...react-headless-components-preview/library/src/components/Toast/AriaLive/AriaLive.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import * as React from 'react'; | ||
| import { act, render } from '@testing-library/react'; | ||
| import { AnnounceProvider } from '@fluentui/react-shared-contexts'; | ||
| import { AriaLiveAnnouncer } from '@fluentui/react-aria'; | ||
| import type { ToastAnnounce } from '@fluentui/react-toast'; | ||
| import { AriaLive } from './AriaLive'; | ||
|
|
||
| describe('AriaLive', () => { | ||
| it('renders nothing visible', () => { | ||
| const { container } = render( | ||
| <AnnounceProvider value={{ announce: jest.fn() }}> | ||
| <AriaLive announceRef={React.createRef()} /> | ||
| </AnnounceProvider>, | ||
| ); | ||
|
|
||
| expect(container.firstChild).toBeNull(); | ||
| }); | ||
|
|
||
| it('populates announceRef with a callable function once mounted', () => { | ||
| const announceRef = React.createRef<ToastAnnounce>(); | ||
|
|
||
| render( | ||
| <AnnounceProvider value={{ announce: jest.fn() }}> | ||
| <AriaLive announceRef={announceRef} /> | ||
| </AnnounceProvider>, | ||
| ); | ||
|
|
||
| expect(typeof announceRef.current).toBe('function'); | ||
| }); | ||
|
|
||
| it('forwards calls to the surrounding AnnounceProvider', () => { | ||
| const announceSpy = jest.fn(); | ||
| const announceRef = React.createRef<ToastAnnounce>(); | ||
|
|
||
| render( | ||
| <AnnounceProvider value={{ announce: announceSpy }}> | ||
| <AriaLive announceRef={announceRef} /> | ||
| </AnnounceProvider>, | ||
| ); | ||
|
|
||
| act(() => { | ||
| announceRef.current?.('hello', { politeness: 'assertive' }); | ||
| }); | ||
|
|
||
| expect(announceSpy).toHaveBeenCalledTimes(1); | ||
| expect(announceSpy).toHaveBeenCalledWith('hello', { polite: false }); | ||
| }); | ||
|
|
||
| it('adapts politeness "polite" → polite: true', () => { | ||
| const announceSpy = jest.fn(); | ||
| const announceRef = React.createRef<ToastAnnounce>(); | ||
|
|
||
| render( | ||
| <AnnounceProvider value={{ announce: announceSpy }}> | ||
| <AriaLive announceRef={announceRef} /> | ||
| </AnnounceProvider>, | ||
| ); | ||
|
|
||
| act(() => { | ||
| announceRef.current?.('polite message', { politeness: 'polite' }); | ||
| }); | ||
|
|
||
| expect(announceSpy).toHaveBeenLastCalledWith('polite message', { polite: true }); | ||
| }); | ||
|
|
||
| it('adapts politeness "assertive" → polite: false', () => { | ||
| const announceSpy = jest.fn(); | ||
| const announceRef = React.createRef<ToastAnnounce>(); | ||
|
|
||
| render( | ||
| <AnnounceProvider value={{ announce: announceSpy }}> | ||
| <AriaLive announceRef={announceRef} /> | ||
| </AnnounceProvider>, | ||
| ); | ||
|
|
||
| act(() => { | ||
| announceRef.current?.('assertive message', { politeness: 'assertive' }); | ||
| }); | ||
|
|
||
| expect(announceSpy).toHaveBeenLastCalledWith('assertive message', { polite: false }); | ||
| }); | ||
|
|
||
| it('updates the bound ref when the context announce changes', () => { | ||
| const first = jest.fn(); | ||
| const second = jest.fn(); | ||
| const announceRef = React.createRef<ToastAnnounce>(); | ||
|
|
||
| const { rerender } = render( | ||
| <AnnounceProvider value={{ announce: first }}> | ||
| <AriaLive announceRef={announceRef} /> | ||
| </AnnounceProvider>, | ||
| ); | ||
|
|
||
| act(() => { | ||
| announceRef.current?.('msg-1', { politeness: 'polite' }); | ||
| }); | ||
| expect(first).toHaveBeenCalledWith('msg-1', { polite: true }); | ||
|
|
||
| rerender( | ||
| <AnnounceProvider value={{ announce: second }}> | ||
| <AriaLive announceRef={announceRef} /> | ||
| </AnnounceProvider>, | ||
| ); | ||
|
|
||
| act(() => { | ||
| announceRef.current?.('msg-2', { politeness: 'polite' }); | ||
| }); | ||
| expect(second).toHaveBeenCalledWith('msg-2', { polite: true }); | ||
| expect(first).toHaveBeenCalledTimes(1); // no double-fire | ||
| }); | ||
|
|
||
| it('is a no-op when rendered without an AnnounceProvider ancestor', () => { | ||
| const announceRef = React.createRef<ToastAnnounce>(); | ||
|
|
||
| render(<AriaLive announceRef={announceRef} />); | ||
|
|
||
| expect(() => { | ||
| act(() => { | ||
| announceRef.current?.('orphan', { politeness: 'polite' }); | ||
| }); | ||
| }).not.toThrow(); | ||
| }); | ||
|
|
||
| describe('end-to-end via AriaLiveAnnouncer', () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| it('routes assertive announcements into the document.body live region', () => { | ||
| const announceRef = React.createRef<ToastAnnounce>(); | ||
|
|
||
| render( | ||
| <AriaLiveAnnouncer> | ||
| <AriaLive announceRef={announceRef} /> | ||
| </AriaLiveAnnouncer>, | ||
| ); | ||
|
|
||
| act(() => { | ||
| announceRef.current?.('end-to-end message', { politeness: 'assertive' }); | ||
| }); | ||
| // useDomAnnounce schedules a 0ms cycle that commits the message into a | ||
| // child span of the live region. | ||
| act(() => { | ||
| jest.advanceTimersByTime(0); | ||
| }); | ||
|
|
||
| const liveRegion = document.body.querySelector('[aria-live="assertive"]'); | ||
| // useDomAnnounce sets the message via `.innerText` on a wrapping span | ||
| // (a workaround for some SR/browsers); jsdom stores `innerText` but | ||
| // doesn't reflect it to `textContent`, so we read it directly. | ||
| const wrappingSpan = liveRegion?.querySelector('span'); | ||
| expect(wrappingSpan?.innerText).toContain('end-to-end message'); | ||
| }); | ||
| }); | ||
| }); |
89 changes: 17 additions & 72 deletions
89
...ents/react-headless-components-preview/library/src/components/Toast/AriaLive/AriaLive.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,85 +1,30 @@ | ||
| 'use client'; | ||
|
|
||
| import * as React from 'react'; | ||
| import { createPriorityQueue, useEventCallback, useTimeout } from '@fluentui/react-utilities'; | ||
| import type { ToastAnnounce, ToastAnnounceOptions, ToastLiveMessage } from '@fluentui/react-toast'; | ||
|
|
||
| /** Duration the message stays in DOM so screen readers register the change. */ | ||
| const MESSAGE_DURATION = 500; | ||
|
|
||
| const visuallyHiddenStyle: React.CSSProperties = { | ||
| position: 'absolute', | ||
| width: '1px', | ||
| height: '1px', | ||
| margin: '-1px', | ||
| padding: 0, | ||
| overflow: 'hidden', | ||
| clip: 'rect(0px, 0px, 0px, 0px)', | ||
| }; | ||
| import type { JSXElement } from '@fluentui/react-utilities'; | ||
| import { useAnnounce } from '@fluentui/react-shared-contexts'; | ||
| import type { ToastAnnounce } from '@fluentui/react-toast'; | ||
|
|
||
| export type AriaLiveProps = { | ||
| /** | ||
| * Receives the announce function resolved from `AnnounceContext`. | ||
| * Requires an `AnnounceProvider` ancestor (e.g. `<AriaLiveAnnouncer>`); otherwise announcements are a no-op. | ||
| */ | ||
| announceRef: React.Ref<ToastAnnounce>; | ||
|
dmytrokirpa marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| /** | ||
| * Renders two visually-hidden `aria-live` regions (one polite, one assertive) | ||
| * and exposes an imperative `announce(message, { politeness })` API via | ||
| * `announceRef`. No Griffel; visually-hidden via inline styles only. | ||
| */ | ||
| export const AriaLive = ({ announceRef }: AriaLiveProps): React.ReactNode => { | ||
| const [currentMessage, setCurrentMessage] = React.useState<ToastLiveMessage | undefined>(undefined); | ||
| // Date.now() loses ordering when announce fires multiple times in the same tick. | ||
| const order = React.useRef(0); | ||
| const [messageQueue] = React.useState(() => | ||
| createPriorityQueue<ToastLiveMessage>((a, b) => { | ||
| if (a.politeness === b.politeness) { | ||
| return a.createdAt - b.createdAt; | ||
| } | ||
| return a.politeness === 'assertive' ? -1 : 1; | ||
| }), | ||
| ); | ||
|
|
||
| const announce = useEventCallback((message: string, options: ToastAnnounceOptions) => { | ||
| const { politeness } = options; | ||
| if (message === currentMessage?.message) { | ||
| return; | ||
| } | ||
| const liveMessage: ToastLiveMessage = { message, politeness, createdAt: order.current++ }; | ||
| if (!currentMessage) { | ||
| setCurrentMessage(liveMessage); | ||
| } else { | ||
| messageQueue.enqueue(liveMessage); | ||
| } | ||
| }); | ||
| export const AriaLive = (props: AriaLiveProps): JSXElement | null => { | ||
| const { announce } = useAnnounce(); | ||
|
|
||
| const [setMessageTimeout, clearMessageTimeout] = useTimeout(); | ||
|
|
||
| React.useEffect(() => { | ||
| setMessageTimeout(() => { | ||
| if (messageQueue.peek()) { | ||
| setCurrentMessage(messageQueue.dequeue()); | ||
| } else { | ||
| setCurrentMessage(undefined); | ||
| } | ||
| }, MESSAGE_DURATION); | ||
| return () => clearMessageTimeout(); | ||
| }, [currentMessage, messageQueue, setMessageTimeout, clearMessageTimeout]); | ||
|
|
||
| React.useImperativeHandle(announceRef, () => announce); | ||
|
|
||
| const politeMessage = currentMessage?.politeness === 'polite' ? currentMessage.message : undefined; | ||
| const assertiveMessage = currentMessage?.politeness === 'assertive' ? currentMessage.message : undefined; | ||
|
|
||
| return ( | ||
| <> | ||
| <div aria-live="assertive" style={visuallyHiddenStyle}> | ||
| {assertiveMessage} | ||
| </div> | ||
| <div aria-live="polite" style={visuallyHiddenStyle}> | ||
| {politeMessage} | ||
| </div> | ||
| </> | ||
| React.useImperativeHandle<ToastAnnounce, ToastAnnounce>( | ||
| props.announceRef, | ||
| () => (message, options) => { | ||
| announce(message, { polite: options?.politeness === 'polite' }); | ||
| }, | ||
| [announce], | ||
| ); | ||
|
|
||
| return null; | ||
| }; | ||
|
|
||
| AriaLive.displayName = 'AriaLive'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.