| 1 |
import { canUseDOM } from './util'; |
| 2 |
|
| 3 |
/** |
| 4 |
* This function creates a ResizeObserver used to handle resize events for the given containerRef. If ResizeObserver |
| 5 |
* or the given containerRef are not available, a window resize event listener is used by default. |
| 6 |
* |
| 7 |
* Example 1: |
| 8 |
* |
| 9 |
* private containerRef = createRef<HTMLDivElement>(); |
| 10 |
* private observer: any = () => {}; |
| 11 |
* |
| 12 |
* public componentDidMount() { |
| 13 |
* this.observer = getResizeObserver(this.containerRef.current, this.handleResize, true); |
| 14 |
* } |
| 15 |
* |
| 16 |
* public componentWillUnmount() { |
| 17 |
* this.observer(); |
| 18 |
* } |
| 19 |
* |
| 20 |
* private handleResize = () => { |
| 21 |
* if (this.containerRef.current && this.containerRef.current.clientWidth) { |
| 22 |
* this.setState({ width: this.containerRef.current.clientWidth }); |
| 23 |
* } |
| 24 |
* }; |
| 25 |
* |
| 26 |
* public render() { |
| 27 |
* return ( |
| 28 |
* <div ref={this.containerRef} > |
| 29 |
* <Chart width={this.state.width} ... /> |
| 30 |
* </div> |
| 31 |
* ); |
| 32 |
* } |
| 33 |
* |
| 34 |
* Example 2: |
| 35 |
* |
| 36 |
* private inputRef = createRef<HTMLInputElement>(); |
| 37 |
* private observer: any = () => {}; |
| 38 |
* |
| 39 |
* public componentDidMount() { |
| 40 |
* this.observer = getResizeObserver(this.inputRef.current, this.handleResize, true); |
| 41 |
* } |
| 42 |
* |
| 43 |
* public componentWillUnmount() { |
| 44 |
* this.observer(); |
| 45 |
* } |
| 46 |
* |
| 47 |
* private handleResize = () => { |
| 48 |
* if (this.inputRef.current) { |
| 49 |
* trimLeft(inputRef.current, String(this.props.value)); |
| 50 |
* } |
| 51 |
* }; |
| 52 |
* |
| 53 |
* public render() { |
| 54 |
* return ( |
| 55 |
* <input ref={this.inputRef} ... /> |
| 56 |
* ); |
| 57 |
* } |
| 58 |
* |
| 59 |
* Example 3 - With debounced method passed in: |
| 60 |
* |
| 61 |
* public componentDidMount() { |
| 62 |
* this.observer = getResizeObserver(this.inputRef.current, debounce(this.handleResize, 250)); |
| 63 |
* } |
| 64 |
* |
| 65 |
* @param {Element} containerRefElement The container reference to observe |
| 66 |
* @param {Function} handleResize The function to call for resize events |
| 67 |
* @param {boolean} useRequestAnimationFrame Whether to pass the handleResize function as a callback to requestAnimationFrame. Pass in true when the function passed in is not debounced. |
| 68 |
* @return {Function} The function used to unobserve resize events |
| 69 |
*/ |
| 70 |
export const getResizeObserver = ( |
| 71 |
containerRefElement: Element, |
| 72 |
handleResize: () => void, |
| 73 |
useRequestAnimationFrame?: boolean |
| 74 |
) => { |
| 75 |
let unobserve: any; |
| 76 |
|
| 77 |
if (canUseDOM) { |
| 78 |
const { ResizeObserver } = window as any; |
| 79 |
|
| 80 |
if (containerRefElement && ResizeObserver) { |
| 81 |
const resizeObserver = new ResizeObserver((entries: any) => { |
| 82 |
// Wrap resize function in requestAnimationFrame to avoid "ResizeObserver loop limit exceeded" errors |
| 83 |
if (useRequestAnimationFrame) { |
| 84 |
window.requestAnimationFrame(() => { |
| 85 |
if (Array.isArray(entries) && entries.length > 0) { |
| 86 |
handleResize(); |
| 87 |
} |
| 88 |
}); |
| 89 |
// Avoid wrapping function in requestAnimationFrame if the function is debounced |
| 90 |
} else { |
| 91 |
if (Array.isArray(entries) && entries.length > 0) { |
| 92 |
handleResize(); |
| 93 |
} |
| 94 |
} |
| 95 |
}); |
| 96 |
resizeObserver.observe(containerRefElement); |
| 97 |
unobserve = () => resizeObserver.unobserve(containerRefElement); |
| 98 |
} else { |
| 99 |
window.addEventListener('resize', handleResize); |
| 100 |
unobserve = () => window.removeEventListener('resize', handleResize); |
| 101 |
} |
| 102 |
} |
| 103 |
return () => { |
| 104 |
if (unobserve) { |
| 105 |
unobserve(); |
| 106 |
} |
| 107 |
}; |
| 108 |
}; |