1.0.13 • Published 1 month ago

@tanzhenxing/zx-icon v1.0.13

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

ZX-Icon 图标组件

组件介绍

ZX-Icon是一个功能丰富的图标组件,可以同时使用字体图标、图片和SVG。支持SVG字符串和SVG文件路径,通过简单的配置,可以实现各种图标展示效果,支持自定义前缀、大小、颜色等多种属性。

适用范围

平台兼容性

框架支持

Vue3uni-app

小程序支持

App快应用微信小程序支付宝小程序百度小程序字节小程序QQ小程序

浏览器支持

H5-SafariAndroid Browser微信浏览器(Android)QQ浏览器(Android)ChromeIEEdgeFirefox

框架兼容性

  • 仅支持 Vue 3 框架
  • 支持 uni-app 使用的 HBuilderX 和 CLI 方式搭建的项目
  • 支持 vite 和 webpack 构建方式

适用场景

  • 需要使用图标的各种界面
  • 需要使用自定义图标库的场景
  • 需要使用图片作为图标的场景
  • 需要控制图标大小、颜色的场景
  • 按钮、列表、导航等界面元素中使用图标

注意事项

  • 本组件使用 Vue 3 的 <script setup> 语法开发,不兼容 Vue 2 项目
  • 图标名称需要与引入的图标库名称一致
  • 使用图片作为图标时,需要提供图片的路径

安装说明

NPM 安装

# 使用 npm 安装
npm install @tanzhenxing/zx-icon

# 使用 yarn 安装
yarn add @tanzhenxing/zx-icon

# 使用 pnpm 安装
pnpm add @tanzhenxing/zx-icon

在项目中使用

全局注册

main.js 中注册组件:

import { createSSRApp } from 'vue'
import App from './App.vue'
import ZxIcon from '@tanzhenxing/zx-icon'

export function createApp() {
  const app = createSSRApp(App)
  app.component('zx-icon', ZxIcon)
  return {
    app
  }
}

局部注册

在需要使用的组件中单独引入:

<script setup>
import ZxIcon from '@tanzhenxing/zx-icon';
</script>

<template>
  <zx-icon name="eye"></zx-icon>
</template>

easycom 模式(推荐)

pages.json 中配置 easycom 规则:

// pages.json
{
  "easycom": {
    "autoscan": true,
    "custom": {
      "zx-icon": "@tanzhenxing/zx-icon"
    }
  }
}

配置完成后,可以在任意页面直接使用组件,无需额外引入:

<template>
  <zx-icon name="eye"></zx-icon>
</template>

引入方式

<script setup>
import ZxIcon from '@tanzhenxing/zx-icon';
</script>

基础用法

<zx-icon name="eye"></zx-icon>

组件属性

属性名类型默认值说明
nameString-图标名称、图片路径、SVG字符串或SVG文件路径
colorString#b0b0b0图标颜色
sizeString/Number34rpx图标字体大小
boldBooleanfalse是否显示粗体
customPrefixStringzx-icon自定义扩展前缀,方便用户扩展自己的图标库
modeStringaspectFill图片的mode,用于图片图标
widthString/Number-用于显示图片小图标时,图片的宽度
heightString/Number-用于显示图片小图标时,图片的高度
topString/Number-图标在垂直方向上的定位,用于解决某些情况下,让图标垂直居中的用途

事件

事件名说明回调参数
click点击图标时触发event: Event
load图片加载成功时触发event: Event
error图片加载失败时触发event: Event
svgLoadSVG加载成功时触发event: Event
svgErrorSVG加载失败时触发event: Event

方法

方法名说明返回值
getSvgBase64获取SVG的base64编码String
getIconType获取图标类型String (font/image/svg)

使用示例

基础用法

使用内置图标:

<zx-icon name="eye"></zx-icon>
<zx-icon name="home"></zx-icon>
<zx-icon name="user"></zx-icon>

图标颜色

自定义图标颜色:

<zx-icon name="heart" color="#3e8ed0"></zx-icon>
<zx-icon name="check" color="#48c78e"></zx-icon>
<zx-icon name="exclamation-triangle" color="#ffe08a"></zx-icon>
<zx-icon name="times" color="#f14668"></zx-icon>

图标大小

自定义图标大小:

<zx-icon name="star" size="16px"></zx-icon>
<zx-icon name="star" size="24px"></zx-icon>
<zx-icon name="star" size="32px"></zx-icon>

粗体图标

<zx-icon name="alert" bold></zx-icon>

使用图片作为图标

当name属性中包含"/"时,会被识别为图片路径:

<zx-icon name="https://zxui.org/logo.png" width="40px" height="40px"></zx-icon>
<zx-icon name="/static/images/icon.png" width="40px" height="40px"></zx-icon>

使用SVG图标

SVG文件路径

<zx-icon name="/static/icons/heart.svg" width="40px" height="40px"></zx-icon>
<zx-icon name="https://example.com/icon.svg" size="32px"></zx-icon>

SVG字符串内容

<zx-icon :name="svgString" size="32px" color="#ff6b6b"></zx-icon>

<script setup>
const svgString = `<svg viewBox="0 0 24 24" fill="currentColor">
  <path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
</svg>`;
</script>

自定义图标库

使用customPrefix属性可以自定义图标前缀,用于使用第三方图标库:

<zx-icon name="camera" customPrefix="custom-icon"></zx-icon>

垂直对齐

使用top属性可以调整图标的垂直位置,解决对齐问题:

<zx-icon name="align-center" top="2rpx"></zx-icon>

图标点击事件

