Skip to content

Commit bcbdaa7

Browse files
committed
refactor(theme): remove theme scale style in theme-provider, remove space style in common scss and theme-provider scss, use dynamicStyles to generate them in runtime, so that inherited scale value can work correctly
1 parent 800ad7a commit bcbdaa7

File tree

7 files changed

+42
-9
lines changed

7 files changed

+42
-9
lines changed

packages/components/src/components/config/config.context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const GlobalContextConfig = reactive({
3838
sm: '2',
3939
xl: '3',
4040
},
41+
scale: 1,
4142
} as ThemeConfig,
4243
zIndex: {
4344
teleport: 1000,

packages/components/src/components/theme-provider/ThemeProvider.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { themeProviderProps } from './type';
44
import { provideContextConfig } from '../config';
55
import { provide } from 'vue';
66
import { useSetupEdit } from '@lun/core';
7-
import { useNamespace } from 'hooks';
87

98
export const ThemeProviderKey = Symbol(__DEV__ ? 'ThemeProviderKey' : '');
109

@@ -23,11 +22,9 @@ export const ThemeProvider = defineSSRCustomElement({
2322
theme: props,
2423
});
2524
useSetupEdit();
26-
const ns = useNamespace(name);
2725
// return () => undefined;
2826
return () => (
2927
<>
30-
<style>{`:host{${ns.vn('scale', false)}:${props.scale}}`}</style>
3128
<slot></slot>
3229
</>
3330
);

packages/theme/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from './scss/common';
12
export * from './scss/components';

packages/theme/src/scss/common/index.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
@use '../mixins/config.scss' as *;
44
@use '../mixins/theme.scss' as *;
55
@use './radius.scss' as *;
6-
@use './space.scss' as *;
76

87
:host([hidden]) {
98
display: none;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { createImportStyle, getThemeValueFromInstance, TGlobalContextConfig } from '@lun/components';
2+
import { GlobalContextConfig, GlobalStaticConfig } from '@lun/components';
3+
import { once, toPxIfNum } from '@lun/utils';
4+
import commonStyles from './index.scss?inline';
5+
6+
export const importCommonDynamicTheme = once(() => {
7+
GlobalContextConfig.dynamicStyles.common.push((vm, name, context) => {
8+
const { theme, namespace } = context as TGlobalContextConfig;
9+
const themeScale = theme.scale as any;
10+
const n = namespace || GlobalStaticConfig.namespace;
11+
const { commonSeparator } = GlobalStaticConfig;
12+
const scale = getThemeValueFromInstance(vm, 'scale') || themeScale?.[name] || themeScale?.common || themeScale;
13+
14+
function getVarName(...list: (string | number)[]) {
15+
return '--' + n + commonSeparator + list.join(commonSeparator);
16+
}
17+
const scaleVar = `var(${getVarName('scale')})`;
18+
19+
const scaleStyle = scale > 0 ? `:host{${getVarName('scale')}:${scale}}` : '';
20+
const spaces = [4, 8, 12, 16, 24, 32, 40, 48, 64],
21+
spaceStyle = `:host([scale]),:host([root]){${spaces
22+
.map(
23+
(s, i) =>
24+
`${getVarName('space', i + 1)}:calc(${toPxIfNum(s)} * ${scaleVar});` +
25+
`${getVarName('space', 'hypot', i + 1)}:calc(${toPxIfNum(Math.hypot(s, s))} * ${scaleVar});`,
26+
)
27+
.join('')}}`;
28+
// TODO font-size line-height... radius
29+
return scaleStyle + spaceStyle;
30+
});
31+
});
32+
33+
export const importCommonStaticTheme = once(createImportStyle('common', commonStyles));
34+
35+
export const importCommonTheme = () => {
36+
importCommonStaticTheme();
37+
importCommonDynamicTheme();
38+
};

packages/theme/src/scss/components/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ import { importMessageBasicTheme } from './message/index.ts';
4141
import { importSpinBasicTheme } from './spin/index.ts';
4242
import { importProgressBasicTheme } from './progress/index.ts';
4343
import { importTextareaBasicTheme, importTextareaSurfaceTheme } from './textarea/index.ts';
44-
import { __internal_defineSubscriber, createImportStyle } from '@lun/components';
45-
import commonStyles from '../common/index.scss?inline';
44+
import { __internal_defineSubscriber } from '@lun/components';
4645
import { once } from '@lun/utils';
4746
import { importMentionsBasicTheme, importMentionsSurfaceTheme } from './mentions/index.ts';
4847
import { importRadioGroupBasicTheme } from './radio-group/index.ts';
4948
import { importThemeProviderBasicTheme } from './theme-provider/index.ts';
5049
import { importIconBasicTheme } from './icon/index.ts';
50+
import { importCommonTheme } from '../common/index.ts';
5151

5252
export * from './button';
5353
export * from './callout';
@@ -134,8 +134,6 @@ export const importGhostTheme = once(() => {
134134
importInputGhostTheme();
135135
});
136136

137-
export const importCommonTheme = once(createImportStyle('common', commonStyles));
138-
139137
export const autoImportTheme = once(() => {
140138
importCommonTheme();
141139
__internal_defineSubscriber.push((comp) => {
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
@use "./colors.scss";
22
@use "./radius.scss";
33
@use "./shadow.scss";
4-
@use "./space.scss";
54
@use "./theme-colors.scss";
65
@use "./typography.scss";

0 commit comments

Comments
 (0)