Skip to main content
Since Shoelace 2.0 Code stable

Resize Observer

sl-resize-observer

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

Examples

Resize Observer Basics

The resize observer will report changes to the dimensions of the elements it wraps through the sl-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.

Resize this box and watch the console 👉
<div class="resize-observer-overview">
  <sl-resize-observer>
    <div>Resize this box and watch the console 👉</div>
  </sl-resize-observer>
</div>

<script>
  const container = document.querySelector('.resize-observer-overview');
  const resizeObserver = container.querySelector('sl-resize-observer');

  resizeObserver.addEventListener('sl-resize', event => {
    console.log(event.detail);
  });
</script>

<style>
  .resize-observer-overview div {
    display: flex;
    border: solid 2px var(--sl-input-border-color);
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 4rem 2rem;
  }
</style>
div.resize-observer-overview
  sl-resize-observer
    div Resize this box and watch the console 👉

javascript:
  const container = document.querySelector(.resize-observer-overview);
  const resizeObserver = container.querySelector(sl-resize-observer);

  resizeObserver.addEventListener(sl-resize, event => {
    console.log(event.detail);
  });

css:
  .resize-observer-overview div {
    display: flex;
    border: solid 2px var(--sl-input-border-color);
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 4rem 2rem;
  }

Component Props

Property Default Details
disabled false

boolean

Disables the observer.

updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Slots

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

Learn more about using slots.

Events

Name Name Name React Event Details
sl-resize sl-resize sl-resize onSlResize

{ entries: ResizeObserverEntry[] }

Emitted when the element is resized.

Learn more about events.