-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent initial rendering of DoenetML when it is hidden (#279)
- Loading branch information
Showing
6 changed files
with
80 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,52 @@ | ||
import { RefObject, useEffect, useState } from "react"; | ||
|
||
/** | ||
* Returns `true` if the any portion of the element referenced by `ref` | ||
* is visible in the browser's viewport | ||
* | ||
* From: https://dev.to/jmalvarez/check-if-an-element-is-visible-with-react-hooks-27h8 | ||
*/ | ||
export function useIsVisible(ref: RefObject<HTMLElement>) { | ||
const [isIntersecting, setIntersecting] = useState(false); | ||
|
||
useEffect(() => { | ||
const observer = new IntersectionObserver(([entry]) => | ||
setIntersecting(entry.isIntersecting), | ||
); | ||
|
||
if (ref.current) { | ||
observer.observe(ref.current); | ||
} | ||
return () => { | ||
observer.disconnect(); | ||
}; | ||
}, [ref]); | ||
|
||
return isIntersecting; | ||
} | ||
|
||
/** | ||
* Returns true if the element referenced by `ref` is anywhere on the page | ||
* (more precisely, within 1000000px of the browser's viewport). | ||
* | ||
* Used to approximately detect if the element is not hidden. | ||
*/ | ||
export function useIsOnPage(ref: RefObject<HTMLElement>) { | ||
const [isIntersecting, setIntersecting] = useState(false); | ||
|
||
useEffect(() => { | ||
const observer = new IntersectionObserver( | ||
([entry]) => setIntersecting(entry.isIntersecting), | ||
{ rootMargin: "1000000px" }, | ||
); | ||
|
||
if (ref.current) { | ||
observer.observe(ref.current); | ||
} | ||
return () => { | ||
observer.disconnect(); | ||
}; | ||
}, [ref]); | ||
|
||
return isIntersecting; | ||
} |
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