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

Add a reusable Icon component in our ui package #835

Open
Tadjaur opened this issue Apr 13, 2024 · 2 comments
Open

Add a reusable Icon component in our ui package #835

Tadjaur opened this issue Apr 13, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@Tadjaur
Copy link
Collaborator

Tadjaur commented Apr 13, 2024

@andrew-bierman:
we should probably have a reusable Icon component in our ui package, that can be typesafe and use either expo vector icons, or the tamagui icons more easily. Ideally we can drop some of the expo icons as they bloat the bundle a good bit.

@Tadjaur Tadjaur added the enhancement New feature or request label Apr 13, 2024
@andrew-bierman
Copy link
Owner

I have a rough draft on a component for this

@andrew-bierman
Copy link
Owner

Maybe something like this?

import React from 'react';
import * as ExpoIcons from '@expo/vector-icons';
import * as TamaguiIcons from '@tamagui/lucide-icons';

export type IconSource = 'expo' | 'tamagui';

export type ExpoIconProvider = keyof typeof ExpoIcons;

export type TamaguiIconName = keyof typeof TamaguiIcons;
export type ExpoIconName = IconProps<'expo', ExpoIconProvider>['name'];

export type IconProps<T extends IconSource, P extends ExpoIconProvider> = T extends 'expo'
  ? React.ComponentProps<(typeof ExpoIcons)[P]>
  : T extends 'tamagui'
    ? React.ComponentProps<(typeof TamaguiIcons)[keyof typeof TamaguiIcons]>
    : never;

export interface Props<T extends IconSource, P extends ExpoIconProvider> {
  source?: T;
  provider?: P;
  name: T extends 'expo' ? IconProps<T, P>['name'] : keyof typeof TamaguiIcons;
  size?: number;
  color?: string;
  [key: string]: any;
}

export const Icon = <
  T extends IconSource = 'expo',
  P extends ExpoIconProvider = 'MaterialCommunityIcons',
>({
  source = 'expo' as T,
  provider = 'FontAwesome' as P,
  name,
  size = 24,
  color,
  ...props
}: Props<T, P>) => {
  let IconComponent: React.ComponentType<any> | undefined;

  switch (source) {
    case 'expo':
      IconComponent = ExpoIcons[provider];
      break;
    case 'tamagui':
      IconComponent = TamaguiIcons[name as keyof typeof TamaguiIcons];
      break;
    default:
      console.warn(`Unsupported icon source: ${source}`);
  }

  if (!IconComponent) {
    console.warn(
      `Icon '${name}' not found in the specified source '${source}' and provider '${provider}'.`,
    );
    return null;
  }

  return <IconComponent name={name} size={size} color={color} {...props} />;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants