你如何在Hyperstack中使用FontAwsome图标



带有Rails和ReactJS的Hyperstack项目中使用FontAwsome图标的最佳方法是什么,使用Yarn只包含您需要的图标?

这是最好的方法:

使用 Yarn 添加模块:

yarn add @fortawesome/fontawesome-svg-core
yarn add @fortawesome/free-solid-svg-icons
yarn add @fortawesome/react-fontawesome

将它们导入到您的一个 Webpacker 包中:

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { library } from '@fortawesome/fontawesome-svg-core';
import { faAngleDown, faAngleUp, faCoffee } from '@fortawesome/free-solid-svg-icons';
library.add(faAngleDown, faAngleUp, faCoffee);
global.Fa = FontAwesomeIcon;

然后在组件中使用它们:

H1 { Fa(icon: 'coffee') } # to inherit your H1 style
Fa(icon: 'angle-down', size: 'xs) 
Fa(icon: 'angle-up', className: 'special') 

如果您经常使用某些图标,则可以向HyperComponent类添加帮助程序方法:

class HyperComponent
  include Hyperstack::Component
  include Hyperstack::State::Observable
  def icon_check
    Fa(icon: 'check', className: 'green-color', size: 'lg')
  end
end

真的就是这么简单!

最新更新