feat: 系统菜单增加自定义svg支持

This commit is contained in:
2026-08-19 17:44:48 +08:00
parent 22a5300e55
commit 4a634bb5db
13 changed files with 163 additions and 17 deletions
@@ -1,35 +1,108 @@
<script setup lang="ts">
import type { Component } from 'vue';
import { computed } from 'vue';
import { computed, useAttrs } from 'vue';
import { IconDefault, IconifyIcon } from '@vben-core/icons';
import {
cn,
isFunction,
isHttpUrl,
isObject,
isString,
} from '@vben-core/shared/utils';
defineOptions({ inheritAttrs: false });
const props = defineProps<{
// 没有是否显示默认图标
fallback?: boolean;
icon?: Component | Function | string;
}>();
const attrs = useAttrs();
const isRemoteIcon = computed(() => {
return isString(props.icon) && isHttpUrl(props.icon);
});
/** 直接内联的 SVG 字符串(如菜单 icon_svg) */
const isSvgIcon = computed(() => {
if (!isString(props.icon)) {
return false;
}
const trimmed = props.icon.trim();
return trimmed.startsWith('<svg') || trimmed.startsWith('<?xml');
});
const isComponent = computed(() => {
const { icon } = props;
return !isString(icon) && (isObject(icon) || isFunction(icon));
});
/**
* 将硬编码 fill/stroke 改为 currentColor,使其随深浅色主题(与 Iconify 一致)。
* 保留 none / currentColor / url(#...) 不改。
*/
function toThemeAwareSvg(raw: string): string {
const keep = (v: string) => {
const s = v.trim().toLowerCase();
return (
s === 'none' ||
s === 'currentcolor' ||
s.startsWith('url(') ||
s === 'transparent'
);
};
const attr = (name: 'fill' | 'stroke', v: string, q: string) =>
keep(v) ? ` ${name}=${q}${v}${q}` : ` ${name}=${q}currentColor${q}`;
const styleVal = (style: string) =>
style
.replace(/(?:^|;)\s*fill\s*:\s*([^;]+)/gi, (m, v: string) =>
keep(v) ? m : ';fill:currentColor',
)
.replace(/(?:^|;)\s*stroke\s*:\s*([^;]+)/gi, (m, v: string) =>
keep(v) ? m : ';stroke:currentColor',
);
return raw
.replace(/\sfill=(["'])([^"']*)\1/gi, (_, q: string, v: string) =>
attr('fill', v, q),
)
.replace(/\sstroke=(["'])([^"']*)\1/gi, (_, q: string, v: string) =>
attr('stroke', v, q),
)
.replace(/style=(["'])([^"']*)\1/gi, (_, q: string, style: string) =>
`style=${q}${styleVal(style)}${q}`,
);
}
const themedSvgHtml = computed(() =>
isSvgIcon.value && isString(props.icon)
? toThemeAwareSvg(props.icon.trim())
: '',
);
/** 默认 1em;外部 size-* 由 cn 合并覆盖 */
const svgRootClass = computed(() =>
cn(
// text-current:继承父级文字色;CSS 兜底把未改干净的 fill 也拉成 currentColor
'inline-flex size-[1em] shrink-0 items-center justify-center overflow-hidden text-current [&>svg]:!size-full [&>svg]:fill-current [&_path:not([fill=none])]:fill-current [&_g:not([fill=none])]:fill-current [&_circle:not([fill=none])]:fill-current [&_rect:not([fill=none])]:fill-current',
attrs.class as string | string[] | Record<string, boolean> | undefined,
),
);
</script>
<template>
<component :is="icon as Component" v-if="isComponent" v-bind="$attrs" />
<img v-else-if="isRemoteIcon" :src="icon as string" v-bind="$attrs" />
<IconifyIcon v-else-if="icon" v-bind="$attrs" :icon="icon as string" />
<IconDefault v-else-if="fallback" v-bind="$attrs" />
<component :is="icon as Component" v-if="isComponent" v-bind="attrs" />
<img v-else-if="isRemoteIcon" :src="icon as string" v-bind="attrs" />
<span
v-else-if="isSvgIcon"
v-bind="{ ...attrs, class: svgRootClass }"
v-html="themedSvgHtml"
></span>
<IconifyIcon v-else-if="icon" v-bind="attrs" :icon="icon as string" />
<IconDefault v-else-if="fallback" v-bind="attrs" />
</template>