clippedBox.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import {PureComponent} from 'react';
  2. import {findDOMNode} from 'react-dom';
  3. import {css} from '@emotion/react';
  4. import styled from '@emotion/styled';
  5. import color from 'color';
  6. import {Button} from 'sentry/components/button';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. type DefaultProps = {
  10. btnText?: string;
  11. /**
  12. * The "show more" button is 28px tall.
  13. * Do not clip if there is only a few more pixels
  14. */
  15. clipFlex?: number;
  16. clipHeight?: number;
  17. defaultClipped?: boolean;
  18. };
  19. type Props = {
  20. clipFlex: number;
  21. clipHeight: number;
  22. children?: React.ReactNode;
  23. className?: string;
  24. /**
  25. * When available replaces the default clipFade component
  26. */
  27. clipFade?: ({showMoreButton}: {showMoreButton: React.ReactNode}) => React.ReactNode;
  28. /**
  29. * Triggered when user clicks on the show more button
  30. */
  31. onReveal?: () => void;
  32. /**
  33. * Its trigged when the component is mounted and its height available
  34. */
  35. onSetRenderedHeight?: (renderedHeight: number) => void;
  36. renderedHeight?: number;
  37. title?: string;
  38. } & DefaultProps;
  39. type State = {
  40. isClipped: boolean;
  41. isRevealed: boolean;
  42. renderedHeight?: number;
  43. };
  44. class ClippedBox extends PureComponent<Props, State> {
  45. static defaultProps: DefaultProps = {
  46. defaultClipped: false,
  47. clipHeight: 200,
  48. clipFlex: 28,
  49. btnText: t('Show More'),
  50. };
  51. state: State = {
  52. isClipped: !!this.props.defaultClipped,
  53. isRevealed: false, // True once user has clicked "Show More" button
  54. renderedHeight: this.props.renderedHeight,
  55. };
  56. componentDidMount() {
  57. // eslint-disable-next-line react/no-find-dom-node
  58. const renderedHeight = (findDOMNode(this) as HTMLElement).offsetHeight;
  59. this.props.onSetRenderedHeight?.(renderedHeight);
  60. this.calcHeight(renderedHeight);
  61. }
  62. componentDidUpdate(_prevProps: Props, prevState: State) {
  63. if (prevState.renderedHeight !== this.props.renderedHeight) {
  64. this.setRenderedHeight();
  65. }
  66. if (prevState.renderedHeight !== this.state.renderedHeight) {
  67. this.calcHeight(this.state.renderedHeight);
  68. }
  69. if (this.state.isRevealed || !this.state.isClipped) {
  70. return;
  71. }
  72. if (!this.props.renderedHeight) {
  73. // eslint-disable-next-line react/no-find-dom-node
  74. const renderedHeight = (findDOMNode(this) as HTMLElement).offsetHeight;
  75. if (renderedHeight < this.props.clipHeight) {
  76. this.reveal();
  77. }
  78. }
  79. }
  80. setRenderedHeight() {
  81. this.setState({
  82. renderedHeight: this.props.renderedHeight,
  83. });
  84. }
  85. calcHeight(renderedHeight?: number) {
  86. if (!renderedHeight) {
  87. return;
  88. }
  89. if (
  90. !this.state.isClipped &&
  91. renderedHeight > this.props.clipHeight + this.props.clipFlex
  92. ) {
  93. /* eslint react/no-did-mount-set-state:0 */
  94. // okay if this causes re-render; cannot determine until
  95. // rendered first anyways
  96. this.setState({
  97. isClipped: true,
  98. });
  99. }
  100. }
  101. reveal = () => {
  102. const {onReveal} = this.props;
  103. this.setState({
  104. isClipped: false,
  105. isRevealed: true,
  106. });
  107. if (onReveal) {
  108. onReveal();
  109. }
  110. };
  111. handleClickReveal = (event: React.MouseEvent) => {
  112. event.stopPropagation();
  113. this.reveal();
  114. };
  115. render() {
  116. const {isClipped, isRevealed} = this.state;
  117. const {title, children, clipHeight, btnText, className, clipFade} = this.props;
  118. const showMoreButton = (
  119. <Button
  120. onClick={this.reveal}
  121. priority="primary"
  122. size="xs"
  123. aria-label={btnText ?? t('Show More')}
  124. >
  125. {btnText}
  126. </Button>
  127. );
  128. return (
  129. <Wrapper
  130. clipHeight={clipHeight}
  131. isClipped={isClipped}
  132. isRevealed={isRevealed}
  133. className={className}
  134. >
  135. {title && <Title>{title}</Title>}
  136. {children}
  137. {isClipped &&
  138. (clipFade?.({showMoreButton}) ?? <ClipFade>{showMoreButton}</ClipFade>)}
  139. </Wrapper>
  140. );
  141. }
  142. }
  143. export default ClippedBox;
  144. const Wrapper = styled('div', {
  145. shouldForwardProp: prop =>
  146. prop !== 'clipHeight' && prop !== 'isClipped' && prop !== 'isRevealed',
  147. })<State & {clipHeight: number}>`
  148. position: relative;
  149. padding: ${space(1.5)} 0;
  150. /* For "Show More" animation */
  151. ${p =>
  152. p.isRevealed &&
  153. css`
  154. transition: all 5s ease-in-out;
  155. max-height: 50000px;
  156. `};
  157. ${p =>
  158. p.isClipped &&
  159. css`
  160. max-height: ${p.clipHeight}px;
  161. overflow: hidden;
  162. `};
  163. `;
  164. const Title = styled('h5')`
  165. margin-bottom: ${space(1)};
  166. `;
  167. const ClipFade = styled('div')`
  168. position: absolute;
  169. left: 0;
  170. right: 0;
  171. bottom: 0;
  172. padding: 40px 0 0;
  173. background-image: linear-gradient(
  174. 180deg,
  175. ${p => color(p.theme.background).alpha(0.15).string()},
  176. ${p => p.theme.background}
  177. );
  178. text-align: center;
  179. border-bottom: ${space(1.5)} solid ${p => p.theme.background};
  180. /* Let pointer-events pass through ClipFade to visible elements underneath it */
  181. pointer-events: none;
  182. /* Ensure pointer-events trigger event listeners on "Expand" button */
  183. > * {
  184. pointer-events: auto;
  185. }
  186. `;