|
| 1 | +import * as React from 'react'; |
| 2 | +import { useTimeout } from '@fluentui/react-utilities'; |
| 3 | +import { useAnnounce, useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts'; |
| 4 | +import type { AnnounceOptions } from '@fluentui/react-shared-contexts'; |
| 5 | +import { AriaLiveAnnounceFn } from '../AriaLiveAnnouncer/AriaLiveAnnouncer.types'; |
| 6 | + |
| 7 | +type Message = { |
| 8 | + message: string; |
| 9 | + options: AnnounceOptions; |
| 10 | +}; |
| 11 | + |
| 12 | +const valueMutationOptions = { |
| 13 | + attributes: true, |
| 14 | + subtree: true, |
| 15 | + characterData: true, |
| 16 | + attributeFilter: ['value'], |
| 17 | +}; |
| 18 | + |
| 19 | +interface TypingAnnounceReturn<TInputElement extends HTMLElement = HTMLElement> { |
| 20 | + typingAnnounce: AriaLiveAnnounceFn; |
| 21 | + inputRef: React.RefObject<TInputElement>; |
| 22 | +} |
| 23 | + |
| 24 | +export function useTypingAnnounce< |
| 25 | + TInputElement extends HTMLElement = HTMLElement, |
| 26 | +>(): TypingAnnounceReturn<TInputElement> { |
| 27 | + const { targetDocument } = useFluent(); |
| 28 | + const { announce } = useAnnounce(); |
| 29 | + |
| 30 | + const inputRef = React.useRef<TInputElement>(null); |
| 31 | + const observer = React.useRef<MutationObserver>(); |
| 32 | + const [setTypingTimeout, clearTypingTimeout] = useTimeout(); |
| 33 | + const messageQueue = React.useRef<Message[]>([]); |
| 34 | + |
| 35 | + const callback: MutationCallback = React.useCallback( |
| 36 | + (mutationList, mutationObserver) => { |
| 37 | + setTypingTimeout(() => { |
| 38 | + messageQueue.current.forEach(({ message, options }) => { |
| 39 | + announce(message, options); |
| 40 | + }); |
| 41 | + messageQueue.current.length = 0; |
| 42 | + mutationObserver.disconnect(); |
| 43 | + }, 500); |
| 44 | + }, |
| 45 | + [announce, setTypingTimeout], |
| 46 | + ); |
| 47 | + |
| 48 | + const typingAnnounce: AriaLiveAnnounceFn = React.useCallback( |
| 49 | + (message: string, options: AnnounceOptions = {}) => { |
| 50 | + messageQueue.current.push({ message, options }); |
| 51 | + |
| 52 | + if (inputRef.current && observer.current) { |
| 53 | + observer.current.observe(inputRef.current, valueMutationOptions); |
| 54 | + } |
| 55 | + |
| 56 | + setTypingTimeout(() => { |
| 57 | + observer.current && callback([], observer.current); |
| 58 | + }, 500); |
| 59 | + }, |
| 60 | + [callback, inputRef, setTypingTimeout], |
| 61 | + ); |
| 62 | + |
| 63 | + React.useEffect(() => { |
| 64 | + const win = targetDocument?.defaultView; |
| 65 | + if (!win) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (!observer.current) { |
| 70 | + observer.current = new win.MutationObserver(callback); |
| 71 | + } |
| 72 | + |
| 73 | + return () => { |
| 74 | + // Clean up the observer when the component unmounts |
| 75 | + if (observer.current) { |
| 76 | + observer.current.disconnect(); |
| 77 | + clearTypingTimeout(); |
| 78 | + } |
| 79 | + }; |
| 80 | + }, [callback, clearTypingTimeout, targetDocument]); |
| 81 | + |
| 82 | + return { typingAnnounce, inputRef }; |
| 83 | +} |
0 commit comments