Skip to content

Commit d1a514e

Browse files
committed
feat: make panel target selection less naive
Take the option of a single panel in the viewport into account. Warn about PanelSnap not supporting space between panels yet, and make a best effort snap back to the visible panel.
1 parent e69e20d commit d1a514e

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/panelSnap.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,25 @@ export default class PanelSnap {
102102
}
103103

104104
const panelsInViewport = getElementsInContainerViewport(this.container, this.panelList);
105-
106105
if (panelsInViewport.length === 0) {
107106
throw new Error('PanelSnap could not find a snappable panel, aborting.');
108107
}
109108

110-
const targetIndex = delta > 0 ? panelsInViewport.length - 1 : 0;
111-
this.snapToPanel(panelsInViewport[targetIndex], delta < 0);
109+
if (panelsInViewport.length > 1) {
110+
const targetIndex = delta > 0 ? 1 : panelsInViewport.length - 2;
111+
this.snapToPanel(panelsInViewport[targetIndex], delta < 0);
112+
return;
113+
}
114+
115+
const visiblePanel = panelsInViewport[0];
116+
if (elementFillsContainer(this.container, visiblePanel)) {
117+
return;
118+
}
119+
120+
// TODO: Only one partial panel in viewport, add support for space between panels?
121+
// eslint-disable-next-line no-console
122+
console.error('PanelSnap does not support space between panels, snapping back.');
123+
this.snapToPanel(visiblePanel, delta > 0);
112124
}
113125

114126
snapToPanel(panel, toBottom = false) {
@@ -118,10 +130,6 @@ export default class PanelSnap {
118130

119131
this.activatePanel(panel);
120132

121-
if (elementFillsContainer(this.container, panel)) {
122-
return;
123-
}
124-
125133
if (this.animation) {
126134
this.animation.stop();
127135
}

0 commit comments

Comments
 (0)