problem with the clientHeight attribute in window event handlers

The component below is designed to react to changes in window resize based on the container height.

However, there seems to be an issue where the containerHeight variable increases as the window size decreases, but does not decrease when I make the window size bigger. It remains at its last maximum value. *** What's confusing is that if I remove the "line-height: 1.25rem;" from the code-container class, everything works perfectly!

Some notes:

  1. I am using debouncing on the resize handler to prevent frequent calculations.
  2. Flexbox is being used for the code-container and line-numbers classes.

import { useState, useEffect, useRef } from "react";
import styles from "./CodeBlock.module.css";

function debounce(fn, ms) {
  let timer;
  return (_) => {
    clearTimeout(timer);
    timer = setTimeout((_) => {
      timer = null;
      fn.apply(this, arguments);
    }, ms);
  };
}

const CodeBase = (props) => {
  const codeContainerRef = useRef(null);
  const [lineCount, setLineCount] = useState(0);

  useEffect(() => {
    const debouncedHandleResize = debounce(() => {
      const lineHeight = 20; // Value based on CSS
      const containerHeight = codeContainerRef.current.clientHeight;
      console.log(containerHeight);
      const calculatedLineCount = Math.floor(containerHeight / lineHeight);
      setLineCount(calculatedLineCount);
    }, 500);
    debouncedHandleResize();
    window.addEventListener("resize", debouncedHandleResize);
    return () => {
      window.removeEventListener("resize", debouncedHandleResize);
    };
  }, []);

  return (
    <div className={styles["code-container"]} ref={codeContainerRef}>
      <div className={styles["line-numbers"]}>
        {Array.from({ length: lineCount }, (_, index) => (
          <div key={index + 1} className={styles["line-number"]}>
            {index + 1}
          </div>
        ))}
      </div>
      <div className={styles["code-line"]}>{props.children}</div>
    </div>
  );
};

export default CodeBase;

And here is the CSS code:

    .code-container {
      display: flex;
      line-height: 1.25rem;
    }
    
    .line-numbers {
      display: flex;
      flex-direction: column;
      max-height: 100%; /*Ensure it doesn't exceed container height */
    }
    
    .line-number {
      color: #777;
      font-size: 12px;
    }
    
    .code {
      overflow-x: auto;
      white-space: pre-wrap;
    }
    
    .code-line {
      padding-left: 8px; /* Add spacing between line numbers and code */
    }

Answer №1

If you encounter this issue, it is likely because you are using an import statement in a setting that does not support ES Modules. This could happen in environments like browsers that lack module support.

import React, { useState, useEffect, useRef } from "react";
import styles from "./CodeBlock.module.css";

function debounce(fn, ms) {
  let timer;
  return (_) => {
    clearTimeout(timer);
    timer = setTimeout((_) => {
      timer = null;
      fn.apply(this, arguments);
    }, ms);
  };
}

const CodeBlock = (props) => {
  const codeContainerRef = useRef(null);
  const [lineCount, setLineCount] = useState(0);

  useEffect(() => {
    const debouncedHandleResize = debounce(() => {
      const lineHeight = 20; // Value based on CSS
      const containerHeight = codeContainerRef.current.clientHeight;
      const calculatedLineCount = Math.floor(containerHeight / lineHeight);
      setLineCount(calculatedLineCount);
    }, 500);
    
    debouncedHandleResize();
    window.addEventListener("resize", debouncedHandleResize);
    return () => {
      window.removeEventListener("resize", debouncedHandleResize);
    };
  }, []);

  return (
    <div className={styles["code-container"]} ref={codeContainerRef}>
      <div className={styles["line-numbers"]}>
        {Array.from({ length: lineCount }, (_, index) => (
          <div key={index + 1} className={styles["line-number"]}>
            {index + 1}
          </div>
        ))}
      </div>
      <div className={styles["code-line"]}>{props.children}</div>
    </div>
  );
};

export default CodeBlock;

To resolve the issue in your CSS code, make sure to update your class names...

/* CodeBlock.module.css */
.code-container {
  display: flex;
  line-height: 1.25rem;
}

.line-numbers {
  display: flex;
  flex-direction: column;
  max-height: 100%; /* Ensure it doesn't exceed container height */
}

.line-number {
  color: #777;
  font-size: 12px;
}

.code-line {
  padding-left: 8px; /* Add spacing between line numbers and code */
}

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

Is there a feature in Angular similar to Vue.js's "computed property" functionality?

After mastering Vue.js, I recently dove into Angular 4 for a new project. I've found that most things in Angular are quite similar to Vue, with the notable exception of "Computed Property". In Vue, I could easily create a computed property that would ...

Removing the Tooltip for Sorting in React-Table: Here's How to Remove the 'Toggle sortBy' Tooltip from Column Headers

