FontAwesome更改默认前缀

时间:2018-03-12 20:56:56

标签: reactjs font-awesome font-awesome-5

我想将默认前缀更改为Font Awesome Regular 所以试着做了

fontawesome.config = {
  familyPrefix: "far"
};

但是我还是要做一个"远"使其有效的前缀

import React from "react";
import { render } from "react-dom";
import Hello from "./Hello";
import fontawesome from "@fortawesome/fontawesome";
import FontAwesomeIcon from "@fortawesome/react-fontawesome";
import faCalendar from "@fortawesome/fontawesome-free-regular/faCalendar";
import faSquare from "@fortawesome/fontawesome-free-regular/faSquare";

fontawesome.config = {
  familyPrefix: "far"
};

fontawesome.library.add(faCalendar, faSquare);

const styles = {
  fontFamily: "sans-serif",
  textAlign: "center"
};

const App = () => (
  <div style={styles}>
    <Hello name="CodeSandbox" />
    <h2>Start editing to see some magic happen {"\u2728"}</h2>
    Favorite beverage: <FontAwesomeIcon icon="calendar" /> //does not work
    Icon from regular: <FontAwesomeIcon icon={["far", "square"]} /> //works
  </div>
);

render(<App />, document.getElementById("root"));

https://codesandbox.io/s/40v5jk6no7

1 个答案:

答案 0 :(得分:1)

我担心通过FontAwesome组件配置无法实现所需的行为。

在源代码分析后,我可以说前缀为fa的图标已解析为属于font-family: solid的图标,而'fa'前缀在此处是硬编码的(fontawesome/index.es.js):

function findIconDefinition(params) {
  var _params$prefix = params.prefix,
      prefix = _params$prefix === undefined ? 'fa' : _params$prefix,
      iconName = params.iconName;

  if (!iconName) return;

  return iconFromMapping(library.definitions, prefix, iconName) || iconFromMapping(namespace.styles, prefix, iconName);
}

您仍然可以选择为项目自定义组件的源代码(我的意思是您只需将prefix = _params$prefix === undefined ? 'fa' : _params$prefix替换为prefix = _params$prefix === undefined ? 'far' : _params$prefix)但我不推荐这种方法,因为它看起来像code smell对我来说。