-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters