question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deal with custom icons in TS

See original GitHub issue

Following the guide to add custom icons I get an TypeScript eror that the name is not assignable.

<Icon name="custom-icon" size="20px" color="gray.400" />
Type '"custom-icon"' is not assignable to type '"link" | "time" | ...

Is there a ways to override the export type Icons .. ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

20reactions
cpouldevcommented, Aug 19, 2021

@tony (and future others who’ll face this)

this is what I do instead, without the need to load the full CDN (also tree shaking works 😃 )

A more “chakra-y” way

Each Font Awesome react icon is an IconDefinition which has the following format

interface IconDefinition  {
  prefix: string;
  iconName: string;
  icon: [
    number, // width
    number, // height
    string[], // ligatures
    string, // unicode
    IconPathData // svgPathData
  ];
}

So based on that data, I created a helper function which in conjunction with chakra’s createIcon helper, it accepts an IconDefinition and spits out a supported element to use with chakra’s Icon

import { Icon, createIcon } from '@chakra-ui/react'
import { faImage, IconDefinition } from '@fortawesome/pro-solid-svg-icons'

function faIcon(icon: IconDefinition) {
    let displayName = icon.iconName
    let [width, height, _, __, svgPath] = icon.icon

    return createIcon({
        viewBox: `0 0 ${width} ${height}`,
        displayName,
        path:
            typeof svgPath === 'string' ? (
                <path fill="currentColor" d={svgPath} />
            ) : (
                <g>
                    {svgPath.map((p, ix) => (
                        <path key={ix} d={p} />
                    ))}
                </g>
            ),
    })
}

Example

<Icon as={faIcon(faImage)} />

pinging @segunadebayo about this. Is it worth adding a better version of this snippet in your docs (or even better, provide an official adapter) for react-fontawesome users? FA is being used by lots and lots of people and I’m pretty sure that many will find it super useful

5reactions
tonycommented, Feb 8, 2020

In my situation, I opted to use font awesome bundles from CDN

There isn’t an easy way to do this with Icon, but here’s what I did:

import Box from "@chakra-ui/core/dist/Box";
import Icon from "@chakra-ui/core/dist/Icon";

const FaIcon = ({ name }: { name: string }) => (
  <Box
    as="em"
    className={`fas fa-${name}`}
    lineHeight="1.2rem"
    marginLeft="-.25rem"
    marginRight="0.5rem"
    verticalAlign="middle"
    display="block"
  />
);

const FaBook = () => <FaIcon name="book" />;
const FaBookmark = () => <FaIcon name="bookmark" />;
const FaDownload = () => <FaIcon name="download" />;
const FaHome = () => <FaIcon name="home" />;
const FaInfo = () => <FaIcon name="info" />;
const FaNewspaper = () => <FaIcon name="newspaper" />;

const MyComponent = () => {
  return (
    <Button leftIcon={FaBook} mr="1rem">
      Dictionary
    </Button>
  )
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Use Custom SVG Icons in Angular Material
In order to use the default Material Icons, you'll need to first import them in the global stylesheet. To do this, open the...
Read more >
Custom Icons with Ionic 5? - Stack Overflow
You need to remove the md- from the icon name. For example, your icon is md-my-custom-icon.svg. Change to my-custom-icon.svg .
Read more >
How you're dealing with hundreds of custom icons which ...
Currently I'm working with team which designer provides the icons and all of them custom which means I can't use any icon package...
Read more >
Build your own icon library in Angular part 1/4 - YouTube
In this part, we will first explore how FontAwesome delivers their SVG icons library. We will then visualize their concept and show you ......
Read more >
How to create an icon library in Angular - Medium
The first thing we need to do is to refactor our dinosaur-icons.ts from one object to multiple constants. Having multiple constants allows us...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found