UNPKG

896 BJavaScriptView Raw
1export default function getContextAndFocus({ currentFocus, next, last }) {
2 // focus works in relation to the currentFocus, so try to do what the user wants
3 let focus = currentFocus + next.focus
4 if (focus < 0 || focus > last) {
5 // however, they may go out of boundaries in such cases we default to the last route
6 focus = last
7 }
8
9 // context works in relation to the new focus, so try to do what the user wants,
10 // it tells how many panels we should preferably show before the focus.
11 // we say preferably because it might be the case that it isn't possible to fit them all in the
12 // current viewport
13 let context = 0
14 if (typeof next.context === 'number') {
15 context = focus - next.context
16
17 if (context < 0 || context > focus) {
18 // if the value is out of boundaries, default to showing all
19 context = 0
20 }
21 }
22
23 return {
24 context,
25 focus,
26 }
27}