-
Notifications
You must be signed in to change notification settings - Fork 81
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
1 parent
398c28d
commit 8f8a0b0
Showing
8 changed files
with
78 additions
and
77 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,68 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { history } from '@edx/frontend-platform'; | ||
|
||
export const useScrollToHashElement = ({ isLoading }: { isLoading: boolean }) => { | ||
const [elementWithHash, setElementWithHash] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const currentHash = window.location.hash.substring(1); | ||
|
||
if (currentHash) { | ||
const element = document.getElementById(currentHash); | ||
if (element) { | ||
element.scrollIntoView(); | ||
history.replace({ hash: '' }); | ||
} | ||
setElementWithHash(currentHash); | ||
} | ||
}, [isLoading]); | ||
|
||
return { elementWithHash }; | ||
}; | ||
|
||
export const useEscapeClick = ({ onEscape, dependency }: { onEscape: () => void, dependency: any }) => { | ||
useEffect(() => { | ||
const handleEscapeClick = (event: KeyboardEvent) => { | ||
if (event.key === 'Escape') { | ||
onEscape(); | ||
} | ||
}; | ||
|
||
window.addEventListener('keydown', handleEscapeClick); | ||
|
||
return () => { | ||
window.removeEventListener('keydown', handleEscapeClick); | ||
}; | ||
}, [dependency]); | ||
}; | ||
|
||
/** | ||
* Hook which loads next page of items on scroll | ||
*/ | ||
export const useLoadOnScroll = ( | ||
hasNextPage: boolean | undefined, | ||
isFetchingNextPage: boolean, | ||
fetchNextPage: () => void, | ||
enabled: boolean, | ||
) => { | ||
useEffect(() => { | ||
if (enabled) { | ||
const onscroll = () => { | ||
// Verify the position of the scroll to implementa a infinite scroll. | ||
// Used `loadLimit` to fetch next page before reach the end of the screen. | ||
const loadLimit = 300; | ||
const scrolledTo = window.scrollY + window.innerHeight; | ||
const scrollDiff = document.body.scrollHeight - scrolledTo; | ||
const isNearToBottom = scrollDiff <= loadLimit; | ||
if (isNearToBottom && hasNextPage && !isFetchingNextPage) { | ||
fetchNextPage(); | ||
} | ||
}; | ||
window.addEventListener('scroll', onscroll); | ||
return () => { | ||
window.removeEventListener('scroll', onscroll); | ||
}; | ||
} | ||
return () => { }; | ||
}, [hasNextPage, isFetchingNextPage, fetchNextPage]); | ||
}; |
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
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