Dealing with unsupported CSS properties in Next.js or implementing @supports directives in Chakra-UI

Currently, I am working on a React component that adjusts opacity values based on the support for the CSS backdrop-filter directive:

background={(() => {
  const opacity = isBackdropFilterSupported() ? 0.75 : 0.98
  return (
    `linear-gradient(
      180deg, rgba(76, 63, 143, ${opacity}) 62.76%,
      rgba(184, 169, 255, ${opacity}) 100%
    )`
  )
})()}

The challenge arises when the site is server-side rendered with Next.js. The server generates false as the value returned by

CSS.supports('backdrop-filter', 'blur(1px)')
, leading to inconsistencies regardless of client properties.

To address this issue, one potential solution involves utilizing CSS in the following manner:

.drawer {
  --opacity: 0.75;
  background: linear-gradient(
    180deg, rgba(76, 63, 143, var(--opacity)) 62.76%,
    rgba(184, 169, 255, var(--opacity)) 100%
  );
}
@supports not (backdrop-filter: blur(1px)) {
  .drawer { --opacity: 0.98; }
}

This approach aims to circumvent the server-side rendering issue and be interpreted correctly by the client. However, integrating such styles into Chakra-UI, which this project is built upon, poses a challenge due to the lack of guidance on how to do so.

Answer №1

In my initial post, I failed to mention that I encountered an error message stating:

Prop id did not match. Server: "toggle--gxfg3t7xwo" Client: "toggle--ki0j10p2l"
.

This discrepancy indicated that the browser-generated DOM differed from the Next.js-generated DOM. As a result, Next.js abandoned its rehydration attempt, leading to server-rendered values being displayed.

To address this issue, I implemented a hook to detect when the component was mounted on the client side. The implementation of this hook is as follows:

export const useMounted = () => {
  // https://www.joshwcomeau.com/react/the-perils-of-rehydration/
  const [hasMounted, setHasMounted] = React.useState(false);
  React.useEffect(() => {
    setHasMounted(true);
  }, []);
  return hasMounted;
};

Subsequently, the opacity calculation was modified to account for the mounting status:

const hasMounted = useMounted()
⋮
const opacity = hasMounted && isBackdropFilterSupported() ? 0.75 : 0.98

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

Having trouble with my initialData in react-query - any suggestions on how to fix it?

I encountered an issue while trying to implement code using initialData in React Query. The output does not display as expected, and the key is not visible in the react-query-dev-tool. View image here import { useQuery, useQueryClient } from 'react-qu ...

What is the best way to retrieve the URL of a website using scrapy?

Currently, I am faced with the challenge of scraping the Amazon website using Scrapy. While I have successfully extracted items like product titles and prices, I am struggling to figure out how to extract the URL of a product (as shown in the image at the ...

Struggling to locate production files with the combination of `fast-glob` and `process.cwd()` when dealing with dynamic route segments

In the directory structure, I have a blog page located at app/[locale]/home/blog/page.tsx. import glob from 'fast-glob' import path from 'path' const BlogPage = async () => { let pages = await glob('**/*.mdx', { cwd: ...

Exploring ways to style font families for individual options within ng-options

I am looking to display a combobox where each option has a different font. While using the ng-options directive in AngularJS to populate the options for a <select> tag, I am struggling to set the font-family for individual options. $scope.reportFon ...

The 'dehydratedState' attribute is not found in the 'object' type

Encountered an error stating "Property 'dehydratedState' does not exist on type '{}'" when attempting to include a pageProps dehydratedState. The goal was to set up the Next.js app for React Query following the documentation provided on ...

Product sketching libraries in JavaScript

Can somebody assist me in locating a suitable JS library that is capable of generating a product sketch similar to this one: Partition sketch? I am currently using Next.js, and I am encountering difficulties with most libraries due to Next.js utilizing SSR ...

What is the method for triggering two actions within a single linked tag?

I have a link tag structured like this: <a href="<?php echo base_url().'dashboard' ?>" class="check_session">Home</a> Upon clicking the "Home" link, it should navigate to the dashboard. At the dashboard page, I want to check i ...

Adjusting the CSS class name based on the screen size and applying styles to that class only during the "onload" event

The website I am currently developing can be found at . It is built on WordPress using the Avada theme and everything seems to be functioning correctly. The image move up effect on this site is achieved with the following JavaScript: window.onload = funct ...

Using display:inline-block will increase the vertical spacing

I am currently dealing with the following HTML and CSS setup: * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } body { margin: 0; } .row { background-color: red; /* display: inline-block; */ } ul { ...

`The logo does not dim when the popup is displayed`

I'm currently experiencing an issue with pop-ups on my page - when they appear, they create a shade over all elements of the page except for my two logos and are displayed with a border around them. Here's what my page looks like before the popu ...

Even after assigning the class "img-fluid" to my image, it still fails to properly adjust to the screen size

I added the class "img-fluid" to my image in Bootstrap, but it's not fitting on my webpage. What should I do? <img src="images\406201.jpg" class="img-fluid" alt="..."> <div style="margin-top: 0p ...

TinyMCE version 5.x - Stand out with a specific selection in a personalized drop-down navigation bar

In my customized TinyMCE 5.x dropdown menu, there are 3 options that adjust the width of the editor. I am looking for a way to indicate the currently selected option, but I am unable to interact with the menu items once they are initialized. It seems like ...

What techniques can be used to maintain the alignment of an image while the surrounding text changes?

Having trouble aligning my image without affecting its position. When the wind text is hidden (JavaScript not functioning in CodePen), meaning it is not displayed below the temperature, the weather image shifts upwards. I attempted to use 'absolute&ap ...

Error message: Next.js throws a ReferenceError stating that the 'exports' is not defined

I have encountered an error while using react-datetime-picker v3.5.0 with Next.js. Unhandled Runtime Error ReferenceError: exports is not defined Call Stack eval node_modules/react-datetime-picker/dist/DateTimePicker.js (8:0) ./node_modules/react-datetime ...

Issue: Unable to connect to Cloud Firestore backend following transition from static data to process.env variables

Encountered an error: @firebase/firestore: Firestore (9.9.0): Unable to establish connection with Cloud Firestore backend. Connection failed 1 times. after updating my firebase configuration from apiKey: "SecretApiKey", authDomain: "Se ...

Creating a fullscreen layout in HTML with specific minimum and maximum widths

Looking to create a layout where one item takes up 1/3 of the screen-width with a minimum width of 500px and a maximum width of 700px, while another item fills the rest of the screen. Ideally, setting a width of 66% should work, but issues arise when the h ...

How can I address the variations in CSS appearance between Firefox and Google Chrome?

I'm currently working on a table with two cells in each row just for fun. For the first row, I want both cells to display the same picture. To achieve this, I wrote the following CSS: tr:nth-child(1){ background-image:url("cat.jpg"); background-size: ...

What is causing justify-content to not function properly in React?

Struggling to center two divs within a container using flexbox. Flex-wrap and align-items are working fine, but justify-content just won't cooperate. :( <div className="container"> <div className="card- ...

Unable to submit form in Nextjs using react-bootstrap

Instructions To create a registration form, follow these steps: Fill out the form on the register page and then click submit. The react-bootstrap button will trigger the handleSubmit() function using onSubmit={}. Expected vs Actual Outcome I attempted va ...

Having trouble with jQuery's recursive animation functionality

I have developed a function to scroll multiple images horizontally in the header of my website. Inside my header, I have implemented code that looks like this: <div class='images'> <!-- this div is 150% wide with overflow hidden --> ...