-
Notifications
You must be signed in to change notification settings - Fork 8
/
resolveSlidesNumber.js
38 lines (35 loc) · 1.32 KB
/
resolveSlidesNumber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* This function returns the number of slides to show depending on the size of the window.
* If you pass a number it just returns that number.
* If you pass an object it will return the number of slides of the closest breakpoint to the size of the window.
* If you pass for example
* perPage = {
* 400: 2,
* 1000: 3
* }
*
* If the size of the window is something between 400px and 999px if will return 2,
* if it is 1000px or bigger, it will return 3, and if it is smaller than 400px it
* will return the default value; that is, 1.
* @param {number|object} perPage
* @param {number|undefined} minPerPage
*/
function resolveSlidesNumber(minPerPage, perPage, runtime) {
let result = minPerPage || 1
if (typeof perPage === 'number') {
result = perPage
} else if (typeof perPage === 'object') {
const isMobile = runtime && runtime.hints && runtime.hints.mobile
const innerWidth = window && window.innerWidth
/** If it's on SSR, use placeholder screen sizes to get an approximate
* guess of how many items are displayed per page */
const windowSize = innerWidth || (isMobile ? 320 : 1024)
for (const viewport in perPage) {
if (windowSize >= viewport) {
result = perPage[viewport]
}
}
}
return result < minPerPage ? minPerPage : result
}
export default resolveSlidesNumber