index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import { genComponentStyleHook, mergeToken } from '../../theme/internal';
  3. import { resetComponent, textEllipsis } from '../../style';
  4. // ============================== Mixins ==============================
  5. function getItemDisabledStyle(cls, token) {
  6. return {
  7. [`${cls}, ${cls}:hover, ${cls}:focus`]: {
  8. color: token.colorTextDisabled,
  9. cursor: 'not-allowed'
  10. }
  11. };
  12. }
  13. function getItemSelectedStyle(token) {
  14. return {
  15. backgroundColor: token.bgColorSelected,
  16. boxShadow: token.boxShadow
  17. };
  18. }
  19. const segmentedTextEllipsisCss = _extends({
  20. overflow: 'hidden'
  21. }, textEllipsis);
  22. // ============================== Styles ==============================
  23. const genSegmentedStyle = token => {
  24. const {
  25. componentCls
  26. } = token;
  27. return {
  28. [componentCls]: _extends(_extends(_extends(_extends(_extends({}, resetComponent(token)), {
  29. display: 'inline-block',
  30. padding: token.segmentedContainerPadding,
  31. color: token.labelColor,
  32. backgroundColor: token.bgColor,
  33. borderRadius: token.borderRadius,
  34. transition: `all ${token.motionDurationMid} ${token.motionEaseInOut}`,
  35. [`${componentCls}-group`]: {
  36. position: 'relative',
  37. display: 'flex',
  38. alignItems: 'stretch',
  39. justifyItems: 'flex-start',
  40. width: '100%'
  41. },
  42. // RTL styles
  43. [`&${componentCls}-rtl`]: {
  44. direction: 'rtl'
  45. },
  46. // block styles
  47. [`&${componentCls}-block`]: {
  48. display: 'flex'
  49. },
  50. [`&${componentCls}-block ${componentCls}-item`]: {
  51. flex: 1,
  52. minWidth: 0
  53. },
  54. // item styles
  55. [`${componentCls}-item`]: {
  56. position: 'relative',
  57. textAlign: 'center',
  58. cursor: 'pointer',
  59. transition: `color ${token.motionDurationMid} ${token.motionEaseInOut}`,
  60. borderRadius: token.borderRadiusSM,
  61. '&-selected': _extends(_extends({}, getItemSelectedStyle(token)), {
  62. color: token.labelColorHover
  63. }),
  64. '&::after': {
  65. content: '""',
  66. position: 'absolute',
  67. width: '100%',
  68. height: '100%',
  69. top: 0,
  70. insetInlineStart: 0,
  71. borderRadius: 'inherit',
  72. transition: `background-color ${token.motionDurationMid}`,
  73. pointerEvents: 'none'
  74. },
  75. [`&:hover:not(${componentCls}-item-selected):not(${componentCls}-item-disabled)`]: {
  76. color: token.labelColorHover,
  77. '&::after': {
  78. backgroundColor: token.bgColorHover
  79. }
  80. },
  81. '&-label': _extends({
  82. minHeight: token.controlHeight - token.segmentedContainerPadding * 2,
  83. lineHeight: `${token.controlHeight - token.segmentedContainerPadding * 2}px`,
  84. padding: `0 ${token.segmentedPaddingHorizontal}px`
  85. }, segmentedTextEllipsisCss),
  86. // syntactic sugar to add `icon` for Segmented Item
  87. '&-icon + *': {
  88. marginInlineStart: token.marginSM / 2
  89. },
  90. '&-input': {
  91. position: 'absolute',
  92. insetBlockStart: 0,
  93. insetInlineStart: 0,
  94. width: 0,
  95. height: 0,
  96. opacity: 0,
  97. pointerEvents: 'none'
  98. }
  99. },
  100. // thumb styles
  101. [`${componentCls}-thumb`]: _extends(_extends({}, getItemSelectedStyle(token)), {
  102. position: 'absolute',
  103. insetBlockStart: 0,
  104. insetInlineStart: 0,
  105. width: 0,
  106. height: '100%',
  107. padding: `${token.paddingXXS}px 0`,
  108. borderRadius: token.borderRadiusSM,
  109. [`& ~ ${componentCls}-item:not(${componentCls}-item-selected):not(${componentCls}-item-disabled)::after`]: {
  110. backgroundColor: 'transparent'
  111. }
  112. }),
  113. // size styles
  114. [`&${componentCls}-lg`]: {
  115. borderRadius: token.borderRadiusLG,
  116. [`${componentCls}-item-label`]: {
  117. minHeight: token.controlHeightLG - token.segmentedContainerPadding * 2,
  118. lineHeight: `${token.controlHeightLG - token.segmentedContainerPadding * 2}px`,
  119. padding: `0 ${token.segmentedPaddingHorizontal}px`,
  120. fontSize: token.fontSizeLG
  121. },
  122. [`${componentCls}-item, ${componentCls}-thumb`]: {
  123. borderRadius: token.borderRadius
  124. }
  125. },
  126. [`&${componentCls}-sm`]: {
  127. borderRadius: token.borderRadiusSM,
  128. [`${componentCls}-item-label`]: {
  129. minHeight: token.controlHeightSM - token.segmentedContainerPadding * 2,
  130. lineHeight: `${token.controlHeightSM - token.segmentedContainerPadding * 2}px`,
  131. padding: `0 ${token.segmentedPaddingHorizontalSM}px`
  132. },
  133. [`${componentCls}-item, ${componentCls}-thumb`]: {
  134. borderRadius: token.borderRadiusXS
  135. }
  136. }
  137. }), getItemDisabledStyle(`&-disabled ${componentCls}-item`, token)), getItemDisabledStyle(`${componentCls}-item-disabled`, token)), {
  138. // transition effect when `appear-active`
  139. [`${componentCls}-thumb-motion-appear-active`]: {
  140. transition: `transform ${token.motionDurationSlow} ${token.motionEaseInOut}, width ${token.motionDurationSlow} ${token.motionEaseInOut}`,
  141. willChange: 'transform, width'
  142. }
  143. })
  144. };
  145. };
  146. // ============================== Export ==============================
  147. export default genComponentStyleHook('Segmented', token => {
  148. const {
  149. lineWidthBold,
  150. lineWidth,
  151. colorTextLabel,
  152. colorText,
  153. colorFillSecondary,
  154. colorBgLayout,
  155. colorBgElevated
  156. } = token;
  157. const segmentedToken = mergeToken(token, {
  158. segmentedPaddingHorizontal: token.controlPaddingHorizontal - lineWidth,
  159. segmentedPaddingHorizontalSM: token.controlPaddingHorizontalSM - lineWidth,
  160. segmentedContainerPadding: lineWidthBold,
  161. labelColor: colorTextLabel,
  162. labelColorHover: colorText,
  163. bgColor: colorBgLayout,
  164. bgColorHover: colorFillSecondary,
  165. bgColorSelected: colorBgElevated
  166. });
  167. return [genSegmentedStyle(segmentedToken)];
  168. });