MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useCopyImport

Function useCopyImport

packages/dev/s2-docs/src/IconSearchView.tsx:62–92  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

60}
61
62export function useCopyImport() {
63 let [copiedId, setCopiedId] = useState<string | null>(null);
64 let timeout = useRef<ReturnType<typeof setTimeout> | null>(null);
65
66 useEffect(() => {
67 return () => {
68 if (timeout.current) {
69 clearTimeout(timeout.current);
70 }
71 };
72 }, []);
73
74 let handleCopyImport = useCallback((id: string) => {
75 if (timeout.current) {
76 clearTimeout(timeout.current);
77 }
78 // Use underscore prefix for names starting with a number (invalid JS identifier)
79 let importName = id.replace(/^(\d)/, '_$1');
80 navigator.clipboard
81 .writeText(`import ${importName} from '@react-spectrum/s2/icons/${id}';`)
82 .then(() => {
83 setCopiedId(id);
84 timeout.current = setTimeout(() => setCopiedId(null), 2000);
85 })
86 .catch(() => {
87 ToastQueue.negative('Failed to copy import statement.');
88 });
89 }, []);
90
91 return {copiedId, handleCopyImport};
92}
93
94interface IconListBoxProps {
95 items: typeof iconList;

Callers 2

IconSearchViewFunction · 0.70
IconsPageSearchFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected