index.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import { TinyColor } from '@ctrl/tinycolor';
  3. import { genComponentStyleHook, mergeToken } from '../../theme/internal';
  4. import { resetComponent } from '../../style';
  5. // =============================== Base ===============================
  6. const genBaseStyle = token => {
  7. const {
  8. componentCls,
  9. controlSize,
  10. dotSize,
  11. marginFull,
  12. marginPart,
  13. colorFillContentHover
  14. } = token;
  15. return {
  16. [componentCls]: _extends(_extends({}, resetComponent(token)), {
  17. position: 'relative',
  18. height: controlSize,
  19. margin: `${marginPart}px ${marginFull}px`,
  20. padding: 0,
  21. cursor: 'pointer',
  22. touchAction: 'none',
  23. [`&-vertical`]: {
  24. margin: `${marginFull}px ${marginPart}px`
  25. },
  26. [`${componentCls}-rail`]: {
  27. position: 'absolute',
  28. backgroundColor: token.colorFillTertiary,
  29. borderRadius: token.borderRadiusXS,
  30. transition: `background-color ${token.motionDurationMid}`
  31. },
  32. [`${componentCls}-track`]: {
  33. position: 'absolute',
  34. backgroundColor: token.colorPrimaryBorder,
  35. borderRadius: token.borderRadiusXS,
  36. transition: `background-color ${token.motionDurationMid}`
  37. },
  38. '&:hover': {
  39. [`${componentCls}-rail`]: {
  40. backgroundColor: token.colorFillSecondary
  41. },
  42. [`${componentCls}-track`]: {
  43. backgroundColor: token.colorPrimaryBorderHover
  44. },
  45. [`${componentCls}-dot`]: {
  46. borderColor: colorFillContentHover
  47. },
  48. [`${componentCls}-handle::after`]: {
  49. boxShadow: `0 0 0 ${token.handleLineWidth}px ${token.colorPrimaryBorderHover}`
  50. },
  51. [`${componentCls}-dot-active`]: {
  52. borderColor: token.colorPrimary
  53. }
  54. },
  55. [`${componentCls}-handle`]: {
  56. position: 'absolute',
  57. width: token.handleSize,
  58. height: token.handleSize,
  59. outline: 'none',
  60. [`${componentCls}-dragging`]: {
  61. zIndex: 1
  62. },
  63. // 扩大选区
  64. '&::before': {
  65. content: '""',
  66. position: 'absolute',
  67. insetInlineStart: -token.handleLineWidth,
  68. insetBlockStart: -token.handleLineWidth,
  69. width: token.handleSize + token.handleLineWidth * 2,
  70. height: token.handleSize + token.handleLineWidth * 2,
  71. backgroundColor: 'transparent'
  72. },
  73. '&::after': {
  74. content: '""',
  75. position: 'absolute',
  76. insetBlockStart: 0,
  77. insetInlineStart: 0,
  78. width: token.handleSize,
  79. height: token.handleSize,
  80. backgroundColor: token.colorBgElevated,
  81. boxShadow: `0 0 0 ${token.handleLineWidth}px ${token.colorPrimaryBorder}`,
  82. borderRadius: '50%',
  83. cursor: 'pointer',
  84. transition: `
  85. inset-inline-start ${token.motionDurationMid},
  86. inset-block-start ${token.motionDurationMid},
  87. width ${token.motionDurationMid},
  88. height ${token.motionDurationMid},
  89. box-shadow ${token.motionDurationMid}
  90. `
  91. },
  92. '&:hover, &:active, &:focus': {
  93. '&::before': {
  94. insetInlineStart: -((token.handleSizeHover - token.handleSize) / 2 + token.handleLineWidthHover),
  95. insetBlockStart: -((token.handleSizeHover - token.handleSize) / 2 + token.handleLineWidthHover),
  96. width: token.handleSizeHover + token.handleLineWidthHover * 2,
  97. height: token.handleSizeHover + token.handleLineWidthHover * 2
  98. },
  99. '&::after': {
  100. boxShadow: `0 0 0 ${token.handleLineWidthHover}px ${token.colorPrimary}`,
  101. width: token.handleSizeHover,
  102. height: token.handleSizeHover,
  103. insetInlineStart: (token.handleSize - token.handleSizeHover) / 2,
  104. insetBlockStart: (token.handleSize - token.handleSizeHover) / 2
  105. }
  106. }
  107. },
  108. [`${componentCls}-mark`]: {
  109. position: 'absolute',
  110. fontSize: token.fontSize
  111. },
  112. [`${componentCls}-mark-text`]: {
  113. position: 'absolute',
  114. display: 'inline-block',
  115. color: token.colorTextDescription,
  116. textAlign: 'center',
  117. wordBreak: 'keep-all',
  118. cursor: 'pointer',
  119. userSelect: 'none',
  120. '&-active': {
  121. color: token.colorText
  122. }
  123. },
  124. [`${componentCls}-step`]: {
  125. position: 'absolute',
  126. background: 'transparent',
  127. pointerEvents: 'none'
  128. },
  129. [`${componentCls}-dot`]: {
  130. position: 'absolute',
  131. width: dotSize,
  132. height: dotSize,
  133. backgroundColor: token.colorBgElevated,
  134. border: `${token.handleLineWidth}px solid ${token.colorBorderSecondary}`,
  135. borderRadius: '50%',
  136. cursor: 'pointer',
  137. transition: `border-color ${token.motionDurationSlow}`,
  138. '&-active': {
  139. borderColor: token.colorPrimaryBorder
  140. }
  141. },
  142. [`&${componentCls}-disabled`]: {
  143. cursor: 'not-allowed',
  144. [`${componentCls}-rail`]: {
  145. backgroundColor: `${token.colorFillSecondary} !important`
  146. },
  147. [`${componentCls}-track`]: {
  148. backgroundColor: `${token.colorTextDisabled} !important`
  149. },
  150. [`
  151. ${componentCls}-dot
  152. `]: {
  153. backgroundColor: token.colorBgElevated,
  154. borderColor: token.colorTextDisabled,
  155. boxShadow: 'none',
  156. cursor: 'not-allowed'
  157. },
  158. [`${componentCls}-handle::after`]: {
  159. backgroundColor: token.colorBgElevated,
  160. cursor: 'not-allowed',
  161. width: token.handleSize,
  162. height: token.handleSize,
  163. boxShadow: `0 0 0 ${token.handleLineWidth}px ${new TinyColor(token.colorTextDisabled).onBackground(token.colorBgContainer).toHexString()}`,
  164. insetInlineStart: 0,
  165. insetBlockStart: 0
  166. },
  167. [`
  168. ${componentCls}-mark-text,
  169. ${componentCls}-dot
  170. `]: {
  171. cursor: `not-allowed !important`
  172. }
  173. }
  174. })
  175. };
  176. };
  177. // ============================ Horizontal ============================
  178. const genDirectionStyle = (token, horizontal) => {
  179. const {
  180. componentCls,
  181. railSize,
  182. handleSize,
  183. dotSize
  184. } = token;
  185. const railPadding = horizontal ? 'paddingBlock' : 'paddingInline';
  186. const full = horizontal ? 'width' : 'height';
  187. const part = horizontal ? 'height' : 'width';
  188. const handlePos = horizontal ? 'insetBlockStart' : 'insetInlineStart';
  189. const markInset = horizontal ? 'top' : 'insetInlineStart';
  190. return {
  191. [railPadding]: railSize,
  192. [part]: railSize * 3,
  193. [`${componentCls}-rail`]: {
  194. [full]: '100%',
  195. [part]: railSize
  196. },
  197. [`${componentCls}-track`]: {
  198. [part]: railSize
  199. },
  200. [`${componentCls}-handle`]: {
  201. [handlePos]: (railSize * 3 - handleSize) / 2
  202. },
  203. [`${componentCls}-mark`]: {
  204. // Reset all
  205. insetInlineStart: 0,
  206. top: 0,
  207. [markInset]: handleSize,
  208. [full]: '100%'
  209. },
  210. [`${componentCls}-step`]: {
  211. // Reset all
  212. insetInlineStart: 0,
  213. top: 0,
  214. [markInset]: railSize,
  215. [full]: '100%',
  216. [part]: railSize
  217. },
  218. [`${componentCls}-dot`]: {
  219. position: 'absolute',
  220. [handlePos]: (railSize - dotSize) / 2
  221. }
  222. };
  223. };
  224. // ============================ Horizontal ============================
  225. const genHorizontalStyle = token => {
  226. const {
  227. componentCls,
  228. marginPartWithMark
  229. } = token;
  230. return {
  231. [`${componentCls}-horizontal`]: _extends(_extends({}, genDirectionStyle(token, true)), {
  232. [`&${componentCls}-with-marks`]: {
  233. marginBottom: marginPartWithMark
  234. }
  235. })
  236. };
  237. };
  238. // ============================= Vertical =============================
  239. const genVerticalStyle = token => {
  240. const {
  241. componentCls
  242. } = token;
  243. return {
  244. [`${componentCls}-vertical`]: _extends(_extends({}, genDirectionStyle(token, false)), {
  245. height: '100%'
  246. })
  247. };
  248. };
  249. // ============================== Export ==============================
  250. export default genComponentStyleHook('Slider', token => {
  251. const sliderToken = mergeToken(token, {
  252. marginPart: (token.controlHeight - token.controlSize) / 2,
  253. marginFull: token.controlSize / 2,
  254. marginPartWithMark: token.controlHeightLG - token.controlSize
  255. });
  256. return [genBaseStyle(sliderToken), genHorizontalStyle(sliderToken), genVerticalStyle(sliderToken)];
  257. }, token => {
  258. // Handle line width is always width-er 1px
  259. const increaseHandleWidth = 1;
  260. const controlSize = token.controlHeightLG / 4;
  261. const controlSizeHover = token.controlHeightSM / 2;
  262. const handleLineWidth = token.lineWidth + increaseHandleWidth;
  263. const handleLineWidthHover = token.lineWidth + increaseHandleWidth * 3;
  264. return {
  265. controlSize,
  266. railSize: 4,
  267. handleSize: controlSize,
  268. handleSizeHover: controlSizeHover,
  269. dotSize: 8,
  270. handleLineWidth,
  271. handleLineWidthHover
  272. };
  273. });