Struggles arise when the Flexbox container fails to accurately calculate its width and causing overflow

My goal is to create a reusable React library that can take a list of components and display them in a flex container. One key feature is that the library recalculates the component's width when it mounts or when the window is resized. If the total width of the child components exceeds the parent component's width, the excess children are hidden and a button is added at the end of the list to display them in a vertical dropdown. The component has a minimum width defined by a prop, but otherwise calculates its width based on the flex dom node containing all the children elements. The component itself is also a flex item with a flex property of

1 0 <value of minWidth property>
.

The issue arises when I try to resize the component without using overflow: hidden on the flex container. Without this styling, the component fails to shrink and maintain its specified minimum width as intended. This behavior can be observed in the provided demo below. With overflow: hidden enabled, resizing works as expected, but it causes problems when child components render sub-components like dropdowns or date-pickers that need to remain visible.

I have considered several solutions involving tinkering with the child components themselves, such as using position: fixed, but since this library is meant to be flexible and work with any list of components, I prefer a fix at the parent container level if possible.

.js file

const left = document.querySelector('.left');

const right = document.querySelector('.right');

const handleWindowResize = event => {
  console.log('left: ', left.clientWidth);
  console.log('right: ', right.clientWidth);
};

window.addEventListener('resize', handleWindowResize);

.scss file

.toolbar {
  display: flex;
  width: 100%;
  border: 1px solid black;
}

.left, .right {
  display: flex;
  // overflow: hidden;
}

.left {
  flex: 1 0 200px;

}

.right {
  flex: 1 0 300px;
  justify-content: flex-end;
}

.item {
  border: 1px solid black;
  width: 100px;
  height: 50px;
}

.html file

<div class='toolbar'>
  <div class='left'>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
  </div>
  <div class='right'>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
  </div>
</div>

Check out the Codepen demo here

Answer №1

The issue with using "overflow: hidden" as a solution is that it can cause problems when certain child components render sub-components like dropdowns or date pickers, which may end up being hidden due to the parent's "overflow: hidden". In scenarios where this is necessary, and the sub-component is absolutely positioned (anchored to the child component)...

This is a common dilemma, especially with modal windows. In such cases, utilizing "overflow: hidden" might be the only viable option available.

Many comprehensive libraries for dropdowns and date pickers provide a workaround known as an "escape hatch" feature, allowing you to append the dropdown portion to a specified element, usually the body. If a library lacks this feature, it's likely because no one has raised this concern yet.

To emphasize my point, consider the following examples:

  • React Select:

    React-select offers a menuPortalTarget prop, enabling you to portal the select menu to a designated DOM node.

  • react-dates: example
  • A previous response of mine

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

In order to avoid conflicts, please update the version code of your APK as there is currently one with version code 1. How can I go about changing the version?

My attempt to upload an apk file on Google Play has hit a roadblock. The apk file was created using Unity3D. Google Play is giving me an error message stating that I need to use a different version code for my APK because a version with code 1 already exis ...

In a game developed with Polymer technology: showcasing different images based on the accumulated score

I am currently in the process of developing a JavaScript and HTML game using the Polymer framework. As it stands, players earn points upon completing tasks, and at the end of each round, their total score is displayed on the screen. What I aim to do now i ...

Experiencing difficulty inputting data into database using PDO

Attempting to save user input data to a database using PDO, I have created a form for users to input their information. However, when I submit the form, it redirects to proses_input.php and displays a blank page. I tried combining the code into one file, b ...

Broken responsive carousel using bootstrap

Looking for some help with a responsive bootstrap carousel issue. The carousel items adjust themselves below each other when the screen size is reduced. I want them to display as single items in the carousel. Here are the screenshots and the code. Please ...

Angular 8: ISSUE TypeError: Unable to access the 'invalid' property of an undefined variable

