-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3945 from serlo/replace-mailchimp-popup-code
feat(frontend): add native newsletter popup
- Loading branch information
Showing
2 changed files
with
81 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useEffect, useRef } from 'react' | ||
|
||
export function useScrollUpTrigger(handleScroll: () => void, active: boolean) { | ||
const throttleTimer = useRef<NodeJS.Timeout | false>() | ||
const oldScrollY = useRef<number>(0) | ||
|
||
useEffect(() => { | ||
const handler = () => { | ||
const isUp = window.scrollY < oldScrollY.current | ||
if (isUp) handleScroll() | ||
oldScrollY.current = window.scrollY | ||
} | ||
|
||
const cleanup = () => { | ||
window.removeEventListener('scroll', handler) | ||
if (throttleTimer.current) clearTimeout(throttleTimer.current) | ||
} | ||
|
||
if (active) { | ||
window.addEventListener('scroll', handler, { passive: true }) | ||
} else cleanup() | ||
|
||
return () => cleanup() | ||
}, [active, handleScroll]) | ||
} |