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

Function useCopyImport

packages/dev/s2-docs/src/IllustrationCards.tsx:150–187  ·  view source on GitHub ↗
(variant: string, gradientStyle: string)

Source from the content-addressed store, hash-verified

148}
149
150function useCopyImport(variant: string, gradientStyle: string) {
151 let [copiedId, setCopiedId] = useState<string | null>(null);
152 let timeout = useRef<ReturnType<typeof setTimeout> | null>(null);
153
154 useEffect(() => {
155 return () => {
156 if (timeout.current) {
157 clearTimeout(timeout.current);
158 }
159 };
160 }, []);
161
162 let handleCopyImport = useCallback(
163 (id: string) => {
164 if (timeout.current) {
165 clearTimeout(timeout.current);
166 }
167 // Use underscore prefix for names starting with a number (invalid JS identifier)
168 let importName = id.replace(/^(\d)/, '_$1');
169 let importText =
170 variant === 'gradient'
171 ? `import ${importName} from '@react-spectrum/s2/illustrations/gradient/${gradientStyle}/${id}';`
172 : `import ${importName} from '@react-spectrum/s2/illustrations/linear/${id}';`;
173 navigator.clipboard
174 .writeText(importText)
175 .then(() => {
176 setCopiedId(id);
177 timeout.current = setTimeout(() => setCopiedId(null), 2000);
178 })
179 .catch(() => {
180 ToastQueue.negative('Failed to copy import statement.');
181 });
182 },
183 [variant, gradientStyle]
184 );
185
186 return {copiedId, handleCopyImport};
187}
188
189function IllustrationList({variant, gradientStyle}) {
190 let items = use(loadIllustrations(variant, gradientStyle));

Callers 1

IllustrationListFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected