Is it possible for ResizeObserver to only observe changes in width?

Is there a way to have ResizeObserver only trigger on width changes and not on height changes?

For example:

const resizeObserver = new ResizeObserver(updateLayout);
resizeObserver.observe(layout.current);

const updateLayout = () => {/*perform action*/}

Answer №1

If you need to detect changes in width within an element, you can create a custom function that specifically checks for this:

const checkWidthChange = function($element, callback) {
  let lastDimensions = $element.getBoundingClientRect();
  const observer = new ResizeObserver(function() {
    const currentDimensions = $element.getBoundingClientRect();
    if(currentDimensions.width !== lastDimensions.width && currentDimensions.height === lastDimensions.height)
      callback();
    lastDimensions = currentDimensions;
  });
  observer.observe($element);
}

You can then implement it like so:

checkWidthChange(layout.current, setLayout);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to utilize `request.on` event listeners for terminating stream requests in NextJS

Currently, I am in the process of developing an API endpoint that streams data in order to provide live updates to components based on database events. While I have made significant progress in getting everything to function correctly, I have encountered a ...

Having issues with Npm installation not finishing up. Can anyone provide a solution to rectify this

I've been waiting for 30 minutes and my upload is still not completed. How can I resolve this issue? Click here to see the problem ...

Include an element in a secondary list based on its position in the initial list

Having 2 lists presents a challenge. An Angular service is utilized with a splice-based method to remove items from the first list (named "items") according to their index through a ng-click action. service.removeItem = function (itemIndex) { items ...

Adjusting Google Maps API v3 Autocomplete dropdown width with JavaScript and SASS to match input field dimensions

I am facing an issue where the autocomplete dropdown (div with class "pac-container") is consistently 4 pixels shy of aligning perfectly with the right side of the input field. It looks like this: https://i.sstatic.net/Y0oq1.png Below is the HTML code: ...

Troubleshooting Ajax contact form's failure to refresh (PHP and Bootstrap 4)

I have successfully implemented this code on one website, but it is not working as expected on another website. It sends the email but refreshes the page and redirects to a contact.php page with a message. Despite double-checking everything, including cha ...

What is the best way to implement a calendar view for selecting time periods on an HTML page?

Is there a way to implement a calendar view when clicking on the input box within an HTML page? I am looking to create a function where users can select a time period to generate a file. How can this be accomplished? If possible, could you share some samp ...

The canvas's document.getElementById function is unable to find any matching element,

Hello everyone, I am currently diving into the world of javascript and trying to expand my knowledge. However, I have encountered a problem that has me stumped, and I could really use some assistance. While exploring, I came across this page: http://www.w ...

The JQuery datepicker fails to display the current date

I am experiencing an issue with the datepicker on my webpage. While it is working correctly, the default date being displayed is '01/01/2001' instead of '11/23/2012', as I intended. Here is the jquery code I am using: $(":inpu ...

I am considering open-sourcing my website, but I am concerned about the file containing my database credentials

I developed a NextJS website that I am considering open sourcing, but I have sensitive credentials stored in a .env file for my database access. While I don't anticipate major issues if these credentials were exposed to the public, I prefer not to tak ...

When implementing components, Material UI encounters errors in trying to read properties of null

Currently, I am facing a challenge in displaying text using MUI in Next.js. I initialized my Next app using npx create-next-app. To troubleshoot the issue, I have attempted deleting node-modules and running npm i, repeating the process in the parent direct ...

When adding Tailwind CSS to my Next.js app, the MDX styling through next-mdx-remote stops working properly

I've integrated MDX into my Next.js project using next-mdx-remote. While following JetBrains WebStorm's detailed tutorial on building a blog with MDX, they utilized Bootstrap for styling. However, I opted for Tailwind CSS as my preferred CSS fra ...

execute numerous Jssor Caption instances simultaneously

I have a Jssor slider and I want to create an animation for the first slide. My goal is to make two images come from the sides and merge in the center. I tried using a div for each image with a caption, and it works well. However, the second image starts ...

Updates to Providers in the latest release of Angular 2

When working with angular 2.0.0-rc.1, we implemented a Provider using the new Provider method as shown in the code snippet below: var constAccessor = new Provider(NG_VALUE_ACCESSOR, { useExisting: forwardRef(() => EJDefaultValueAccessor), ...

Leverage the values of object properties from a pair of JavaScript arrays containing objects

I am working with two arrays let arr1 = [{'id': 'ee', 'seat': '12'}, {'id': 'aa', 'seat': '8'} ] let arr2 = [ {'id': 's22', 'num': '&ap ...

Checking the validity of an input element with jQuery: How to determine if the valid or invalid pseudo-class has been applied?

Is there a way to check for the application of the CSS3 :valid or :invalid pseudo-class on an input element using jQuery? I want to verify if the form element has passed CSS validation before allowing the submit button to be enabled. Here are some methods ...

What method can be used to ensure Moment.js gives priority to the day over the month when interpreting dates with ambiguity

Is there a way to set Moment.js to default to assuming that the day comes before the month in ambiguous situations? For instance: moment("10-05-2018") would interpret as 10 May 2018 moment("06/08/2018") would be assumed as 06 August 2018 moment("01 03 ...

What is the best way to access props from a different file in a React application?

I am facing a challenge with my two files: EnhancedTableHead and OrderDialog. I need to access the props data from EnhancedTabledHead in my OrderDialog file. Is there a way to achieve this? Here is how my files are structured: //OrderDialog.jsx import ...

Set the style properties of one class to match the style properties of another class using JavaScript

I need to dynamically create a div using the Bootstrap panel for displaying it. The div's class will either be "panel panel-success" or "panel panel-danger" based on the server response, which can be either "success" or "failure." To assign the class ...

Once you've made the switch to .mjs files, make sure to replace all

I recently made the switch from using a js file to an mjs file, as I heard this is now the standard practice. However, I am encountering a beginner's issue. In my mongoose.js file, I had the following code: const mongoose = require('mongoose&apos ...

A step-by-step guide on accessing the data from an uploaded JSON file in a React application

One exciting feature is the drag and drop component that allows users to add multiple files. However, there seems to be an issue with displaying the content of a JSON file once it's added. Below is the code snippet in question: if (files?.length) ...