Last active
July 8, 2019 09:57
-
-
Save antoniocapelo/95dc3e25cd495191d78afac905d344d4 to your computer and use it in GitHub Desktop.
Revisions
-
antoniocapelo revised this gist
Mar 30, 2019 . 1 changed file with 7 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import { useState, useRef, useEffect } from 'react'; export function useScroll() { const [scrollY, setScrollY] = useState(0); const [progress, setProgress] = useState(0); const previousScrollY = useRef(); const [scrollDirection, setScrollDirection] = useState("down"); @@ -19,19 +20,23 @@ export function useScroll() { const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; setScrollY(scrollTop); setProgress( scrollTop / (document.documentElement.offsetHeight - window.innerHeight) ); const direction = previousScrollY.current < scrollTop ? "down" : "up"; setScrollDirection(direction); } window.addEventListener("scroll", listener, { passive: true }); return () => { window.removeEventListener("scroll", listener, { passive: true }); }; }, []); return { scrollY, progress, scrollDirection }; } -
antoniocapelo revised this gist
Mar 30, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ * Usage: const { scrollY, scrollDirection } = useScrollListener(); */ import { useState, useRef, useEffect } from 'react'; export function useScroll() { const [scrollY, setScrollY] = useState(0); -
antoniocapelo revised this gist
Mar 30, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ * Usage: const { scrollY, scrollDirection } = useScrollListener(); */ import { useState, useEffect } from 'react'; export function useScroll() { const [scrollY, setScrollY] = useState(0); -
antoniocapelo revised this gist
Mar 30, 2019 . 1 changed file with 20 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,41 +5,33 @@ import { useReducer, useEffect } from 'react'; export function useScroll() { const [scrollY, setScrollY] = useState(0); const previousScrollY = useRef(); const [scrollDirection, setScrollDirection] = useState("down"); useEffect(() => { previousScrollY.current = scrollY; }); useEffect(() => { function listener() { const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; setScrollY(scrollTop); const direction = previousScrollY.current < scrollTop ? "down" : "up"; setScrollDirection(direction); } window.addEventListener("scroll", listener, false); return () => { window.removeEventListener("scroll", listener, false); }; }, []); return { scrollY, scrollDirection }; } -
antoniocapelo created this gist
Mar 12, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ /** * useScrollListener hook * Usage: const { scrollY, scrollDirection } = useScrollListener(); */ import { useReducer, useEffect } from 'react'; function reducer(state, action) { switch (action.type) { case 'set': return { scrollY: action.payload.scrollY, scrollDirection: action.payload.scrollY > state.scrollY ? 'down' : 'up' }; } } export function useScrollListener() { const [state, dispatch] = useReducer(reducer, { scrollY: 0, scrollDirection: 'down' }); function listener() { const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; dispatch({ type: 'set', payload: { scrollY: scrollTop } }); } useEffect(() => { window.addEventListener('scroll', listener, false); return () => { window.removeEventListener('scroll', listener, false); }; }, []); return { ...state }; }