Why does React-Perfect-Scrollbar not work properly when the height of an element is not specified in pixels?

Currently, I am developing a chat application with React 18 for its user interface.

The app includes a sidebar that displays user profiles. To ensure the app fits within the browser window height, I've made the list of user profiles scrollable when necessary.

https://i.sstatic.net/XWXq7.png

To achieve this functionality, I have utilized React-Perfect-Scrollbar in the UsersList.jsx file which can be found on NPM:

import './_usersList.css';
import axios from 'axios';
import PerfectScrollbar from 'react-perfect-scrollbar';
import { useLocation } from 'react-router-dom';
import { useEffect, useState } from 'react';

export default function UsersList() {

  const API_URL = 'https://randomuser.me/api';
  const location = useLocation();
  const [users, setUsers] = useState([]);

  const getUsers = async () => {
    const { data: { results } } = await axios.get(`${API_URL}/?&results=20&inc=id,name,email,picture`, {
    });

    setUsers(results);
  }

  const usersList = () => {
    return users.map(user => (
      <li className="d-table-row w-100">
        <div className="user-image-container d-table-cell">
          <img src={user.picture.thumbnail} alt={`user.name.first user.name.last`} className="rounded-circle" />
        </div>
        <div className="text d-table-cell">
          <h3 className="display-name m-0">{user.name.first} {user.name.last}</h3>
        </div>
      </li>
    ))
  }

  useEffect(() => {
    getUsers();
  }, [location])

  return (
    <div className="chat-users">
      <PerfectScrollbar>
        <ul className="users-list list-unstyled m-0 d-table w-100">
          { usersList() }
        </ul>
      </PerfectScrollbar>
    </div>
  );
}

In the _usersList.css file, instead of setting a fixed height for the

<div className="chat-users">
, I have specified:

.chat-users {
  position: relative;
  flex: 1;
}

This allows the element to expand to occupy all available space.

However, an issue arises where the scrollbar is not visible unless a fixed height in pixels (e.g. height: 400px;) is defined for the element, even if scrolling is required.

The problem is resolved for mobile devices by adding overflow-y: auto like so:

.chat-users {
  position: relative;
  flex: 1;
  overflow-y: auto;
}

Nevertheless, the problem persists on desktops.

If you want to explore the code further, there's a sandbox available here.

What could be causing this issue and what would be the most effective solution?

Answer №2

Here's how I successfully resolved the issue:

I included overflow-y: auto for all screen sizes.

.chat-users {
  position: relative;
  flex: 1;
  overflow-y: auto;
}

Additionally, I applied a calculated height specifically for desktop screens:

@media screen and (min-width: 992px) {
  .chat-users {
    max-height: calc(100vh - 144px);
  }
}

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

Encountering an Invalid hook call error - Ensure that you are only utilizing hooks within the body of a functional component specifically designed for use with

Understanding the Error sp-webpart-workbench-assembly_default.js:26401 [1599066762186][OtherGlobalError.window.onerro] Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of ...

CSS: Adjusting the vertical alignment of numbers in an ordered list

Hey there, I have a question about the vertical alignment of numbering in an ordered list. It seems that the numbers are being pushed to the baseline when <li> elements contain floated divs only. Is there any way to prevent this and have the numbers ...

Utilize the three.js library within a Vue component by importing it and incorporating its

Can someone please help me understand the proper way to import and utilize the three.js library in a vue component? Despite my extensive research, it seems that most individuals use the following code line to import three.js into a vue component. However, ...

Diverse Browser Image Mapping Coordinates

I am in the process of creating an image map, where I specify coordinates on an image that link to other pages. However, I'm encountering an issue where the position of these coordinates is not relative. When viewing the image on a different browser ...

Is it possible to utilize Dictionaries (key, value) collections with JQuery?

Is jQuery capable of supporting a collection of Dictionaries which consists of keys and values? I am interested in organizing the following data into a structured format: [1, false] [2, true] [3, false] This structure should allow for adding, looking up ...

