platformList.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import {css} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import {PlatformIcon} from 'platformicons';
  4. import Tooltip from 'app/components/tooltip';
  5. import {PlatformKey} from 'app/data/platformCategories';
  6. import {tn} from 'app/locale';
  7. import getPlatformName from 'app/utils/getPlatformName';
  8. import {Theme} from 'app/utils/theme';
  9. type Props = {
  10. platforms?: PlatformKey[];
  11. direction?: 'right' | 'left';
  12. /**
  13. * Maximum number of platform icons to display
  14. */
  15. max?: number;
  16. /**
  17. * Platform icon size in pixels
  18. */
  19. size?: number;
  20. /**
  21. * Will set container width to be size of having `this.props.max` icons
  22. * This is good for lists where the project name is displayed
  23. */
  24. consistentWidth?: boolean;
  25. /**
  26. * If true and if the number of children is greater than the max prop,
  27. * a counter will be displayed at the end of the stack
  28. */
  29. showCounter?: boolean;
  30. className?: string;
  31. };
  32. type WrapperProps = Required<
  33. Pick<Props, 'showCounter' | 'size' | 'direction' | 'consistentWidth' | 'max'>
  34. >;
  35. function PlatformList({
  36. platforms = [],
  37. direction = 'right',
  38. max = 3,
  39. size = 16,
  40. consistentWidth = false,
  41. showCounter = false,
  42. className,
  43. }: Props) {
  44. const visiblePlatforms = platforms.slice(0, max);
  45. const numNotVisiblePlatforms = platforms.length - visiblePlatforms.length;
  46. const displayCounter = showCounter && !!numNotVisiblePlatforms;
  47. function renderContent() {
  48. if (!platforms.length) {
  49. return <StyledPlatformIcon size={size} platform="default" />;
  50. }
  51. const platformIcons = visiblePlatforms.slice().reverse();
  52. if (displayCounter) {
  53. return (
  54. <InnerWrapper>
  55. <PlatformIcons>
  56. {platformIcons.map((visiblePlatform, index) => (
  57. <Tooltip
  58. key={visiblePlatform + index}
  59. title={getPlatformName(visiblePlatform)}
  60. containerDisplayMode="inline-flex"
  61. >
  62. <StyledPlatformIcon platform={visiblePlatform} size={size} />
  63. </Tooltip>
  64. ))}
  65. </PlatformIcons>
  66. <Tooltip
  67. title={tn('%s other platform', '%s other platforms', numNotVisiblePlatforms)}
  68. containerDisplayMode="inline-flex"
  69. >
  70. <Counter>
  71. {numNotVisiblePlatforms}
  72. <Plus>{'\u002B'}</Plus>
  73. </Counter>
  74. </Tooltip>
  75. </InnerWrapper>
  76. );
  77. }
  78. return (
  79. <PlatformIcons>
  80. {platformIcons.map((visiblePlatform, index) => (
  81. <StyledPlatformIcon
  82. key={visiblePlatform + index}
  83. platform={visiblePlatform}
  84. size={size}
  85. />
  86. ))}
  87. </PlatformIcons>
  88. );
  89. }
  90. return (
  91. <Wrapper
  92. consistentWidth={consistentWidth}
  93. className={className}
  94. size={size}
  95. showCounter={displayCounter}
  96. direction={direction}
  97. max={max}
  98. >
  99. {renderContent()}
  100. </Wrapper>
  101. );
  102. }
  103. export default PlatformList;
  104. function getOverlapWidth(size: number) {
  105. return Math.round(size / 4);
  106. }
  107. const commonStyles = ({theme}: {theme: Theme}) => `
  108. cursor: default;
  109. border-radius: ${theme.borderRadius};
  110. box-shadow: 0 0 0 1px ${theme.background};
  111. :hover {
  112. z-index: 1;
  113. }
  114. `;
  115. const PlatformIcons = styled('div')`
  116. display: flex;
  117. `;
  118. const InnerWrapper = styled('div')`
  119. display: flex;
  120. position: relative;
  121. `;
  122. const Plus = styled('span')`
  123. font-size: 10px;
  124. `;
  125. const StyledPlatformIcon = styled(PlatformIcon)`
  126. ${p => commonStyles(p)};
  127. `;
  128. const Counter = styled('div')`
  129. ${p => commonStyles(p)};
  130. display: flex;
  131. align-items: center;
  132. justify-content: center;
  133. text-align: center;
  134. font-weight: 600;
  135. font-size: ${p => p.theme.fontSizeExtraSmall};
  136. background-color: ${p => p.theme.gray200};
  137. color: ${p => p.theme.gray300};
  138. padding: 0 1px;
  139. position: absolute;
  140. right: -1px;
  141. `;
  142. const Wrapper = styled('div')<WrapperProps>`
  143. display: flex;
  144. flex-shrink: 0;
  145. justify-content: ${p => (p.direction === 'right' ? 'flex-end' : 'flex-start')};
  146. ${p =>
  147. p.consistentWidth && `width: ${p.size + (p.max - 1) * getOverlapWidth(p.size)}px;`};
  148. ${PlatformIcons} {
  149. ${p =>
  150. p.showCounter
  151. ? css`
  152. z-index: 1;
  153. flex-direction: row-reverse;
  154. > * :not(:first-child) {
  155. margin-right: ${p.size * -1 + getOverlapWidth(p.size)}px;
  156. }
  157. `
  158. : css`
  159. > * :not(:first-child) {
  160. margin-left: ${p.size * -1 + getOverlapWidth(p.size)}px;
  161. }
  162. `}
  163. }
  164. ${InnerWrapper} {
  165. padding-right: ${p => p.size / 2 + 1}px;
  166. }
  167. ${Counter} {
  168. height: ${p => p.size}px;
  169. min-width: ${p => p.size}px;
  170. }
  171. `;