theme.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { unit } from '@ant-design/cssinjs';
  2. import { genFocusOutline } from '../../style';
  3. const accessibilityFocus = token => genFocusOutline(token);
  4. const getThemeStyle = (token, themeSuffix) => {
  5. const {
  6. componentCls,
  7. itemColor,
  8. itemSelectedColor,
  9. subMenuItemSelectedColor,
  10. groupTitleColor,
  11. itemBg,
  12. subMenuItemBg,
  13. itemSelectedBg,
  14. activeBarHeight,
  15. activeBarWidth,
  16. activeBarBorderWidth,
  17. motionDurationSlow,
  18. motionEaseInOut,
  19. motionEaseOut,
  20. itemPaddingInline,
  21. motionDurationMid,
  22. itemHoverColor,
  23. lineType,
  24. colorSplit,
  25. // Disabled
  26. itemDisabledColor,
  27. // Danger
  28. dangerItemColor,
  29. dangerItemHoverColor,
  30. dangerItemSelectedColor,
  31. dangerItemActiveBg,
  32. dangerItemSelectedBg,
  33. // Bg
  34. popupBg,
  35. itemHoverBg,
  36. itemActiveBg,
  37. menuSubMenuBg,
  38. // Horizontal
  39. horizontalItemSelectedColor,
  40. horizontalItemSelectedBg,
  41. horizontalItemBorderRadius,
  42. horizontalItemHoverBg
  43. } = token;
  44. return {
  45. [`${componentCls}-${themeSuffix}, ${componentCls}-${themeSuffix} > ${componentCls}`]: {
  46. color: itemColor,
  47. background: itemBg,
  48. [`&${componentCls}-root:focus-visible`]: Object.assign({}, accessibilityFocus(token)),
  49. // ======================== Item ========================
  50. [`${componentCls}-item`]: {
  51. '&-group-title, &-extra': {
  52. color: groupTitleColor
  53. }
  54. },
  55. [`${componentCls}-submenu-selected > ${componentCls}-submenu-title`]: {
  56. color: subMenuItemSelectedColor
  57. },
  58. [`${componentCls}-item, ${componentCls}-submenu-title`]: {
  59. color: itemColor,
  60. [`&:not(${componentCls}-item-disabled):focus-visible`]: Object.assign({}, accessibilityFocus(token))
  61. },
  62. // Disabled
  63. [`${componentCls}-item-disabled, ${componentCls}-submenu-disabled`]: {
  64. color: `${itemDisabledColor} !important`
  65. },
  66. // Hover
  67. [`${componentCls}-item:not(${componentCls}-item-selected):not(${componentCls}-submenu-selected)`]: {
  68. [`&:hover, > ${componentCls}-submenu-title:hover`]: {
  69. color: itemHoverColor
  70. }
  71. },
  72. [`&:not(${componentCls}-horizontal)`]: {
  73. [`${componentCls}-item:not(${componentCls}-item-selected)`]: {
  74. '&:hover': {
  75. backgroundColor: itemHoverBg
  76. },
  77. '&:active': {
  78. backgroundColor: itemActiveBg
  79. }
  80. },
  81. [`${componentCls}-submenu-title`]: {
  82. '&:hover': {
  83. backgroundColor: itemHoverBg
  84. },
  85. '&:active': {
  86. backgroundColor: itemActiveBg
  87. }
  88. }
  89. },
  90. // Danger - only Item has
  91. [`${componentCls}-item-danger`]: {
  92. color: dangerItemColor,
  93. [`&${componentCls}-item:hover`]: {
  94. [`&:not(${componentCls}-item-selected):not(${componentCls}-submenu-selected)`]: {
  95. color: dangerItemHoverColor
  96. }
  97. },
  98. [`&${componentCls}-item:active`]: {
  99. background: dangerItemActiveBg
  100. }
  101. },
  102. [`${componentCls}-item a`]: {
  103. '&, &:hover': {
  104. color: 'inherit'
  105. }
  106. },
  107. [`${componentCls}-item-selected`]: {
  108. color: itemSelectedColor,
  109. // Danger
  110. [`&${componentCls}-item-danger`]: {
  111. color: dangerItemSelectedColor
  112. },
  113. 'a, a:hover': {
  114. color: 'inherit'
  115. }
  116. },
  117. [`& ${componentCls}-item-selected`]: {
  118. backgroundColor: itemSelectedBg,
  119. // Danger
  120. [`&${componentCls}-item-danger`]: {
  121. backgroundColor: dangerItemSelectedBg
  122. }
  123. },
  124. [`&${componentCls}-submenu > ${componentCls}`]: {
  125. backgroundColor: menuSubMenuBg
  126. },
  127. // ===== 设置浮层的颜色 =======
  128. // !dark 模式会被popupBg 会被rest 为 darkPopupBg
  129. [`&${componentCls}-popup > ${componentCls}`]: {
  130. backgroundColor: popupBg
  131. },
  132. [`&${componentCls}-submenu-popup > ${componentCls}`]: {
  133. backgroundColor: popupBg
  134. },
  135. // ===== 设置浮层的颜色 end =======
  136. // ====================== Horizontal ======================
  137. [`&${componentCls}-horizontal`]: Object.assign(Object.assign({}, themeSuffix === 'dark' ? {
  138. borderBottom: 0
  139. } : {}), {
  140. [`> ${componentCls}-item, > ${componentCls}-submenu`]: {
  141. top: activeBarBorderWidth,
  142. marginTop: token.calc(activeBarBorderWidth).mul(-1).equal(),
  143. marginBottom: 0,
  144. borderRadius: horizontalItemBorderRadius,
  145. '&::after': {
  146. position: 'absolute',
  147. insetInline: itemPaddingInline,
  148. bottom: 0,
  149. borderBottom: `${unit(activeBarHeight)} solid transparent`,
  150. transition: `border-color ${motionDurationSlow} ${motionEaseInOut}`,
  151. content: '""'
  152. },
  153. '&:hover, &-active, &-open': {
  154. background: horizontalItemHoverBg,
  155. '&::after': {
  156. borderBottomWidth: activeBarHeight,
  157. borderBottomColor: horizontalItemSelectedColor
  158. }
  159. },
  160. '&-selected': {
  161. color: horizontalItemSelectedColor,
  162. backgroundColor: horizontalItemSelectedBg,
  163. '&:hover': {
  164. backgroundColor: horizontalItemSelectedBg
  165. },
  166. '&::after': {
  167. borderBottomWidth: activeBarHeight,
  168. borderBottomColor: horizontalItemSelectedColor
  169. }
  170. }
  171. }
  172. }),
  173. // ================== Inline & Vertical ===================
  174. //
  175. [`&${componentCls}-root`]: {
  176. [`&${componentCls}-inline, &${componentCls}-vertical`]: {
  177. borderInlineEnd: `${unit(activeBarBorderWidth)} ${lineType} ${colorSplit}`
  178. }
  179. },
  180. // ======================== Inline ========================
  181. [`&${componentCls}-inline`]: {
  182. // Sub
  183. [`${componentCls}-sub${componentCls}-inline`]: {
  184. background: subMenuItemBg
  185. },
  186. [`${componentCls}-item`]: {
  187. position: 'relative',
  188. '&::after': {
  189. position: 'absolute',
  190. insetBlock: 0,
  191. insetInlineEnd: 0,
  192. borderInlineEnd: `${unit(activeBarWidth)} solid ${itemSelectedColor}`,
  193. transform: 'scaleY(0.0001)',
  194. opacity: 0,
  195. transition: [`transform ${motionDurationMid} ${motionEaseOut}`, `opacity ${motionDurationMid} ${motionEaseOut}`].join(','),
  196. content: '""'
  197. },
  198. // Danger
  199. [`&${componentCls}-item-danger`]: {
  200. '&::after': {
  201. borderInlineEndColor: dangerItemSelectedColor
  202. }
  203. }
  204. },
  205. [`${componentCls}-selected, ${componentCls}-item-selected`]: {
  206. '&::after': {
  207. transform: 'scaleY(1)',
  208. opacity: 1,
  209. transition: [`transform ${motionDurationMid} ${motionEaseInOut}`, `opacity ${motionDurationMid} ${motionEaseInOut}`].join(',')
  210. }
  211. }
  212. }
  213. }
  214. };
  215. };
  216. export default getThemeStyle;