index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { resetComponent } from '../../style';
  2. import { initZoomMotion } from '../../style/motion';
  3. import getArrowStyle, { getArrowOffsetToken } from '../../style/placementArrow';
  4. import { getArrowToken } from '../../style/roundedArrow';
  5. import { genStyleHooks, mergeToken, PresetColors } from '../../theme/internal';
  6. const genBaseStyle = token => {
  7. const {
  8. componentCls,
  9. popoverColor,
  10. titleMinWidth,
  11. fontWeightStrong,
  12. innerPadding,
  13. boxShadowSecondary,
  14. colorTextHeading,
  15. borderRadiusLG,
  16. zIndexPopup,
  17. titleMarginBottom,
  18. colorBgElevated,
  19. popoverBg,
  20. titleBorderBottom,
  21. innerContentPadding,
  22. titlePadding
  23. } = token;
  24. return [{
  25. [componentCls]: Object.assign(Object.assign({}, resetComponent(token)), {
  26. position: 'absolute',
  27. top: 0,
  28. // use `left` to fix https://github.com/ant-design/ant-design/issues/39195
  29. left: {
  30. _skip_check_: true,
  31. value: 0
  32. },
  33. zIndex: zIndexPopup,
  34. fontWeight: 'normal',
  35. whiteSpace: 'normal',
  36. textAlign: 'start',
  37. cursor: 'auto',
  38. userSelect: 'text',
  39. // When use `autoArrow`, origin will follow the arrow position
  40. '--valid-offset-x': 'var(--arrow-offset-horizontal, var(--arrow-x))',
  41. transformOrigin: [`var(--valid-offset-x, 50%)`, `var(--arrow-y, 50%)`].join(' '),
  42. '--antd-arrow-background-color': colorBgElevated,
  43. width: 'max-content',
  44. maxWidth: '100vw',
  45. '&-rtl': {
  46. direction: 'rtl'
  47. },
  48. '&-hidden': {
  49. display: 'none'
  50. },
  51. [`${componentCls}-content`]: {
  52. position: 'relative'
  53. },
  54. [`${componentCls}-inner`]: {
  55. backgroundColor: popoverBg,
  56. backgroundClip: 'padding-box',
  57. borderRadius: borderRadiusLG,
  58. boxShadow: boxShadowSecondary,
  59. padding: innerPadding
  60. },
  61. [`${componentCls}-title`]: {
  62. minWidth: titleMinWidth,
  63. marginBottom: titleMarginBottom,
  64. color: colorTextHeading,
  65. fontWeight: fontWeightStrong,
  66. borderBottom: titleBorderBottom,
  67. padding: titlePadding
  68. },
  69. [`${componentCls}-inner-content`]: {
  70. color: popoverColor,
  71. padding: innerContentPadding
  72. }
  73. })
  74. },
  75. // Arrow Style
  76. getArrowStyle(token, 'var(--antd-arrow-background-color)'),
  77. // Pure Render
  78. {
  79. [`${componentCls}-pure`]: {
  80. position: 'relative',
  81. maxWidth: 'none',
  82. margin: token.sizePopupArrow,
  83. display: 'inline-block',
  84. [`${componentCls}-content`]: {
  85. display: 'inline-block'
  86. }
  87. }
  88. }];
  89. };
  90. const genColorStyle = token => {
  91. const {
  92. componentCls
  93. } = token;
  94. return {
  95. [componentCls]: PresetColors.map(colorKey => {
  96. const lightColor = token[`${colorKey}6`];
  97. return {
  98. [`&${componentCls}-${colorKey}`]: {
  99. '--antd-arrow-background-color': lightColor,
  100. [`${componentCls}-inner`]: {
  101. backgroundColor: lightColor
  102. },
  103. [`${componentCls}-arrow`]: {
  104. background: 'transparent'
  105. }
  106. }
  107. };
  108. })
  109. };
  110. };
  111. export const prepareComponentToken = token => {
  112. const {
  113. lineWidth,
  114. controlHeight,
  115. fontHeight,
  116. padding,
  117. wireframe,
  118. zIndexPopupBase,
  119. borderRadiusLG,
  120. marginXS,
  121. lineType,
  122. colorSplit,
  123. paddingSM
  124. } = token;
  125. const titlePaddingBlockDist = controlHeight - fontHeight;
  126. const popoverTitlePaddingBlockTop = titlePaddingBlockDist / 2;
  127. const popoverTitlePaddingBlockBottom = titlePaddingBlockDist / 2 - lineWidth;
  128. const popoverPaddingHorizontal = padding;
  129. return Object.assign(Object.assign(Object.assign({
  130. titleMinWidth: 177,
  131. zIndexPopup: zIndexPopupBase + 30
  132. }, getArrowToken(token)), getArrowOffsetToken({
  133. contentRadius: borderRadiusLG,
  134. limitVerticalRadius: true
  135. })), {
  136. // internal
  137. innerPadding: wireframe ? 0 : 12,
  138. titleMarginBottom: wireframe ? 0 : marginXS,
  139. titlePadding: wireframe ? `${popoverTitlePaddingBlockTop}px ${popoverPaddingHorizontal}px ${popoverTitlePaddingBlockBottom}px` : 0,
  140. titleBorderBottom: wireframe ? `${lineWidth}px ${lineType} ${colorSplit}` : 'none',
  141. innerContentPadding: wireframe ? `${paddingSM}px ${popoverPaddingHorizontal}px` : 0
  142. });
  143. };
  144. export default genStyleHooks('Popover', token => {
  145. const {
  146. colorBgElevated,
  147. colorText
  148. } = token;
  149. const popoverToken = mergeToken(token, {
  150. popoverBg: colorBgElevated,
  151. popoverColor: colorText
  152. });
  153. return [genBaseStyle(popoverToken), genColorStyle(popoverToken), initZoomMotion(popoverToken, 'zoom-big')];
  154. }, prepareComponentToken, {
  155. resetStyle: false,
  156. deprecatedTokens: [['width', 'titleMinWidth'], ['minWidth', 'titleMinWidth']]
  157. });