【发布时间】:2022-07-09 01:55:21
【问题描述】:
我的问题
我有一个项目需要到处都有图标。我没有在每个脚本中渲染Fontawesome Icon,而是有一个功能组件,它在给定道具时渲染一个图标。
调用函数时,有时它不接受color 属性。似乎只有某些颜色有效,例如darkBlue、lightBlue 和green。未接受该道具的颜色默认为白色。
我正在使用Tailwindcss 将类注入到组件中。
顺风配置
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:图标渲染
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;
调用图标渲染
import React from "react";
import Button from "../../shared/components/Button";
import Typography from "../../shared/utilities/Typography";
import Mugshot from "../../shared/assets/mugshot.jpg";
import Icon from "../../shared/components/Icon";
import {
faGlobe,
faHandSpock,
faComment,
} from "@fortawesome/free-solid-svg-icons";
import Avatar from "../../shared/components/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;
我的尝试/有趣的事实
- 控制台中没有错误。
- 某些颜色可能会保留顺风颜色名称?
- 尝试在顺风配置中更改颜色名称
- 尝试在顺风配置中更改十六进制值
结论
编辑:发现了一种更简单的方法:
<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;
【问题讨论】:
标签: javascript reactjs functional-programming tailwind-css