1- import type { CSSInterpolation , CSSObject } from '@ant-design/cssinjs' ;
2- import { unit , useStyleRegister } from '@ant-design/cssinjs' ;
1+ import {
2+ CSSInterpolation ,
3+ CSSObject ,
4+ unit ,
5+ useCSSVarRegister ,
6+ useStyleRegister ,
7+ } from '@ant-design/cssinjs' ;
38import classNames from 'classnames' ;
49import React from 'react' ;
510import type { DerivativeToken } from './theme' ;
611import { useToken } from './theme' ;
712
13+ interface ButtonToken extends DerivativeToken {
14+ buttonPadding : string ;
15+ }
16+
817// 通用框架
918const genSharedButtonStyle = (
1019 prefixCls : string ,
11- token : DerivativeToken ,
20+ token : ButtonToken ,
1221) : CSSInterpolation => ( {
1322 [ `.${ prefixCls } ` ] : {
1423 borderColor : token . borderColor ,
1524 borderWidth : token . borderWidth ,
1625 borderRadius : token . borderRadius ,
1726 lineHeight : token . lineHeight ,
27+ padding : token . buttonPadding ,
1828
1929 cursor : 'pointer' ,
2030
@@ -33,7 +43,7 @@ const genSharedButtonStyle = (
3343// 实心底色样式
3444const genSolidButtonStyle = (
3545 prefixCls : string ,
36- token : DerivativeToken ,
46+ token : ButtonToken ,
3747 postFn : ( ) => CSSObject ,
3848) : CSSInterpolation => [
3949 genSharedButtonStyle ( prefixCls , token ) ,
@@ -47,7 +57,7 @@ const genSolidButtonStyle = (
4757// 默认样式
4858const genDefaultButtonStyle = (
4959 prefixCls : string ,
50- token : DerivativeToken ,
60+ token : ButtonToken ,
5161) : CSSInterpolation =>
5262 genSolidButtonStyle ( prefixCls , token , ( ) => ( {
5363 backgroundColor : token . componentBackgroundColor ,
@@ -62,7 +72,7 @@ const genDefaultButtonStyle = (
6272// 主色样式
6373const genPrimaryButtonStyle = (
6474 prefixCls : string ,
65- token : DerivativeToken ,
75+ token : ButtonToken ,
6676) : CSSInterpolation =>
6777 genSolidButtonStyle ( prefixCls , token , ( ) => ( {
6878 backgroundColor : token . primaryColor ,
@@ -77,7 +87,7 @@ const genPrimaryButtonStyle = (
7787// 透明按钮
7888const genGhostButtonStyle = (
7989 prefixCls : string ,
80- token : DerivativeToken ,
90+ token : ButtonToken ,
8191) : CSSInterpolation => [
8292 genSharedButtonStyle ( prefixCls , token ) ,
8393 {
@@ -103,23 +113,45 @@ const Button = ({ className, type, ...restProps }: ButtonProps) => {
103113 const prefixCls = 'ant-btn' ;
104114
105115 // 【自定义】制造样式
106- const [ theme , token , hashId , cssVarKey ] = useToken ( ) ;
116+ const [ theme , token , hashId , cssVarKey , realToken ] = useToken ( ) ;
107117
108118 // default 添加默认样式选择器后可以省很多冲突解决问题
109119 const defaultCls = `${ prefixCls } -default` ;
110120 const primaryCls = `${ prefixCls } -primary` ;
111121 const ghostCls = `${ prefixCls } -ghost` ;
112122
113- // 全局注册,内部会做缓存优化
114- useStyleRegister (
115- { theme, token, hashId, path : [ prefixCls ] } ,
116- ( ) => [
117- genDefaultButtonStyle ( defaultCls , token ) ,
118- genPrimaryButtonStyle ( primaryCls , token ) ,
119- genGhostButtonStyle ( ghostCls , token ) ,
120- ] ,
123+ const [ cssVarToken ] = useCSSVarRegister (
124+ {
125+ path : [ prefixCls ] ,
126+ key : cssVarKey ,
127+ token : realToken ,
128+ prefix : prefixCls ,
129+ unitless : {
130+ lineHeight : true ,
131+ } ,
132+ ignore : {
133+ lineHeightBase : true ,
134+ } ,
135+ scope : prefixCls ,
136+ hashId,
137+ } ,
138+ ( ) => ( {
139+ buttonPadding : '4px 8px' ,
140+ } ) ,
121141 ) ;
122142
143+ const mergedToken : any = {
144+ ...token ,
145+ ...cssVarToken ,
146+ } ;
147+
148+ // 全局注册,内部会做缓存优化
149+ useStyleRegister ( { theme, token, hashId, path : [ prefixCls ] } , ( ) => [
150+ genDefaultButtonStyle ( defaultCls , mergedToken ) ,
151+ genPrimaryButtonStyle ( primaryCls , mergedToken ) ,
152+ genGhostButtonStyle ( ghostCls , mergedToken ) ,
153+ ] ) ;
154+
123155 const typeCls =
124156 (
125157 {
0 commit comments