diff --git a/lib/ui/package.json b/lib/ui/package.json index d27cc5ddc0ec..1644db0cb442 100644 --- a/lib/ui/package.json +++ b/lib/ui/package.json @@ -28,6 +28,7 @@ "lodash.debounce": "^4.0.8", "lodash.pick": "^4.4.0", "lodash.sortby": "^4.7.0", + "lodash.throttle": "^4.1.1", "prop-types": "^15.6.1", "qs": "^6.5.1", "react-fuzzy": "^0.5.2", diff --git a/lib/ui/src/modules/ui/components/layout/index.js b/lib/ui/src/modules/ui/components/layout/index.js index e3e3864356f5..016670037f4e 100755 --- a/lib/ui/src/modules/ui/components/layout/index.js +++ b/lib/ui/src/modules/ui/components/layout/index.js @@ -2,6 +2,7 @@ import { localStorage, window } from 'global'; import PropTypes from 'prop-types'; import React from 'react'; import SplitPane from 'react-split-pane'; +import throttle from 'lodash.throttle'; import USplit from './usplit'; import Dimensions from './dimensions'; @@ -135,17 +136,18 @@ class Layout extends React.Component { isDragging: false, }; - this.onResize = this.onResize.bind(this); + this.throttledUpdatePreviewPanelState = throttle(this.updatePrevewPanelState.bind(this), 200); + this.throttledSaveSizes = throttle(this.saveSizes, 25); this.onDragStart = this.onDragStart.bind(this); this.onDragEnd = this.onDragEnd.bind(this); } componentDidMount() { - window.addEventListener('resize', this.onResize); + window.addEventListener('resize', this.throttledUpdatePreviewPanelState); } componentWillUnmount() { - window.removeEventListener('resize', this.onResize); + window.removeEventListener('resize', this.throttledUpdatePreviewPanelState); } onDragStart() { @@ -156,20 +158,25 @@ class Layout extends React.Component { this.setState({ isDragging: false }); } - onResize(pane, mode) { - return size => { - this.layerSizes[pane][mode] = size; - saveSizes(this.layerSizes); + onResize(pane, mode, size) { + this.throttledSaveSizes(pane, mode, size); + this.throttledUpdatePreviewPanelState(); + } - const { clientWidth, clientHeight } = this.previewPanelRef; + saveSizes(pane, mode, size) { + this.layerSizes[pane][mode] = size; + saveSizes(this.layerSizes); + } - this.setState({ - previewPanelDimensions: { - width: clientWidth, - height: clientHeight, - }, - }); - }; + updatePrevewPanelState() { + const { clientWidth, clientHeight } = this.previewPanelRef; + + this.setState({ + previewPanelDimensions: { + width: clientWidth, + height: clientHeight, + }, + }); } render() { @@ -216,7 +223,7 @@ class Layout extends React.Component { resizerStyle={storiesResizerStyle(showStoriesPanel, storiesPanelOnTop)} onDragStarted={this.onDragStart} onDragFinished={this.onDragEnd} - onChange={this.onResize('storiesPanel', storiesPanelOnTop ? 'top' : 'left')} + onChange={size => this.onResize('storiesPanel', storiesPanelOnTop ? 'top' : 'left', size)} >