-
Notifications
You must be signed in to change notification settings - Fork 341
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
8 changed files
with
116 additions
and
16 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
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
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,28 +1,63 @@ | ||
<template> | ||
<slot /> | ||
<CssVarsRenderer v-bind="$attrs"> | ||
<slot /> | ||
</CssVarsRenderer> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { computed, defineComponent, PropType } from 'vue' | ||
import { computed, defineComponent, PropType, h, Fragment } from 'vue' | ||
import { useComponentPresetProp } from '../../composables/useComponentPreset' | ||
import { ComponentConfig } from '../../services/component-config' | ||
import { provideLocalConfig, useLocalConfig } from '../../composables/useLocalConfig' | ||
import { useGlobalConfigProvider } from './hooks/useGlobalConfigProvider' | ||
import { PartialGlobalConfig } from '../../services/global-config' | ||
import { renderSlotNodes } from '../../utils/headless' | ||
import { useColors } from '../../composables' | ||
const CssVarsRenderer = defineComponent({ | ||
name: 'VaCssVarsRenderer', | ||
inheritAttrs: false, | ||
setup (props, { slots, attrs }) { | ||
const { colorsToCSSVariable, colors } = useColors() | ||
const style = computed(() => { | ||
return colorsToCSSVariable(colors) | ||
}) | ||
return () => h(Fragment, attrs, renderSlotNodes(slots.default, {}, { | ||
style: style.value, | ||
}) || undefined) | ||
}, | ||
}) | ||
export default defineComponent({ | ||
name: 'VaConfig', | ||
components: { CssVarsRenderer }, | ||
props: { | ||
...useComponentPresetProp, | ||
components: { type: Object as PropType<ComponentConfig>, default: () => ({}) }, | ||
colors: { type: Object as PropType<PartialGlobalConfig['colors']>, default: () => ({}) }, | ||
}, | ||
inheritAttrs: false, | ||
setup (props) { | ||
const prevChain = useLocalConfig() | ||
// We want it to be an array and not a merged object for optimization purposes | ||
const nextChain = computed(() => [...prevChain.value, props.components]) | ||
provideLocalConfig(nextChain) | ||
return {} | ||
const newConfig = useGlobalConfigProvider(computed(() => { | ||
return { | ||
colors: props.colors, | ||
} | ||
})) | ||
return { | ||
newConfig, | ||
} | ||
}, | ||
}) | ||
</script> |
45 changes: 45 additions & 0 deletions
45
packages/ui/src/components/va-config/hooks/useGlobalConfigProvider.ts
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,45 @@ | ||
import { mergeDeep } from './../../../utils/merge-deep' | ||
import cloneDeep from 'lodash/cloneDeep' | ||
import { provide, computed, Ref } from 'vue' | ||
import { useGlobalConfig } from '../../../composables' | ||
import { GLOBAL_CONFIG, GlobalConfig, GlobalConfigUpdater, PartialGlobalConfig } from '../../../services/global-config' | ||
|
||
export const useGlobalConfigProvider = (next: Ref<PartialGlobalConfig>) => { | ||
const { globalConfig } = useGlobalConfig() | ||
|
||
const nextChain = computed(() => { | ||
const gcCopy = cloneDeep(globalConfig.value) | ||
const compiledCopy: GlobalConfig = { | ||
...gcCopy, | ||
colors: { | ||
...gcCopy.colors, | ||
get variables () { | ||
return this.presets[this.currentPresetName] | ||
}, | ||
set variables (value) { | ||
this.presets[this.currentPresetName] = value | ||
}, | ||
}, | ||
} | ||
|
||
return mergeDeep(compiledCopy, next.value) as GlobalConfig | ||
}) | ||
|
||
const getGlobalConfig = (): GlobalConfig => nextChain.value | ||
const setGlobalConfig = (updater: GlobalConfig | GlobalConfigUpdater<GlobalConfig>) => { | ||
throw new Error('setGlobalConfig can not be used in VaConfig') | ||
} | ||
|
||
const mergeGlobalConfig = (updater: PartialGlobalConfig | GlobalConfigUpdater<PartialGlobalConfig>) => { | ||
throw new Error('mergeGlobalConfig can not be used in VaConfig') | ||
} | ||
|
||
provide(GLOBAL_CONFIG, { | ||
getGlobalConfig, | ||
setGlobalConfig, | ||
mergeGlobalConfig, | ||
globalConfig: nextChain, | ||
}) | ||
|
||
return nextChain | ||
} |
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
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
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
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