generated from react-component/footer
-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add useResizeObserver hook #68
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## hook | ||
|
||
<code src="../../examples/hook.tsx"> |
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,53 @@ | ||
import '../assets/index.less'; | ||
import React from 'react'; | ||
import type { OnResizeHandler } from '../src/OnResizeHandler'; | ||
import { useResizeObserver } from '../src/useResizeObserver'; | ||
|
||
export default function App() { | ||
const [times, setTimes] = React.useState(0); | ||
const [disabled, setDisabled] = React.useState(false); | ||
const textareaRef = React.useRef<HTMLTextAreaElement>(null); | ||
|
||
const onResize: OnResizeHandler = ({ width, height, offsetWidth, offsetHeight }) => { | ||
setTimes((prevTimes) => prevTimes + 1); | ||
console.log( | ||
'Resize:', | ||
'\n', | ||
'BoundingBox', | ||
width, | ||
height, | ||
'\n', | ||
'Offset', | ||
offsetWidth, | ||
offsetHeight, | ||
); | ||
}; | ||
|
||
useResizeObserver({ ref: textareaRef, onResize, disabled }); | ||
|
||
React.useEffect(() => { | ||
console.log('Ref:', textareaRef.current); | ||
}, []); | ||
|
||
return ( | ||
<React.StrictMode> | ||
<div style={{ transform: 'scale(1.1)', transformOrigin: '0% 0%' }}> | ||
<div> | ||
<label> | ||
<input | ||
type="checkbox" | ||
onChange={() => { | ||
setDisabled(!disabled); | ||
}} | ||
checked={disabled} | ||
/>{' '} | ||
Disabled Observe | ||
</label> | ||
{' >>> '} | ||
<span>Resize times: {times}</span> | ||
</div> | ||
<textarea ref={textareaRef} placeholder="I'm a textarea!" /> | ||
</div> | ||
</React.StrictMode> | ||
); | ||
} |
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,3 @@ | ||
import type { Size } from './Size'; | ||
|
||
export type OnResizeHandler = (size: Size, element: HTMLElement) => void; |
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,6 @@ | ||
export type Size = { | ||
width: number; | ||
height: number; | ||
offsetWidth: number; | ||
offsetHeight: number; | ||
}; |
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,7 @@ | ||
import { ReactResizeObserver } from './ResizeObserver'; | ||
|
||
export { ResizeObserverProps } from './ResizeObserver'; | ||
export { useResizeObserver, UseResizeObserverArgs } from './useResizeObserver'; | ||
export { Size } from './Size.d'; | ||
export { OnResizeHandler } from './OnResizeHandler.d'; | ||
export default ReactResizeObserver; |
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,111 @@ | ||
import * as React from 'react'; | ||
import type { OnResizeHandler } from './OnResizeHandler'; | ||
import type { Size } from './Size'; | ||
|
||
export interface UseResizeObserverArgs { | ||
ref: React.RefObject<React.ReactNode>; | ||
onResize?: OnResizeHandler; | ||
disabled?: boolean; | ||
} | ||
|
||
export function useResizeObserver({ | ||
ref, | ||
onResize: resizeHandler, | ||
disabled, | ||
}: UseResizeObserverArgs): Size { | ||
const observerRef = React.useRef<ResizeObserver>(); | ||
const elementRef = React.useRef<HTMLElement>(); | ||
|
||
const [height, setHeight] = React.useState(0); | ||
const [width, setWidth] = React.useState(0); | ||
const [offsetHeight, setOffsetHeight] = React.useState(0); | ||
const [offsetWidth, setOffsetWidth] = React.useState(0); | ||
|
||
const destroyObserver = React.useCallback(() => { | ||
if (observerRef.current) { | ||
observerRef.current.disconnect(); | ||
observerRef.current = null; | ||
} | ||
}, [observerRef]); | ||
|
||
const onResize = React.useCallback( | ||
(entries: ResizeObserverEntry[]): void => { | ||
const target = entries[0].target as HTMLElement; | ||
|
||
const { width: _width, height: _height } = target.getBoundingClientRect(); | ||
const { offsetWidth: _offsetWidth, offsetHeight: _offsetHeight } = target; | ||
|
||
/** | ||
* Resize observer trigger when content size changed. | ||
* In most case we just care about element size, | ||
* let's use `boundary` instead of `contentRect` here to avoid shaking. | ||
*/ | ||
const fixedWidth = Math.floor(_width); | ||
const fixedHeight = Math.floor(_height); | ||
|
||
if ( | ||
width !== fixedWidth || | ||
height !== fixedHeight || | ||
offsetWidth !== _offsetWidth || | ||
offsetHeight !== _offsetHeight | ||
) { | ||
setHeight(fixedHeight); | ||
setWidth(fixedWidth); | ||
setOffsetHeight(_offsetHeight); | ||
setOffsetWidth(_offsetWidth); | ||
|
||
if (resizeHandler) { | ||
Promise.resolve().then(() => { | ||
resizeHandler( | ||
{ | ||
width: fixedWidth, | ||
height: fixedHeight, | ||
offsetWidth: _offsetWidth, | ||
offsetHeight: _offsetHeight, | ||
}, | ||
target, | ||
); | ||
}); | ||
} | ||
} | ||
}, | ||
[width, height, offsetWidth, offsetHeight, resizeHandler], | ||
); | ||
|
||
const repopulateObserver = React.useCallback(() => { | ||
if (disabled) { | ||
destroyObserver(); | ||
return; | ||
} | ||
|
||
const element = ref.current as HTMLElement; | ||
const elementChanged = element !== elementRef.current; | ||
if (elementChanged) { | ||
destroyObserver(); | ||
elementRef.current = element; | ||
} | ||
|
||
if (!observerRef.current && element) { | ||
observerRef.current = new ResizeObserver(onResize); | ||
observerRef.current.observe(element); | ||
} | ||
}, [destroyObserver, disabled, onResize, ref]); | ||
|
||
React.useEffect(() => { | ||
repopulateObserver(); | ||
|
||
// eslint-disable-next-line consistent-return | ||
return () => { | ||
destroyObserver(); | ||
}; | ||
}, [repopulateObserver, destroyObserver]); | ||
|
||
return { | ||
width, | ||
height, | ||
offsetWidth, | ||
offsetHeight, | ||
}; | ||
} | ||
|
||
export default useResizeObserver; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`
/**
*
*/
`