mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { type IconName } from 'public/icons/name';
|
|
import React from 'react';
|
|
|
|
import config from 'configs/app';
|
|
import { Skeleton } from '@luxfi/ui/skeleton';
|
|
|
|
export const href = config.app.spriteHash ? `/icons/sprite.${ config.app.spriteHash }.svg` : '/icons/sprite.svg';
|
|
|
|
export { IconName };
|
|
|
|
export interface Props extends React.HTMLAttributes<HTMLDivElement> {
|
|
name: IconName;
|
|
isLoading?: boolean;
|
|
}
|
|
|
|
const DEFAULT_SIZE = 'w-5 h-5';
|
|
|
|
const hasSize = (cls?: string): boolean => {
|
|
if (!cls) return false;
|
|
return /(?:^|\s)(?:w-|h-|size-)/.test(cls);
|
|
};
|
|
|
|
const IconSvg = ({ name, isLoading = false, className, ref, ...props }: Props & { ref?: React.Ref<HTMLDivElement> }) => {
|
|
const resolvedClass = hasSize(className) ? className! : `${ DEFAULT_SIZE } ${ className ?? '' }`.trim();
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<Skeleton loading display="inline-block" flexShrink={ 0 } className={ resolvedClass } ref={ ref }>
|
|
<svg className="w-full h-full">
|
|
<use href={ `${ href }#${ name }` }/>
|
|
</svg>
|
|
</Skeleton>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className={ `inline-block shrink-0 ${ resolvedClass }`.trim() } ref={ ref } { ...props }>
|
|
<svg className="w-full h-full">
|
|
<use href={ `${ href }#${ name }` }/>
|
|
</svg>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
IconSvg.displayName = 'IconSvg';
|
|
|
|
export default IconSvg;
|