Skip to content

Commit 171dc9e

Browse files
authored
feat[cssvar]: Divider support cssvar (#8328)
1 parent c75e80a commit 171dc9e

File tree

3 files changed

+100
-25
lines changed

3 files changed

+100
-25
lines changed

components/config-provider/hooks/useTheme.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defaultConfig } from '../../theme/internal';
33
import type { Ref } from 'vue';
44
import { computed } from 'vue';
55
import devWarning from '../../vc-util/warning';
6+
67
const themeKey = 'antdvtheme';
78
export default function useTheme(theme?: Ref<ThemeConfig>, parentTheme?: Ref<ThemeConfig>) {
89
const themeConfig = computed(() => theme?.value || {});

components/divider/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ExtractPropTypes, PropType } from 'vue';
33
import { computed, defineComponent } from 'vue';
44
import { withInstall } from '../_util/type';
55
import useConfigInject from '../config-provider/hooks/useConfigInject';
6+
import useCSSVarCls from '../config-provider/hooks/useCssVarCls';
67
import useStyle from './style';
78

89
export const dividerProps = () => ({
@@ -34,7 +35,8 @@ const Divider = defineComponent({
3435
props: dividerProps(),
3536
setup(props, { slots, attrs }) {
3637
const { prefixCls: prefixClsRef, direction } = useConfigInject('divider', props);
37-
const [wrapSSR, hashId] = useStyle(prefixClsRef);
38+
const rootCls = useCSSVarCls(prefixClsRef);
39+
const [wrapSSR, hashId, cssVarCls] = useStyle(prefixClsRef, rootCls);
3840
const hasCustomMarginLeft = computed(
3941
() => props.orientation === 'left' && props.orientationMargin != null,
4042
);
@@ -46,6 +48,8 @@ const Divider = defineComponent({
4648
const prefixCls = prefixClsRef.value;
4749
return {
4850
[prefixCls]: true,
51+
[cssVarCls.value]: true,
52+
[rootCls.value]: true,
4953
[hashId.value]: !!hashId.value,
5054
[`${prefixCls}-${type}`]: true,
5155
[`${prefixCls}-dashed`]: !!dashed,

components/divider/style/index.ts

Lines changed: 94 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,94 @@
1+
import type { CSSProperties } from 'vue';
12
import type { CSSObject } from '../../_util/cssinjs';
2-
import type { FullToken, GenerateStyle } from '../../theme/internal';
3-
import { genComponentStyleHook, mergeToken } from '../../theme/internal';
3+
import { unit } from '../../_util/cssinjs';
4+
45
import { resetComponent } from '../../style';
6+
import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';
7+
import { genStyleHooks, mergeToken } from '../../theme/internal';
58

69
/** Component only token. Which will handle additional calculation of alias token */
710
export interface ComponentToken {
8-
sizePaddingEdgeHorizontal: number;
11+
/**
12+
* @desc 文本横向内间距
13+
* @descEN Horizontal padding of text
14+
*/
15+
textPaddingInline: CSSProperties['paddingInline'];
16+
/**
17+
* @desc 文本与边缘距离,取值 0 ~ 1
18+
* @descEN Distance between text and edge, which should be a number between 0 and 1.
19+
*/
20+
orientationMargin: number;
21+
/**
22+
* @desc 纵向分割线的横向外间距
23+
* @descEN Horizontal margin of vertical Divider
24+
*/
25+
verticalMarginInline: CSSProperties['marginInline'];
926
}
1027

28+
/**
29+
* @desc Divider 组件的 Token
30+
* @descEN Token for Divider component
31+
*/
1132
interface DividerToken extends FullToken<'Divider'> {
12-
dividerVerticalGutterMargin: number;
13-
dividerHorizontalWithTextGutterMargin: number;
14-
dividerHorizontalGutterMargin: number;
33+
/**
34+
* @desc 尺寸边距
35+
* @descEN Size padding edge horizontal
36+
*/
37+
sizePaddingEdgeHorizontal: number | string;
38+
/**
39+
* @desc 带文本的水平分割线的外边距
40+
* @descEN Horizontal margin of divider with text
41+
*/
42+
dividerHorizontalWithTextGutterMargin: number | string;
43+
/**
44+
* @desc 水平分割线的外边距
45+
* @descEN Horizontal margin of divider
46+
*/
47+
dividerHorizontalGutterMargin: number | string;
1548
}
1649

1750
// ============================== Shared ==============================
1851
const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject => {
19-
const { componentCls, sizePaddingEdgeHorizontal, colorSplit, lineWidth } = token;
52+
const {
53+
componentCls,
54+
sizePaddingEdgeHorizontal,
55+
colorSplit,
56+
lineWidth,
57+
textPaddingInline,
58+
orientationMargin,
59+
verticalMarginInline,
60+
} = token;
2061

2162
return {
2263
[componentCls]: {
2364
...resetComponent(token),
24-
borderBlockStart: `${lineWidth}px solid ${colorSplit}`,
65+
borderBlockStart: `${unit(lineWidth)} solid ${colorSplit}`,
2566

2667
// vertical
2768
'&-vertical': {
2869
position: 'relative',
2970
top: '-0.06em',
3071
display: 'inline-block',
3172
height: '0.9em',
32-
margin: `0 ${token.dividerVerticalGutterMargin}px`,
73+
marginInline: verticalMarginInline,
74+
marginBlock: 0,
3375
verticalAlign: 'middle',
3476
borderTop: 0,
35-
borderInlineStart: `${lineWidth}px solid ${colorSplit}`,
77+
borderInlineStart: `${unit(lineWidth)} solid ${colorSplit}`,
3678
},
3779

3880
'&-horizontal': {
3981
display: 'flex',
4082
clear: 'both',
4183
width: '100%',
4284
minWidth: '100%', // Fix https://github.com/ant-design/ant-design/issues/10914
43-
margin: `${token.dividerHorizontalGutterMargin}px 0`,
85+
margin: `${unit(token.dividerHorizontalGutterMargin)} 0`,
4486
},
4587

4688
[`&-horizontal${componentCls}-with-text`]: {
4789
display: 'flex',
4890
alignItems: 'center',
49-
margin: `${token.dividerHorizontalWithTextGutterMargin}px 0`,
91+
margin: `${unit(token.dividerHorizontalWithTextGutterMargin)} 0`,
5092
color: token.colorTextHeading,
5193
fontWeight: 500,
5294
fontSize: token.fontSizeLG,
@@ -57,7 +99,7 @@ const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject =>
5799
'&::before, &::after': {
58100
position: 'relative',
59101
width: '50%',
60-
borderBlockStart: `${lineWidth}px solid transparent`,
102+
borderBlockStart: `${unit(lineWidth)} solid transparent`,
61103
// Chrome not accept `inherit` in `border-top`
62104
borderBlockStartColor: 'inherit',
63105
borderBlockEnd: 0,
@@ -68,34 +110,33 @@ const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject =>
68110

69111
[`&-horizontal${componentCls}-with-text-left`]: {
70112
'&::before': {
71-
width: '5%',
113+
width: `calc(${orientationMargin} * 100%)`,
72114
},
73-
74115
'&::after': {
75-
width: '95%',
116+
width: `calc(100% - ${orientationMargin} * 100%)`,
76117
},
77118
},
78119

79120
[`&-horizontal${componentCls}-with-text-right`]: {
80121
'&::before': {
81-
width: '95%',
122+
width: `calc(100% - ${orientationMargin} * 100%)`,
82123
},
83-
84124
'&::after': {
85-
width: '5%',
125+
width: `calc(${orientationMargin} * 100%)`,
86126
},
87127
},
88128

89129
[`${componentCls}-inner-text`]: {
90130
display: 'inline-block',
91-
padding: '0 1em',
131+
paddingBlock: 0,
132+
paddingInline: textPaddingInline,
92133
},
93134

94135
'&-dashed': {
95136
background: 'none',
96137
borderColor: colorSplit,
97138
borderStyle: 'dashed',
98-
borderWidth: `${lineWidth}px 0 0`,
139+
borderWidth: `${unit(lineWidth)} 0 0`,
99140
},
100141

101142
[`&-horizontal${componentCls}-with-text${componentCls}-dashed`]: {
@@ -111,6 +152,26 @@ const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject =>
111152
borderBlockEnd: 0,
112153
},
113154

155+
'&-dotted': {
156+
background: 'none',
157+
borderColor: colorSplit,
158+
borderStyle: 'dotted',
159+
borderWidth: `${unit(lineWidth)} 0 0`,
160+
},
161+
162+
[`&-horizontal${componentCls}-with-text${componentCls}-dotted`]: {
163+
'&::before, &::after': {
164+
borderStyle: 'dotted none none',
165+
},
166+
},
167+
168+
[`&-vertical${componentCls}-dotted`]: {
169+
borderInlineStartWidth: lineWidth,
170+
borderInlineEnd: 0,
171+
borderBlockStart: 0,
172+
borderBlockEnd: 0,
173+
},
174+
114175
[`&-plain${componentCls}-with-text`]: {
115176
color: token.colorText,
116177
fontWeight: 'normal',
@@ -150,18 +211,27 @@ const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject =>
150211
};
151212
};
152213

214+
export const prepareComponentToken: GetDefaultToken<'Divider'> = token => ({
215+
textPaddingInline: '1em',
216+
orientationMargin: 0.05,
217+
verticalMarginInline: token.marginXS,
218+
});
219+
153220
// ============================== Export ==============================
154-
export default genComponentStyleHook(
221+
export default genStyleHooks(
155222
'Divider',
156223
token => {
157224
const dividerToken = mergeToken<DividerToken>(token, {
158-
dividerVerticalGutterMargin: token.marginXS,
159225
dividerHorizontalWithTextGutterMargin: token.margin,
160226
dividerHorizontalGutterMargin: token.marginLG,
227+
sizePaddingEdgeHorizontal: 0,
161228
});
162229
return [genSharedDividerStyle(dividerToken)];
163230
},
231+
prepareComponentToken,
164232
{
165-
sizePaddingEdgeHorizontal: 0,
233+
unitless: {
234+
orientationMargin: true,
235+
},
166236
},
167237
);

0 commit comments

Comments
 (0)