-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hooks: useMutationObserver and useOverflowDetection init + PageFrame and Editor: Overflowing detection + BgContainer: image position fixed + minor changes
- Loading branch information
Showing
8 changed files
with
131 additions
and
52 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
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,34 @@ | ||
import {useEffect, RefObject} from "react"; | ||
|
||
type MutationObserverCallback = ( | ||
mutations: MutationRecord[], | ||
observer: MutationObserver | ||
) => void; | ||
|
||
type MutationObserverOptions = { | ||
attributes?: boolean; | ||
characterData?: boolean; | ||
childList?: boolean; | ||
subtree?: boolean; | ||
}; | ||
|
||
const useMutationObserver = ( | ||
ref: RefObject<HTMLElement>, | ||
callback: MutationObserverCallback, | ||
options: MutationObserverOptions = { | ||
attributes: true, | ||
characterData: true, | ||
childList: true, | ||
subtree: true, | ||
} | ||
) => { | ||
useEffect(() => { | ||
if (ref.current) { | ||
const observer = new MutationObserver(callback); | ||
observer.observe(ref.current, options); | ||
return () => observer.disconnect(); | ||
} | ||
}, [ref, callback, options]); | ||
}; | ||
|
||
export default useMutationObserver; |
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,39 @@ | ||
import {useEffect, useState, RefObject} from "react"; | ||
import {debounce} from "lodash"; | ||
import useMutationObserver from "./useMutationObserver"; | ||
|
||
const useOverflowDetection = ( | ||
ref: RefObject<HTMLElement>, | ||
debounceDelay = 200 | ||
) => { | ||
const [isOverflowing, setIsOverflowing] = useState(false); | ||
|
||
const checkOverflow = debounce(() => { | ||
if (ref.current) { | ||
setIsOverflowing(ref.current.scrollWidth > ref.current.clientWidth); | ||
} | ||
}, debounceDelay); | ||
|
||
useEffect(() => { | ||
checkOverflow(); | ||
|
||
const handleResize = () => { | ||
checkOverflow(); | ||
}; | ||
window.addEventListener("resize", handleResize); | ||
|
||
return () => { | ||
window.removeEventListener("resize", handleResize); | ||
checkOverflow.cancel(); | ||
}; | ||
}, [ref, checkOverflow]); | ||
|
||
useMutationObserver(ref, checkOverflow, { | ||
childList: true, | ||
subtree: true, | ||
}); | ||
|
||
return isOverflowing; | ||
}; | ||
|
||
export default useOverflowDetection; |
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