index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import { unit } from '@ant-design/cssinjs';
  2. import { FastColor } from '@ant-design/fast-color';
  3. import { resetComponent } from '../../style';
  4. import { genStyleHooks, mergeToken } from '../../theme/internal';
  5. // =============================== Base ===============================
  6. const genBaseStyle = token => {
  7. const {
  8. componentCls,
  9. antCls,
  10. controlSize,
  11. dotSize,
  12. marginFull,
  13. marginPart,
  14. colorFillContentHover,
  15. handleColorDisabled,
  16. calc,
  17. handleSize,
  18. handleSizeHover,
  19. handleActiveColor,
  20. handleActiveOutlineColor,
  21. handleLineWidth,
  22. handleLineWidthHover,
  23. motionDurationMid
  24. } = token;
  25. return {
  26. [componentCls]: Object.assign(Object.assign({}, resetComponent(token)), {
  27. position: 'relative',
  28. height: controlSize,
  29. margin: `${unit(marginPart)} ${unit(marginFull)}`,
  30. padding: 0,
  31. cursor: 'pointer',
  32. touchAction: 'none',
  33. '&-vertical': {
  34. margin: `${unit(marginFull)} ${unit(marginPart)}`
  35. },
  36. [`${componentCls}-rail`]: {
  37. position: 'absolute',
  38. backgroundColor: token.railBg,
  39. borderRadius: token.borderRadiusXS,
  40. transition: `background-color ${motionDurationMid}`
  41. },
  42. [`${componentCls}-track,${componentCls}-tracks`]: {
  43. position: 'absolute',
  44. transition: `background-color ${motionDurationMid}`
  45. },
  46. [`${componentCls}-track`]: {
  47. backgroundColor: token.trackBg,
  48. borderRadius: token.borderRadiusXS
  49. },
  50. [`${componentCls}-track-draggable`]: {
  51. boxSizing: 'content-box',
  52. backgroundClip: 'content-box',
  53. border: 'solid rgba(0,0,0,0)'
  54. },
  55. '&:hover': {
  56. [`${componentCls}-rail`]: {
  57. backgroundColor: token.railHoverBg
  58. },
  59. [`${componentCls}-track`]: {
  60. backgroundColor: token.trackHoverBg
  61. },
  62. [`${componentCls}-dot`]: {
  63. borderColor: colorFillContentHover
  64. },
  65. [`${componentCls}-handle::after`]: {
  66. boxShadow: `0 0 0 ${unit(handleLineWidth)} ${token.colorPrimaryBorderHover}`
  67. },
  68. [`${componentCls}-dot-active`]: {
  69. borderColor: token.dotActiveBorderColor
  70. }
  71. },
  72. [`${componentCls}-handle`]: {
  73. position: 'absolute',
  74. width: handleSize,
  75. height: handleSize,
  76. outline: 'none',
  77. userSelect: 'none',
  78. // Dragging status
  79. '&-dragging-delete': {
  80. opacity: 0
  81. },
  82. // 扩大选区
  83. '&::before': {
  84. content: '""',
  85. position: 'absolute',
  86. insetInlineStart: calc(handleLineWidth).mul(-1).equal(),
  87. insetBlockStart: calc(handleLineWidth).mul(-1).equal(),
  88. width: calc(handleSize).add(calc(handleLineWidth).mul(2)).equal(),
  89. height: calc(handleSize).add(calc(handleLineWidth).mul(2)).equal(),
  90. backgroundColor: 'transparent'
  91. },
  92. '&::after': {
  93. content: '""',
  94. position: 'absolute',
  95. insetBlockStart: 0,
  96. insetInlineStart: 0,
  97. width: handleSize,
  98. height: handleSize,
  99. backgroundColor: token.colorBgElevated,
  100. boxShadow: `0 0 0 ${unit(handleLineWidth)} ${token.handleColor}`,
  101. outline: `0px solid transparent`,
  102. borderRadius: '50%',
  103. cursor: 'pointer',
  104. transition: `
  105. inset-inline-start ${motionDurationMid},
  106. inset-block-start ${motionDurationMid},
  107. width ${motionDurationMid},
  108. height ${motionDurationMid},
  109. box-shadow ${motionDurationMid},
  110. outline ${motionDurationMid}
  111. `
  112. },
  113. '&:hover, &:active, &:focus': {
  114. '&::before': {
  115. insetInlineStart: calc(handleSizeHover).sub(handleSize).div(2).add(handleLineWidthHover).mul(-1).equal(),
  116. insetBlockStart: calc(handleSizeHover).sub(handleSize).div(2).add(handleLineWidthHover).mul(-1).equal(),
  117. width: calc(handleSizeHover).add(calc(handleLineWidthHover).mul(2)).equal(),
  118. height: calc(handleSizeHover).add(calc(handleLineWidthHover).mul(2)).equal()
  119. },
  120. '&::after': {
  121. boxShadow: `0 0 0 ${unit(handleLineWidthHover)} ${handleActiveColor}`,
  122. outline: `6px solid ${handleActiveOutlineColor}`,
  123. width: handleSizeHover,
  124. height: handleSizeHover,
  125. insetInlineStart: token.calc(handleSize).sub(handleSizeHover).div(2).equal(),
  126. insetBlockStart: token.calc(handleSize).sub(handleSizeHover).div(2).equal()
  127. }
  128. }
  129. },
  130. [`&-lock ${componentCls}-handle`]: {
  131. '&::before, &::after': {
  132. transition: 'none'
  133. }
  134. },
  135. [`${componentCls}-mark`]: {
  136. position: 'absolute',
  137. fontSize: token.fontSize
  138. },
  139. [`${componentCls}-mark-text`]: {
  140. position: 'absolute',
  141. display: 'inline-block',
  142. color: token.colorTextDescription,
  143. textAlign: 'center',
  144. wordBreak: 'keep-all',
  145. cursor: 'pointer',
  146. userSelect: 'none',
  147. '&-active': {
  148. color: token.colorText
  149. }
  150. },
  151. [`${componentCls}-step`]: {
  152. position: 'absolute',
  153. background: 'transparent',
  154. pointerEvents: 'none'
  155. },
  156. [`${componentCls}-dot`]: {
  157. position: 'absolute',
  158. width: dotSize,
  159. height: dotSize,
  160. backgroundColor: token.colorBgElevated,
  161. border: `${unit(handleLineWidth)} solid ${token.dotBorderColor}`,
  162. borderRadius: '50%',
  163. cursor: 'pointer',
  164. transition: `border-color ${token.motionDurationSlow}`,
  165. pointerEvents: 'auto',
  166. '&-active': {
  167. borderColor: token.dotActiveBorderColor
  168. }
  169. },
  170. [`&${componentCls}-disabled`]: {
  171. cursor: 'not-allowed',
  172. [`${componentCls}-rail`]: {
  173. backgroundColor: `${token.railBg} !important`
  174. },
  175. [`${componentCls}-track`]: {
  176. backgroundColor: `${token.trackBgDisabled} !important`
  177. },
  178. [`
  179. ${componentCls}-dot
  180. `]: {
  181. backgroundColor: token.colorBgElevated,
  182. borderColor: token.trackBgDisabled,
  183. boxShadow: 'none',
  184. cursor: 'not-allowed'
  185. },
  186. [`${componentCls}-handle::after`]: {
  187. backgroundColor: token.colorBgElevated,
  188. cursor: 'not-allowed',
  189. width: handleSize,
  190. height: handleSize,
  191. boxShadow: `0 0 0 ${unit(handleLineWidth)} ${handleColorDisabled}`,
  192. insetInlineStart: 0,
  193. insetBlockStart: 0
  194. },
  195. [`
  196. ${componentCls}-mark-text,
  197. ${componentCls}-dot
  198. `]: {
  199. cursor: `not-allowed !important`
  200. }
  201. },
  202. [`&-tooltip ${antCls}-tooltip-inner`]: {
  203. minWidth: 'unset'
  204. }
  205. })
  206. };
  207. };
  208. // ============================ Horizontal ============================
  209. const genDirectionStyle = (token, horizontal) => {
  210. const {
  211. componentCls,
  212. railSize,
  213. handleSize,
  214. dotSize,
  215. marginFull,
  216. calc
  217. } = token;
  218. const railPadding = horizontal ? 'paddingBlock' : 'paddingInline';
  219. const full = horizontal ? 'width' : 'height';
  220. const part = horizontal ? 'height' : 'width';
  221. const handlePos = horizontal ? 'insetBlockStart' : 'insetInlineStart';
  222. const markInset = horizontal ? 'top' : 'insetInlineStart';
  223. const handlePosSize = calc(railSize).mul(3).sub(handleSize).div(2).equal();
  224. const draggableBorderSize = calc(handleSize).sub(railSize).div(2).equal();
  225. const draggableBorder = horizontal ? {
  226. borderWidth: `${unit(draggableBorderSize)} 0`,
  227. transform: `translateY(${unit(calc(draggableBorderSize).mul(-1).equal())})`
  228. } : {
  229. borderWidth: `0 ${unit(draggableBorderSize)}`,
  230. transform: `translateX(${unit(token.calc(draggableBorderSize).mul(-1).equal())})`
  231. };
  232. return {
  233. [railPadding]: railSize,
  234. [part]: calc(railSize).mul(3).equal(),
  235. [`${componentCls}-rail`]: {
  236. [full]: '100%',
  237. [part]: railSize
  238. },
  239. [`${componentCls}-track,${componentCls}-tracks`]: {
  240. [part]: railSize
  241. },
  242. [`${componentCls}-track-draggable`]: Object.assign({}, draggableBorder),
  243. [`${componentCls}-handle`]: {
  244. [handlePos]: handlePosSize
  245. },
  246. [`${componentCls}-mark`]: {
  247. // Reset all
  248. insetInlineStart: 0,
  249. top: 0,
  250. // https://github.com/ant-design/ant-design/issues/43731
  251. [markInset]: calc(railSize).mul(3).add(horizontal ? 0 : marginFull).equal(),
  252. [full]: '100%'
  253. },
  254. [`${componentCls}-step`]: {
  255. // Reset all
  256. insetInlineStart: 0,
  257. top: 0,
  258. [markInset]: railSize,
  259. [full]: '100%',
  260. [part]: railSize
  261. },
  262. [`${componentCls}-dot`]: {
  263. position: 'absolute',
  264. [handlePos]: calc(railSize).sub(dotSize).div(2).equal()
  265. }
  266. };
  267. };
  268. // ============================ Horizontal ============================
  269. const genHorizontalStyle = token => {
  270. const {
  271. componentCls,
  272. marginPartWithMark
  273. } = token;
  274. return {
  275. [`${componentCls}-horizontal`]: Object.assign(Object.assign({}, genDirectionStyle(token, true)), {
  276. [`&${componentCls}-with-marks`]: {
  277. marginBottom: marginPartWithMark
  278. }
  279. })
  280. };
  281. };
  282. // ============================= Vertical =============================
  283. const genVerticalStyle = token => {
  284. const {
  285. componentCls
  286. } = token;
  287. return {
  288. [`${componentCls}-vertical`]: Object.assign(Object.assign({}, genDirectionStyle(token, false)), {
  289. height: '100%'
  290. })
  291. };
  292. };
  293. // ============================== Export ==============================
  294. export const prepareComponentToken = token => {
  295. // Handle line width is always width-er 1px
  296. const increaseHandleWidth = 1;
  297. const controlSize = token.controlHeightLG / 4;
  298. const controlSizeHover = token.controlHeightSM / 2;
  299. const handleLineWidth = token.lineWidth + increaseHandleWidth;
  300. const handleLineWidthHover = token.lineWidth + increaseHandleWidth * 1.5;
  301. const handleActiveColor = token.colorPrimary;
  302. const handleActiveOutlineColor = new FastColor(handleActiveColor).setA(0.2).toRgbString();
  303. return {
  304. controlSize,
  305. railSize: 4,
  306. handleSize: controlSize,
  307. handleSizeHover: controlSizeHover,
  308. dotSize: 8,
  309. handleLineWidth,
  310. handleLineWidthHover,
  311. railBg: token.colorFillTertiary,
  312. railHoverBg: token.colorFillSecondary,
  313. trackBg: token.colorPrimaryBorder,
  314. trackHoverBg: token.colorPrimaryBorderHover,
  315. handleColor: token.colorPrimaryBorder,
  316. handleActiveColor,
  317. handleActiveOutlineColor,
  318. handleColorDisabled: new FastColor(token.colorTextDisabled).onBackground(token.colorBgContainer).toHexString(),
  319. dotBorderColor: token.colorBorderSecondary,
  320. dotActiveBorderColor: token.colorPrimaryBorder,
  321. trackBgDisabled: token.colorBgContainerDisabled
  322. };
  323. };
  324. export default genStyleHooks('Slider', token => {
  325. const sliderToken = mergeToken(token, {
  326. marginPart: token.calc(token.controlHeight).sub(token.controlSize).div(2).equal(),
  327. marginFull: token.calc(token.controlSize).div(2).equal(),
  328. marginPartWithMark: token.calc(token.controlHeightLG).sub(token.controlSize).equal()
  329. });
  330. return [genBaseStyle(sliderToken), genHorizontalStyle(sliderToken), genVerticalStyle(sliderToken)];
  331. }, prepareComponentToken);