@bnidev/js-utils
    Preparing search index...

    Function onResize

    • Registers a resize callback that works for either the window, document, or a specific HTMLElement.

      Internally uses ResizeObserver for elements, and a debounced resize event for the window or document.

      Parameters

      • callback: () => void

        The function to call when the target is resized.

      • Optionaloptions: {
            delay?: number;
            element?: HTMLElement | Document | Window & typeof globalThis;
        }

        Optional configuration:

        • element: Target to observe (window, document, or an HTMLElement). Defaults to window.
        • delay: Debounce delay in milliseconds (only applies to window/document). Defaults to 50.

      Returns () => void

      A cleanup function to remove the listener or disconnect the observer.

      // ES Module
      import { onResize } from '@bnidev/js-utils'

      // CommonJS
      const { onResize } = require('@bnidev/js-utils')
      // Register a resize listener for the window
      onResize(() => {
      console.log('Window resized')
      }, { delay: 100 })

      // Register a resize listener for a specific element
      const div = document.querySelector('#myDiv')!
      onResize(() => {
      console.log('Element resized')
      }, { element: div })