fix: an error was reported in the adaptation of the naive component library theme (#4041)

* fix: naive组件库 主题适配报错,需将hsl转换为rgb格式

* feat: 增加NDataTable示例

* chore: hsl转换函数移动到@vben/utils内

* fix: 优化正则表达式

* fix: 优化正则表达式2

* fix: 正则表达式优化,优化hlsStringToRGB函数

* fix: 使用tinyColor进行转换

* Update packages/@core/base/shared/src/colorful/convert.ts

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* fix: lint error

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: vince <vince292007@gmail.com>
This commit is contained in:
jinmao88 2024-08-07 12:13:54 +08:00 committed by GitHub
parent 279a3a4c21
commit d9ba9917ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 76 additions and 6 deletions

View File

@ -2,7 +2,8 @@
"page": {
"demos": {
"title": "Demos",
"naive": "Naive UI"
"naive": "Naive UI",
"table": "Table"
}
}
}

View File

@ -2,7 +2,8 @@
"page": {
"demos": {
"title": "演示",
"naive": "Naive UI"
"naive": "Naive UI",
"table": "Table"
}
}
}

View File

@ -24,6 +24,15 @@ const routes: RouteRecordRaw[] = [
path: '/demos/naive',
component: () => import('#/views/demos/naive/index.vue'),
},
{
meta: {
icon: 'mdi:shield-key-outline',
title: $t('page.demos.table'),
},
name: 'Table',
path: '/demos/table',
component: () => import('#/views/demos/table/index.vue'),
},
],
},
];

View File

@ -0,0 +1,31 @@
<script setup lang="ts">
import { ref } from 'vue';
import { NDataTable } from 'naive-ui';
const columns = ref([
{
key: 'no',
title: 'No',
},
{
key: 'title',
title: 'Title',
},
{
key: 'length',
title: 'Length',
},
]);
const data = [
{ length: '4:18', no: 3, title: 'Wonderwall' },
{ length: '4:48', no: 4, title: "Don't Look Back in Anger" },
{ length: '7:27', no: 12, title: 'Champagne Supernova' },
];
</script>
<template>
<NDataTable :columns="columns" :data="data" />
</template>
<style scoped></style>

View File

@ -40,5 +40,34 @@ function isValidColor(color?: string) {
}
return new TinyColor(color).isValid;
}
/**
* HLS字符串转换为RGB颜色字符串
*
* HLS值的字符串
* TinyColor对象便
*
* RGB颜色字符串
*
* @param str HLS颜色值的字符串'deg''grad''rad''turn'
* @returns RGB颜色字符串
*/
function hlsStringToRGBString(str: string): string {
// 移除HLS字符串中的度量单位以便正确解析
const color = new TinyColor(
`hsl(${str.replaceAll(/deg|grad|rad|turn/g, '')})`,
);
// 检查颜色是否有效,如果无效则直接返回原始字符串
if (!color.isValid) {
return str;
}
// 返回转换后的RGB颜色字符串
return color.toRgbString();
}
export { convertToHsl, convertToHslCssVar, isValidColor, TinyColor };
export {
convertToHsl,
convertToHslCssVar,
hlsStringToRGBString,
isValidColor,
TinyColor,
};

View File

@ -1,7 +1,7 @@
import { reactive, watch } from 'vue';
import { preferences } from '@vben/preferences';
import { updateCSSVariables } from '@vben/utils';
import { hlsStringToRGBString, updateCSSVariables } from '@vben/utils';
/**
*
@ -102,7 +102,7 @@ export function useNaiveDesignTokens() {
const getCssVariableValue = (variable: string, isColor: boolean = true) => {
const value = rootStyles.getPropertyValue(variable);
return isColor ? `hsl(${value})` : value;
return isColor ? hlsStringToRGBString(value) : value;
};
watch(
@ -150,7 +150,6 @@ export function useNaiveDesignTokens() {
},
{ immediate: true },
);
return {
commonTokens,
};