Sha256: e2cdf3458e4b28f56c284003080727ccc85e5230c73c81aefc566718ab98f286

Contents?: true

Size: 802 Bytes

Versions: 16

Compression:

Stored size: 802 Bytes

Contents

export default function withSizeObserver(props = {}) {
  return function (component) {
    const resizeTarget = props.target || component.$root;
    const oldDestroy = component.destroy;

    const sizeObserver = Alpine.reactive({
      observer: null,
      handler(target) {
        if (component.onResize) {
          component.onResize(target.contentRect, target);
        }
      },
    });

    sizeObserver.observer = new ResizeObserver((entries) =>
      sizeObserver.handler(entries[0])
    );

    Object.assign(component, {
      destroy() {
        sizeObserver.observer.disconnect();
        sizeObserver.observer = null;

        if (oldDestroy) {
          oldDestroy.call(this);
        }
      },
    });

    sizeObserver.observer.observe(resizeTarget);

    return component;
  };
}

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
coveragebook_components-0.7.1 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.7.0 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.6.5 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.6.4 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.6.3 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.6.2 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.6.1 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.6.0 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.5.7 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.5.6 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.5.5 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.5.4 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.5.3 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.5.2 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.5.1 app/assets/js/base/mixins/size-observer.js
coveragebook_components-0.5.0 app/assets/js/base/mixins/size-observer.js