(description?: string)
| 18 | const descriptionNodes = new Map<string, {refCount: number; element: Element}>(); |
| 19 | |
| 20 | export function useDescription(description?: string): AriaLabelingProps { |
| 21 | let [id, setId] = useState<string | undefined>(() => |
| 22 | description ? descriptionNodes.get(description)?.element.id : undefined |
| 23 | ); |
| 24 | |
| 25 | useLayoutEffect(() => { |
| 26 | if (!description) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | let desc = descriptionNodes.get(description); |
| 31 | if (!desc) { |
| 32 | let id = `react-aria-description-${descriptionId++}`; |
| 33 | setId(id); |
| 34 | |
| 35 | let node = document.createElement('div'); |
| 36 | node.id = id; |
| 37 | node.style.display = 'none'; |
| 38 | node.textContent = description; |
| 39 | document.body.appendChild(node); |
| 40 | desc = {refCount: 0, element: node}; |
| 41 | descriptionNodes.set(description, desc); |
| 42 | } else { |
| 43 | setId(desc.element.id); |
| 44 | } |
| 45 | |
| 46 | desc.refCount++; |
| 47 | return () => { |
| 48 | if (desc && --desc.refCount === 0) { |
| 49 | desc.element.remove(); |
| 50 | descriptionNodes.delete(description); |
| 51 | } |
| 52 | }; |
| 53 | }, [description]); |
| 54 | |
| 55 | return { |
| 56 | 'aria-describedby': description ? id : undefined |
| 57 | }; |
| 58 | } |
no test coverage detected