Adjusting the media query breakpoints based on the visibility of the side panel

I have been tasked with enhancing an already extensive React project by adding a 400px wide side panel that is only visible under certain conditions.
Depending on the screen size, the side panel should either appear in the empty space to the right of the main content (at its maximum width) or alongside the main content (reducing the content width by 400px). The main content should adapt responsively in both scenarios. For example, if the side panel is displayed, a three-column layout should switch to a one-column layout "400px earlier".

The project utilizes Bootstrap 4.6 and its media queries.

My initial approach was to replace the media queries for the main content with container queries. While this find and replace process was straightforward and easily automated, it functioned well in Chromium browsers but not in Firefox (despite using a polyfill like this, likely due to conflict with the Bootstrap classes named as container, see here).

This led me to the following solution:

Here is a basic snippet of HTML:

<body>
  <div className=`site-wrapper ${ showPanel ? '' : 'full-width'}`>
    <div class="container">
    <!--- main content --->
    ...
    </div>
    { showPanel && (
    <div class="side-panel">
    ...
    </div>
    )}
  <div>
</body>

Next, I need to convert all media queries similar to the one below:

@media (min-width: 992px) {
  .container{
    max-width: 960px;
  }
}

into the following format:

@media (min-width: calc(992px + 400px)) {
  .container {
    max-width: 960px;
  }
}

@media (min-width: 992px) {
  .site-wrapper.full-width .container{
    max-width: 960px;
  }
}

In essence, this involves:

1.) Duplicating each media query
2.) Replacing the <breakpoint> in the first copy with calc(<breakpoint> + 400px)
3.) Adding .site-wrapper.full-width to each rule in the second copy (which can be facilitated with postcss-nested)

While effective, this method is laborious and error-prone. I have been unable to automate the "conversion" process entirely (possibly achievable with tools like find-matching-bracket). However, overall, the solution feels more like a hack rather than a refined approach.

Is there a more efficient way to achieve this? Perhaps a reliable method to automate the CSS transformation task?

Answer №1

Here is the code snippet for creating a selector based on a visible or hidden sidebar that changes the background color of the content block when the panel is expanded.

document.querySelector('.show').addEventListener('click', function() {
  document.querySelector('.side-panel').classList.toggle('active')
});
document.querySelector('.close').addEventListener('click', function() {
  document.querySelector('.side-panel').classList.toggle('active')
});
body {
  padding: 0;
  margin: 0;
}

.side-panel {
  background-color: #ddd;
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  width: 200px;
  min-height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  transition: .2s;
  transform: translate(-200px);
}

.side-panel.active {
  transform: translate(0);
}

.container {
  transition: .2s;
  min-height: 100vh;
}

.active+.container {
  background-color: red;
}
<div class="side-panel">
  <button class="close">X</button>
</div>
<div class="container">
  <button class="show">show side-panel</button>
</div>

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

Position the <a> to the right side of the div

Is there a way to right-align the <a> element, which contains a Button with the text Push Me, within the <div> (<Paper>)? https://codesandbox.io/s/eager-noyce-j356qe This scenario is found in the demo.tsx file. Keep in mind that using ...

Guidelines for attaching a div to the bottom of a page?

I have a navigation menu inside div.wrapper, and I am trying to make the div.wrapper stick to the footer. <div class="wrapper"> <div id="menu"> <ul> <li>1.</li> <li>2.</li> ...

Looking to modify the contents of a shopping cart by utilizing javascript/jQuery to add or remove items?

I'm dealing with a challenge on my assignment. I've been tasked with creating a Shopping Cart using Javascript, HTML5, and JQuery. It needs to collect all the items from the shop inside an Array. While I believe I have most of it figured out, I a ...

SVGs appear at the forefront of all other elements

is where this issue is arising.... specifically in the date selection feature on the left side of the options panel. When using both Google Charts and Material Icons with the date picker component from https://github.com/nickeljew/react-month-picker, we a ...

Safely render user-generated HTML content within a React application to prevent cross-site scripting vulnerabilities

A unique situation has arisen in our React app where a user inputs a string that is displayed to other users. The challenge lies in searching for specific characters within the string and replacing them with HTML elements. For example, transforming the wor ...

