generated from eternallycyf/ims-monorepo-template
-
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.
✨ feat: @ims-view/hooks add useResizeEffect
- Loading branch information
1 parent
d2c5f8f
commit 929331b
Showing
4 changed files
with
81 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
title: useResizeEffect | ||
order: 2 | ||
apiHeader: | ||
pkg: '@ims-view/hooks' | ||
docUrl: https://github.com/eternallycyf/ims-view-pc/blob/master/packages/hooks/src/useResizeEffect/index.md | ||
sourceUrl: https://github.com/eternallycyf/ims-view-pc/blob/master/packages/hooks/src/useResizeEffect/index.ts | ||
--- | ||
|
||
## useResizeEffect | ||
|
||
```ts | ||
import { RefObject } from 'react'; | ||
import useIsomorphicLayoutEffect from '../useIsomorphicLayoutEffect'; | ||
import useMemoizedFn from '../useMemoizedFn'; | ||
|
||
export default function useResizeEffect<T extends HTMLElement>( | ||
effect: (target: T) => void, | ||
targetRef: RefObject<T>, | ||
) { | ||
const fn = useMemoizedFn(effect); | ||
useIsomorphicLayoutEffect(() => { | ||
const target = targetRef.current; | ||
if (!target) return; | ||
if (window.ResizeObserver) { | ||
let animationFrame: number; | ||
const observer = new ResizeObserver(() => { | ||
animationFrame = window.requestAnimationFrame(() => fn(target)); | ||
}); | ||
observer.observe(target); | ||
return () => { | ||
window.cancelAnimationFrame(animationFrame); | ||
observer.disconnect(); | ||
}; | ||
} else { | ||
fn(target); | ||
} | ||
}, [targetRef]); | ||
} | ||
``` | ||
|
||
## Params | ||
|
||
| 属性 | 说明 | 类型 | 默认值 | | ||
| ----------- | ---------------- | --------------------- | ------ | | ||
| `effect` | 变化时触发的事件 | `(target: T) => void` | | ||
| `targetRef` | 监听的元素 | `HTMLDivElement` | | ||
|
||
## Result | ||
|
||
| 属性 | 说明 | 类型 | | ||
| ---- | ---- | ---- | |
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,27 @@ | ||
import { RefObject } from 'react'; | ||
import useIsomorphicLayoutEffect from '../useIsomorphicLayoutEffect'; | ||
import useMemoizedFn from '../useMemoizedFn'; | ||
|
||
export default function useResizeEffect<T extends HTMLElement>( | ||
effect: (target: T) => void, | ||
targetRef: RefObject<T>, | ||
) { | ||
const fn = useMemoizedFn(effect); | ||
useIsomorphicLayoutEffect(() => { | ||
const target = targetRef.current; | ||
if (!target) return; | ||
if (window.ResizeObserver) { | ||
let animationFrame: number; | ||
const observer = new ResizeObserver(() => { | ||
animationFrame = window.requestAnimationFrame(() => fn(target)); | ||
}); | ||
observer.observe(target); | ||
return () => { | ||
window.cancelAnimationFrame(animationFrame); | ||
observer.disconnect(); | ||
}; | ||
} else { | ||
fn(target); | ||
} | ||
}, [targetRef]); | ||
} |