-
Notifications
You must be signed in to change notification settings - Fork 37
/
useMaxActions.jsx
36 lines (27 loc) · 1.02 KB
/
useMaxActions.jsx
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
import { useState } from 'react'
import throttle from 'lodash/throttle'
import breakpointDefs, { getBreakpointsStatus } from '../helpers/breakpoints'
import useEventListener from '../hooks/useEventListener'
import { computeMaxAction } from './helpers'
const huge = 1400
const large = 1200
const breakpoints = {
isHuge: [large + 1, huge],
...breakpointDefs
}
const getCurrentMaxAction = maxAction =>
computeMaxAction(maxAction, getBreakpointsStatus(breakpoints))
/**
* Compute the maximum number of actions to display according to the breakpoint
* @param {number|object} maxAction an number or a set of maximum for each breakpoint
* @returns the maximum number or undefined if there is no matching result
*/
const useMaxActions = maxAction => {
const [currentMax, setCurrentMax] = useState(getCurrentMaxAction(maxAction))
const handleResize = throttle(() => {
setCurrentMax(getCurrentMaxAction(maxAction))
}, 100)
useEventListener(window, 'resize', handleResize)
return currentMax
}
export default useMaxActions