HOW - SVG 图标组件封装(Lucide React)

HOW - 图形格式SVG及其应用(图标库) 中我们介绍过 “动态 Fetch CDN SVG 图标”,这种在开发者将需要用到 图标 依次上传到 CDN 再在项目通过请求引入使用的场景。

但其实,更常见的做法还是统一维护一组 SVG 图标,然后将其封装成组件,直接在项目中通过 Import 的形式使用。

Lucide React

lucide-react 是一个为 React 提供图标的库,它是基于 Lucide 项目构建的。Lucide 是一个开源图标库,具有灵活、可定制和轻量的特点。lucide-react 允许在 React 项目中轻松地使用这些图标。

以下是 lucide-react 的工作原理和一些关键点:

主要原理

  1. 基于 SVG 的图标:
    Lucide 图标库中的所有图标都是基于 SVG(可缩放矢量图形)的。这意味着图标可以在任何大小下保持清晰和不失真,非常适合现代响应式设计。

  2. React 组件:
    lucide-react 将这些 SVG 图标封装成 React 组件。每个图标都可以作为一个独立的 React 组件使用,这使得它们的集成非常方便,并且能够利用 React 的声明式特性和组件化优势。

  3. 灵活可定制:
    由于这些图标是基于 SVG 的,开发者可以轻松地通过 React 的属性来定制图标的大小、颜色、样式等。lucide-react 组件通常接受标准的 SVG 属性,如 width, height, stroke, fill 等,使得定制变得非常简单。

安装和使用

安装 lucide-react:

你可以通过 npm 或 yarn 安装 lucide-react

npm install lucide-react

yarn add lucide-react

使用 lucide-react:

安装之后,你可以在你的 React 项目中导入和使用图标。例如:

import React from 'react';
import { ArrowRight, Home } from 'lucide-react';

function App() {
  return (
    <div>
      <h1>My App</h1>
      <ArrowRight size={24} color="red" />
      <Home size={32} color="blue" />
    </div>
  );
}

export default App;

在这个例子中,ArrowRightHome 图标作为 React 组件使用。你可以通过传递属性来定制它们的大小和颜色。

工作原理详解

  1. SVG 图标文件:
    Lucide 项目包含了一系列的 SVG 图标文件,每个图标都是一个独立的 SVG 文件。

  2. React 组件封装:
    lucide-react 库会将这些 SVG 文件转换为 React 组件。这个转换过程通常会在构建阶段完成,通过脚本将每个 SVG 文件转换为一个对应的 React 组件。

  3. 组件导出:
    转换后的 React 组件会被导出,并作为 lucide-react 库的一部分发布到 npm 上。开发者可以在他们的 React 项目中导入这些组件并使用。

React 组件封装:SVG 文件转换为 React 组件

两个源码方法:createLucideIcon、Icon

去翻阅源码实现,可以发现有如下两个重要的方法:

// https://github.com/lucide-icons/lucide/blob/main/packages/lucide-react/src/createLucideIcon.ts
import { createElement, forwardRef } from 'react';
import { mergeClasses, toKebabCase } from '@lucide/shared';
import { IconNode, LucideProps } from './types';
import Icon from './Icon';

/**
 * Create a Lucide icon component
 * @param {string} iconName
 * @param {array} iconNode
 * @returns {ForwardRefExoticComponent} LucideIcon
 */
const createLucideIcon = (iconName: string, iconNode: IconNode) => {
  const Component = forwardRef<SVGSVGElement, LucideProps>(({ className, ...props }, ref) =>
    createElement(Icon, {
      ref,
      iconNode,
      className: mergeClasses(`lucide-${toKebabCase(iconName)}`, className),
      ...props,
    }),
  );

  Component.displayName = `${iconName}`;

  return Component;
};

export default createLucideIcon;
// https://github.com/lucide-icons/lucide/blob/main/packages/lucide-react/src/Icon.ts
import { createElement, forwardRef } from 'react';
import defaultAttributes from './defaultAttributes';
import { IconNode, LucideProps } from './types';
import { mergeClasses } from '@lucide/shared';

interface IconComponentProps extends LucideProps {
  iconNode: IconNode;
}

/**
 * Lucide icon component
 *
 * @component Icon
 * @param {object} props
 * @param {string} props.color - The color of the icon
 * @param {number} props.size - The size of the icon
 * @param {number} props.strokeWidth - The stroke width of the icon
 * @param {boolean} props.absoluteStrokeWidth - Whether to use absolute stroke width
 * @param {string} props.className - The class name of the icon
 * @param {IconNode} props.children - The children of the icon
 * @param {IconNode} props.iconNode - The icon node of the icon
 *
 * @returns {ForwardRefExoticComponent} LucideIcon
 */
