Skip to content

Commit

Permalink
feat(ConfigProvider): 支持嵌套主题配置
Browse files Browse the repository at this point in the history
  • Loading branch information
ocean-gao committed Nov 27, 2024
1 parent 21985aa commit f45bc05
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 19 deletions.
40 changes: 26 additions & 14 deletions components/_theme/useTheme.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,54 @@
import { type Ref, ref, watch } from 'vue';
import { createSharedComposable } from '@vueuse/core';

import { tryOnMounted } from '@vueuse/core';
import { useConfig } from '../config-provider';
import { applyTheme } from './applyTheme';
import { baseTheme } from './base';
import type { TThemeVars } from './base';

function _useTheme() {
// TODO: theme 和当前组件 config provider 的 getContainer 关联,目前只有第一个调用 useTheme 的组件生效
const config = useConfig();
const themeVars: Ref<TThemeVars> = ref(baseTheme());

const handleApplyTheme = () => {
const getContainer = config.getContainer?.value;
const theme = config.theme?.value;
const themeOverrides = config.themeOverrides?.value;

if (!getContainer) {
return;
}

const { themeVars: currentThemeVars } = applyTheme(
getContainer(),
theme,
themeOverrides,
);
themeVars.value = currentThemeVars;
};

watch(
[
() => config.getContainer?.value,
() => config.theme?.value,
() => config.themeOverrides?.value,
],
([getContainer, theme, themeOverrides]) => {
if (!getContainer) {
return;
}
const { themeVars: currentThemeVars } = applyTheme(
getContainer(),
theme,
themeOverrides,
);
themeVars.value = currentThemeVars;
() => {
handleApplyTheme();
},
{
immediate: true,
immediate: false,
},
);

tryOnMounted(() => {
handleApplyTheme();
});

return {
config,
themeVars,
};
}

export const useTheme = createSharedComposable(_useTheme);
export const useTheme = _useTheme;
46 changes: 46 additions & 0 deletions docs/.vitepress/components/configProvider/customTheme.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<template>
<FConfigProvider>
<FSpace>
<FButton type="info"> Info </FButton>
<FButton type="success"> Success</FButton>
<FButton type="warning"> Warning </FButton>
<FButton type="danger"> Danger </FButton>
</FSpace>

<FDivider />

<div :ref="(el) => containerSub = el">
<FConfigProvider :getContainer="getContainerSub" :themeOverrides="themeOverrides">
<FSpace>
<FButton type="info"> Info </FButton>
<FButton type="success"> Success</FButton>
<FButton type="warning"> Warning </FButton>
<FButton type="danger"> Danger </FButton>
</FSpace>
</FConfigProvider>
</div>
</FConfigProvider>
</template>

<script setup>
import { FDivider } from '@fesjs/fes-design';
import { ref } from 'vue';
const containerSub = ref(null);
const getContainerSub = () => {
return containerSub.value;
};
const themeOverrides = ref({
common: {
fontColorBase: '#606266',
primaryColor: '#679a4d',
successColor: '#67c23a',
dangerColor: '#d32323',
warningColor: '#cf9847',
},
});
</script>

<style scoped></style>
18 changes: 13 additions & 5 deletions docs/.vitepress/components/configProvider/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Config Provider 全局配置
# ConfigProvider 全局配置

为组件提供统一的全局化配置。
Config Provider 使用了 [Vue 的 provide/inject 特性](https://v3.vuejs.org/guide/composition-api-provide-inject.html#using-provide)
`ConfigProvider` 使用了 [Vue 的 provide/inject 特性](https://v3.vuejs.org/guide/composition-api-provide-inject.html#using-provide)

## 组件注册

Expand Down Expand Up @@ -29,13 +29,21 @@ const getContainer = () => {

### 切换语言

--CHANGELOCALE
:::demo
changeLocale.vue
:::

### 自定义语言

--CUSTOMLOCALE
:::demo
customLocale.vue
:::

--CODE
### 自定义主题

:::demo
customTheme.vue
:::

## Props

Expand Down

0 comments on commit f45bc05

Please sign in to comment.