PanelResizer.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { throttle } from 'lodash';
  2. import React, { PureComponent } from 'react';
  3. import Draggable, { DraggableEventHandler } from 'react-draggable';
  4. import { PanelModel } from '../state/PanelModel';
  5. interface Props {
  6. isEditing: boolean;
  7. render: (styles: object) => JSX.Element;
  8. panel: PanelModel;
  9. }
  10. interface State {
  11. editorHeight: number;
  12. }
  13. export class PanelResizer extends PureComponent<Props, State> {
  14. initialHeight: number = Math.floor(document.documentElement.scrollHeight * 0.3);
  15. prevEditorHeight?: number;
  16. throttledChangeHeight: (height: number) => void;
  17. throttledResizeDone?: () => void;
  18. noStyles: object = {};
  19. constructor(props: Props) {
  20. super(props);
  21. this.state = {
  22. editorHeight: this.initialHeight,
  23. };
  24. this.throttledChangeHeight = throttle(this.changeHeight, 20, { trailing: true });
  25. }
  26. get largestHeight() {
  27. return document.documentElement.scrollHeight * 0.9;
  28. }
  29. get smallestHeight() {
  30. return 100;
  31. }
  32. changeHeight = (height: number) => {
  33. const sh = this.smallestHeight;
  34. const lh = this.largestHeight;
  35. height = height < sh ? sh : height;
  36. height = height > lh ? lh : height;
  37. this.prevEditorHeight = this.state.editorHeight;
  38. this.setState({
  39. editorHeight: height,
  40. });
  41. };
  42. onDrag: DraggableEventHandler = (evt, data) => {
  43. const newHeight = this.state.editorHeight + data.y;
  44. this.throttledChangeHeight(newHeight);
  45. };
  46. render() {
  47. const { render, isEditing } = this.props;
  48. const { editorHeight } = this.state;
  49. return (
  50. <>
  51. {render(isEditing ? { height: editorHeight } : this.noStyles)}
  52. {isEditing && (
  53. <div className="panel-editor-container__resizer">
  54. <Draggable axis="y" grid={[100, 1]} onDrag={this.onDrag} position={{ x: 0, y: 0 }}>
  55. <div className="panel-editor-resizer">
  56. <div className="panel-editor-resizer__handle" />
  57. </div>
  58. </Draggable>
  59. </div>
  60. )}
  61. </>
  62. );
  63. }
  64. }