You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The log role is used to identify an element that creates a live region.
Using JavaScript, it is possible to dynamically change parts of a page without requiring the entire page to reload. ARIA live regions rovide a way to programmatically expose dynamic content changes in a way that can be announced by assistive technologies.
// 1) Create Context: AriaLogProvider.tsximport{createContext,FC,useCallback,useContext,useState}from'react';typeRemoveMessageFn=()=>void;interfaceIAriaLogContext{addMessage: (message: string)=>RemoveMessageFn;}constAriaLogContext=createContext<IAriaLogContext|undefined>(undefined);exportconstAriaLogProvider: FC=({ children })=>{const[messages,setMessages]=useState<string[]>([]);constaddMessage=useCallback((message: string)=>{setMessages((existing)=>[message, ...existing]);return()=>setMessages((existing)=>existing.filter((otherMessage)=>otherMessage!==message));},[]);return(<AriaLogContext.Providervalue={{ addMessage }}><divrole='log'className='aria-log'>{messages.map((message)=>(<pkey={message}>{message}</p>))}</div>{children}</AriaLogContext.Provider>);};exportconstuseAriaLog=()=>{constcontext=useContext(AriaLogContext);if(!context){thrownewError("Missing context for 'useAriaLog' hook.");}returncontext;};
// 2) Make aria-message-log only available to screen readers.aria-log {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
// 3) Create AriaMessage Componentimport{useEffect,VFC}from'react';import{useAriaLog}from'./AriaLogProvider';interfaceAriaMessageProps{children: string;}exportconstAriaMessage: VFC<AriaMessageProps>=({children: message})=>{const{ addMessage }=useAriaLog();// message is automatically removed with remove function that is returned from addMessage()useEffect(()=>addMessage(message),[addMessage,message]);returnnull;};
// 4) Example of implementation in Page Componentimport{FC}from'react';import{AriaMessage}from'../../atoms';import{Page}from'../../../utils/pages';import{Sidebar}from'../../organisms/sidebar';interfacePageProps{page: Page;}exportconstPage: FC<PageProps>=({ page, children })=>{return(<divclassName='page'><divclassName='page__sidebar'><SidebarcurrentPage={page}/></div><h2className='page__title'>Elterngeldrechner mit Planer</h2><AriaMessage>{page.text}</AriaMessage><divclassName='page__content'>{children}</div></div>);};exportinterfacePage{text: string;route: string;}exporttypePages=Record<string,Page>;exportconstpages: Pages={home: {text: 'Home',route: '/',},// ...};constHome=()=>{// ...return<Pagepage={pages.home}>{/* ... */}</Page>;};
ARIA Attributes in JSX Elements
aria-label is important, when content of HTML element is not self-explanatory; aria-label explicitly tells what the button will do
aria-controls identifies an element whose content is controlled by the current element: here name is form field whose value is changed by a button click