When viewed on mobile browsers, the side-by-side divs in a tabbed layout appear to be

Looking for some help with responsive design for the "PORTFOLIO ATTRIBUTES" tab on my website. On desktop, the content displays fine, but on mobile it overlaps and cuts off. Here's the link to the page in question: . Any suggestions on how to maintai ...

Understanding how much of a video stream has been viewed by a user using PHP

I am currently involved in a project that involves displaying video advertisements to users, and after 60 seconds, users are presented with a link. My supervisor wants to ensure that users watch the entire 60 seconds of the video. I am now wondering if th ...

Leveraging Vue's "v-slot" functionality to create a specified slot within a JavaScript object

I have turned to this platform seeking guidance on using "v-slot" while utilizing a syntax that involves a javascript object. This specific method was introduced in the online course Intro to Vue3 which I recently completed. Below is an image de ...

When an excessive amount of text is added, a column begins to resemble a row

As I work on developing a practice app that involves sending messages, I've encountered a styling issue when the message length exceeds a certain limit. My scenario includes a user profile picture with the message displayed alongside it. However, whe ...

Exploring CSS animations by utilizing the transform and color properties

I am currently working on a project that involves animating text (specifically "happy birthday to you") and a heart. The idea is for the heart to drop and hit each word one by one, turning them yellow upon impact. Once the last word is hit, the text shou ...

Utilize PHP and a dynamic web framework like Spry to showcase data from two MySQL tables in an

My goal is to effectively showcase data from two separate tables in mysql using PHP and spry. Initially, I considered implementing spry tabbed panels, but I am uncertain of its feasibility. Essentially, I envision the tab names being populated from one tab ...

Data loaded by the load() function disappears following an ajax action

I need assistance with a sorting page I am creating that displays content from a database and allows users to sort it using listjs. The issue I am facing is that when I click on any button, the loaded content disappears. Strangely, manually adding content ...

Check if any element within a class satisfies the specified condition using Jquery

I am looking to determine if any element within a specific class meets a set of conditions. For example: $(document).on('click','.modalInner_form_nav', function(){ var input = $(this).parents('.modalInner_form').find(' ...

Is there a way to adjust the placement of the h1 tag using the before pseudo-element?

I'm looking to place an image on the left side of each h1 tag. I've been attempting to utilize the before pseudo element for this task. The issue arises when there is no content, causing the before pseudo element to overlap with the h1 tag. How ...

Ensuring image stays centered always

I have a logo image that I want to center regardless of window size. Despite trying auto margins, I haven't been successful in achieving this. The logo image needs to overlap the bottom of my navigation bar, which is positioned above the logo. HTML ...

Getting rid of the excess space at the bottom of a Bootstrap card - tips and tricks!

I encountered an issue while developing a blog app. The problem is that instead of resizing to fit the size of the card, the content is occupying the entire column space. https://i.sstatic.net/CGRig.png Here's the code snippet (Jinja2) {% extends &q ...

CSS menu - Shifting bar: What's the issue?

I'm struggling to add a moving bar to my website's navigation and need some help. I tried following a tutorial at the following link: (http://css-tricks.com/jquery-magicline-navigation/) Here is what I ended up with: If anyone could take a look ...

Changing the size of circles in text and adjusting the dimensions of SVG elements

As a novice JavaScript programmer, I am attempting to resize circles and SVG elements based on the window size. Currently, my code creates circles of varying sizes, but I haven't been able to adjust them in relation to text size. var width = 600; va ...

Implementing a customized mobile style sheet for Google Maps v3

Currently, I am attempting to enforce the Mobile stylesheet for Google Maps v3 JavaScript within a jQueryMobile application. Within the screen stylesheet, the zoom control consistently stays in the top left corner and my efforts to relocate it have proven ...

Using CSS to overlay multiple text boxes onto an image at different positions

Can you help me solve this challenge? I have an image of a real property that will be randomly loaded into a DIV with a width of 200 pixels. The image needs to be resized to 200 pixels wide while maintaining its proportional height. Additionally, I have fo ...