index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.prepareComponentToken = exports.default = void 0;
  6. var _cssinjs = require("@ant-design/cssinjs");
  7. var _fastColor = require("@ant-design/fast-color");
  8. var _style = require("../../style");
  9. var _internal = require("../../theme/internal");
  10. const genSwitchSmallStyle = token => {
  11. const {
  12. componentCls,
  13. trackHeightSM,
  14. trackPadding,
  15. trackMinWidthSM,
  16. innerMinMarginSM,
  17. innerMaxMarginSM,
  18. handleSizeSM,
  19. calc
  20. } = token;
  21. const switchInnerCls = `${componentCls}-inner`;
  22. const trackPaddingCalc = (0, _cssinjs.unit)(calc(handleSizeSM).add(calc(trackPadding).mul(2)).equal());
  23. const innerMaxMarginCalc = (0, _cssinjs.unit)(calc(innerMaxMarginSM).mul(2).equal());
  24. return {
  25. [componentCls]: {
  26. [`&${componentCls}-small`]: {
  27. minWidth: trackMinWidthSM,
  28. height: trackHeightSM,
  29. lineHeight: (0, _cssinjs.unit)(trackHeightSM),
  30. [`${componentCls}-inner`]: {
  31. paddingInlineStart: innerMaxMarginSM,
  32. paddingInlineEnd: innerMinMarginSM,
  33. [`${switchInnerCls}-checked, ${switchInnerCls}-unchecked`]: {
  34. minHeight: trackHeightSM
  35. },
  36. [`${switchInnerCls}-checked`]: {
  37. marginInlineStart: `calc(-100% + ${trackPaddingCalc} - ${innerMaxMarginCalc})`,
  38. marginInlineEnd: `calc(100% - ${trackPaddingCalc} + ${innerMaxMarginCalc})`
  39. },
  40. [`${switchInnerCls}-unchecked`]: {
  41. marginTop: calc(trackHeightSM).mul(-1).equal(),
  42. marginInlineStart: 0,
  43. marginInlineEnd: 0
  44. }
  45. },
  46. [`${componentCls}-handle`]: {
  47. width: handleSizeSM,
  48. height: handleSizeSM
  49. },
  50. [`${componentCls}-loading-icon`]: {
  51. top: calc(calc(handleSizeSM).sub(token.switchLoadingIconSize)).div(2).equal(),
  52. fontSize: token.switchLoadingIconSize
  53. },
  54. [`&${componentCls}-checked`]: {
  55. [`${componentCls}-inner`]: {
  56. paddingInlineStart: innerMinMarginSM,
  57. paddingInlineEnd: innerMaxMarginSM,
  58. [`${switchInnerCls}-checked`]: {
  59. marginInlineStart: 0,
  60. marginInlineEnd: 0
  61. },
  62. [`${switchInnerCls}-unchecked`]: {
  63. marginInlineStart: `calc(100% - ${trackPaddingCalc} + ${innerMaxMarginCalc})`,
  64. marginInlineEnd: `calc(-100% + ${trackPaddingCalc} - ${innerMaxMarginCalc})`
  65. }
  66. },
  67. [`${componentCls}-handle`]: {
  68. insetInlineStart: `calc(100% - ${(0, _cssinjs.unit)(calc(handleSizeSM).add(trackPadding).equal())})`
  69. }
  70. },
  71. [`&:not(${componentCls}-disabled):active`]: {
  72. [`&:not(${componentCls}-checked) ${switchInnerCls}`]: {
  73. [`${switchInnerCls}-unchecked`]: {
  74. marginInlineStart: calc(token.marginXXS).div(2).equal(),
  75. marginInlineEnd: calc(token.marginXXS).mul(-1).div(2).equal()
  76. }
  77. },
  78. [`&${componentCls}-checked ${switchInnerCls}`]: {
  79. [`${switchInnerCls}-checked`]: {
  80. marginInlineStart: calc(token.marginXXS).mul(-1).div(2).equal(),
  81. marginInlineEnd: calc(token.marginXXS).div(2).equal()
  82. }
  83. }
  84. }
  85. }
  86. }
  87. };
  88. };
  89. const genSwitchLoadingStyle = token => {
  90. const {
  91. componentCls,
  92. handleSize,
  93. calc
  94. } = token;
  95. return {
  96. [componentCls]: {
  97. [`${componentCls}-loading-icon${token.iconCls}`]: {
  98. position: 'relative',
  99. top: calc(calc(handleSize).sub(token.fontSize)).div(2).equal(),
  100. color: token.switchLoadingIconColor,
  101. verticalAlign: 'top'
  102. },
  103. [`&${componentCls}-checked ${componentCls}-loading-icon`]: {
  104. color: token.switchColor
  105. }
  106. }
  107. };
  108. };
  109. const genSwitchHandleStyle = token => {
  110. const {
  111. componentCls,
  112. trackPadding,
  113. handleBg,
  114. handleShadow,
  115. handleSize,
  116. calc
  117. } = token;
  118. const switchHandleCls = `${componentCls}-handle`;
  119. return {
  120. [componentCls]: {
  121. [switchHandleCls]: {
  122. position: 'absolute',
  123. top: trackPadding,
  124. insetInlineStart: trackPadding,
  125. width: handleSize,
  126. height: handleSize,
  127. transition: `all ${token.switchDuration} ease-in-out`,
  128. '&::before': {
  129. position: 'absolute',
  130. top: 0,
  131. insetInlineEnd: 0,
  132. bottom: 0,
  133. insetInlineStart: 0,
  134. backgroundColor: handleBg,
  135. borderRadius: calc(handleSize).div(2).equal(),
  136. boxShadow: handleShadow,
  137. transition: `all ${token.switchDuration} ease-in-out`,
  138. content: '""'
  139. }
  140. },
  141. [`&${componentCls}-checked ${switchHandleCls}`]: {
  142. insetInlineStart: `calc(100% - ${(0, _cssinjs.unit)(calc(handleSize).add(trackPadding).equal())})`
  143. },
  144. [`&:not(${componentCls}-disabled):active`]: {
  145. [`${switchHandleCls}::before`]: {
  146. insetInlineEnd: token.switchHandleActiveInset,
  147. insetInlineStart: 0
  148. },
  149. [`&${componentCls}-checked ${switchHandleCls}::before`]: {
  150. insetInlineEnd: 0,
  151. insetInlineStart: token.switchHandleActiveInset
  152. }
  153. }
  154. }
  155. };
  156. };
  157. const genSwitchInnerStyle = token => {
  158. const {
  159. componentCls,
  160. trackHeight,
  161. trackPadding,
  162. innerMinMargin,
  163. innerMaxMargin,
  164. handleSize,
  165. calc
  166. } = token;
  167. const switchInnerCls = `${componentCls}-inner`;
  168. const trackPaddingCalc = (0, _cssinjs.unit)(calc(handleSize).add(calc(trackPadding).mul(2)).equal());
  169. const innerMaxMarginCalc = (0, _cssinjs.unit)(calc(innerMaxMargin).mul(2).equal());
  170. return {
  171. [componentCls]: {
  172. [switchInnerCls]: {
  173. display: 'block',
  174. overflow: 'hidden',
  175. borderRadius: 100,
  176. height: '100%',
  177. paddingInlineStart: innerMaxMargin,
  178. paddingInlineEnd: innerMinMargin,
  179. transition: `padding-inline-start ${token.switchDuration} ease-in-out, padding-inline-end ${token.switchDuration} ease-in-out`,
  180. [`${switchInnerCls}-checked, ${switchInnerCls}-unchecked`]: {
  181. display: 'block',
  182. color: token.colorTextLightSolid,
  183. fontSize: token.fontSizeSM,
  184. transition: `margin-inline-start ${token.switchDuration} ease-in-out, margin-inline-end ${token.switchDuration} ease-in-out`,
  185. pointerEvents: 'none',
  186. minHeight: trackHeight
  187. },
  188. [`${switchInnerCls}-checked`]: {
  189. marginInlineStart: `calc(-100% + ${trackPaddingCalc} - ${innerMaxMarginCalc})`,
  190. marginInlineEnd: `calc(100% - ${trackPaddingCalc} + ${innerMaxMarginCalc})`
  191. },
  192. [`${switchInnerCls}-unchecked`]: {
  193. marginTop: calc(trackHeight).mul(-1).equal(),
  194. marginInlineStart: 0,
  195. marginInlineEnd: 0
  196. }
  197. },
  198. [`&${componentCls}-checked ${switchInnerCls}`]: {
  199. paddingInlineStart: innerMinMargin,
  200. paddingInlineEnd: innerMaxMargin,
  201. [`${switchInnerCls}-checked`]: {
  202. marginInlineStart: 0,
  203. marginInlineEnd: 0
  204. },
  205. [`${switchInnerCls}-unchecked`]: {
  206. marginInlineStart: `calc(100% - ${trackPaddingCalc} + ${innerMaxMarginCalc})`,
  207. marginInlineEnd: `calc(-100% + ${trackPaddingCalc} - ${innerMaxMarginCalc})`
  208. }
  209. },
  210. [`&:not(${componentCls}-disabled):active`]: {
  211. [`&:not(${componentCls}-checked) ${switchInnerCls}`]: {
  212. [`${switchInnerCls}-unchecked`]: {
  213. marginInlineStart: calc(trackPadding).mul(2).equal(),
  214. marginInlineEnd: calc(trackPadding).mul(-1).mul(2).equal()
  215. }
  216. },
  217. [`&${componentCls}-checked ${switchInnerCls}`]: {
  218. [`${switchInnerCls}-checked`]: {
  219. marginInlineStart: calc(trackPadding).mul(-1).mul(2).equal(),
  220. marginInlineEnd: calc(trackPadding).mul(2).equal()
  221. }
  222. }
  223. }
  224. }
  225. };
  226. };
  227. const genSwitchStyle = token => {
  228. const {
  229. componentCls,
  230. trackHeight,
  231. trackMinWidth
  232. } = token;
  233. return {
  234. [componentCls]: Object.assign(Object.assign(Object.assign(Object.assign({}, (0, _style.resetComponent)(token)), {
  235. position: 'relative',
  236. display: 'inline-block',
  237. boxSizing: 'border-box',
  238. minWidth: trackMinWidth,
  239. height: trackHeight,
  240. lineHeight: (0, _cssinjs.unit)(trackHeight),
  241. verticalAlign: 'middle',
  242. background: token.colorTextQuaternary,
  243. border: '0',
  244. borderRadius: 100,
  245. cursor: 'pointer',
  246. transition: `all ${token.motionDurationMid}`,
  247. userSelect: 'none',
  248. [`&:hover:not(${componentCls}-disabled)`]: {
  249. background: token.colorTextTertiary
  250. }
  251. }), (0, _style.genFocusStyle)(token)), {
  252. [`&${componentCls}-checked`]: {
  253. background: token.switchColor,
  254. [`&:hover:not(${componentCls}-disabled)`]: {
  255. background: token.colorPrimaryHover
  256. }
  257. },
  258. [`&${componentCls}-loading, &${componentCls}-disabled`]: {
  259. cursor: 'not-allowed',
  260. opacity: token.switchDisabledOpacity,
  261. '*': {
  262. boxShadow: 'none',
  263. cursor: 'not-allowed'
  264. }
  265. },
  266. // rtl style
  267. [`&${componentCls}-rtl`]: {
  268. direction: 'rtl'
  269. }
  270. })
  271. };
  272. };
  273. // ============================== Export ==============================
  274. const prepareComponentToken = token => {
  275. const {
  276. fontSize,
  277. lineHeight,
  278. controlHeight,
  279. colorWhite
  280. } = token;
  281. const height = fontSize * lineHeight;
  282. const heightSM = controlHeight / 2;
  283. const padding = 2; // Fixed value
  284. const handleSize = height - padding * 2;
  285. const handleSizeSM = heightSM - padding * 2;
  286. return {
  287. trackHeight: height,
  288. trackHeightSM: heightSM,
  289. trackMinWidth: handleSize * 2 + padding * 4,
  290. trackMinWidthSM: handleSizeSM * 2 + padding * 2,
  291. trackPadding: padding,
  292. // Fixed value
  293. handleBg: colorWhite,
  294. handleSize,
  295. handleSizeSM,
  296. handleShadow: `0 2px 4px 0 ${new _fastColor.FastColor('#00230b').setA(0.2).toRgbString()}`,
  297. innerMinMargin: handleSize / 2,
  298. innerMaxMargin: handleSize + padding + padding * 2,
  299. innerMinMarginSM: handleSizeSM / 2,
  300. innerMaxMarginSM: handleSizeSM + padding + padding * 2
  301. };
  302. };
  303. exports.prepareComponentToken = prepareComponentToken;
  304. var _default = exports.default = (0, _internal.genStyleHooks)('Switch', token => {
  305. const switchToken = (0, _internal.mergeToken)(token, {
  306. switchDuration: token.motionDurationMid,
  307. switchColor: token.colorPrimary,
  308. switchDisabledOpacity: token.opacityLoading,
  309. switchLoadingIconSize: token.calc(token.fontSizeIcon).mul(0.75).equal(),
  310. switchLoadingIconColor: `rgba(0, 0, 0, ${token.opacityLoading})`,
  311. switchHandleActiveInset: '-30%'
  312. });
  313. return [genSwitchStyle(switchToken),
  314. // inner style
  315. genSwitchInnerStyle(switchToken),
  316. // handle style
  317. genSwitchHandleStyle(switchToken),
  318. // loading style
  319. genSwitchLoadingStyle(switchToken),
  320. // small style
  321. genSwitchSmallStyle(switchToken)];
  322. }, prepareComponentToken);