Is there a way to remove the 'Toggle sortBy' tooltip from the column header when using the Material-UI Table components in the most recent version (7.5.x) of React-Table? Two tooltips Upon hover, I have a tooltip displaying the column header na ...

Using a For Loop in VueJS with TypeScript for an array of objects

I'm currently working on a for loop within an object array. Below is the code snippet I am working with: private async rightsOverview() { let item: any[] = []; const prod = await fetchFromApi<ProductionBaseType>(`/productions/${id ...

Rearrange the position of the customized google map marker to appear just above the latitude

Can you provide guidance on how to move a customized Google Map marker above a specific latitude and longitude point? You can view the code structure in this JSBIN: Link. I have used three object arrays for reference. Where should I insert the "anchor" i ...

Why are @Inject and Injectable important in Angular's Dependency Injection system?

constructor(private smartphoneService: smartphoneService) { } Although I am able to execute the code above without encountering any errors, I find myself pondering on the necessity of using @Inject and Injectable on services, Pipes, and other components. ...

The function is not defined after the button is clicked when an if-else statement is added to the

Hello there, I am currently working on a form to submit data to the database using ajax and CodeIgniter. The form seems to work fine for inserting data, but it lacks validation to check whether the fields are empty or not. I am attempting to add an if-else ...

Is React.lazy() compatible with Create React App?

I recently developed a React App using npx create-react-app my-app command, utilizing the React 17 version. However, I ran into an issue when trying to update a traditional component. import Component from './Component' to const Component = Rea ...

Image loading failure detected in ReactJS

In my current project using Reactjs (Nextjs framework), I encountered an issue where I am unable to display an image on a page without specifying the "height" and "width" attributes in the Image tag. I attempted the following code snippet but the image is ...

What is the best way to duplicate a component while customizing certain attributes, such as text and images, in a different component within a

As a newcomer to coding and React, I appreciate your patience with me. I have developed a component that I intend to use in many different components. Here's how it currently looks: import React from 'react'; const Feature = () => ( ...

Solving the problem of communication between sibling components in React

Hey there, I'm just getting started with React and I've run into a small issue with my app. I'm working on a feature where my Checkbox.js component needs to communicate with my SubmitButton.js component to enable the button only when the che ...

React-file-viewer shrinks any document to a compact size

I have been searching extensively for information on how to adjust file sizing in react-file-viewer without any success. My objective is to utilize the react-file-viewer to allow users to click on a filename hyperlink and open the file (be it an image, do ...

How can I effectively implement a withAuth higher order component (HOC) in TypeScript within Next.js?

Currently, I am working on a Next.js application and implementing the next-auth package. My goal is to develop a Higher Order Component (HOC) that can determine if the component has an active session or not. Along with this, I am utilizing eslint for code ...

Creating a Typescript interface for a anonymous function being passed into a React component

I have been exploring the use of Typescript in conjunction with React functional components, particularly when utilizing a Bootstrap modal component. I encountered some confusion regarding how to properly define the Typescript interface for the component w ...

Loading animation reminiscent of a whirlpool, similar to the movement

In my quest for the perfect animation, I have scoured far and wide. Unfortunately, the one I found at http://jsfiddle.net/pedox/yed68/embedded/result/ is created using css, which many browsers do not yet support fully. I also explored , but found it to be ...

Leveraging the result of one ajax function within a different ajax function

My current project involves the following steps: 1. User creates a template with various elements. 2. When the user clicks a button: *The first ajax function establishes a new entry in the custom template database. *The second ajax function retrieves the ...

jQuery template does not respond to input text when a click event is enabled on an iPhone device

Below is a jQuery template I have: <div class="parent-class"> <div class="sub-class"> <div clas="sub-input-class"> <input type="text" /> </div> </div> </div> Recently, I ...

What is the best way to display the JSON data?

<!DOCTYPE HTML> <html> <head> <title></title> <link href="/bundles/hoaxpartner/css/style.css" type="text/css" rel="stylesheet" /> </head> <body> <div id="header">Backbone</div> &l ...

SASS mixin that enables flexbox capabilities without supporting older, legacy versions of flexbox

Is it possible to provide support for browsers lacking flexbox compatibility (such as IE) while using SASS mixins? I have been quickly implementing the display:flex property with the following mixin. However, I am facing challenges with IE and need to man ...

creating a new date instance with a specific time zone

Creating a Date object with a dynamically selected timezone is my current goal while I am located in the IST time zone. To avoid the unpredictable behavior of Date.parse(), I am looking for an alternative method. Let's say we set the tzOffset to +05:3 ...

Caution: A value of "true" was provided for the attribute "exact" which is not a boolean

I'm currently utilizing version "6.16.0" of the "react-router-dom" library in my project. Upon running the program, I encountered a warning message stating: react-dom.development.js:86 Warning: Received true for a non-boolean attribute exact. If you i ...