feat[cssvar]: Divider support cssvar (#8328)

feat-4.3
Shuhari 2025-08-23 09:23:56 +08:00 committed by GitHub
parent c75e80aaee
commit 171dc9e369
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 100 additions and 25 deletions

View File

@ -3,6 +3,7 @@ import { defaultConfig } from '../../theme/internal';
import type { Ref } from 'vue'; import type { Ref } from 'vue';
import { computed } from 'vue'; import { computed } from 'vue';
import devWarning from '../../vc-util/warning'; import devWarning from '../../vc-util/warning';
const themeKey = 'antdvtheme'; const themeKey = 'antdvtheme';
export default function useTheme(theme?: Ref<ThemeConfig>, parentTheme?: Ref<ThemeConfig>) { export default function useTheme(theme?: Ref<ThemeConfig>, parentTheme?: Ref<ThemeConfig>) {
const themeConfig = computed(() => theme?.value || {}); const themeConfig = computed(() => theme?.value || {});

View File

@ -3,6 +3,7 @@ import type { ExtractPropTypes, PropType } from 'vue';
import { computed, defineComponent } from 'vue'; import { computed, defineComponent } from 'vue';
import { withInstall } from '../_util/type'; import { withInstall } from '../_util/type';
import useConfigInject from '../config-provider/hooks/useConfigInject'; import useConfigInject from '../config-provider/hooks/useConfigInject';
import useCSSVarCls from '../config-provider/hooks/useCssVarCls';
import useStyle from './style'; import useStyle from './style';
export const dividerProps = () => ({ export const dividerProps = () => ({
@ -34,7 +35,8 @@ const Divider = defineComponent({
props: dividerProps(), props: dividerProps(),
setup(props, { slots, attrs }) { setup(props, { slots, attrs }) {
const { prefixCls: prefixClsRef, direction } = useConfigInject('divider', props); const { prefixCls: prefixClsRef, direction } = useConfigInject('divider', props);
const [wrapSSR, hashId] = useStyle(prefixClsRef); const rootCls = useCSSVarCls(prefixClsRef);
const [wrapSSR, hashId, cssVarCls] = useStyle(prefixClsRef, rootCls);
const hasCustomMarginLeft = computed( const hasCustomMarginLeft = computed(
() => props.orientation === 'left' && props.orientationMargin != null, () => props.orientation === 'left' && props.orientationMargin != null,
); );
@ -46,6 +48,8 @@ const Divider = defineComponent({
const prefixCls = prefixClsRef.value; const prefixCls = prefixClsRef.value;
return { return {
[prefixCls]: true, [prefixCls]: true,
[cssVarCls.value]: true,
[rootCls.value]: true,
[hashId.value]: !!hashId.value, [hashId.value]: !!hashId.value,
[`${prefixCls}-${type}`]: true, [`${prefixCls}-${type}`]: true,
[`${prefixCls}-dashed`]: !!dashed, [`${prefixCls}-dashed`]: !!dashed,

View File

@ -1,27 +1,68 @@
import type { CSSProperties } from 'vue';
import type { CSSObject } from '../../_util/cssinjs'; import type { CSSObject } from '../../_util/cssinjs';
import type { FullToken, GenerateStyle } from '../../theme/internal'; import { unit } from '../../_util/cssinjs';
import { genComponentStyleHook, mergeToken } from '../../theme/internal';
import { resetComponent } from '../../style'; import { resetComponent } from '../../style';
import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';
import { genStyleHooks, mergeToken } from '../../theme/internal';
/** Component only token. Which will handle additional calculation of alias token */ /** Component only token. Which will handle additional calculation of alias token */
export interface ComponentToken { export interface ComponentToken {
sizePaddingEdgeHorizontal: number; /**
* @desc
* @descEN Horizontal padding of text
*/
textPaddingInline: CSSProperties['paddingInline'];
/**
* @desc 0 1
* @descEN Distance between text and edge, which should be a number between 0 and 1.
*/
orientationMargin: number;
/**
* @desc 线
* @descEN Horizontal margin of vertical Divider
*/
verticalMarginInline: CSSProperties['marginInline'];
} }
/**
* @desc Divider Token
* @descEN Token for Divider component
*/
interface DividerToken extends FullToken<'Divider'> { interface DividerToken extends FullToken<'Divider'> {
dividerVerticalGutterMargin: number; /**
dividerHorizontalWithTextGutterMargin: number; * @desc
dividerHorizontalGutterMargin: number; * @descEN Size padding edge horizontal
*/
sizePaddingEdgeHorizontal: number | string;
/**
* @desc 线
* @descEN Horizontal margin of divider with text
*/
dividerHorizontalWithTextGutterMargin: number | string;
/**
* @desc 线
* @descEN Horizontal margin of divider
*/
dividerHorizontalGutterMargin: number | string;
} }
// ============================== Shared ============================== // ============================== Shared ==============================
const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject => { const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject => {
const { componentCls, sizePaddingEdgeHorizontal, colorSplit, lineWidth } = token; const {
componentCls,
sizePaddingEdgeHorizontal,
colorSplit,
lineWidth,
textPaddingInline,
orientationMargin,
verticalMarginInline,
} = token;
return { return {
[componentCls]: { [componentCls]: {
...resetComponent(token), ...resetComponent(token),
borderBlockStart: `${lineWidth}px solid ${colorSplit}`, borderBlockStart: `${unit(lineWidth)} solid ${colorSplit}`,
// vertical // vertical
'&-vertical': { '&-vertical': {
@ -29,10 +70,11 @@ const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject =>
top: '-0.06em', top: '-0.06em',
display: 'inline-block', display: 'inline-block',
height: '0.9em', height: '0.9em',
margin: `0 ${token.dividerVerticalGutterMargin}px`, marginInline: verticalMarginInline,
marginBlock: 0,
verticalAlign: 'middle', verticalAlign: 'middle',
borderTop: 0, borderTop: 0,
borderInlineStart: `${lineWidth}px solid ${colorSplit}`, borderInlineStart: `${unit(lineWidth)} solid ${colorSplit}`,
}, },
'&-horizontal': { '&-horizontal': {
@ -40,13 +82,13 @@ const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject =>
clear: 'both', clear: 'both',
width: '100%', width: '100%',
minWidth: '100%', // Fix https://github.com/ant-design/ant-design/issues/10914 minWidth: '100%', // Fix https://github.com/ant-design/ant-design/issues/10914
margin: `${token.dividerHorizontalGutterMargin}px 0`, margin: `${unit(token.dividerHorizontalGutterMargin)} 0`,
}, },
[`&-horizontal${componentCls}-with-text`]: { [`&-horizontal${componentCls}-with-text`]: {
display: 'flex', display: 'flex',
alignItems: 'center', alignItems: 'center',
margin: `${token.dividerHorizontalWithTextGutterMargin}px 0`, margin: `${unit(token.dividerHorizontalWithTextGutterMargin)} 0`,
color: token.colorTextHeading, color: token.colorTextHeading,
fontWeight: 500, fontWeight: 500,
fontSize: token.fontSizeLG, fontSize: token.fontSizeLG,
@ -57,7 +99,7 @@ const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject =>
'&::before, &::after': { '&::before, &::after': {
position: 'relative', position: 'relative',
width: '50%', width: '50%',
borderBlockStart: `${lineWidth}px solid transparent`, borderBlockStart: `${unit(lineWidth)} solid transparent`,
// Chrome not accept `inherit` in `border-top` // Chrome not accept `inherit` in `border-top`
borderBlockStartColor: 'inherit', borderBlockStartColor: 'inherit',
borderBlockEnd: 0, borderBlockEnd: 0,
@ -68,34 +110,33 @@ const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject =>
[`&-horizontal${componentCls}-with-text-left`]: { [`&-horizontal${componentCls}-with-text-left`]: {
'&::before': { '&::before': {
width: '5%', width: `calc(${orientationMargin} * 100%)`,
}, },
'&::after': { '&::after': {
width: '95%', width: `calc(100% - ${orientationMargin} * 100%)`,
}, },
}, },
[`&-horizontal${componentCls}-with-text-right`]: { [`&-horizontal${componentCls}-with-text-right`]: {
'&::before': { '&::before': {
width: '95%', width: `calc(100% - ${orientationMargin} * 100%)`,
}, },
'&::after': { '&::after': {
width: '5%', width: `calc(${orientationMargin} * 100%)`,
}, },
}, },
[`${componentCls}-inner-text`]: { [`${componentCls}-inner-text`]: {
display: 'inline-block', display: 'inline-block',
padding: '0 1em', paddingBlock: 0,
paddingInline: textPaddingInline,
}, },
'&-dashed': { '&-dashed': {
background: 'none', background: 'none',
borderColor: colorSplit, borderColor: colorSplit,
borderStyle: 'dashed', borderStyle: 'dashed',
borderWidth: `${lineWidth}px 0 0`, borderWidth: `${unit(lineWidth)} 0 0`,
}, },
[`&-horizontal${componentCls}-with-text${componentCls}-dashed`]: { [`&-horizontal${componentCls}-with-text${componentCls}-dashed`]: {
@ -111,6 +152,26 @@ const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject =>
borderBlockEnd: 0, borderBlockEnd: 0,
}, },
'&-dotted': {
background: 'none',
borderColor: colorSplit,
borderStyle: 'dotted',
borderWidth: `${unit(lineWidth)} 0 0`,
},
[`&-horizontal${componentCls}-with-text${componentCls}-dotted`]: {
'&::before, &::after': {
borderStyle: 'dotted none none',
},
},
[`&-vertical${componentCls}-dotted`]: {
borderInlineStartWidth: lineWidth,
borderInlineEnd: 0,
borderBlockStart: 0,
borderBlockEnd: 0,
},
[`&-plain${componentCls}-with-text`]: { [`&-plain${componentCls}-with-text`]: {
color: token.colorText, color: token.colorText,
fontWeight: 'normal', fontWeight: 'normal',
@ -150,18 +211,27 @@ const genSharedDividerStyle: GenerateStyle<DividerToken> = (token): CSSObject =>
}; };
}; };
export const prepareComponentToken: GetDefaultToken<'Divider'> = token => ({
textPaddingInline: '1em',
orientationMargin: 0.05,
verticalMarginInline: token.marginXS,
});
// ============================== Export ============================== // ============================== Export ==============================
export default genComponentStyleHook( export default genStyleHooks(
'Divider', 'Divider',
token => { token => {
const dividerToken = mergeToken<DividerToken>(token, { const dividerToken = mergeToken<DividerToken>(token, {
dividerVerticalGutterMargin: token.marginXS,
dividerHorizontalWithTextGutterMargin: token.margin, dividerHorizontalWithTextGutterMargin: token.margin,
dividerHorizontalGutterMargin: token.marginLG, dividerHorizontalGutterMargin: token.marginLG,
sizePaddingEdgeHorizontal: 0,
}); });
return [genSharedDividerStyle(dividerToken)]; return [genSharedDividerStyle(dividerToken)];
}, },
prepareComponentToken,
{ {
sizePaddingEdgeHorizontal: 0, unitless: {
orientationMargin: true,
},
}, },
); );