Only scroll to selected item in <LayerList/> if not already in view.

This commit is contained in:
orangemug 2020-04-06 15:30:16 +01:00
parent 3be6cb5926
commit 313b639a5f
3 changed files with 19 additions and 5 deletions

View file

@ -26,9 +26,7 @@ class AppLayout extends React.Component {
return <div className="maputnik-layout">
{this.props.toolbar}
<div className="maputnik-layout-list">
<ScrollContainer>
{this.props.layerList}
</ScrollContainer>
{this.props.layerList}
</div>
<div className="maputnik-layout-drawer">
<ScrollContainer>

View file

@ -45,6 +45,7 @@ class LayerListContainer extends React.Component {
constructor(props) {
super(props);
this.selectedItemRef = React.createRef();
this.scrollContainerRef = React.createRef();
this.state = {
collapsedGroups: {},
areAllGroupsExpanded: false,
@ -169,7 +170,19 @@ class LayerListContainer extends React.Component {
if (prevProps.selectedLayerIndex !== this.props.selectedLayerIndex) {
const selectedItemNode = this.selectedItemRef.current;
if (selectedItemNode && selectedItemNode.node) {
selectedItemNode.node.scrollIntoView();
const target = selectedItemNode.node;
const options = {
root: this.scrollContainerRef.current,
threshold: 1.0
}
const observer = new IntersectionObserver(entries => {
observer.unobserve(target);
if (entries.length > 0 && entries[0].intersectionRatio < 1) {
target.scrollIntoView();
}
}, options);
observer.observe(target);
}
}
}
@ -238,7 +251,7 @@ class LayerListContainer extends React.Component {
})
})
return <div className="maputnik-layer-list">
return <div className="maputnik-layer-list" ref={this.scrollContainerRef}>
<AddModal
layers={this.props.layers}
sources={this.props.sources}

View file

@ -1,5 +1,8 @@
// LAYER LIST
.maputnik-layer-list {
height: 100%;
overflow: auto;
&-header {
padding: $margin-2 $margin-2 $margin-3;