| import type { CSSProperties } from 'react'; |
| import type { CSSObject } from '@ant-design/cssinjs'; |
| import { unit } from '@ant-design/cssinjs'; |
|
|
| import { AggregationColor } from '../../color-picker/color'; |
| import { isBright } from '../../color-picker/components/ColorPresets'; |
| import type { FullToken, GenStyleFn, GetDefaultToken, PresetColorKey } from '../../theme/internal'; |
| import { getLineHeight, mergeToken } from '../../theme/internal'; |
| import { PresetColors } from '../../theme/interface'; |
| import getAlphaColor from '../../theme/util/getAlphaColor'; |
|
|
| |
| export interface ComponentToken { |
| |
| |
| |
| |
| fontWeight: CSSProperties['fontWeight']; |
| |
| |
| |
| |
| defaultShadow: string; |
| |
| |
| |
| |
| primaryShadow: string; |
| |
| |
| |
| |
| dangerShadow: string; |
| |
| |
| |
| |
| primaryColor: string; |
| |
| |
| |
| |
| defaultColor: string; |
| |
| |
| |
| |
| defaultBg: string; |
| |
| |
| |
| |
| defaultBorderColor: string; |
| |
| |
| |
| |
| dangerColor: string; |
| |
| |
| |
| |
| defaultHoverBg: string; |
| |
| |
| |
| |
| defaultHoverColor: string; |
| |
| |
| |
| |
| defaultHoverBorderColor: string; |
| |
| |
| |
| |
| defaultActiveBg: string; |
| |
| |
| |
| |
| defaultActiveColor: string; |
| |
| |
| |
| |
| defaultActiveBorderColor: string; |
| |
| |
| |
| |
| borderColorDisabled: string; |
| |
| |
| |
| |
| defaultGhostColor: string; |
| |
| |
| |
| |
| ghostBg: string; |
| |
| |
| |
| |
| defaultGhostBorderColor: string; |
| |
| |
| |
| |
| |
| |
| |
| |
| solidTextColor: string; |
| |
| |
| |
| |
| textTextColor: string; |
| |
| |
| |
| |
| textTextHoverColor: string; |
| |
| |
| |
| |
| textTextActiveColor: string; |
| |
| |
| |
| |
| paddingInline: CSSProperties['paddingInline']; |
| |
| |
| |
| |
| paddingInlineLG: CSSProperties['paddingInline']; |
| |
| |
| |
| |
| paddingInlineSM: CSSProperties['paddingInline']; |
| |
| |
| |
| |
| paddingBlock: CSSProperties['paddingBlock']; |
| |
| |
| |
| |
| paddingBlockLG: CSSProperties['paddingBlock']; |
| |
| |
| |
| |
| paddingBlockSM: CSSProperties['paddingBlock']; |
| |
| |
| |
| |
| onlyIconSize: number | string; |
| |
| |
| |
| |
| onlyIconSizeLG: number | string; |
| |
| |
| |
| |
| onlyIconSizeSM: number | string; |
| |
| |
| |
| |
| groupBorderColor: string; |
| |
| |
| |
| |
| linkHoverBg: string; |
| |
| |
| |
| |
| textHoverBg: string; |
| |
| |
| |
| |
| contentFontSize: number; |
| |
| |
| |
| |
| contentFontSizeLG: number; |
| |
| |
| |
| |
| contentFontSizeSM: number; |
| |
| |
| |
| |
| contentLineHeight: number; |
| |
| |
| |
| |
| contentLineHeightLG: number; |
| |
| |
| |
| |
| contentLineHeightSM: number; |
| } |
|
|
| type ShadowColorMap = { |
| |
| |
| |
| |
| [Key in `${PresetColorKey}ShadowColor`]: string; |
| }; |
|
|
| export interface ButtonToken extends FullToken<'Button'>, ShadowColorMap { |
| |
| |
| |
| |
| buttonPaddingHorizontal: CSSProperties['paddingInline']; |
| |
| |
| |
| |
| buttonPaddingVertical: CSSProperties['paddingBlock']; |
| |
| |
| |
| |
| buttonIconOnlyFontSize: number | string; |
| } |
|
|
| export const prepareToken: (token: Parameters<GenStyleFn<'Button'>>[0]) => ButtonToken = ( |
| token, |
| ) => { |
| const { paddingInline, onlyIconSize } = token; |
|
|
| const buttonToken = mergeToken<ButtonToken>(token, { |
| buttonPaddingHorizontal: paddingInline, |
| buttonPaddingVertical: 0, |
| buttonIconOnlyFontSize: onlyIconSize, |
| }); |
|
|
| return buttonToken; |
| }; |
|
|
| export const prepareComponentToken: GetDefaultToken<'Button'> = (token) => { |
| const contentFontSize = token.contentFontSize ?? token.fontSize; |
| const contentFontSizeSM = token.contentFontSizeSM ?? token.fontSize; |
| const contentFontSizeLG = token.contentFontSizeLG ?? token.fontSizeLG; |
| const contentLineHeight = token.contentLineHeight ?? getLineHeight(contentFontSize); |
| const contentLineHeightSM = token.contentLineHeightSM ?? getLineHeight(contentFontSizeSM); |
| const contentLineHeightLG = token.contentLineHeightLG ?? getLineHeight(contentFontSizeLG); |
| const solidTextColor = isBright(new AggregationColor(token.colorBgSolid), '#fff') |
| ? '#000' |
| : '#fff'; |
|
|
| const shadowColorTokens = PresetColors.reduce<CSSObject>( |
| (prev: CSSObject, colorKey: PresetColorKey) => ({ |
| ...prev, |
| [`${colorKey}ShadowColor`]: `0 ${unit(token.controlOutlineWidth)} 0 ${getAlphaColor(token[`${colorKey}1`], token.colorBgContainer)}`, |
| }), |
| {}, |
| ); |
|
|
| return { |
| ...shadowColorTokens, |
| fontWeight: 400, |
| defaultShadow: `0 ${token.controlOutlineWidth}px 0 ${token.controlTmpOutline}`, |
| primaryShadow: `0 ${token.controlOutlineWidth}px 0 ${token.controlOutline}`, |
| dangerShadow: `0 ${token.controlOutlineWidth}px 0 ${token.colorErrorOutline}`, |
| primaryColor: token.colorTextLightSolid, |
| dangerColor: token.colorTextLightSolid, |
| borderColorDisabled: token.colorBorder, |
| defaultGhostColor: token.colorBgContainer, |
| ghostBg: 'transparent', |
| defaultGhostBorderColor: token.colorBgContainer, |
| paddingInline: token.paddingContentHorizontal - token.lineWidth, |
| paddingInlineLG: token.paddingContentHorizontal - token.lineWidth, |
| paddingInlineSM: 8 - token.lineWidth, |
| onlyIconSize: 'inherit', |
| onlyIconSizeSM: 'inherit', |
| onlyIconSizeLG: 'inherit', |
| groupBorderColor: token.colorPrimaryHover, |
| linkHoverBg: 'transparent', |
| textTextColor: token.colorText, |
| textTextHoverColor: token.colorText, |
| textTextActiveColor: token.colorText, |
| textHoverBg: token.colorFillTertiary, |
| defaultColor: token.colorText, |
| defaultBg: token.colorBgContainer, |
| defaultBorderColor: token.colorBorder, |
| defaultBorderColorDisabled: token.colorBorder, |
| defaultHoverBg: token.colorBgContainer, |
| defaultHoverColor: token.colorPrimaryHover, |
| defaultHoverBorderColor: token.colorPrimaryHover, |
| defaultActiveBg: token.colorBgContainer, |
| defaultActiveColor: token.colorPrimaryActive, |
| defaultActiveBorderColor: token.colorPrimaryActive, |
| solidTextColor, |
| contentFontSize, |
| contentFontSizeSM, |
| contentFontSizeLG, |
| contentLineHeight, |
| contentLineHeightSM, |
| contentLineHeightLG, |
| paddingBlock: Math.max( |
| (token.controlHeight - contentFontSize * contentLineHeight) / 2 - token.lineWidth, |
| 0, |
| ), |
| paddingBlockSM: Math.max( |
| (token.controlHeightSM - contentFontSizeSM * contentLineHeightSM) / 2 - token.lineWidth, |
| 0, |
| ), |
| paddingBlockLG: Math.max( |
| (token.controlHeightLG - contentFontSizeLG * contentLineHeightLG) / 2 - token.lineWidth, |
| 0, |
| ), |
| }; |
| }; |
|
|