Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Dynamically import Icon and support of Default Export #182

Open
Dey-Sumit opened this issue May 30, 2024 · 0 comments
Open

How to Dynamically import Icon and support of Default Export #182

Dey-Sumit opened this issue May 30, 2024 · 0 comments

Comments

@Dey-Sumit
Copy link

UseCase : I am building a no code tool where user selects one icon from the edit page and in the final page they can see the icon and other text.
Now, in the final page, I only want to import that specific icon by name that is selected by the user . I am thinking about creating a custom component like this?

// components/LazyIcon.tsx
import React from 'react';
import dynamic from 'next/dynamic';

interface LazyIconProps {
  iconName: string;
}

const LazyIcon: React.FC<LazyIconProps> = ({ iconName }) => {
  let IconComponent: React.ComponentType | null = null;

  switch (iconName) {
    case 'AccessibilityIcon':
      IconComponent = dynamic(() => import('@radix-ui/react-icons/AccessibilityIcon'), { ssr: false });
      break;
    case 'ActivityLogIcon':
      IconComponent = dynamic(() => import('@radix-ui/react-icons/ActivityLogIcon'), { ssr: false });
      break;
    case 'AirplaneIcon':
      IconComponent = dynamic(() => import('@radix-ui/react-icons/AirplaneIcon'), { ssr: false });
      break;
    // Add other cases here
    default:
      break;
  }

  if (!IconComponent) {
    return <span>Icon not found</span>;
  }

  return <IconComponent />;
};

export default LazyIcon;

<LazyIcon iconName=""/>

I believe this will reduce the bundle size and will only import the needed Icon .

Problem : radix icons do not have a default export , So, this is not possible . import('@radix-ui/react-icons/AccessibilityIcon') How can I do this?

Is there actually any way to import a specific icon dynamically from the library, instead of importing the entire icon set ?

@Dey-Sumit Dey-Sumit changed the title How to Dynamically import Icon and Default Export How to Dynamically import Icon and support of Default Export May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant