GithubHelp home page GithubHelp logo

iconfont-cli / react-iconfont-cli Goto Github PK

View Code? Open in Web Editor NEW
68.0 2.0 25.0 252 KB

转换iconfont图标为React标准组件,不依赖字体,支持多色彩

License: MIT License

Shell 2.44% JavaScript 36.29% TypeScript 61.27%
iconfont-cli react-iconfont react iconfont icons icon

react-iconfont-cli's Introduction

react-iconfont-cli

用纯JS把iconfont.cn的图标转换成React组件,不依赖字体,支持多色彩

特性

1、纯组件,不依赖字体,体积小
2、支持渲染多色彩图标,支持自定义颜色
3、自动化生成图标组件,支持es6和typescript两种文件格式

Step 1

安装插件

# Yarn
yarn add react-iconfont-cli --dev

# Npm
npm install react-iconfont-cli --save-dev

Step 2

生成配置文件

npx iconfont-init

此时项目根目录会生成一个iconfont.json的文件,内容如下:

{
    "symbol_url": "请参考README.md,复制官网提供的JS链接",
    "use_typescript": false,
    "save_dir": "./src/components/iconfont",
    "trim_icon_prefix": "icon",
    "unit": "px",
    "default_icon_size": 18
}

配置参数说明:

symbol_url

请直接复制iconfont官网提供的项目链接。请务必看清是.js后缀而不是.css后缀。如果你现在还没有创建iconfont的仓库,那么可以填入这个链接去测试:http://at.alicdn.com/t/font_1373348_ghk94ooopqr.js


use_typescript

如果您的项目使用Typescript编写,请设置为true。这个选项将决定生成的图标组件是.tsx还是.js后缀。

当该值为false时,我们会为您的图标生成.js.d.ts两个文件,以便您能享受到最好的开发体验。

save_dir

根据iconfont图标生成的组件存放的位置。每次生成组件之前,该文件夹都会被清空。

trim_icon_prefix

如果你的图标有通用的前缀,而你在使用的时候又不想重复去写,那么可以通过这种配置这个选项把前缀统一去掉。

注意,这个选项只针对 <Icon /> 组件有效

unit

图标的单位,默认是网页常用单位px即像素,也推荐您在手机网页中使用自适应的rem单位。

default_icon_size

我们将为每个生成的图标组件加入默认的字体大小,当然,你也可以通过传入props的方式改变这个size值。

Step 3

开始生成React标准组件

npx iconfont-h5

生成后查看您设置的保存目录中是否含有所有的图标,你可以参考snapshots目录的快照文件,以区分不同模式下的图标结构。

使用

现在我们提供了两种引入方式供您选择:

1、使用汇总Icon组件:

import React from 'react';
import IconFont from '../src/iconfont';

export const App = () => {
  return (
    <div>
      <IconFont name="alipay" size={20} />
      <IconFont name="wechat" />
    </div>
  );
};

2、使用单个图标。这样可以避免没用到的图标也打包进App:

import React from 'react';
import IconAlipay from '../src/iconfont/IconAlipay';
import IconWechat from '../src/iconfont/IconWechat';

export const App = () => {
  return (
    <div>
      <IconAlipay size={20} />
      <IconWechat />
    </div>
  );
};

图标尺寸

根据配置default_icon_size,每个图标都会有一个默认的尺寸,你可以随时覆盖。

<IconFont name="alipay" size={20} />

图标单色

单色图标,如果不指定颜色值,图标将渲染原本的颜色。如果你想设置为其他的颜色,那么设置一个你想要的颜色即可。

注意:如果你在props传入的color是字符串而不是数组,那么即使原本是多色彩的图标,也会变成单色图标。

<IconFont name="alipay" color="green" />

图标多色彩

多色彩的图标,如果不指定颜色值,图标将渲染原本的多色彩。如果你想设置为其他的颜色,那么设置一组你想要的颜色即可

<IconFont name="alipay" color={['green', 'orange']} />

颜色组的数量以及排序,需要根据当前图标的信息来确定。您需要进入图标组件中查看并得出结论。

与文字并排

图标是块状的容器,如果您想与文字并排混合,请使用flex布局以保证文字和图标能对齐

<div style={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}>
  <span>Hello</span>
  <IconFont name="alipay" />
</div>

更新图标

当您在iconfont.cn中的图标有变更时,只需更改配置symbol_url,然后再次执行Step 3即可生成最新的图标组件

# 修改 symbol_url 配置后执行:
npx iconfont-h5

扩展

平台
小程序 mini-program-iconfont-cli
Taro taro-iconfont-cli
React Native react-native-iconfont-cli
Flutter flutter-iconfont-cli
Remax remax-iconfont-cli

欢迎使用,并给我一些反馈和建议,让这个库做的更好

react-iconfont-cli's People

Contributors

fwh1990 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-iconfont-cli's Issues

为什么为 svg 添加 display: block 的样式?

image

发现组件默认有 display: block 的样式,不是很清楚为什么。
一般图标是当内联元素来使用的,现在display: block的样式,导致图标默认会占用一整行,使用的时候经常要重置样式成 inline-block

默认颜色改为currentColor

组件的默认颜色属性值希望可以设置为“currentColor",或者在配置文件中提供默认颜色选项。
使用currentColor可以方便的利用当前字体的颜色作为图标颜色,这样就不用再单独给组件传颜色值

应该把DOMAttributes替换为SVGAttributes

在图标组件与index.tsx中,接口Props都是继承自DOMAttributes,如下

interface Props extends DOMAttributes<SVGElement>

这样实际上没有继承到svg元素应有的属性,如width、height等,需要改成

interface Props extends SVGAttributes<SVGElement>

Error when running npx iconfont-init: `Cannot find module 'fs-extra'`

When trying to run npx iconfont-init in my React project after installing react-iconfont-cli using npm, I encountered the following error:

Error: Cannot find module 'fs-extra'
Require stack:
- .../node_modules/react-iconfont-cli/commands/createJson.js

To resolve this issue, I installed the fs-extra module manually using npm i fs-extra, and after that, the command worked as expected.

Steps to Reproduce:

  1. Create a new React project or use an existing one.
  2. Install react-iconfont-cli by running npm install react-iconfont-cli --save-dev.
  3. Attempt to run npx iconfont-init in the project directory.

Expected Behavior:

npx iconfont-init should run without any errors and initiate the icon font setup successfully.

Actual Behavior:

The command throws an error: Cannot find module 'fs-extra'.

Proposed Solution:

It appears that the fs-extra module is required by the react-iconfont-cli package, but it was not installed automatically as a dependency. To resolve this issue, users should manually install fs-extra using the following command:

npm install fs-extra

After installing fs-extra, running npx iconfont-init should work as expected.

Additional Information:

  • Node version: 18.15.0
  • NPM version: 9.5.0
  • OS: macOS

defaultProps控制台警告

const IconHome: FunctionComponent<Props> = ({ size, color, style: _style, ...rest }) => <svg />
IconHome.defaultProps = { size: 16, };
现在react对defaultProps会报warning,在控制台会看到一片红,可以使用以下方式优化吗
const IconHome: FunctionComponent<Props> = ({ size = 16, color, style: _style, ...rest }) => <svg />

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.