What is the best way to make a panel width adjust automatically to fit the screen width?

I have developed a React/Material UI application that includes a Portal which showcases a Panel and a Widget.

Main App: Show Portal and Set Background Color to Blue

export default function App() {
  return (
    <div
      style={{
        backgroundColor: "blue"
      }}
    >
      <Portal></Portal>
    </div>
  );
}

You can access my codesandbox via this link: codesandbox.

The focal points of interest are Portal.js, and Layout.js.

Layout sets out specifications for different screen sizes in order to optimize the responsiveness of the Portal when adjusting to window dimensions.

Portal positions the Widget at the top left corner, and effectively "flexes" the Panel to shift below the widget at "xs" and "sm" breakpoints, and to the right of the widget at "md", "lg", and "xl" breakpoints.

While the Widget maintains its aspect ratio, its dimensions may vary depending on user interaction.

Portal incorporates two primary methods - the React functional component and the Material UI makeStyles function.

Portal: React Functional Component

export default function Portal() {
  const selWidHeight = 400;
  const selWidWidth = 800;

  const layout = Layout(selWidHeight / selWidWidth);
  const sizes = {
    containerWidth: layout.selWidgetWidth,
    containerHeight: layout.selWidgetHeight,
    
    // more code here...
  };

  // more code here...

  return (
    <>
      <>
        
        // more code here...
        
      </>
    </>
  );
}

Answer №1

I have successfully implemented a solution that I am quite pleased with. It took some effort to refactor my application in order to accommodate this change.

I would like to give credit to various sources that provided key elements for my fix: you can find more information here and here.

To sum it up, now my Portal can be displayed in two modes: Large Width and Small Width. The Small Width mode arranges the Panel under the Widget, using the Grid mechanism as described in my initial question.

For the Large Width mode, I opted not to use the material ui Grid as it did not offer the level of control I needed to ensure dynamic resizing of the Panel. Instead, I used material-ui Container both as the container and the Panel item. Then, following the suggestions from the link above, I applied styling accordingly. The crucial step here is to dynamically monitor changes in the width and height of the containing windows, and adjust the widths of the div and its contents accordingly.

<Container className={classes.rootContainer}>
              <Container
                ref={playerContainerRef}
                className={classes.widgetContainer}
                disableGutters={true}
                maxWidth={false}
              >
                {my_widget}
              </Container>

              <Container
                className={classes.bookmarkPanelContainer}
                disableGutters={true}
              >
                {my_panel}
              </Container>
            </Container>

The styling for this setup includes:

rootContainer: {
      display: "flex",
      maxWidth: (props) => props.viewWidth,
    },
    widgetContainer: {
      width: (props) => props.widgetContainerWidth,
      height: (props) => props.widgetContainerHeight,
      minWidth: (props) => props.widgetContainerWidth,
      backgroundColor: "red",
      display: "flex",
      flexDirection: "row",
      flexWrap: "wrap",
      justifyContent: "center",
      alignItems: "center",
      position: "relative",
    },
    bookmarkPanelContainer: {
      overflowY: "scroll",
      border: "1px solid red",
      float: "left",
      height: (props) => props.bookmarkPanelContainerHeight,
      position: "relative",
    },

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

Choosing a sibling element before another using CSS

Is there a way to make both of my icons display some text when hovered over? I managed to make it work, but the leftmost icon doesn't trigger the span element when hovered. The text needs to appear on the left side of the Yelp icon for design reasons. ...

What causes fs to produce an error when routing to a new page, yet refreshing the page resolves the issue?

Concern: I have developed a NextJs application with 4 input fields, each connected to a predefined options list read in as a json file within the project. The user can select two fields and then proceed to a search page by clicking a button. From the sear ...

Utilizing the onCLICK event handler with numerous parameters

I'm in need of assistance with creating a function that involves multiple variables, preferably at least two... The table I am working with was generated using PHP and MySQL, all IDs were dynamically created which is why I am looking for a way to cap ...

Styling the navigation menu for mobile devices

How can we transition from a menu like this : https://i.sstatic.net/qQ2k1.png to this when the screen size reaches a specific width: https://i.sstatic.net/AUM4v.png In essence, replacing certain texts with icons. Is pre-defining it and changing the dis ...

