index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _cssinjs = require("@ant-design/cssinjs");
  8. var _style = require("../../style");
  9. var _interface = require("../../theme/interface");
  10. var _internal = require("../../theme/internal");
  11. var _group = _interopRequireDefault(require("./group"));
  12. var _token = require("./token");
  13. // ============================== Shared ==============================
  14. const genSharedButtonStyle = token => {
  15. const {
  16. componentCls,
  17. iconCls,
  18. fontWeight,
  19. opacityLoading,
  20. motionDurationSlow,
  21. motionEaseInOut,
  22. iconGap,
  23. calc
  24. } = token;
  25. return {
  26. [componentCls]: {
  27. outline: 'none',
  28. position: 'relative',
  29. display: 'inline-flex',
  30. gap: iconGap,
  31. alignItems: 'center',
  32. justifyContent: 'center',
  33. fontWeight,
  34. whiteSpace: 'nowrap',
  35. textAlign: 'center',
  36. backgroundImage: 'none',
  37. background: 'transparent',
  38. border: `${(0, _cssinjs.unit)(token.lineWidth)} ${token.lineType} transparent`,
  39. cursor: 'pointer',
  40. transition: `all ${token.motionDurationMid} ${token.motionEaseInOut}`,
  41. userSelect: 'none',
  42. touchAction: 'manipulation',
  43. color: token.colorText,
  44. '&:disabled > *': {
  45. pointerEvents: 'none'
  46. },
  47. // https://github.com/ant-design/ant-design/issues/51380
  48. [`${componentCls}-icon > svg`]: (0, _style.resetIcon)(),
  49. '> a': {
  50. color: 'currentColor'
  51. },
  52. '&:not(:disabled)': (0, _style.genFocusStyle)(token),
  53. [`&${componentCls}-two-chinese-chars::first-letter`]: {
  54. letterSpacing: '0.34em'
  55. },
  56. [`&${componentCls}-two-chinese-chars > *:not(${iconCls})`]: {
  57. marginInlineEnd: '-0.34em',
  58. letterSpacing: '0.34em'
  59. },
  60. [`&${componentCls}-icon-only`]: {
  61. paddingInline: 0,
  62. // make `btn-icon-only` not too narrow
  63. [`&${componentCls}-compact-item`]: {
  64. flex: 'none'
  65. }
  66. },
  67. // Loading
  68. [`&${componentCls}-loading`]: {
  69. opacity: opacityLoading,
  70. cursor: 'default'
  71. },
  72. [`${componentCls}-loading-icon`]: {
  73. transition: ['width', 'opacity', 'margin'].map(transition => `${transition} ${motionDurationSlow} ${motionEaseInOut}`).join(',')
  74. },
  75. // iconPosition
  76. [`&:not(${componentCls}-icon-end)`]: {
  77. [`${componentCls}-loading-icon-motion`]: {
  78. '&-appear-start, &-enter-start': {
  79. marginInlineEnd: calc(iconGap).mul(-1).equal()
  80. },
  81. '&-appear-active, &-enter-active': {
  82. marginInlineEnd: 0
  83. },
  84. '&-leave-start': {
  85. marginInlineEnd: 0
  86. },
  87. '&-leave-active': {
  88. marginInlineEnd: calc(iconGap).mul(-1).equal()
  89. }
  90. }
  91. },
  92. '&-icon-end': {
  93. flexDirection: 'row-reverse',
  94. [`${componentCls}-loading-icon-motion`]: {
  95. '&-appear-start, &-enter-start': {
  96. marginInlineStart: calc(iconGap).mul(-1).equal()
  97. },
  98. '&-appear-active, &-enter-active': {
  99. marginInlineStart: 0
  100. },
  101. '&-leave-start': {
  102. marginInlineStart: 0
  103. },
  104. '&-leave-active': {
  105. marginInlineStart: calc(iconGap).mul(-1).equal()
  106. }
  107. }
  108. }
  109. }
  110. };
  111. };
  112. const genHoverActiveButtonStyle = (btnCls, hoverStyle, activeStyle) => ({
  113. [`&:not(:disabled):not(${btnCls}-disabled)`]: {
  114. '&:hover': hoverStyle,
  115. '&:active': activeStyle
  116. }
  117. });
  118. // ============================== Shape ===============================
  119. const genCircleButtonStyle = token => ({
  120. minWidth: token.controlHeight,
  121. paddingInline: 0,
  122. borderRadius: '50%'
  123. });
  124. const genDisabledStyle = token => ({
  125. cursor: 'not-allowed',
  126. borderColor: token.borderColorDisabled,
  127. color: token.colorTextDisabled,
  128. background: token.colorBgContainerDisabled,
  129. boxShadow: 'none'
  130. });
  131. const genGhostButtonStyle = (btnCls, background, textColor, borderColor, textColorDisabled, borderColorDisabled, hoverStyle, activeStyle) => ({
  132. [`&${btnCls}-background-ghost`]: Object.assign(Object.assign({
  133. color: textColor || undefined,
  134. background,
  135. borderColor: borderColor || undefined,
  136. boxShadow: 'none'
  137. }, genHoverActiveButtonStyle(btnCls, Object.assign({
  138. background
  139. }, hoverStyle), Object.assign({
  140. background
  141. }, activeStyle))), {
  142. '&:disabled': {
  143. cursor: 'not-allowed',
  144. color: textColorDisabled || undefined,
  145. borderColor: borderColorDisabled || undefined
  146. }
  147. })
  148. });
  149. const genSolidDisabledButtonStyle = token => ({
  150. [`&:disabled, &${token.componentCls}-disabled`]: Object.assign({}, genDisabledStyle(token))
  151. });
  152. const genPureDisabledButtonStyle = token => ({
  153. [`&:disabled, &${token.componentCls}-disabled`]: {
  154. cursor: 'not-allowed',
  155. color: token.colorTextDisabled
  156. }
  157. });
  158. // ============================== Variant =============================
  159. const genVariantButtonStyle = (token, hoverStyle, activeStyle, variant) => {
  160. const isPureDisabled = variant && ['link', 'text'].includes(variant);
  161. const genDisabledButtonStyle = isPureDisabled ? genPureDisabledButtonStyle : genSolidDisabledButtonStyle;
  162. return Object.assign(Object.assign({}, genDisabledButtonStyle(token)), genHoverActiveButtonStyle(token.componentCls, hoverStyle, activeStyle));
  163. };
  164. const genSolidButtonStyle = (token, textColor, background, hoverStyle, activeStyle) => ({
  165. [`&${token.componentCls}-variant-solid`]: Object.assign({
  166. color: textColor,
  167. background
  168. }, genVariantButtonStyle(token, hoverStyle, activeStyle))
  169. });
  170. const genOutlinedDashedButtonStyle = (token, borderColor, background, hoverStyle, activeStyle) => ({
  171. [`&${token.componentCls}-variant-outlined, &${token.componentCls}-variant-dashed`]: Object.assign({
  172. borderColor,
  173. background
  174. }, genVariantButtonStyle(token, hoverStyle, activeStyle))
  175. });
  176. const genDashedButtonStyle = token => ({
  177. [`&${token.componentCls}-variant-dashed`]: {
  178. borderStyle: 'dashed'
  179. }
  180. });
  181. const genFilledButtonStyle = (token, background, hoverStyle, activeStyle) => ({
  182. [`&${token.componentCls}-variant-filled`]: Object.assign({
  183. boxShadow: 'none',
  184. background
  185. }, genVariantButtonStyle(token, hoverStyle, activeStyle))
  186. });
  187. const genTextLinkButtonStyle = (token, textColor, variant, hoverStyle, activeStyle) => ({
  188. [`&${token.componentCls}-variant-${variant}`]: Object.assign({
  189. color: textColor,
  190. boxShadow: 'none'
  191. }, genVariantButtonStyle(token, hoverStyle, activeStyle, variant))
  192. });
  193. // =============================== Color ==============================
  194. const genPresetColorStyle = token => {
  195. const {
  196. componentCls
  197. } = token;
  198. return _interface.PresetColors.reduce((prev, colorKey) => {
  199. const darkColor = token[`${colorKey}6`];
  200. const lightColor = token[`${colorKey}1`];
  201. const hoverColor = token[`${colorKey}5`];
  202. const lightHoverColor = token[`${colorKey}2`];
  203. const lightBorderColor = token[`${colorKey}3`];
  204. const activeColor = token[`${colorKey}7`];
  205. return Object.assign(Object.assign({}, prev), {
  206. [`&${componentCls}-color-${colorKey}`]: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({
  207. color: darkColor,
  208. boxShadow: token[`${colorKey}ShadowColor`]
  209. }, genSolidButtonStyle(token, token.colorTextLightSolid, darkColor, {
  210. background: hoverColor
  211. }, {
  212. background: activeColor
  213. })), genOutlinedDashedButtonStyle(token, darkColor, token.colorBgContainer, {
  214. color: hoverColor,
  215. borderColor: hoverColor,
  216. background: token.colorBgContainer
  217. }, {
  218. color: activeColor,
  219. borderColor: activeColor,
  220. background: token.colorBgContainer
  221. })), genDashedButtonStyle(token)), genFilledButtonStyle(token, lightColor, {
  222. color: darkColor,
  223. background: lightHoverColor
  224. }, {
  225. color: darkColor,
  226. background: lightBorderColor
  227. })), genTextLinkButtonStyle(token, darkColor, 'link', {
  228. color: hoverColor
  229. }, {
  230. color: activeColor
  231. })), genTextLinkButtonStyle(token, darkColor, 'text', {
  232. color: hoverColor,
  233. background: lightColor
  234. }, {
  235. color: activeColor,
  236. background: lightBorderColor
  237. }))
  238. });
  239. }, {});
  240. };
  241. const genDefaultButtonStyle = token => Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({
  242. color: token.defaultColor,
  243. boxShadow: token.defaultShadow
  244. }, genSolidButtonStyle(token, token.solidTextColor, token.colorBgSolid, {
  245. color: token.solidTextColor,
  246. background: token.colorBgSolidHover
  247. }, {
  248. color: token.solidTextColor,
  249. background: token.colorBgSolidActive
  250. })), genDashedButtonStyle(token)), genFilledButtonStyle(token, token.colorFillTertiary, {
  251. color: token.defaultColor,
  252. background: token.colorFillSecondary
  253. }, {
  254. color: token.defaultColor,
  255. background: token.colorFill
  256. })), genGhostButtonStyle(token.componentCls, token.ghostBg, token.defaultGhostColor, token.defaultGhostBorderColor, token.colorTextDisabled, token.colorBorder)), genTextLinkButtonStyle(token, token.textTextColor, 'link', {
  257. color: token.colorLinkHover,
  258. background: token.linkHoverBg
  259. }, {
  260. color: token.colorLinkActive
  261. }));
  262. const genPrimaryButtonStyle = token => Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({
  263. color: token.colorPrimary,
  264. boxShadow: token.primaryShadow
  265. }, genOutlinedDashedButtonStyle(token, token.colorPrimary, token.colorBgContainer, {
  266. color: token.colorPrimaryTextHover,
  267. borderColor: token.colorPrimaryHover,
  268. background: token.colorBgContainer
  269. }, {
  270. color: token.colorPrimaryTextActive,
  271. borderColor: token.colorPrimaryActive,
  272. background: token.colorBgContainer
  273. })), genDashedButtonStyle(token)), genFilledButtonStyle(token, token.colorPrimaryBg, {
  274. color: token.colorPrimary,
  275. background: token.colorPrimaryBgHover
  276. }, {
  277. color: token.colorPrimary,
  278. background: token.colorPrimaryBorder
  279. })), genTextLinkButtonStyle(token, token.colorPrimaryText, 'text', {
  280. color: token.colorPrimaryTextHover,
  281. background: token.colorPrimaryBg
  282. }, {
  283. color: token.colorPrimaryTextActive,
  284. background: token.colorPrimaryBorder
  285. })), genTextLinkButtonStyle(token, token.colorPrimaryText, 'link', {
  286. color: token.colorPrimaryTextHover,
  287. background: token.linkHoverBg
  288. }, {
  289. color: token.colorPrimaryTextActive
  290. })), genGhostButtonStyle(token.componentCls, token.ghostBg, token.colorPrimary, token.colorPrimary, token.colorTextDisabled, token.colorBorder, {
  291. color: token.colorPrimaryHover,
  292. borderColor: token.colorPrimaryHover
  293. }, {
  294. color: token.colorPrimaryActive,
  295. borderColor: token.colorPrimaryActive
  296. }));
  297. const genDangerousStyle = token => Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({
  298. color: token.colorError,
  299. boxShadow: token.dangerShadow
  300. }, genSolidButtonStyle(token, token.dangerColor, token.colorError, {
  301. background: token.colorErrorHover
  302. }, {
  303. background: token.colorErrorActive
  304. })), genOutlinedDashedButtonStyle(token, token.colorError, token.colorBgContainer, {
  305. color: token.colorErrorHover,
  306. borderColor: token.colorErrorBorderHover
  307. }, {
  308. color: token.colorErrorActive,
  309. borderColor: token.colorErrorActive
  310. })), genDashedButtonStyle(token)), genFilledButtonStyle(token, token.colorErrorBg, {
  311. color: token.colorError,
  312. background: token.colorErrorBgFilledHover
  313. }, {
  314. color: token.colorError,
  315. background: token.colorErrorBgActive
  316. })), genTextLinkButtonStyle(token, token.colorError, 'text', {
  317. color: token.colorErrorHover,
  318. background: token.colorErrorBg
  319. }, {
  320. color: token.colorErrorHover,
  321. background: token.colorErrorBgActive
  322. })), genTextLinkButtonStyle(token, token.colorError, 'link', {
  323. color: token.colorErrorHover
  324. }, {
  325. color: token.colorErrorActive
  326. })), genGhostButtonStyle(token.componentCls, token.ghostBg, token.colorError, token.colorError, token.colorTextDisabled, token.colorBorder, {
  327. color: token.colorErrorHover,
  328. borderColor: token.colorErrorHover
  329. }, {
  330. color: token.colorErrorActive,
  331. borderColor: token.colorErrorActive
  332. }));
  333. const genLinkStyle = token => Object.assign(Object.assign({}, genTextLinkButtonStyle(token, token.colorLink, 'link', {
  334. color: token.colorLinkHover
  335. }, {
  336. color: token.colorLinkActive
  337. })), genGhostButtonStyle(token.componentCls, token.ghostBg, token.colorInfo, token.colorInfo, token.colorTextDisabled, token.colorBorder, {
  338. color: token.colorInfoHover,
  339. borderColor: token.colorInfoHover
  340. }, {
  341. color: token.colorInfoActive,
  342. borderColor: token.colorInfoActive
  343. }));
  344. const genColorButtonStyle = token => {
  345. const {
  346. componentCls
  347. } = token;
  348. return Object.assign({
  349. [`${componentCls}-color-default`]: genDefaultButtonStyle(token),
  350. [`${componentCls}-color-primary`]: genPrimaryButtonStyle(token),
  351. [`${componentCls}-color-dangerous`]: genDangerousStyle(token),
  352. [`${componentCls}-color-link`]: genLinkStyle(token)
  353. }, genPresetColorStyle(token));
  354. };
  355. // =========== Compatible with versions earlier than 5.21.0 ===========
  356. const genCompatibleButtonStyle = token => Object.assign(Object.assign(Object.assign(Object.assign({}, genOutlinedDashedButtonStyle(token, token.defaultBorderColor, token.defaultBg, {
  357. color: token.defaultHoverColor,
  358. borderColor: token.defaultHoverBorderColor,
  359. background: token.defaultHoverBg
  360. }, {
  361. color: token.defaultActiveColor,
  362. borderColor: token.defaultActiveBorderColor,
  363. background: token.defaultActiveBg
  364. })), genTextLinkButtonStyle(token, token.textTextColor, 'text', {
  365. color: token.textTextHoverColor,
  366. background: token.textHoverBg
  367. }, {
  368. color: token.textTextActiveColor,
  369. background: token.colorBgTextActive
  370. })), genSolidButtonStyle(token, token.primaryColor, token.colorPrimary, {
  371. background: token.colorPrimaryHover,
  372. color: token.primaryColor
  373. }, {
  374. background: token.colorPrimaryActive,
  375. color: token.primaryColor
  376. })), genTextLinkButtonStyle(token, token.colorLink, 'link', {
  377. color: token.colorLinkHover,
  378. background: token.linkHoverBg
  379. }, {
  380. color: token.colorLinkActive
  381. }));
  382. // =============================== Size ===============================
  383. const genButtonStyle = (token, prefixCls = '') => {
  384. const {
  385. componentCls,
  386. controlHeight,
  387. fontSize,
  388. borderRadius,
  389. buttonPaddingHorizontal,
  390. iconCls,
  391. buttonPaddingVertical,
  392. buttonIconOnlyFontSize
  393. } = token;
  394. return [{
  395. [prefixCls]: {
  396. fontSize,
  397. height: controlHeight,
  398. padding: `${(0, _cssinjs.unit)(buttonPaddingVertical)} ${(0, _cssinjs.unit)(buttonPaddingHorizontal)}`,
  399. borderRadius,
  400. [`&${componentCls}-icon-only`]: {
  401. width: controlHeight,
  402. [iconCls]: {
  403. fontSize: buttonIconOnlyFontSize
  404. }
  405. }
  406. }
  407. },
  408. // Shape - patch prefixCls again to override solid border radius style
  409. {
  410. [`${componentCls}${componentCls}-circle${prefixCls}`]: genCircleButtonStyle(token)
  411. }, {
  412. [`${componentCls}${componentCls}-round${prefixCls}`]: {
  413. borderRadius: token.controlHeight,
  414. [`&:not(${componentCls}-icon-only)`]: {
  415. paddingInline: token.buttonPaddingHorizontal
  416. }
  417. }
  418. }];
  419. };
  420. const genSizeBaseButtonStyle = token => {
  421. const baseToken = (0, _internal.mergeToken)(token, {
  422. fontSize: token.contentFontSize
  423. });
  424. return genButtonStyle(baseToken, token.componentCls);
  425. };
  426. const genSizeSmallButtonStyle = token => {
  427. const smallToken = (0, _internal.mergeToken)(token, {
  428. controlHeight: token.controlHeightSM,
  429. fontSize: token.contentFontSizeSM,
  430. padding: token.paddingXS,
  431. buttonPaddingHorizontal: token.paddingInlineSM,
  432. buttonPaddingVertical: 0,
  433. borderRadius: token.borderRadiusSM,
  434. buttonIconOnlyFontSize: token.onlyIconSizeSM
  435. });
  436. return genButtonStyle(smallToken, `${token.componentCls}-sm`);
  437. };
  438. const genSizeLargeButtonStyle = token => {
  439. const largeToken = (0, _internal.mergeToken)(token, {
  440. controlHeight: token.controlHeightLG,
  441. fontSize: token.contentFontSizeLG,
  442. buttonPaddingHorizontal: token.paddingInlineLG,
  443. buttonPaddingVertical: 0,
  444. borderRadius: token.borderRadiusLG,
  445. buttonIconOnlyFontSize: token.onlyIconSizeLG
  446. });
  447. return genButtonStyle(largeToken, `${token.componentCls}-lg`);
  448. };
  449. const genBlockButtonStyle = token => {
  450. const {
  451. componentCls
  452. } = token;
  453. return {
  454. [componentCls]: {
  455. [`&${componentCls}-block`]: {
  456. width: '100%'
  457. }
  458. }
  459. };
  460. };
  461. // ============================== Export ==============================
  462. var _default = exports.default = (0, _internal.genStyleHooks)('Button', token => {
  463. const buttonToken = (0, _token.prepareToken)(token);
  464. return [
  465. // Shared
  466. genSharedButtonStyle(buttonToken),
  467. // Size
  468. genSizeBaseButtonStyle(buttonToken), genSizeSmallButtonStyle(buttonToken), genSizeLargeButtonStyle(buttonToken),
  469. // Block
  470. genBlockButtonStyle(buttonToken),
  471. // Color
  472. genColorButtonStyle(buttonToken),
  473. // https://github.com/ant-design/ant-design/issues/50969
  474. genCompatibleButtonStyle(buttonToken),
  475. // Button Group
  476. (0, _group.default)(buttonToken)];
  477. }, _token.prepareComponentToken, {
  478. unitless: {
  479. fontWeight: true,
  480. contentLineHeight: true,
  481. contentLineHeightSM: true,
  482. contentLineHeightLG: true
  483. }
  484. });