@bimdos/icon
Version:
跨框架图标库,支持 React、Vue、Angular、AngularJS、小程序等多种使用方式
89 lines (75 loc) • 3.63 kB
JavaScript
import React from 'react';
import { Image } from '@tarojs/components';
import Taro from '@tarojs/taro';
/**
* Base64 编码函数 (兼容小程序环境)
*/
function base64Encode(str) {
// 使用微信小程序的 base64 编码
if (typeof wx !== 'undefined' && wx.arrayBufferToBase64) {
const buffer = new ArrayBuffer(str.length);
const view = new Uint8Array(buffer);
for (let i = 0; i < str.length; i++) {
view[i] = str.charCodeAt(i);
}
return wx.arrayBufferToBase64(buffer);
}
// 备用方案:简单的base64编码
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
let result = '';
let i = 0;
while (i < str.length) {
const a = str.charCodeAt(i++);
const b = i < str.length ? str.charCodeAt(i++) : 0;
const c = i < str.length ? str.charCodeAt(i++) : 0;
const bitmap = (a << 16) | (b << 8) | c;
result += chars.charAt((bitmap >> 18) & 63);
result += chars.charAt((bitmap >> 12) & 63);
result += i - 2 < str.length ? chars.charAt((bitmap >> 6) & 63) : '=';
result += i - 1 < str.length ? chars.charAt(bitmap & 63) : '=';
}
return result;
}
/**
* Bimdos 图标组件 - Taro 小程序版本
* 使用 Image 组件显示 SVG 图标
*/
const BimdosIcon = ({
name,
size = 16,
color = 'currentColor',
className = '',
style = {},
...props
}) => {
const iconData = iconMap[name];
if (!iconData) {
console.warn(`Icon "${name}" not found`);
return null;
}
// 生成 SVG Data URL
const svgContent = `<svg width="${size}" height="${size}" viewBox="0 0 1024 1024" fill="${color}" xmlns="http://www.w3.org/2000/svg"><path d="${iconData.path}"/></svg>`;
const svgDataUrl = `data:image/svg+xml;base64,${base64Encode(svgContent)}`;
return (
<Image
className={`bimdos-icon ${className}`}
src={svgDataUrl}
style={{
width: `${size}px`,
height: `${size}px`,
display: 'inline-block',
...style
}}
{...props}
/>
);
};
// 图标数据映射
const iconMap = {
'up': { path: 'M489.373 361.373C501.744 349 521.726 348.877 534.25 361l.377.372 256 256c12.497 12.496 12.497 32.758 0 45.254-12.371 12.372-32.353 12.496-44.877.372l-.377-.372L512 429.255 278.627 662.627C266.256 675 246.274 675.123 233.75 663l-.377-.372C221 650.256 220.877 630.274 233 617.75l.372-.377z' },
'right': { path: 'M361.373 233.373C373.744 221 393.726 220.877 406.25 233l.377.372 256 256c12.372 12.371 12.496 32.353.372 44.877l-.372.377-256 256c-12.496 12.497-32.758 12.497-45.254 0-12.373-12.37-12.496-32.352-.373-44.876l.372-.377L594.745 512 361.373 278.627C349 266.256 348.877 246.274 361 233.75z' },
'left': { path: 'M617.373 233.373c12.496-12.497 32.758-12.497 45.254 0 12.372 12.371 12.496 32.353.372 44.877l-.372.377L429.255 512l233.372 233.373c12.372 12.371 12.496 32.353.372 44.877l-.372.377C650.256 803 630.274 803.123 617.75 791l-.377-.372-256-256C349 522.256 348.877 502.274 361 489.75l.372-.377z' },
'down': { path: 'M745.373 361.373c12.496-12.497 32.758-12.497 45.254 0 12.372 12.371 12.496 32.353.372 44.877l-.372.377-256 256C522.256 675 502.274 675.123 489.75 663l-.377-.372-256-256c-12.497-12.496-12.497-32.758 0-45.254C245.744 349 265.726 348.877 278.25 361l.377.372L512 594.745z' }
};
export default BimdosIcon;
export { iconMap };