index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { unit } from '@ant-design/cssinjs';
  2. import { genStyleHooks, mergeToken } from '../../theme/internal';
  3. // ============================== Row-Shared ==============================
  4. const genGridRowStyle = token => {
  5. const {
  6. componentCls
  7. } = token;
  8. return {
  9. // Grid system
  10. [componentCls]: {
  11. display: 'flex',
  12. flexFlow: 'row wrap',
  13. minWidth: 0,
  14. '&::before, &::after': {
  15. display: 'flex'
  16. },
  17. '&-no-wrap': {
  18. flexWrap: 'nowrap'
  19. },
  20. // The origin of the X-axis
  21. '&-start': {
  22. justifyContent: 'flex-start'
  23. },
  24. // The center of the X-axis
  25. '&-center': {
  26. justifyContent: 'center'
  27. },
  28. // The opposite of the X-axis
  29. '&-end': {
  30. justifyContent: 'flex-end'
  31. },
  32. '&-space-between': {
  33. justifyContent: 'space-between'
  34. },
  35. '&-space-around': {
  36. justifyContent: 'space-around'
  37. },
  38. '&-space-evenly': {
  39. justifyContent: 'space-evenly'
  40. },
  41. // Align at the top
  42. '&-top': {
  43. alignItems: 'flex-start'
  44. },
  45. // Align at the center
  46. '&-middle': {
  47. alignItems: 'center'
  48. },
  49. '&-bottom': {
  50. alignItems: 'flex-end'
  51. }
  52. }
  53. };
  54. };
  55. // ============================== Col-Shared ==============================
  56. const genGridColStyle = token => {
  57. const {
  58. componentCls
  59. } = token;
  60. return {
  61. // Grid system
  62. [componentCls]: {
  63. position: 'relative',
  64. maxWidth: '100%',
  65. // Prevent columns from collapsing when empty
  66. minHeight: 1
  67. }
  68. };
  69. };
  70. const genLoopGridColumnsStyle = (token, sizeCls) => {
  71. const {
  72. prefixCls,
  73. componentCls,
  74. gridColumns
  75. } = token;
  76. const gridColumnsStyle = {};
  77. for (let i = gridColumns; i >= 0; i--) {
  78. if (i === 0) {
  79. gridColumnsStyle[`${componentCls}${sizeCls}-${i}`] = {
  80. display: 'none'
  81. };
  82. gridColumnsStyle[`${componentCls}-push-${i}`] = {
  83. insetInlineStart: 'auto'
  84. };
  85. gridColumnsStyle[`${componentCls}-pull-${i}`] = {
  86. insetInlineEnd: 'auto'
  87. };
  88. gridColumnsStyle[`${componentCls}${sizeCls}-push-${i}`] = {
  89. insetInlineStart: 'auto'
  90. };
  91. gridColumnsStyle[`${componentCls}${sizeCls}-pull-${i}`] = {
  92. insetInlineEnd: 'auto'
  93. };
  94. gridColumnsStyle[`${componentCls}${sizeCls}-offset-${i}`] = {
  95. marginInlineStart: 0
  96. };
  97. gridColumnsStyle[`${componentCls}${sizeCls}-order-${i}`] = {
  98. order: 0
  99. };
  100. } else {
  101. gridColumnsStyle[`${componentCls}${sizeCls}-${i}`] = [
  102. // https://github.com/ant-design/ant-design/issues/44456
  103. // Form set `display: flex` on Col which will override `display: block`.
  104. // Let's get it from css variable to support override.
  105. {
  106. ['--ant-display']: 'block',
  107. // Fallback to display if variable not support
  108. display: 'block'
  109. }, {
  110. display: 'var(--ant-display)',
  111. flex: `0 0 ${i / gridColumns * 100}%`,
  112. maxWidth: `${i / gridColumns * 100}%`
  113. }];
  114. gridColumnsStyle[`${componentCls}${sizeCls}-push-${i}`] = {
  115. insetInlineStart: `${i / gridColumns * 100}%`
  116. };
  117. gridColumnsStyle[`${componentCls}${sizeCls}-pull-${i}`] = {
  118. insetInlineEnd: `${i / gridColumns * 100}%`
  119. };
  120. gridColumnsStyle[`${componentCls}${sizeCls}-offset-${i}`] = {
  121. marginInlineStart: `${i / gridColumns * 100}%`
  122. };
  123. gridColumnsStyle[`${componentCls}${sizeCls}-order-${i}`] = {
  124. order: i
  125. };
  126. }
  127. }
  128. // Flex CSS Var
  129. gridColumnsStyle[`${componentCls}${sizeCls}-flex`] = {
  130. flex: `var(--${prefixCls}${sizeCls}-flex)`
  131. };
  132. return gridColumnsStyle;
  133. };
  134. const genGridStyle = (token, sizeCls) => genLoopGridColumnsStyle(token, sizeCls);
  135. const genGridMediaStyle = (token, screenSize, sizeCls) => ({
  136. [`@media (min-width: ${unit(screenSize)})`]: Object.assign({}, genGridStyle(token, sizeCls))
  137. });
  138. export const prepareRowComponentToken = () => ({});
  139. export const prepareColComponentToken = () => ({});
  140. // ============================== Export ==============================
  141. export const useRowStyle = genStyleHooks('Grid', genGridRowStyle, prepareRowComponentToken);
  142. export const getMediaSize = token => {
  143. const mediaSizesMap = {
  144. xs: token.screenXSMin,
  145. sm: token.screenSMMin,
  146. md: token.screenMDMin,
  147. lg: token.screenLGMin,
  148. xl: token.screenXLMin,
  149. xxl: token.screenXXLMin
  150. };
  151. return mediaSizesMap;
  152. };
  153. export const useColStyle = genStyleHooks('Grid', token => {
  154. const gridToken = mergeToken(token, {
  155. gridColumns: 24 // Row is divided into 24 parts in Grid
  156. });
  157. const gridMediaSizesMap = getMediaSize(gridToken);
  158. delete gridMediaSizesMap.xs;
  159. return [genGridColStyle(gridToken), genGridStyle(gridToken, ''), genGridStyle(gridToken, '-xs'), Object.keys(gridMediaSizesMap).map(key => genGridMediaStyle(gridToken, gridMediaSizesMap[key], `-${key}`)).reduce((pre, cur) => Object.assign(Object.assign({}, pre), cur), {})];
  160. }, prepareColComponentToken);