A guide on utilizing the useState hook to tally occurrences of elements within an array - specifically within React's useState hook

Currently, I am tackling an exercise that involves an array containing 10 objects. Each object has properties like txt and color, for example: arr = [{txt: "txt1", color: "red"}, {txt: "txt2", color: "green"}, {txt: ...

The inconsistency of Selenium's StaleElementReferenceException error and the variability of pageload completion codes is causing issues with clicking on elements

Big shoutout to the amazing stackoverflow community for always providing assistance. Lately, I've been grappling with the frustrating "StaleElementReferenceException" issue and haven't found a universal solution yet. Some helpful members have rec ...

The subscription for the second Observable in RxJS concatMap is triggered individually

I am currently developing an Angular 6 application. I want the app to display a loading animation whenever there is a change in the route or if there are any pending HTTP requests. To achieve this, I have set up two Observables as follows: For httpPendingR ...

Issue with Bootstrap error class not functioning in conjunction with hide class

I need to create a form that slides down two input fields when an error occurs, and I want them to have the bootstrap error class. The 'error' class works fine without the 'hide' class being present, but when the 'hide' class ...

Conceal user input field in React using a hook

I am looking for assistance with a form that has 4 input fields: username, password, email, and mobile. My goal is for the email field to disappear if an '@' symbol is typed in the username field, and for the mobile field to disappear if any digi ...

Using jQuery to retrieve a file that has been uploaded through an input field with the type of 'file'

I am looking to extract the file that has been uploaded using an <input type='file'> tag. Currently, when I use $('#inputId').val(), it only retrieves the name of the file, not the actual content of the file itself. I came acros ...

Bypass Security Check in Firefox

I am facing issues while trying to automate selenium on a website owned by a third party. When an authentication prompt like this appears in Firefox, Selenium fails: You can see a similar situation when clicking the Display Image button here Is there a ...

To make table headers remain stationary as the data scrolls with JavaScript

When using Explorer, I was able to fix the "thead" part of my table while scrolling by using CSS expressions. The following CSS code snippet showcases how it's done: .standardTable thead tr { position: relative; top: expression(offsetParent.scrollTo ...

Using jQuery validation to verify that a minimum of one radio button holds a true value

I have a form with two questions. The first question asks if the product value exceeds a certain fixed amount, and the second question asks if the product value is below that fixed amount. Upon submitting the form, validation should ensure that at least on ...

Can javascript be used to swap out the folder in my URL?

I have been searching for solutions on how to change the language of my website using jQuery, but so far I have not found anything that works for me. Let's take my website as an example: www.domain.com I have separate folders for different languages. ...

What is the best way to incorporate raw HTML code into a .jsx file within a NextJS website?

I need to integrate Razorpay code into my NextJS website, but it's currently in pure HTML format. For example, the code looks like this: <form><script src="https://cdn.razorpay.com/static/widget/subscription-button.js" data-subscrip ...

Accessing the current instance in Vue when triggered by a checkbox event

Recently, I tested out a to-do-list application built in Vue that has a checkbox feature to mark completed items. I'm looking for a way to access the current instance from the checkbox event. Here's what I've accomplished so far: myObject ...

What is the method for configuring input in a session without having to submit it?

I am encountering a major issue with my PHP MVC pattern website. On one page, I did not implement a "POST" submit method and now I am facing difficulties in passing quantity to the products/order page. If I use a Submit button, the next page does not funct ...

How can I use React JS to restrict a textbox to only accept numbers and format input as a US mobile number format like (224) - 5623 -2365?

I am currently working on mobile number validation with React. My goal is to allow only digits and format the input as a US mobile number. Although I am successfully restricting the input to only digits, I am facing an issue with formatting. For this tas ...

What could be causing the if/else block in my Redux Toolkit reducer to malfunction?

I'm currently working on a shopping cart feature where I need to increase the quantity of an item if it's already in the cart. If the same item with different sizes is added, they should be displayed separately. I've successfully implemented ...