Skip to main content Design tokens Components Style utilities
Skip to table of contents

    Resize Observer

    The Resize Observer component offers a thin, declarative interface to the ResizeObserver API.

    The resize observer will report changes to the dimensions of the elements it wraps through the pc-resize event. When emitted, a collection of ResizeObserverEntry objects will be attached to event.detail that contains the target element and information about its dimensions.

    <div class="resize-observer-overview">
      <pc-resize-observer>
          <span>Resize this box and watch the console ↘️</span>
      </pc-resize-observer>
    </div>
    
    <script>
      const container = document.querySelector(
          ".resize-observer-overview",
      );
      const resizeObserver =
          container.querySelector("pc-resize-observer");
    
      resizeObserver.addEventListener("pc-resize", (event) => {
          console.log(event.detail);
      });
    </script>
    
    <style>
      .resize-observer-overview {
          display: flex;
          align-items: center;
          justify-content: center;
          padding: var(--pc-spacing-xxxl) var(--pc-spacing-xxl);
          border: 2px solid var(--pc-input-border-color);
          border-radius: var(--pc-border-radius-l);
          overflow: auto;
      }
    </style>
    Code

    Properties

    NameDescriptionReflectsDefault
    disabledDisables the resize observer. booleanfalse
    updateComplete A read‐only promise that resolves when the component has finished updating. -

    Learn more about attributes and properties.

    Slots

    NameDescription
    (default)One or more elements to watch for resizing.

    Learn more about using slots.

    Events

    NameDescriptionEvent detail
    pc-resizeEmitted when the element is resized.{ entries: ResizeObserverEntry[] }

    Learn more about events.

    Importing

    If you’re using the autoloader or the standard loader, you can ignore this section. If you’re cherry picking, you can use any of the following snippets to import this component.

    ScriptImportBundler

    To import this component from the CDN with a script tag, copy this snippet and paste it in your HTML.

    <script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@0.5.1/dist/components/resize-observer/resize-observer.js"></script>

    To import this component from the CDN using a JavaScript import, copy this snippet and paste it in your JavaScript:

    import "https://cdn.jsdelivr.net/npm/placer-toolkit@0.5.1/dist/components/resize-observer/resize-observer.js";

    To import this component with a bundler using a JavaScript import, copy this snippet and paste it in your JavaScript:

    import "placer-toolkit/dist/components/resize-observer/resize-observer.js";