<zx-icon name="bell" @click="handleClick"></zx-icon>

<script setup>
const handleClick = (event) => {
  console.log('图标被点击了', event);
}
</script>

图片和SVG加载事件

<zx-icon 
  name="https://example.com/image.png" 
  @load="onLoad" 
  @error="onError"
></zx-icon>

<zx-icon 
  name="/static/icon.svg" 
  @svgLoad="onSvgLoad" 
  @svgError="onSvgError"
></zx-icon>

<script setup>
const onLoad = (event) => {
  console.log('图片加载成功', event);
};

const onError = (event) => {
  console.log('图片加载失败', event);
};

const onSvgLoad = (event) => {
  console.log('SVG加载成功', event);
};

const onSvgError = (event) => {
  console.log('SVG加载失败', event);
};
</script>

获取图标信息

<zx-icon ref="iconRef" name="heart"></zx-icon>

<script setup>
import { ref, onMounted } from 'vue';

const iconRef = ref(null);

onMounted(() => {
  // 获取图标类型
  const iconType = iconRef.value.getIconType();
  console.log('图标类型:', iconType); // 'font', 'image', 或 'svg'
  
  // 如果是SVG,获取base64编码
  if (iconType === 'svg') {
    const base64 = iconRef.value.getSvgBase64();
    console.log('SVG Base64:', base64);
  }
});
</script>

注意事项

  1. 字体图标:使用图标前,需要确保相应的图标字体已经引入到项目中
  2. 图片识别:使用图片作为图标时,图片路径中必须包含"/"或具有图片文件扩展名
  3. SVG支持:支持SVG文件路径和SVG字符串内容两种方式
  4. 自定义前缀:自定义前缀时,需要确保已经引入对应的图标库
  5. 颜色限制:图标颜色属性对图片图标无效,对SVG图标使用currentColor时有效
  6. 尺寸优先级:当同时设置size和width/height属性时,对于图片和SVG图标,优先使用width和height属性
  7. 平台兼容:SVG字符串在不同平台的base64编码实现可能略有差异,组件已做兼容处理
  8. 性能考虑:大量使用SVG字符串时,建议使用SVG文件路径以获得更好的性能
@tanzhenxing/zx-dropdown-item@tanzhenxing/zx-editor@tanzhenxing/zx-fab@tanzhenxing/zx-fav@tanzhenxing/zx-goods-nav@tanzhenxing/zx-goods-sku@tanzhenxing/zx-popup-message@tanzhenxing/zx-popup-share@tanzhenxing/zx-rate@tanzhenxing/zx-read-more@tanzhenxing/zx-receive-invoice-edit@tanzhenxing/zx-image-preview@tanzhenxing/zx-indexed-list@tanzhenxing/zx-input@tanzhenxing/zx-input-number@tanzhenxing/zx-input-tag@tanzhenxing/zx-invoice-title-list@tanzhenxing/zx-list-item@tanzhenxing/zx-result@tanzhenxing/zx-scroll@tanzhenxing/zx-search-bar@tanzhenxing/zx-mention@tanzhenxing/zx-more@tanzhenxing/zx-navbar@tanzhenxing/zx-no-network@tanzhenxing/zx-notify@tanzhenxing/zx-number-keyboard@tanzhenxing/zx-order-cancel-panel@tanzhenxing/zx-order-remark@tanzhenxing/zx-page-header@tanzhenxing/zx-pagination@tanzhenxing/zx-fixed-button@tanzhenxing/zx-fixed-nav@tanzhenxing/zx-floating-bubble@tanzhenxing/zxui@tanzhenxing/zx-step@tanzhenxing/zx-submenu@tanzhenxing/zx-submit-bar@tanzhenxing/zx-tabbar@tanzhenxing/zx-tabbar-item@tanzhenxing/zx-tag@tanzhenxing/zx-textarea@tanzhenxing/zx-time-picker@tanzhenxing/zx-timeline-item@tanzhenxing/zx-title@tanzhenxing/zx-toast@tanzhenxing/zx-transfer@tanzhenxing/zx-tree-select@tanzhenxing/zx-upload@tanzhenxing/zx-uploader@tanzhenxing/zx-select@tanzhenxing/zx-select-picker@tanzhenxing/zx-settle-bar@tanzhenxing/zx-share-sheet@tanzhenxing/zx-autocomplete@tanzhenxing/zx-avatar@tanzhenxing/zx-avatar-cropper@tanzhenxing/zx-backtop@tanzhenxing/zx-breadcrumb-item@tanzhenxing/zx-button@tanzhenxing/zx-checkbox@tanzhenxing/zx-collapse@tanzhenxing/zx-combox@tanzhenxing/zx-ai-prompts@tanzhenxing/zx-ai-welcome@tanzhenxing/zx-album@tanzhenxing/zx-alert@tanzhenxing/zx-action-sheet@tanzhenxing/zx-address@tanzhenxing/zx-address-edit@tanzhenxing/zx-address-list@tanzhenxing/zx-contact-card@tanzhenxing/zx-contact-list@tanzhenxing/zx-coupon-list@tanzhenxing/zx-date-picker@tanzhenxing/zx-delivery@tanzhenxing/zx-camera@tanzhenxing/zx-carousel@tanzhenxing/zx-cart-bar@tanzhenxing/zx-cascader@tanzhenxing/zx-cell
1.0.9

1 month ago

1.0.8

1 month ago

1.0.7

2 months ago

1.0.11

1 month ago

1.0.10

1 month ago

1.0.13

1 month ago

1.0.12

1 month ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

1 year ago