Adding content to an empty element will not produce the desired result

I'm trying to show each character of a string, which is stored in an array, one at a time. However, when I use threadsleep(a) with the code found here: http://jsfiddle.net/thefiddler99/re3qpuoo/, all the characters are displayed at once. There seems t ...

Extracting the month and year from a datetime string: A simple guide

I am working with a JSON object that includes a field called Month with a string datetime value- { Month : "31-Jan-2022 12:00 AM (EST)" .... .... } Is there a way to extract the Month Name and Year from this string using JavaScript's dat ...

jQuery unable to target Bootstrap button

I've been experiencing some trouble trying to attach a listener to a button I made with Bootstrap. <button type="button" id="buttonone" class="btn btn-default btn-lg good"> <span class="glyphicon glyphicon-minus" aria-hidden="true">< ...

What is the best way to transfer PHP data to an HTML tag?

I have a growing HTML file and a separate PHP document. The PHP document is responsible for fetching subscriber numbers and viewing statistics from my YouTube channel, and I need this information to be displayed in the HTML code. Here is the PHP script: ...

Pressing a button triggers the highlighting of a tab in HTML through the use of Javascript

In the corner of my webpage, I have three tabs: info, question, order. When I click on one tab header, only that tab should highlight. The info section includes two buttons that link to the question and order tabs. When these buttons are pressed, the respe ...

Showing React numerical properties based on whether they are passed from the parent component

I have two distinct containers that utilize the ControlSection component. One passes the bestScore prop, and the second passes the level prop. Within the ControlSection component, I aim to only display the props in an <h2> tag if they are passed fro ...

Encountering an issue with receiving an empty array after submitting a form in redux-form integrated with material

In my project, I am implementing redux-form along with material-ui. You can find detailed instructions on how to do this here. Here is a snippet of the code I have written: import React from 'react'; import { Field, reduxForm } from 'redux ...

How can I construct a frame-like structure using PHP and HTML?

Looking to create a page with a two-frame structure. The left frame will always display a small form with 10 entries that, upon submission, trigger some processing and display results. The left frame must remain accessible at all times, while the right fra ...

Unable to insert line breaks using " " or "<br>" in a textarea element

I'm struggling to insert a predefined string with line breaks into a <textarea> using React/NextJS. I've tried various methods, including using &#13; and <br>, but none seem to be effective: setTextAreaValue('a&#13;bc< ...

I am trying to use ajax to upload a photo in ASP.NET MVC, but when the file reaches the controller, it is showing

I am trying to upload a profile photo using Ajax in ASP.NET MVC, but I am encountering an issue where the upload parameter is coming as null to the action method. Here are the HTML codes for the file upload section: <div class="preview"> <di ...

The issue of font settings not being recognized in NextJS/React

My website allows users to sign in, and I have specified the font for the title as "Carter One". However, when a user signs in, a different font appears that I did not specify. Interestingly, if I click the Refresh button, the correct font will then appea ...

What is the typical layout for a React project's directory structure (or component library)?

Searching for the optimal folder structure for my React project has been a challenge. The current webapp layout I'm using looks like this: app/ scripts/ app.jsx Component.jsx __tests__/ Component-test.jsx ...

Material-UI's simplification of 10px comprehension

I'm a bit confused about why we have to do this for the 10px simplification: html { font-size: 62.5%; /* 62.5% of 16px = 10px */ } Shouldn't the following code handle everything? const theme = createMuiTheme({ typography: { // Set the ...

Using jQuery to show text upon hover using a `for` loop

I'm currently working on a webpage and incorporating a feature where hovering over specific div elements triggers the display of certain text in another div. There are 5 elements that I want to make hoverable and show text upon interaction. After imp ...

How can I open a new window, redirect the current one, and bring focus to the new window using JavaScript?

Trying to troubleshoot a problem I'm having with the following setup: - Using SAP Portal, I am launching an HTML page containing this code. - The goal is for the HTML page to open a new window. - Once the new window opens, the original HTML page ...

Unable to choose the specific div element within the container that has an inset shadow applied by utilizing Pseudo

It seems that I am encountering an issue when trying to select the div elements within a container that has a defined shadow using pseudo elements ::before OR ::after. Once the shadow is applied, the div elements become unselectable. Please refer to the c ...