From fcad636f851dadfe32a8fe9f3b9f7a08714f5865 Mon Sep 17 00:00:00 2001 From: orangemug Date: Sat, 4 Apr 2020 15:41:35 +0100 Subject: [PATCH] Scroll selected into view --- src/components/layers/LayerList.jsx | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/components/layers/LayerList.jsx b/src/components/layers/LayerList.jsx index 8d60bb4..a15bc54 100644 --- a/src/components/layers/LayerList.jsx +++ b/src/components/layers/LayerList.jsx @@ -42,11 +42,15 @@ class LayerListContainer extends React.Component { onLayerSelect: () => {}, } - state = { - collapsedGroups: {}, - areAllGroupsExpanded: false, - isOpen: { - add: false, + constructor(props) { + super(props); + this.selectedItemRef = React.createRef(); + this.state = { + collapsedGroups: {}, + areAllGroupsExpanded: false, + isOpen: { + add: false, + } } } @@ -161,6 +165,13 @@ class LayerListContainer extends React.Component { return propsChanged; } + componentDidUpdate () { + const selectedItemNode = this.selectedItemRef.current; + if (selectedItemNode && selectedItemNode.node) { + selectedItemNode.node.scrollIntoView(); + } + } + render() { const listItems = [] @@ -189,6 +200,11 @@ class LayerListContainer extends React.Component { ); }); + const additionalProps = {}; + if (idx === this.props.selectedLayerIndex) { + additionalProps.ref = this.selectedItemRef; + } + const listItem = 1 && this.isCollapsed(groupPrefix, groupIdx) && idx !== this.props.selectedLayerIndex, @@ -205,6 +221,7 @@ class LayerListContainer extends React.Component { onLayerDestroy={this.props.onLayerDestroy.bind(this)} onLayerCopy={this.props.onLayerCopy.bind(this)} onLayerVisibilityToggle={this.props.onLayerVisibilityToggle.bind(this)} + {...additionalProps} /> listItems.push(listItem) idx += 1