-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82fab63
commit b0cc279
Showing
4 changed files
with
75 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,42 @@ | ||
/// <reference types="vite-plugin-svgr/client" /> | ||
import { useEffect, useState } from "react"; | ||
import { useDynamicSvgImport } from "./useDynamicSvgImport"; | ||
import { useEffect, useState } from "react"; | ||
interface IProps { | ||
iconName: string; | ||
size: number; | ||
onCompleted: ( | ||
name: string, | ||
SvgIcon: React.FC<React.SVGProps<SVGSVGElement>> | undefined, | ||
) => void; | ||
onError: (err: unknown) => void; | ||
className?: string; | ||
} | ||
|
||
const SvgIcon = (props: IProps) => { | ||
const { iconName, size, className } = props; | ||
|
||
const { error, loading, SvgIcon } = useDynamicSvgImport(iconName); | ||
const [isMounted, setIsMounted] = useState(false); | ||
const { iconName, onCompleted, onError, size, className } = props; | ||
const [refreshed, setRefreshed] = useState<boolean>(false); | ||
useEffect(() => { | ||
if (SvgIcon) { | ||
setIsMounted(true); | ||
} else { | ||
setIsMounted(false); | ||
} | ||
}, [SvgIcon, iconName]); | ||
setTimeout(() => { | ||
setRefreshed(true); | ||
}, 1); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
{loading && ( | ||
<div className="h-8 w-8 animate-pulse rounded-full bg-slate-400"> | ||
{iconName} | ||
</div> | ||
)} | ||
{error && ( | ||
<div className="h-8 w-8 animate-pulse rounded-full bg-slate-400"> | ||
{iconName} | ||
</div> | ||
)} | ||
{SvgIcon && isMounted ? ( | ||
<SvgIcon className={className} width={size} height={size} /> | ||
) : ( | ||
iconName | ||
)} | ||
</> | ||
); | ||
const { error, loading, SvgIcon } = useDynamicSvgImport(iconName, { | ||
onCompleted, | ||
onError, | ||
}); | ||
if (error) { | ||
return <p>error</p>; | ||
} | ||
if (loading) { | ||
return <p>Loading...</p>; | ||
} | ||
if (SvgIcon) { | ||
return ( | ||
refreshed && <SvgIcon width={size} height={size} className={className} /> | ||
); | ||
} | ||
return null; | ||
}; | ||
|
||
export default SvgIcon; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,45 @@ | ||
/// <reference types="vite-plugin-svgr/client" /> | ||
import React, { useEffect, useRef, useState } from "react"; | ||
|
||
export function useDynamicSvgImport(iconName: string) { | ||
const importedIconRef = useRef<React.FC<React.SVGProps<SVGElement>>>(); | ||
interface UseDynamicSVGImportOptions { | ||
onCompleted?: ( | ||
name: string, | ||
SvgIcon: React.FC<React.SVGProps<SVGSVGElement>> | undefined, | ||
) => void; | ||
onError?: (err: unknown) => void; | ||
} | ||
export function useDynamicSvgImport( | ||
iconName: string, | ||
options: UseDynamicSVGImportOptions, | ||
) { | ||
const ImportedIconRef = useRef<React.FC<React.SVGProps<SVGSVGElement>>>(); | ||
|
||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState<unknown>(null); | ||
const [error, setError] = useState<unknown>(); | ||
|
||
const { onCompleted, onError } = options; | ||
useEffect(() => { | ||
setLoading(true); | ||
const importSvgIcon = async (): Promise<void> => { | ||
const importIcon = async () => { | ||
try { | ||
importedIconRef.current = ( | ||
ImportedIconRef.current = ( | ||
await import(`../assets/techicons/${iconName}.svg`) | ||
).default; | ||
console.log(ImportedIconRef.current); | ||
if (onCompleted) { | ||
onCompleted(iconName, ImportedIconRef.current); | ||
} | ||
} catch (err) { | ||
if (onError) { | ||
onError(err); | ||
} | ||
setError(err); | ||
console.error(err); | ||
} finally { | ||
setLoading(false); | ||
if (importedIconRef.current === undefined) { | ||
console.error(`Icon ${iconName} not found`); | ||
} else { | ||
console.log(`Icon ${iconName} loaded`); | ||
console.log(importedIconRef.current); | ||
} | ||
} | ||
}; | ||
importIcon(); | ||
}, [iconName, onCompleted, onError]); | ||
|
||
importSvgIcon(); | ||
}, [iconName]); | ||
|
||
return { error, loading, SvgIcon: importedIconRef.current }; | ||
return { error, loading, SvgIcon: ImportedIconRef.current }; | ||
} |