Skip to main content

Handling viewport/screen resizing

The builder automatically monitors changes in the dimensions of the parent element used for rendering the builder. This allows the builder to adapt to changes in the viewport dimensions. If you don't want the builder to monitor this automatically, you can disable that and manually handle the process.

tip

You only need to use this feature when you want to disable automatic detection of viewport changes.

Disable automatic resizing

To disable monitoring viewport changes, use the disableResizing property:

import { Builder } from "@tripetto/builder";

const builder = new Builder({
element: document.getElementById("CustomElement"),
disableResizing: true
});

builder.open();

Detecting viewport resize

The most common scenario is a browser window resize or screen orientation change. In both cases, you should monitor this and then invoke the resize method of your builder instance. You can also use the ResizeObserver API for this when you target modern browsers.

info

There is no need to debounce calls to the resize method. This is already done inside the builder.

Example using global resize events

This example shows how to use the resize and orientationchange events of the browser.

import { Builder } from "@tripetto/builder";

const builder = new Builder({
element: document.getElementById("CustomElement"),
disableResizing: true
});

builder.open();

// Upon window resize notify the builder
window.addEventListener("resize", () => builder.resize());
window.addEventListener("orientationchange", () => builder.resize());

Run Try on CodePen

Example using ResizeObserver

If you target modern browsers, you can use the ResizeObserver API to monitor changes in the dimensions of the host element. This can be more efficient than listening to the global resize event of the browser window. Especially in a component-based approach like when using React.

import { Builder } from "@tripetto/builder";

// Let's assume you have a host element with the id `CustomElement`
const element = document.getElementById("CustomElement");

const builder = new Builder({
element,
disableResizing: true,
onClose: () => {
resizeObserver.disconnect();
}
});

const resizeObserver = new ResizeObserver(() => {
builder.resize();
});

resizeObserver.observe(element);
builder.open();

Run Try on CodePen