Can someone please explain the meaning of this error message? I'm new to Angular and currently using Angular 8. This error is appearing on my console. ERROR TypeError: Cannot read property 'invalid' of undefined at Object.eval [as updat ...

Preserving text input with line breaks in a MERN Stack application

Can you help with saving multiple paragraphs in MongoDB? I have a textarea where users can input multiple paragraphs, but the line space is not being saved correctly in the database. Here is how I want the submitted data to look: Lorem ipsum dolor sit am ...

Enforcing HTTPS in Next.js version 13

I've encountered a challenge while working with Next.js 13 and Heroku - there seems to be limited information on handling SSL with this combination. Although I have implemented a solution derived from this blog post below, Google is not happy with the ...

Forming triangles with outlines

Recently, I had the challenge of designing speech bubbles and found a clever technique to create the triangular tip at the end using CSS. By setting the element's width and height to 0 and playing around with borders, you can achieve diagonal shapes f ...

The validity of AngularJS form is constant and always returns true with formName.$valid

I am facing an issue with my Angular application form where even though the input fields are blank, formName.$valid is always true. Below is the HTML code for my form: <form name="contactForm" novalidate ng-submit="processForm(formData)" autocomplete=" ...

The image conversion should generate a shortened URL instead of the base code

Presently, I am uploading an image onto a canvas and then converting the canvas into an image using toDataURL. However, this conversion results in a lengthy base code being generated. What I'm looking for is a shorter URL instead of the Base Code. ...

Is it possible to create rounded or curved top corners for a box using HTML?

I'm looking to create a card with rounded top corners and wondering if there is a way to achieve this. Currently, here is the code I have: <div id="card1"> <div id="card1i"></div> </div& ...

Picture with bordered corners - area underneath the image

Looking to create an image with borders in opposite corners, using ::after and ::before. Almost there - but the image isn't covering the whole space in the div. There is some weird empty space below the image (marked red in the jsfiddle). I'm stu ...

Receive the button click event outside of the canvas

Is there a way to have separate click events for the button and the ListItem? My focus is on the button click event only, without triggering the ListItem event. Live DEMO import React from "react"; import ReactDOM from "react-dom"; import ListItem from ...

HTML table with scroll feature displays gaps between consecutive rows

I've created a scrolling table with some header line spacing showing up when scrolled. Do you know how to hide it? Check out this Codepen example Here's the HTML structure: <div class="table-container"> <table class=" ...

Updating application state using information received from backend express server

I'm new to development and seeking some advice. I'm working on pulling data from an external API through my backend, which successfully displays the response in the console/backend. However, when I try to use that data on the frontend by modifyin ...

Increasing the font size on a document

Once again, faced with my own silly problems. This time I stumbled upon the following page: http://jsfiddle.net/g3VBT/ I am trying to style the "enter your U.R.L here" text with the following properties: font-weight: 600; font-size: 1.5em; The issue ...

The sidebar is misaligned, not following the left alignment set by Bootstrap

While experimenting with Bootstrap, I encountered an issue where an element in the sidebar remains at the bottom instead of aligning to the right as intended. https://i.sstatic.net/LdoS8.jpg I attempted to float it to the right, but its position didn&apo ...

Tips for changing array items into an object using JavaScript

I am working with a list of arrays. let arr = ["one","two"] This is the code I am currently using: arr.map(item=>{ item }) I am trying to transform the array into an array of sub-arrays [ { "one": [{ ...

Why does Node.js prompt an error when using document.querySelector? Is there an alternative method to document when utilizing express in my project?

An issue arose when attempting to select the form element using const weatherForm = document.querySelector('form') ^ Unfortunately, a ReferenceError was encountered: document is not defined at Object. (/Users/sheebadola/development/quadacademy/w ...

Transform the values of an array within a JSON object into a single string by looping through them

Here is a JSON object that I need to transform: // TableData { "0": { "damage_type": "Scratch", "regions": [ "front side", "back side" ], ...