-
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.
feat: upload dynamic icon in isolation (#44)
* Rework dynamic icons component * Add icon/[iconSymbol] page to make sure that icons loaded separately * Remove 'use client' in DynamicIcon * Use <GithubSVGIcon /> in <StaticIcon />. Get rid of useEffect * Create UnknownIcon component * Move GithubSvgIcon, Image, SVG, UnknownIcon into Base directory * Fix mono icon displaying --------- Co-authored-by: Aleksei <al.gorbunov@slotegrator.space>
- Loading branch information
Showing
18 changed files
with
2,833 additions
and
2,323 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"use client"; | ||
|
||
import { Web3Icon } from "@bgd-labs/react-web3-icons"; | ||
import { FC } from "react"; | ||
|
||
import { IconLoader } from "@/components/IconCard"; | ||
|
||
type PageProps = { | ||
params: { | ||
iconSymbol: string; | ||
}; | ||
}; | ||
|
||
const Page: FC<PageProps> = ({ params }) => { | ||
const { iconSymbol } = params; | ||
return ( | ||
<div> | ||
<Web3Icon | ||
symbol={iconSymbol} | ||
assetTag="a" | ||
mono={false} | ||
loader={<IconLoader />} | ||
className="size-[70px]" | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Page; |
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
29 changes: 29 additions & 0 deletions
29
packages/react-web3-icons/src/components/Base/GithubSVGIcon.tsx
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { FC } from "react"; | ||
import InlineSVG from "react-inlinesvg"; | ||
|
||
import { IconComponentBaseProps } from "../../utils"; | ||
import { generateUniqueHash } from "../../utils/generateUniqueHash"; | ||
|
||
type GithubSVGIconProps = { | ||
githubSrc: string; | ||
} & IconComponentBaseProps; | ||
|
||
const GithubSvgIcon: FC<GithubSVGIconProps> = ({ | ||
githubSrc, | ||
loader, | ||
...props | ||
}) => { | ||
return ( | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
<InlineSVG | ||
{...props} | ||
src={githubSrc} | ||
uniqueHash={generateUniqueHash()} | ||
uniquifyIDs | ||
loader={loader} | ||
/> | ||
); | ||
}; | ||
|
||
export default GithubSvgIcon; |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React, { ComponentProps } from "react"; | ||
|
||
export const Image = ({ | ||
svgCode, | ||
...props | ||
}: { svgCode: string } & ComponentProps<"img">) => { | ||
return ( | ||
<img | ||
{...props} | ||
draggable={false} | ||
onDragStart={(e) => e.preventDefault()} | ||
src={`data:image/svg+xml;base64,${btoa(svgCode)}`} | ||
style={{ outline: "none !important", pointerEvents: "none" }} | ||
alt={props.alt} | ||
/> | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"use client"; | ||
|
||
import React, { ComponentProps } from "react"; | ||
|
||
import { Image } from "./Image"; | ||
import { UnknownIcon } from "./UnknownIcon"; | ||
|
||
type SVGProps = { | ||
svgCode?: string; | ||
} & ComponentProps<"img">; | ||
|
||
/** | ||
* Wrapper for get svg image from svg code | ||
*/ | ||
export const SVG = ({ svgCode, ...props }: SVGProps) => { | ||
if (!svgCode) { | ||
return <UnknownIcon />; | ||
} | ||
return <Image svgCode={svgCode} {...props} />; | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/react-web3-icons/src/components/Base/UnknownIcon.tsx
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React, { ComponentProps } from "react"; | ||
|
||
import { iconUnknown } from "../../icons/full/build/icon-unknown.icon"; | ||
import { Image } from "./Image"; | ||
|
||
type UnknownIconProps = ComponentProps<"img">; | ||
|
||
export const UnknownIcon = (props: UnknownIconProps) => { | ||
return <Image svgCode={iconUnknown.data} {...props} />; | ||
}; |
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
packages/react-web3-icons/src/components/DynamicIcon/DynamicIcon.tsx
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { FC } from "react"; | ||
|
||
import { Web3IconType } from "../../icons/full"; | ||
import { IconComponentBaseProps } from "../../utils"; | ||
import GithubSVGIcon from "../Base/GithubSVGIcon"; | ||
import { LoadableIcon } from "./LoadableIcon"; | ||
|
||
export type DynamicIconProps = IconComponentBaseProps & { | ||
iconKey: Web3IconType | string; | ||
githubSrc?: string; | ||
}; | ||
|
||
/** | ||
* Wrapper for get icons dynamically | ||
*/ | ||
export const DynamicIcon: FC<DynamicIconProps> = ({ | ||
githubSrc, | ||
loader, | ||
...props | ||
}) => { | ||
return ( | ||
<LoadableIcon | ||
{...props} | ||
fallback={loader} | ||
fallbackComponent={ | ||
githubSrc ? ( | ||
<GithubSVGIcon githubSrc={githubSrc} loader={loader} /> | ||
) : undefined | ||
} | ||
/> | ||
); | ||
}; |
57 changes: 57 additions & 0 deletions
57
packages/react-web3-icons/src/components/DynamicIcon/LoadableIcon.tsx
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import loadable from "@loadable/component"; | ||
import camelCase from "lodash.camelcase"; | ||
import React, { JSX } from "react"; | ||
|
||
import { Web3IconType } from "../../icons/full"; | ||
import { | ||
capitalize, | ||
formatMonoSvgCode, | ||
IconComponentBaseProps, | ||
} from "../../utils"; | ||
import { SVG } from "../Base/SVG"; | ||
|
||
export type LoadableIconProps = IconComponentBaseProps & { | ||
iconKey: Web3IconType | string; | ||
fallbackComponent?: JSX.Element; | ||
}; | ||
|
||
export const LoadableIcon = loadable( | ||
async ({ iconKey, mono, fallbackComponent, ...props }: LoadableIconProps) => { | ||
try { | ||
const lowerCasedIconKey = iconKey.toLowerCase(); | ||
const iconFileName = `icon-${lowerCasedIconKey}`; | ||
const folder = mono ? "mono" : "full"; | ||
const icon = await import( | ||
`../../icons/${folder}/build/${iconFileName}.icon.ts` | ||
); | ||
const iconData = icon[`icon${capitalize(camelCase(lowerCasedIconKey))}`]; | ||
|
||
if (!iconData && fallbackComponent) { | ||
return { | ||
default: () => <>{fallbackComponent}</>, | ||
}; | ||
} | ||
|
||
return { | ||
default: () => ( | ||
<SVG | ||
svgCode={formatMonoSvgCode({ | ||
mono, | ||
svgCode: iconData?.data, | ||
...props, | ||
})} | ||
{...props} | ||
/> | ||
), | ||
}; | ||
} catch (e) { | ||
return { | ||
default: () => <SVG svgCode={undefined} {...props} />, | ||
}; | ||
} | ||
}, | ||
{ | ||
ssr: true, | ||
cacheKey: ({ iconKey, mono }) => `${iconKey}-${mono}`, | ||
}, | ||
); |
Oops, something went wrong.