-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: responsive object selections (#389)
Closes #381
- Loading branch information
1 parent
d3381b5
commit 63d8cc1
Showing
10 changed files
with
154 additions
and
56 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
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,33 +1,29 @@ | ||
import { useState, useCallback, useLayoutEffect } from 'react'; | ||
import { useState, useCallback, useLayoutEffect, useRef } from 'react'; | ||
|
||
export default function useRect() { | ||
const [node, setNode] = useState(); | ||
const [rect, setRect] = useState(); | ||
const callbackRef = useCallback(ref => { | ||
if (!ref) { | ||
return; | ||
} | ||
setNode(ref); | ||
}, []); | ||
const handleResize = () => { | ||
const { left, top, width, height } = node.getBoundingClientRect(); | ||
const ref = useRef(); | ||
|
||
const handleResize = useCallback(() => { | ||
const { left, top, width, height } = ref.current.getBoundingClientRect(); | ||
setRect({ left, top, width, height }); | ||
}; | ||
}, [ref.current]); | ||
|
||
useLayoutEffect(() => { | ||
if (!node) return undefined; | ||
if (!ref.current) return undefined; | ||
if (typeof ResizeObserver === 'function') { | ||
let resizeObserver = new ResizeObserver(handleResize); | ||
resizeObserver.observe(node); | ||
resizeObserver.observe(ref.current); | ||
return () => { | ||
resizeObserver.unobserve(node); | ||
resizeObserver.disconnect(node); | ||
resizeObserver.unobserve(ref.current); | ||
resizeObserver.disconnect(ref.current); | ||
resizeObserver = null; | ||
}; | ||
} | ||
window.addEventListener('resize', handleResize); | ||
return () => { | ||
window.removeEventListener('resize', handleResize); | ||
}; | ||
}, [node]); | ||
return [callbackRef, rect, node]; | ||
}, [ref.current]); | ||
return [ref, rect, ref.current]; | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.