|
|
@@ -1,6 +1,50 @@
|
|
|
<script lang="ts">
|
|
|
import { createComponent } from '@/packages/utils/create';
|
|
|
+import { h, PropType } from 'vue';
|
|
|
+import { baseCSS } from './base';
|
|
|
const { componentName, create } = createComponent('config-provider');
|
|
|
-import { component } from './common';
|
|
|
-export default /* @__PURE__ */ create(component);
|
|
|
+export default /* @__PURE__ */ create({
|
|
|
+ props: {
|
|
|
+ theme: { type: String, default: '' },
|
|
|
+ themeVars: { type: Object, default: () => {} },
|
|
|
+ tag: { type: String as PropType<keyof HTMLElementTagNameMap>, default: 'div' }
|
|
|
+ },
|
|
|
+ setup(props: any, { slots }: any) {
|
|
|
+ const kebabCase = (str: string): string => {
|
|
|
+ str = str.replace(str.charAt(0), str.charAt(0).toLocaleLowerCase());
|
|
|
+ return str.replace(/([a-z])([A-Z])/g, (_, p1, p2) => p1 + '-' + p2.toLowerCase());
|
|
|
+ };
|
|
|
+ const mapThemeVarsToCSSVars = (themeVars: Record<string, string>) => {
|
|
|
+ if (!themeVars) return;
|
|
|
+ const cssVars: Record<string, string> = {};
|
|
|
+ Object.keys(themeVars).forEach((key) => {
|
|
|
+ cssVars[`--nut-${kebabCase(key)}`] = themeVars[key];
|
|
|
+ });
|
|
|
+ const base: Record<string, string> = {};
|
|
|
+ baseCSS.map((item: string, index) => {
|
|
|
+ Object.keys(cssVars).forEach((key) => {
|
|
|
+ if (key == item) {
|
|
|
+ base[key] = cssVars[key];
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ if (Object.keys(base).length != 0) {
|
|
|
+ Object.entries(base).forEach(([key, value]) => {
|
|
|
+ document.documentElement.style.setProperty(key, value);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return cssVars;
|
|
|
+ };
|
|
|
+ return () => {
|
|
|
+ return h(
|
|
|
+ props.tag,
|
|
|
+ {
|
|
|
+ class: `nut-theme-${props.theme}`,
|
|
|
+ style: mapThemeVarsToCSSVars(props.themeVars)
|
|
|
+ },
|
|
|
+ slots.default?.()
|
|
|
+ );
|
|
|
+ };
|
|
|
+ }
|
|
|
+});
|
|
|
</script>
|