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

What is the best way to incorporate standard HTML into a React .jsx file?

I'm diving into the world of ReactJS and currently navigating through a web project that was written by someone else. As I work on this project, I am simultaneously learning about React. One specific part of the project is the login screen, which inc ...

Unable to conceal the scrollbar while keeping the div scrollable

I've been attempting to implement the techniques outlined in the guides on this site, but I'm encountering difficulty hiding the scroll bar while still maintaining scrolling functionality! My current approach involves setting the parent as relat ...

What causes images to unexpectedly expand to fill the entire screen upon switching routes in Next.js?

I'm in the process of creating a website using Next and Typescript, with some interesting packages incorporated: Framer-motion for smooth page transitions Gsap for easy animations One issue I encountered was when setting images like this: <Link&g ...

What is the best way to access a component generated using the .map() function from a separate component?

My objective is to create a grid of toggle buttons that store their selected values in an array. This array will then be used to display another row of buttons based on the selections. Once a button in the new row is chosen, it should deselect the corresp ...

Guide to simultaneously displaying two modals in react.js with either MUi or tailwindcss

Can anyone help me implement a modal with the same functionality as the share feature on Google docs or Google drive? I am looking for guidance on how to achieve this and if there are any examples available on CodePen or similar code sharing websites. htt ...

What could be causing the append function to only work during the final iteration of my code?

I find myself in the position of recreating a table based on another existing one. While I am aware that this may not be the ideal approach, it is a task that has been assigned to me rather than being my own choice. My method involves iterating through ea ...

The useEffect hook in React is missing a dependency called Props

In my React/Redux project, I am experiencing an issue with an action that is supposed to retrieve data from the Redux state every time a component is mounted. Unfortunately, my current approach is not working as intended. The error message I'm encoun ...

Modifying an object's label based on a particular value using JavaScript

In my current project involving React.js charts, I am looking to organize data by month. In Django, I have set up a view to display JSON containing the total events per month in the following format: [ { "month": "2022-06-01T00:00:0 ...

Issues with Curved Corner Touchdown Feature

Having trouble with getting the round css to work properly in the below code It seems to be only applying the background color, but not the border styling The border code is on the same line as the round css: style=" border : 1px solid #D6D6D6; and t ...

Error with NEXTJS: Prop type failed - The prop `href` in `<Link>` is expecting a `string` or `object`, but received `undefined`

After importing the Link from next/link and attempting to pass the dynamic endpoint in my components, I encountered an error message. https://i.stack.imgur.com/eqUK8.png https://i.stack.imgur.com/eIC4V.png I searched for a solution and came across a sug ...

Achieving a smooth transition from a blur-out effect to a blur-in effect on a background Div

I created a blur in/out effect for my landing page, but it's not functioning as expected and I'm not sure why. It blurs out correctly, but the issue arises when I want the underlying Div to fade in. Currently, it just appears abruptly after the ...

Tips for utilizing ng class within a loop

Having some trouble with my template that loops through a JSON file using json server. The issue I'm facing is related to correctly applying ng class when clicking on icons. Currently, when I click on an icon, it adds a SCSS class but applies it to al ...

Incorporate the operating hours of a Business

If I specifically choose Sunday and Monday with working hours ranging from 08.00 to 20.00, the output should be 1&08:00&20:00,2&08:00&20:00. How can I achieve this using Vue.js? This is my current code: <script> submitBox = new Vue( ...

Upon reacting with Typescript, the window will transition to the homePage, however, it will also reset

Trying to redirect this component to the HomePage</code causes the data to restart once it reaches the home page.</p> <p>Any recommendations for an alternative to <code>window.location.href = "/HomePage"? import React, { useE ...

Instructions for activating column resizing in MUI DataGrid

Is there a way to enable column resizing for users in MUI DataGrid? It's enabled by default on XGrid, but I would like to enable it on Datagrid as well. Any assistance is appreciated. <DataGrid className={classes.table} ...

Using Javascript to place a form inside a table

When attempting to add a Form inside a Table, only the input tags are being inserted without the form tag itself. $(function () { var table = document.getElementById("transport"); var row = table.insertRow(0); var cell1 = row.insertCell(0); ...

Text overlapping image causing hover state interference

I'm feeling completely lost right now. My goal is to have the title of each project displayed in the center of the screen when you hover over the respective project images. I've resorted to using jQuery for this because it seems like the most str ...

Updates made to the Transform property in Mui Theme are not appearing in the browser

I'm looking to modify the MuiInputLabel.outlined. While I can see various changes when inspecting in the browser, I'm having trouble seeing the transform property change specifically. This is the current configuration of my MuiInputLabel: MuiInp ...

Understanding the process of extracting multiple parameters from dynamic routing in Next.js

Consider the following folder structure: - src - pages - jobs - [id].js If we have a URL like this: http://localhost:3000/jobs/123 You can access 123 inside the [id].js page. Now, what if we need to add another parameter called ti ...

Applying ngClass to a row in an Angular material table

Is there a way I can utilize the select-option in an Angular select element to alter the css-class of a specific row within an Angular Material table? I have successfully implemented my selection functionality, where I am able to mark a planet as "selecte ...