geido commented on a change in pull request #14318:
URL: https://github.com/apache/superset/pull/14318#discussion_r621329021



##########
File path: superset-frontend/src/components/Icons/Icon.tsx
##########
@@ -17,34 +17,52 @@
  * under the License.
  */
 
-import React from 'react';
+import React, { useEffect, useRef, useState } from 'react';
 import AntdIcon from '@ant-design/icons';
 import { styled } from '@superset-ui/core';
-import { CustomIconComponentProps } from 
'@ant-design/icons/lib/components/Icon';
+import { ReactComponent as TransparentIcon } from 
'images/icons/transparent.svg';
 import IconType from './IconType';
 
 // eslint-disable-next-line @typescript-eslint/no-unused-vars
-const EnhancedIcon = ({ iconColor, iconSize, ...rest }: IconType) => (
+const AntdIconComponent = ({ iconColor, iconSize, ...rest }: IconType) => (
   <AntdIcon viewBox={rest.viewBox || '0 0 24 24'} {...rest} />
 );
 
-const Icon = styled(EnhancedIcon)<IconType>`
+const StyledIcon = styled(AntdIconComponent)<IconType>`
   ${({ iconColor }) => iconColor && `color: ${iconColor};`};
   font-size: ${({ iconSize, theme }) =>
     iconSize ? `${theme.typography.sizes[iconSize]}px` : '24px'};
 `;
 
-export const renderIcon = (
-  SVGComponent:
-    | React.ComponentClass<
-        CustomIconComponentProps | React.SVGProps<SVGSVGElement>,
-        any
-      >
-    | React.FunctionComponent<
-        CustomIconComponentProps | React.SVGProps<SVGSVGElement>
-      >
-    | undefined,
-  props: IconType,
-) => <Icon component={SVGComponent} {...props} />;
-
-export default Icon;
+interface IconProps extends IconType {
+  path: string;
+}
+
+export const Icon = (props: IconProps) => {
+  const { path, ...iconProps } = props;
+  const [, setLoaded] = useState(false);
+  const ImportedSVG = useRef<React.FC<React.SVGProps<SVGSVGElement>>>();
+  const name = path.replace('_', '-');
+
+  useEffect(() => {
+    async function importIcon(): Promise<void> {
+      ImportedSVG.current = (
+        await import(
+          `!!@svgr/webpack?-svgo,+titleProp,+ref!images/icons/${path}.svg`
+        )
+      ).default;
+      setLoaded(true);
+    }
+    importIcon();
+  }, [path, ImportedSVG]);
+
+  return (
+    <StyledIcon
+      component={ImportedSVG.current || TransparentIcon}

Review comment:
       Hey Evan. Thanks for your input. Imho this approach is better because 
the TransparentIcon can get the same properties of the actual icon in terms of 
size, aria-label, etc in order to keep the status of the UI consistent until 
the final icon is loaded.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to