index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. import { unit } from '@ant-design/cssinjs';
  2. import { genFocusOutline, resetComponent } from '../../style';
  3. import { genStyleHooks, mergeToken } from '../../theme/internal';
  4. // ============================== Styles ==============================
  5. // styles from RadioGroup only
  6. const getGroupRadioStyle = token => {
  7. const {
  8. componentCls,
  9. antCls
  10. } = token;
  11. const groupPrefixCls = `${componentCls}-group`;
  12. return {
  13. [groupPrefixCls]: Object.assign(Object.assign({}, resetComponent(token)), {
  14. display: 'inline-block',
  15. fontSize: 0,
  16. // RTL
  17. [`&${groupPrefixCls}-rtl`]: {
  18. direction: 'rtl'
  19. },
  20. [`&${groupPrefixCls}-block`]: {
  21. display: 'flex'
  22. },
  23. [`${antCls}-badge ${antCls}-badge-count`]: {
  24. zIndex: 1
  25. },
  26. [`> ${antCls}-badge:not(:first-child) > ${antCls}-button-wrapper`]: {
  27. borderInlineStart: 'none'
  28. }
  29. })
  30. };
  31. };
  32. // Styles from radio-wrapper
  33. const getRadioBasicStyle = token => {
  34. const {
  35. componentCls,
  36. wrapperMarginInlineEnd,
  37. colorPrimary,
  38. radioSize,
  39. motionDurationSlow,
  40. motionDurationMid,
  41. motionEaseInOutCirc,
  42. colorBgContainer,
  43. colorBorder,
  44. lineWidth,
  45. colorBgContainerDisabled,
  46. colorTextDisabled,
  47. paddingXS,
  48. dotColorDisabled,
  49. lineType,
  50. radioColor,
  51. radioBgColor,
  52. calc
  53. } = token;
  54. const radioInnerPrefixCls = `${componentCls}-inner`;
  55. const dotPadding = 4;
  56. const radioDotDisabledSize = calc(radioSize).sub(calc(dotPadding).mul(2));
  57. const radioSizeCalc = calc(1).mul(radioSize).equal({
  58. unit: true
  59. });
  60. return {
  61. [`${componentCls}-wrapper`]: Object.assign(Object.assign({}, resetComponent(token)), {
  62. display: 'inline-flex',
  63. alignItems: 'baseline',
  64. marginInlineStart: 0,
  65. marginInlineEnd: wrapperMarginInlineEnd,
  66. cursor: 'pointer',
  67. '&:last-child': {
  68. marginInlineEnd: 0
  69. },
  70. // RTL
  71. [`&${componentCls}-wrapper-rtl`]: {
  72. direction: 'rtl'
  73. },
  74. '&-disabled': {
  75. cursor: 'not-allowed',
  76. color: token.colorTextDisabled
  77. },
  78. '&::after': {
  79. display: 'inline-block',
  80. width: 0,
  81. overflow: 'hidden',
  82. content: '"\\a0"'
  83. },
  84. '&-block': {
  85. flex: 1,
  86. justifyContent: 'center'
  87. },
  88. // hashId 在 wrapper 上,只能铺平
  89. [`${componentCls}-checked::after`]: {
  90. position: 'absolute',
  91. insetBlockStart: 0,
  92. insetInlineStart: 0,
  93. width: '100%',
  94. height: '100%',
  95. border: `${unit(lineWidth)} ${lineType} ${colorPrimary}`,
  96. borderRadius: '50%',
  97. visibility: 'hidden',
  98. opacity: 0,
  99. content: '""'
  100. },
  101. [componentCls]: Object.assign(Object.assign({}, resetComponent(token)), {
  102. position: 'relative',
  103. display: 'inline-block',
  104. outline: 'none',
  105. cursor: 'pointer',
  106. alignSelf: 'center',
  107. borderRadius: '50%'
  108. }),
  109. [`${componentCls}-wrapper:hover &,
  110. &:hover ${radioInnerPrefixCls}`]: {
  111. borderColor: colorPrimary
  112. },
  113. [`${componentCls}-input:focus-visible + ${radioInnerPrefixCls}`]: genFocusOutline(token),
  114. [`${componentCls}:hover::after, ${componentCls}-wrapper:hover &::after`]: {
  115. visibility: 'visible'
  116. },
  117. [`${componentCls}-inner`]: {
  118. '&::after': {
  119. boxSizing: 'border-box',
  120. position: 'absolute',
  121. insetBlockStart: '50%',
  122. insetInlineStart: '50%',
  123. display: 'block',
  124. width: radioSizeCalc,
  125. height: radioSizeCalc,
  126. marginBlockStart: calc(1).mul(radioSize).div(-2).equal({
  127. unit: true
  128. }),
  129. marginInlineStart: calc(1).mul(radioSize).div(-2).equal({
  130. unit: true
  131. }),
  132. backgroundColor: radioColor,
  133. borderBlockStart: 0,
  134. borderInlineStart: 0,
  135. borderRadius: radioSizeCalc,
  136. transform: 'scale(0)',
  137. opacity: 0,
  138. transition: `all ${motionDurationSlow} ${motionEaseInOutCirc}`,
  139. content: '""'
  140. },
  141. boxSizing: 'border-box',
  142. position: 'relative',
  143. insetBlockStart: 0,
  144. insetInlineStart: 0,
  145. display: 'block',
  146. width: radioSizeCalc,
  147. height: radioSizeCalc,
  148. backgroundColor: colorBgContainer,
  149. borderColor: colorBorder,
  150. borderStyle: 'solid',
  151. borderWidth: lineWidth,
  152. borderRadius: '50%',
  153. transition: `all ${motionDurationMid}`
  154. },
  155. [`${componentCls}-input`]: {
  156. position: 'absolute',
  157. inset: 0,
  158. zIndex: 1,
  159. cursor: 'pointer',
  160. opacity: 0
  161. },
  162. // 选中状态
  163. [`${componentCls}-checked`]: {
  164. [radioInnerPrefixCls]: {
  165. borderColor: colorPrimary,
  166. backgroundColor: radioBgColor,
  167. '&::after': {
  168. transform: `scale(${token.calc(token.dotSize).div(radioSize).equal()})`,
  169. opacity: 1,
  170. transition: `all ${motionDurationSlow} ${motionEaseInOutCirc}`
  171. }
  172. }
  173. },
  174. [`${componentCls}-disabled`]: {
  175. cursor: 'not-allowed',
  176. [radioInnerPrefixCls]: {
  177. backgroundColor: colorBgContainerDisabled,
  178. borderColor: colorBorder,
  179. cursor: 'not-allowed',
  180. '&::after': {
  181. backgroundColor: dotColorDisabled
  182. }
  183. },
  184. [`${componentCls}-input`]: {
  185. cursor: 'not-allowed'
  186. },
  187. [`${componentCls}-disabled + span`]: {
  188. color: colorTextDisabled,
  189. cursor: 'not-allowed'
  190. },
  191. [`&${componentCls}-checked`]: {
  192. [radioInnerPrefixCls]: {
  193. '&::after': {
  194. transform: `scale(${calc(radioDotDisabledSize).div(radioSize).equal()})`
  195. }
  196. }
  197. }
  198. },
  199. [`span${componentCls} + *`]: {
  200. paddingInlineStart: paddingXS,
  201. paddingInlineEnd: paddingXS
  202. }
  203. })
  204. };
  205. };
  206. // Styles from radio-button
  207. const getRadioButtonStyle = token => {
  208. const {
  209. buttonColor,
  210. controlHeight,
  211. componentCls,
  212. lineWidth,
  213. lineType,
  214. colorBorder,
  215. motionDurationMid,
  216. buttonPaddingInline,
  217. fontSize,
  218. buttonBg,
  219. fontSizeLG,
  220. controlHeightLG,
  221. controlHeightSM,
  222. paddingXS,
  223. borderRadius,
  224. borderRadiusSM,
  225. borderRadiusLG,
  226. buttonCheckedBg,
  227. buttonSolidCheckedColor,
  228. colorTextDisabled,
  229. colorBgContainerDisabled,
  230. buttonCheckedBgDisabled,
  231. buttonCheckedColorDisabled,
  232. colorPrimary,
  233. colorPrimaryHover,
  234. colorPrimaryActive,
  235. buttonSolidCheckedBg,
  236. buttonSolidCheckedHoverBg,
  237. buttonSolidCheckedActiveBg,
  238. calc
  239. } = token;
  240. return {
  241. [`${componentCls}-button-wrapper`]: {
  242. position: 'relative',
  243. display: 'inline-block',
  244. height: controlHeight,
  245. margin: 0,
  246. paddingInline: buttonPaddingInline,
  247. paddingBlock: 0,
  248. color: buttonColor,
  249. fontSize,
  250. lineHeight: unit(calc(controlHeight).sub(calc(lineWidth).mul(2)).equal()),
  251. background: buttonBg,
  252. border: `${unit(lineWidth)} ${lineType} ${colorBorder}`,
  253. // strange align fix for chrome but works
  254. // https://gw.alipayobjects.com/zos/rmsportal/VFTfKXJuogBAXcvfAUWJ.gif
  255. borderBlockStartWidth: calc(lineWidth).add(0.02).equal(),
  256. borderInlineEndWidth: lineWidth,
  257. cursor: 'pointer',
  258. transition: [`color ${motionDurationMid}`, `background ${motionDurationMid}`, `box-shadow ${motionDurationMid}`].join(','),
  259. a: {
  260. color: buttonColor
  261. },
  262. [`> ${componentCls}-button`]: {
  263. position: 'absolute',
  264. insetBlockStart: 0,
  265. insetInlineStart: 0,
  266. zIndex: -1,
  267. width: '100%',
  268. height: '100%'
  269. },
  270. '&:not(:last-child)': {
  271. marginInlineEnd: calc(lineWidth).mul(-1).equal()
  272. },
  273. '&:first-child': {
  274. borderInlineStart: `${unit(lineWidth)} ${lineType} ${colorBorder}`,
  275. borderStartStartRadius: borderRadius,
  276. borderEndStartRadius: borderRadius
  277. },
  278. '&:last-child': {
  279. borderStartEndRadius: borderRadius,
  280. borderEndEndRadius: borderRadius
  281. },
  282. '&:first-child:last-child': {
  283. borderRadius
  284. },
  285. [`${componentCls}-group-large &`]: {
  286. height: controlHeightLG,
  287. fontSize: fontSizeLG,
  288. lineHeight: unit(calc(controlHeightLG).sub(calc(lineWidth).mul(2)).equal()),
  289. '&:first-child': {
  290. borderStartStartRadius: borderRadiusLG,
  291. borderEndStartRadius: borderRadiusLG
  292. },
  293. '&:last-child': {
  294. borderStartEndRadius: borderRadiusLG,
  295. borderEndEndRadius: borderRadiusLG
  296. }
  297. },
  298. [`${componentCls}-group-small &`]: {
  299. height: controlHeightSM,
  300. paddingInline: calc(paddingXS).sub(lineWidth).equal(),
  301. paddingBlock: 0,
  302. lineHeight: unit(calc(controlHeightSM).sub(calc(lineWidth).mul(2)).equal()),
  303. '&:first-child': {
  304. borderStartStartRadius: borderRadiusSM,
  305. borderEndStartRadius: borderRadiusSM
  306. },
  307. '&:last-child': {
  308. borderStartEndRadius: borderRadiusSM,
  309. borderEndEndRadius: borderRadiusSM
  310. }
  311. },
  312. '&:hover': {
  313. position: 'relative',
  314. color: colorPrimary
  315. },
  316. '&:has(:focus-visible)': genFocusOutline(token),
  317. [`${componentCls}-inner, input[type='checkbox'], input[type='radio']`]: {
  318. width: 0,
  319. height: 0,
  320. opacity: 0,
  321. pointerEvents: 'none'
  322. },
  323. [`&-checked:not(${componentCls}-button-wrapper-disabled)`]: {
  324. zIndex: 1,
  325. color: colorPrimary,
  326. background: buttonCheckedBg,
  327. borderColor: colorPrimary,
  328. '&::before': {
  329. backgroundColor: colorPrimary
  330. },
  331. '&:first-child': {
  332. borderColor: colorPrimary
  333. },
  334. '&:hover': {
  335. color: colorPrimaryHover,
  336. borderColor: colorPrimaryHover,
  337. '&::before': {
  338. backgroundColor: colorPrimaryHover
  339. }
  340. },
  341. '&:active': {
  342. color: colorPrimaryActive,
  343. borderColor: colorPrimaryActive,
  344. '&::before': {
  345. backgroundColor: colorPrimaryActive
  346. }
  347. }
  348. },
  349. [`${componentCls}-group-solid &-checked:not(${componentCls}-button-wrapper-disabled)`]: {
  350. color: buttonSolidCheckedColor,
  351. background: buttonSolidCheckedBg,
  352. borderColor: buttonSolidCheckedBg,
  353. '&:hover': {
  354. color: buttonSolidCheckedColor,
  355. background: buttonSolidCheckedHoverBg,
  356. borderColor: buttonSolidCheckedHoverBg
  357. },
  358. '&:active': {
  359. color: buttonSolidCheckedColor,
  360. background: buttonSolidCheckedActiveBg,
  361. borderColor: buttonSolidCheckedActiveBg
  362. }
  363. },
  364. '&-disabled': {
  365. color: colorTextDisabled,
  366. backgroundColor: colorBgContainerDisabled,
  367. borderColor: colorBorder,
  368. cursor: 'not-allowed',
  369. '&:first-child, &:hover': {
  370. color: colorTextDisabled,
  371. backgroundColor: colorBgContainerDisabled,
  372. borderColor: colorBorder
  373. }
  374. },
  375. [`&-disabled${componentCls}-button-wrapper-checked`]: {
  376. color: buttonCheckedColorDisabled,
  377. backgroundColor: buttonCheckedBgDisabled,
  378. borderColor: colorBorder,
  379. boxShadow: 'none'
  380. },
  381. '&-block': {
  382. flex: 1,
  383. textAlign: 'center'
  384. }
  385. }
  386. };
  387. };
  388. // ============================== Export ==============================
  389. export const prepareComponentToken = token => {
  390. const {
  391. wireframe,
  392. padding,
  393. marginXS,
  394. lineWidth,
  395. fontSizeLG,
  396. colorText,
  397. colorBgContainer,
  398. colorTextDisabled,
  399. controlItemBgActiveDisabled,
  400. colorTextLightSolid,
  401. colorPrimary,
  402. colorPrimaryHover,
  403. colorPrimaryActive,
  404. colorWhite
  405. } = token;
  406. const dotPadding = 4; // Fixed value
  407. const radioSize = fontSizeLG;
  408. const radioDotSize = wireframe ? radioSize - dotPadding * 2 : radioSize - (dotPadding + lineWidth) * 2;
  409. return {
  410. // Radio
  411. radioSize,
  412. dotSize: radioDotSize,
  413. dotColorDisabled: colorTextDisabled,
  414. // Radio buttons
  415. buttonSolidCheckedColor: colorTextLightSolid,
  416. buttonSolidCheckedBg: colorPrimary,
  417. buttonSolidCheckedHoverBg: colorPrimaryHover,
  418. buttonSolidCheckedActiveBg: colorPrimaryActive,
  419. buttonBg: colorBgContainer,
  420. buttonCheckedBg: colorBgContainer,
  421. buttonColor: colorText,
  422. buttonCheckedBgDisabled: controlItemBgActiveDisabled,
  423. buttonCheckedColorDisabled: colorTextDisabled,
  424. buttonPaddingInline: padding - lineWidth,
  425. wrapperMarginInlineEnd: marginXS,
  426. // internal
  427. radioColor: wireframe ? colorPrimary : colorWhite,
  428. radioBgColor: wireframe ? colorBgContainer : colorPrimary
  429. };
  430. };
  431. export default genStyleHooks('Radio', token => {
  432. const {
  433. controlOutline,
  434. controlOutlineWidth
  435. } = token;
  436. const radioFocusShadow = `0 0 0 ${unit(controlOutlineWidth)} ${controlOutline}`;
  437. const radioButtonFocusShadow = radioFocusShadow;
  438. const radioToken = mergeToken(token, {
  439. radioFocusShadow,
  440. radioButtonFocusShadow
  441. });
  442. return [getGroupRadioStyle(radioToken), getRadioBasicStyle(radioToken), getRadioButtonStyle(radioToken)];
  443. }, prepareComponentToken, {
  444. unitless: {
  445. radioSize: true,
  446. dotSize: true
  447. }
  448. });