From 9c00c2aa9973e2684bf23ab6b240f6d7cc5c2aeb Mon Sep 17 00:00:00 2001 From: Guido Bouman Date: Thu, 3 May 2018 20:29:09 +0200 Subject: [PATCH] fix: snap to last panel in viewport when navigating up Take the posibility of more than 2 panels in the viewport into account. --- src/panelSnap.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/panelSnap.js b/src/panelSnap.js index 695d6c7..939676c 100644 --- a/src/panelSnap.js +++ b/src/panelSnap.js @@ -102,16 +102,12 @@ export default class PanelSnap { const panelsInViewport = getElementsInContainerViewport(this.container, this.panelList); - switch (panelsInViewport.length) { - case 1: - this.snapToPanel(panelsInViewport[0], delta < 0); - break; - case 2: - this.snapToPanel(panelsInViewport[delta > 0 ? 1 : 0], delta < 0); - break; - default: - throw new Error('PanelSnap could not find a snappable panel, aborting.'); + if (panelsInViewport.length === 0) { + throw new Error('PanelSnap could not find a snappable panel, aborting.'); } + + const targetIndex = delta > 0 ? panelsInViewport.length - 1 : 0; + this.snapToPanel(panelsInViewport[targetIndex], delta < 0); } snapToPanel(panel, toBottom = false) {