From 3e2462a77c9284accadc37d96e0878d884a94d5c Mon Sep 17 00:00:00 2001 From: Maksim Nedoshev Date: Mon, 1 May 2023 12:10:38 +0300 Subject: [PATCH] feat(va-config): colors prop --- packages/docs/layouts/landing.vue | 15 ++++--- .../components/va-config/VaConfig.demo.vue | 12 +++++ .../ui/src/components/va-config/VaConfig.vue | 41 +++++++++++++++-- .../hooks/useGlobalConfigProvider.ts | 45 +++++++++++++++++++ packages/ui/src/composables/useColors.ts | 4 +- .../services/color/tests/color-config.spec.ts | 8 ++-- packages/ui/src/utils/headless.ts | 2 +- packages/ui/src/utils/merge-deep.ts | 5 ++- 8 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 packages/ui/src/components/va-config/hooks/useGlobalConfigProvider.ts diff --git a/packages/docs/layouts/landing.vue b/packages/docs/layouts/landing.vue index 296033067e..f56b84d34a 100644 --- a/packages/docs/layouts/landing.vue +++ b/packages/docs/layouts/landing.vue @@ -1,13 +1,15 @@ - diff --git a/packages/ui/src/components/va-config/VaConfig.demo.vue b/packages/ui/src/components/va-config/VaConfig.demo.vue index 3d20e7e2ae..dabf18a60d 100644 --- a/packages/ui/src/components/va-config/VaConfig.demo.vue +++ b/packages/ui/src/components/va-config/VaConfig.demo.vue @@ -70,6 +70,18 @@ + + + + + Button inside va-config + + +
CSS variables
+
+
diff --git a/packages/ui/src/components/va-config/VaConfig.vue b/packages/ui/src/components/va-config/VaConfig.vue index e66fc5e63c..e5df4f4839 100644 --- a/packages/ui/src/components/va-config/VaConfig.vue +++ b/packages/ui/src/components/va-config/VaConfig.vue @@ -1,20 +1,47 @@ diff --git a/packages/ui/src/components/va-config/hooks/useGlobalConfigProvider.ts b/packages/ui/src/components/va-config/hooks/useGlobalConfigProvider.ts new file mode 100644 index 0000000000..ca6f2c38c7 --- /dev/null +++ b/packages/ui/src/components/va-config/hooks/useGlobalConfigProvider.ts @@ -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) => { + 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) => { + throw new Error('setGlobalConfig can not be used in VaConfig') + } + + const mergeGlobalConfig = (updater: PartialGlobalConfig | GlobalConfigUpdater) => { + throw new Error('mergeGlobalConfig can not be used in VaConfig') + } + + provide(GLOBAL_CONFIG, { + getGlobalConfig, + setGlobalConfig, + mergeGlobalConfig, + globalConfig: nextChain, + }) + + return nextChain +} diff --git a/packages/ui/src/composables/useColors.ts b/packages/ui/src/composables/useColors.ts index 89c1e58ea6..34fdac64cb 100644 --- a/packages/ui/src/composables/useColors.ts +++ b/packages/ui/src/composables/useColors.ts @@ -22,6 +22,7 @@ import { normalizeColorName, } from '../services/color/utils' import { isDev } from '../utils/env' +import kebabCase from 'lodash/kebabCase' /** * You can add these props to any component by destructuring them inside props option. @@ -111,7 +112,8 @@ export const useColors = () => { .keys(colors) .filter((key) => colors[key] !== undefined) .reduce((acc: Record, colorName: string) => { - acc[`--${prefix}-${colorName}`] = getColor(colors[colorName], undefined, true) + acc[`--${prefix}-${kebabCase(colorName)}`] = getColor(colors[colorName], undefined, true) + acc[`--${prefix}-on-${kebabCase(colorName)}`] = getColor(getTextColor(getColor(colors[colorName]!)), undefined, true) return acc }, {}) } diff --git a/packages/ui/src/services/color/tests/color-config.spec.ts b/packages/ui/src/services/color/tests/color-config.spec.ts index 0a454c7834..5b84d2e3a0 100644 --- a/packages/ui/src/services/color/tests/color-config.spec.ts +++ b/packages/ui/src/services/color/tests/color-config.spec.ts @@ -44,10 +44,10 @@ describe('useColors', () => { ) it.each([ - [[{ color: '#000000' }, 'va-test'], { '--va-test-color': '#000000' }], - [[{ color: 'secondary' }, 'va-test'], { '--va-test-color': 'var(--va-secondary)' }], - [[{ color: 'var(--va-primary)' }, 'va-test'], { '--va-test-color': 'var(--va-primary)' }], // TODO call Oleg - [[{ color: 'bad-color' }, 'va-test'], { '--va-test-color': '#154EC1' }], + [[{ color: '#000000' }, 'va-test'], { '--va-test-color': '#000000', '--va-test-on-color': 'var(--va-text-inverted)' }], + [[{ color: 'secondary' }, 'va-test'], { '--va-test-color': 'var(--va-secondary)', '--va-test-on-color': 'var(--va-text-inverted)' }], + [[{ color: 'var(--va-primary)' }, 'va-test'], { '--va-test-color': 'var(--va-primary)', '--va-test-on-color': 'var(--va-text-inverted)' }], + [[{ color: 'bad-color' }, 'va-test'], { '--va-test-color': '#154EC1', '--va-test-on-color': 'var(--va-text-inverted)' }], [[{}, 'va-test'], {}], ])( 'colorToCssVariableArgs %s should be %s', diff --git a/packages/ui/src/utils/headless.ts b/packages/ui/src/utils/headless.ts index 517713f15d..e9dad876ef 100644 --- a/packages/ui/src/utils/headless.ts +++ b/packages/ui/src/utils/headless.ts @@ -37,7 +37,7 @@ const toNode = (v: any, attrs: NodeAttributes): VNode | null => { if (v.type === Fragment) { if (v.children === null) { return v } - return toNode(v.children[0], attrs) + return h(Fragment, v.props, v.children.map((v: any) => toNode(v, attrs))) } return h(v, attrs) diff --git a/packages/ui/src/utils/merge-deep.ts b/packages/ui/src/utils/merge-deep.ts index b2fbc00a76..6b5a56ebfc 100644 --- a/packages/ui/src/utils/merge-deep.ts +++ b/packages/ui/src/utils/merge-deep.ts @@ -14,7 +14,10 @@ export const mergeDeep = (target: any, source: any): any => { const sourceValue = source[key] if (isObject(targetValue) && isObject(sourceValue)) { - target[key] = mergeDeep(Object.assign({}, targetValue), sourceValue) + target[key] = mergeDeep(Object.create( + Object.getPrototypeOf(targetValue), + Object.getOwnPropertyDescriptors(targetValue), + ), sourceValue) } else { target[key] = sourceValue }