javascript - Fontawesome icons not accepting color props through react functional components via tailwindcss - Stack Overflow

My ProblemI have a project which requires icons everywhere. Instead of rendering a Fontawesome Icon in

My Problem

I have a project which requires icons everywhere. Instead of rendering a Fontawesome Icon in every script, I have a functional ponent which renders an icon when given props.

When calling the function, sometimes it doesn't accept the color prop. Only certain colors seem to be working, such as darkBlue, lightBlue, and green. Colors which haven't accepted the prop are defaulting to white.

I'm using Tailwindcss to inject classes into the ponents.

Tailwind Config

module.exports = {
  content: ["./src/**/*.{js,jsx,ts,tsx}"],
  theme: {
    colors: {
      dark: "#121212",
      white: "#fff",
      secondary: "#F0A500",
      lightBlue: "#0EA5E9",
      darkBlue: "#2563EB",
      beige: "#FDBA74",
      silver: "#9CA3AF",
      red: "#DC2626",
      green: "#10B981",
      orange: "#F97316",
      hotPink: "#EC4899",
      purple: "#6D28D9",
      yellow: "#FDE047",
    },
    extend: {
    },
  },
  plugins: [],
};

FC: Icon Render

import React from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";

// color props must be passed as a string
function Icon({ name, color, scale }) {
  return (
    <FontAwesomeIcon
      icon={name}
      className={`text-${color}`} 
      size={scale}
    />
  );
}

export default Icon;

Calling Icon Render

import React from "react";
import Button from "../../shared/ponents/Button";
import Typography from "../../shared/utilities/Typography";
import Mugshot from "../../shared/assets/mugshot.jpg";
import Icon from "../../shared/ponents/Icon";
import {
  faGlobe,
  faHandSpock,
  faComment,
} from "@fortawesome/free-solid-svg-icons";
import Avatar from "../../shared/ponents/Avatar";

function example() {
  return (
    <section className="section" id="home-hero">
      <Typography variant="label">Some text</Typography>
      <Typography variant="h2">
        Some text <Icon name={faHandSpock} color="beige" />
      </Typography>
    </section>
  );
}
export default example;

What I've Tried / Fun Facts

  • No errors in the console.
  • Some colors may be preserved tailwind color names?
  • Tried changing color names in tailwind config
  • Tried changing hex values in tailwind config

Conclusion

Edit: Discovered an easier way:

<Icon name={faHandSpock} color="text-beige" /> // full classname

// remove partial className, pass in object
function Icon({ name, color, scale }) {
  return (
    <FontAwesomeIcon
      icon={name}
      className={color}
      size={scale}
    />
  );
}

export default Icon;

My Problem

I have a project which requires icons everywhere. Instead of rendering a Fontawesome Icon in every script, I have a functional ponent which renders an icon when given props.

When calling the function, sometimes it doesn't accept the color prop. Only certain colors seem to be working, such as darkBlue, lightBlue, and green. Colors which haven't accepted the prop are defaulting to white.

I'm using Tailwindcss to inject classes into the ponents.

Tailwind Config

module.exports = {
  content: ["./src/**/*.{js,jsx,ts,tsx}"],
  theme: {
    colors: {
      dark: "#121212",
      white: "#fff",
      secondary: "#F0A500",
      lightBlue: "#0EA5E9",
      darkBlue: "#2563EB",
      beige: "#FDBA74",
      silver: "#9CA3AF",
      red: "#DC2626",
      green: "#10B981",
      orange: "#F97316",
      hotPink: "#EC4899",
      purple: "#6D28D9",
      yellow: "#FDE047",
    },
    extend: {
    },
  },
  plugins: [],
};

FC: Icon Render

import React from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";

// color props must be passed as a string
function Icon({ name, color, scale }) {
  return (
    <FontAwesomeIcon
      icon={name}
      className={`text-${color}`} 
      size={scale}
    />
  );
}

export default Icon;

Calling Icon Render

import React from "react";
import Button from "../../shared/ponents/Button";
import Typography from "../../shared/utilities/Typography";
import Mugshot from "../../shared/assets/mugshot.jpg";
import Icon from "../../shared/ponents/Icon";
import {
  faGlobe,
  faHandSpock,
  faComment,
} from "@fortawesome/free-solid-svg-icons";
import Avatar from "../../shared/ponents/Avatar";

function example() {
  return (
    <section className="section" id="home-hero">
      <Typography variant="label">Some text</Typography>
      <Typography variant="h2">
        Some text <Icon name={faHandSpock} color="beige" />
      </Typography>
    </section>
  );
}
export default example;

What I've Tried / Fun Facts

  • No errors in the console.
  • Some colors may be preserved tailwind color names?
  • Tried changing color names in tailwind config
  • Tried changing hex values in tailwind config

Conclusion

Edit: Discovered an easier way:

<Icon name={faHandSpock} color="text-beige" /> // full classname

// remove partial className, pass in object
function Icon({ name, color, scale }) {
  return (
    <FontAwesomeIcon
      icon={name}
      className={color}
      size={scale}
    />
  );
}

export default Icon;
Share Improve this question edited Apr 8, 2022 at 5:33 Josh asked Apr 8, 2022 at 3:49 JoshJosh 231 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

TailwindCSS doesn't allow you to generate classes dynamically. So when you use the following to generate the class…

className={`text-${color}`}

…TailwindCSS will not pick that up as a valid TailwindCSS class and therefore will not produce the necessary CSS.

Instead, you must include the full name of the class in your source code.

For this you can create any function which returns the required string like this:

function changeFAColor (color) {
  if(color === dark) return "text-dark"
    (color === white) return "text-white"
    (color === secondary) return "text-secondary")
    .
    .
    .
    (color === purple) return "text-purple")
    (color === yellow) return "text-yellow")
}

And use it in the ponent

<FontAwesomeIcon
      icon={name}
      className={`${changeFAcolor(color)}`} 
      size={scale}
    />

Tailwind generates a CSS file which contains only the classes that you've used in the project.

The problem you're experiencing is because Tailwind doesn't recognise the generated class you're applying in "FC: Icon Render". In particular, this line:

className={`text-${color}`}

To quote the documentation:

The most important implication of how Tailwind extracts class names is that it will only find classes that exist as plete unbroken strings in your source files.

If you use string interpolation or concatenate partial class names together, Tailwind will not find them and therefore will not generate the corresponding CSS:

https://tailwindcss./docs/content-configuration#class-detection-in-depth

To resolve your problem, either pass in the full class name instead of generating it or safelist all of your text-{color} classes in your config file.

Assign your colors to a variable:

const colors = {
    dark: "#121212",
    white: "#fff",
    ...

Pass them into your config for theme:

theme: {
    colors,
    . . . 

Safelist your colors:

safelist: Object.keys(colors).map(color => `text-${color}`),

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744387399a4571735.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信