MCPcopy
hub / github.com/adobe/react-spectrum / useDescription

Function useDescription

packages/react-aria/src/utils/useDescription.ts:20–56  ·  view source on GitHub ↗
(description?: string)

Source from the content-addressed store, hash-verified

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

Callers 13

useLongPressFunction · 0.90
useCalendarCellFunction · 0.90
useTableColumnResizeFunction · 0.90
useTableColumnHeaderFunction · 0.90
useTableFunction · 0.90
useDragFunction · 0.90
useDraggableItemFunction · 0.90
useVirtualDropFunction · 0.90
useTagFunction · 0.90
useDateRangePickerFunction · 0.90
useDateFieldFunction · 0.90

Calls 5

createElementMethod · 0.80
appendChildMethod · 0.80
deleteMethod · 0.80
removeMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected