-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useCallback, useEffect } from 'react'; | ||
|
||
import { useCalendarHourHeight } from './calendar'; | ||
|
||
const MIN_HEIGHT = 3; | ||
const MAX_HEIGHT = 10; | ||
const SPEED = 0.08; | ||
|
||
export const useZoom = () => { | ||
const setCalendarHeight = useCalendarHourHeight((s) => s.setHeight); | ||
|
||
const onZoom = useCallback( | ||
(e: WheelEvent) => { | ||
// Check if user is scrolling | ||
if (e.deltaY % 1 === 0) return; | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
setCalendarHeight((previousHeight) => { | ||
const newHeight = previousHeight - e.deltaY * SPEED; | ||
const height = Math.min(Math.max(newHeight, MIN_HEIGHT), MAX_HEIGHT); | ||
return height; | ||
}); | ||
}, | ||
[setCalendarHeight], | ||
); | ||
|
||
useEffect(() => { | ||
document.addEventListener('wheel', onZoom, { passive: false }); | ||
return () => { | ||
document.removeEventListener('wheel', onZoom); | ||
}; | ||
}, [onZoom]); | ||
}; |