soybean-admin/src/composables/icon.ts

61 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { h } from 'vue';
import SvgIcon from '~/src/components/custom/svg-icon.vue';
/**
* 图标渲染
* - 用于vue的render函数
*/
export const useIconRender = () => {
interface IconConfig {
/**
* 图标名称(iconify图标的名称)
* - 例如mdi-account 或者 mdi:account
*/
icon?: string;
/**
* 本地svg图标文件名(assets/svg-icon文件夹下)
*/
localIcon?: string;
/** 图标颜色 */
color?: string;
/** 图标大小 */
fontSize?: number;
}
interface IconStyle {
color?: string;
fontSize?: string;
}
/**
* 图标渲染
* @param config
* @property icon - 图标名称(iconify图标的名称), 例如mdi-account 或者 mdi:account
* @property localIcon - 本地svg图标文件名(assets/svg-icon文件夹下)
* @property color - 图标颜色
* @property fontSize - 图标大小
*/
const iconRender = (config: IconConfig) => {
const { color, fontSize, icon, localIcon } = config;
const style: IconStyle = {};
if (color) {
style.color = color;
}
if (fontSize) {
style.fontSize = `${fontSize}px`;
}
if (!icon && !localIcon) {
window.console.warn('没有传递图标名称请确保给icon或localIcon传递有效值!');
}
return () => h(SvgIcon, { icon, localIcon, style });
};
return {
iconRender
};
};