Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 11 commits into from
Aug 7, 2024
3 changes: 2 additions & 1 deletion apps/web-naive/src/locales/langs/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"page": {
"demos": {
"title": "Demos",
"naive": "Naive UI"
"naive": "Naive UI",
"table": "Table"
}
}
}
3 changes: 2 additions & 1 deletion apps/web-naive/src/locales/langs/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"page": {
"demos": {
"title": "演示",
"naive": "Naive UI"
"naive": "Naive UI",
"table": "Table"
}
}
}
9 changes: 9 additions & 0 deletions apps/web-naive/src/router/routes/modules/demos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
},
],
},
];
Expand Down
31 changes: 31 additions & 0 deletions apps/web-naive/src/views/demos/table/index.vue
Original file line number Diff line number Diff line change
@@ -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>
31 changes: 30 additions & 1 deletion packages/@core/base/shared/src/colorful/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
5 changes: 2 additions & 3 deletions packages/effects/hooks/src/use-design-tokens.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
* 用于适配各个框架的设计系统
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -150,7 +150,6 @@ export function useNaiveDesignTokens() {
},
{ immediate: true },
);

return {
commonTokens,
};
Expand Down
Loading