Resize Observer
<wa-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 wa-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"> <wa-resize-observer> <div>Resize this box and watch the console 👉</div> </wa-resize-observer> </div> <script> const container = document.querySelector('.resize-observer-overview'); const resizeObserver = container.querySelector('wa-resize-observer'); resizeObserver.addEventListener('wa-resize', event => { console.log(event.detail); }); </script> <style> .resize-observer-overview div { display: flex; border: solid 2px var(--wa-color-surface-border); align-items: center; justify-content: center; text-align: center; padding: 4rem 2rem; } </style>
Importing
Jump to heading
Autoloading components via projects is the recommended way to import components. If you prefer to do it manually, use one of the following code snippets.
Let your project code do the work! Sign up for free to use a project with your very own CDN — it's the fastest and easiest way to use Web Awesome.
To manually import this component from NPM, use the following code.
import '@awesome.me/webawesome/dist/components/resize-observer/resize-observer.js';
To manually import this component from React, use the following code.
import WaResizeObserver from '@awesome.me/webawesome/dist/react/resize-observer';
Slots
Jump to heading
Learn more about using slots.
| Name | Description |
|---|---|
| (default) | One or more elements to watch for resizing. |
Attributes & Properties
Jump to heading
Learn more about attributes and properties.
| Name | Description | Reflects | |
|---|---|---|---|
css |
One or more CSSResultGroup to include in the component's shadow root. Host styles are automatically prepended.
Type
CSSResultGroup | undefinedDefault
styles |
||
customStates |
Methods for setting and checking custom states.
Type
objectDefault
{ /** Adds or removes the specified custom state. / set: (customState: string, active: boolean) => { if (!Boolean(this.internals?.states)) return; try { if (active) { this.internals.states.add(customState); } else { this.internals.states.delete(customState); } } catch (e) { if (String(e).includes("must start with '--'")) { / eslint-disable-next-line / console.error('Your browser implements an outdated version of CustomStateSet. Consider using a polyfill'); } else { throw e; } } }, /* Determines whether or not the element currently has the specified state. */ has: (customState: string) => { if (!Boolean(this.internals?.states)) return false; try { return this.internals.states.has(customState); } catch { return false; } }, } |
||
disableddisabled |
Disables the observer.
Type
booleanDefault
false |
|
Methods
Jump to heading
Learn more about methods.
| Name | Description | Arguments |
|---|---|---|
relayNativeEvent() |
Given a native event, this function cancels it and dispatches it again from the host element using the desired event options. |
event: Event,
eventOptions: EventInit
|
Events
Jump to heading
Learn more about events.
| Name | Description |
|---|---|
wa-resize |
Emitted when the element is resized. |