import * as React from "react" ; import { findDOMNode } from "react-dom" ; import { polyfill } from "react-lifecycles-compat" ; import ResizeObserver from "resize-observer-polyfill" ; import { AbstractPureComponent2 } from "../../common" ; import { DISPLAYNAME_PREFIX } from "../../common/props" ; import { ResizeEntry } from "./resizeObserverTypes" ; export type ResizeSensorProps = IResizeSensorProps ; deprecated export interface IResizeSensorProps { /**
     * Callback invoked when the wrapped element resizes.
     *
     * The `entries` array contains an entry for each observed element. In the
     * default case (no `observeParents`), the array will contain only one
     * element: the single child of the `ResizeSensor`.
     *
     * Note that this method is called _asynchronously_ after a resize is
     * detected and typically it will be called no more than once per frame.
     */ onResize : ( entries : ResizeEntry [ ] ) => void ; default observeParents ? : boolean ; } /** `ResizeSensor` requires a single DOM element child and will error otherwise. */ @ polyfill export class ResizeSensor extends AbstractPureComponent2 < ResizeSensorProps > { public static displayName = `${ DISPLAYNAME_PREFIX }.ResizeSensor` ; private element : Element | null = null ; private observer = new ResizeObserver ( entries => this . props . onResize ?. ( entries ) ) ; public render ( ) { return React . Children . only ( this . props . children ) ; } public componentDidMount ( ) { this . observeElement ( ) ; } public componentDidUpdate ( prevProps ) { this . observeElement ( this . props . observeParents !== prevProps . observeParents ) ; } public componentWillUnmount ( ) { this . observer . disconnect ( ) ; } /**
     * Observe the DOM element, if defined and different from the currently
     * observed element. Pass `force` argument to skip element checks and always
     * re-observe.
     */ private observeElement ( force = false ) { const element = this . getElement ( ) ; if ( ! ( element instanceof Element ) ) { this . observer . disconnect ( ) ; return ; } if ( element === this . element && ! force ) { return ; } else { this . observer . disconnect ( ) ; this . element = element ; } this . observer . observe ( element ) ; if ( this . props . observeParents ) { let parent = element . parentElement ; while ( parent != null ) { this . observer . observe ( parent ) ; parent = parent . parentElement ; } } } private getElement ( ) { try { return findDOMNode ( this ) ; } catch { return null ; } } }