index.js 10 KB

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