const Icon = forwardRef<SVGSVGElement, IconComponentProps>(
  (
    {
      color = 'currentColor',
      size = 24,
      strokeWidth = 2,
      absoluteStrokeWidth,
      className = '',
      children,
      iconNode,
      ...rest
    },
    ref,
  ) => {
    return createElement(
      'svg',
      {
        ref,
        ...defaultAttributes,
        width: size,
        height: size,
        stroke: color,
        strokeWidth: absoluteStrokeWidth ? (Number(strokeWidth) * 24) / Number(size) : strokeWidth,
        className: mergeClasses('lucide', className),
        ...rest,
      },
      [
        ...iconNode.map(([tag, attrs]) => createElement(tag, attrs)),
        ...(Array.isArray(children) ? children : [children]),
      ],
    );
  },
);

export default Icon;

从这两个方法可以看出一些端倪,即基于 React 的 createElement 来转换成 React 组件。

模拟构建过程

下面介绍一般如何去构建一个图标转换过程并导出供使用者使用。

1. 源码结构

通常,目录结构会包含以下部分:

  • src/icons: 包含所有 SVG 图标文件的目录。
  • src/components: 存放将 SVG 图标转换为 React 组件的代码。
  • scripts: 包含用于自动化转换和构建的脚本。
  • index.js: 入口文件,导出所有图标组件。

2. SVG 图标转换为 React 组件

核心过程是将每个 SVG 图标文件转换为一个 React 组件。这个转换过程通常包括以下步骤:

a. 读取 SVG 文件

使用 Node.js 的文件系统模块(fs)读取存放在 src/icons 目录中的所有 SVG 文件。

const fs = require('fs');
const path = require('path');

const iconsDir = path.resolve(__dirname, 'src/icons');
const iconFiles = fs.readdirSync(iconsDir);
b. 生成 React 组件代码

遍历每个 SVG 文件,生成对应的 React 组件代码。使用模板字符串创建 React 组件。

iconFiles.forEach(file => {
  const iconName = path.basename(file, '.svg');
  const svgContent = fs.readFileSync(path.join(iconsDir, file), 'utf8');

  const componentCode = `
import React from 'react';

const ${iconName} = (props) => (
  <svg {...props}>
    ${svgContent}
  </svg>
);

export default ${iconName};
  `;

  fs.writeFileSync(path.join(__dirname, `src/components/${iconName}.js`), componentCode);
});
c. 生成索引文件

为了方便导入所有图标组件,生成一个索引文件(index.js),导出所有组件。

const indexContent = iconFiles.map(file => {
  const iconName = path.basename(file, '.svg');
  return `export { default as ${iconName} } from './components/${iconName}';`;
}).join('\n');

fs.writeFileSync(path.join(__dirname, 'src/index.js'), indexContent);

3. 发布到 npm

通过 package.json 配置,指定入口文件为生成的 src/index.js,并发布到 npm 上。

package.json:

{
  "name": "lucide-react",
  "version": "1.0.0",
  "main": "src/index.js",
  "dependencies": {
    "react": "^17.0.0"
  },
  "scripts": {
    "build": "node scripts/build.js"
  }
}

4. 在项目中使用

开发者在项目中通过 npm 安装 lucide-react,并使用导出的图标组件。

npm install lucide-react

然后在 React 项目中导入和使用图标组件:

import React from 'react';
import { ArrowRight, Home } from 'lucide-react';

function App() {
  return (
    <div>
      <h1>My App</h1>
      <ArrowRight size={24} color="red" />
      <Home size={32} color="blue" />
    </div>
  );
}

export default App;

相关推荐

  1. HOW - SVG 图标组件封装(Lucide React)

    2024-07-18 14:26:01       22 阅读
  2. vite+vue3项目中svg图标组件封装

    2024-07-18 14:26:01       39 阅读
  3. uniapp中封装一个svg转base64的组件

    2024-07-18 14:26:01       46 阅读
  4. 前端项目学习记录1:svg图标封装与使用

    2024-07-18 14:26:01       28 阅读
  5. element-ui上传图片组件封装

    2024-07-18 14:26:01       39 阅读
  6. 实现svg图在Element+图片预览组件中显示

    2024-07-18 14:26:01       29 阅读
  7. element ui图片上传组件封装+校验黑白照片

    2024-07-18 14:26:01       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 14:26:01       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 14:26:01       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 14:26:01       57 阅读
  4. Python语言-面向对象

    2024-07-18 14:26:01       68 阅读

热门阅读

  1. Linux-快捷键以及vim工具使用

    2024-07-18 14:26:01       19 阅读
  2. Web前端-Web开发CSS基础3-盒模型

    2024-07-18 14:26:01       18 阅读
  3. C++ bind和placeholders

    2024-07-18 14:26:01       20 阅读
  4. 力扣LCR184.设计自助结算系统

    2024-07-18 14:26:01       19 阅读
  5. SpringBoot 实现整合kafka的简单使用

    2024-07-18 14:26:01       21 阅读
  6. 模乘逆元计算器

    2024-07-18 14:26:01       21 阅读
  7. 算法面试题六

    2024-07-18 14:26:01       21 阅读