Debian Codegraph @

node/@patternfly/react-core/dist/js/helpers/resizeObserver.js

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