clippedBox.tsx 4.4 KB

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