container-query-polyfill icon indicating copy to clipboard operation
container-query-polyfill copied to clipboard

Container query not working after a react-route navigation.

Open Enebz opened this issue 2 years ago • 6 comments

I have a little demo here: https://github.com/Enebz/container-query-demo

Here, I noticed that the container queries doesn't work after navigating to a new page (using react-router v6)

When you are located in / and click the button that forwards you to /demo The grid of items appears with a column width of 3, which is the default style (mobile first) and not the correct container query style even if you are on a wide screen.

The container queries are located at: https://github.com/Enebz/container-query-demo/blob/master/src/index.css#L142

If you open devtools and resize the width of the container, the container queries work and the grid goes to the correct grid column size.

NOTE You probably noticed that the container I am assigning container-type: size; to is of width: 100% and the same width as the screen, so why not just use @media? Well, this is only a demo and in my real project I have a sidebar which "pushes" the main content, such that I need a container query to correctly set the grid-column size.

Enebz avatar Apr 04 '22 12:04 Enebz

I'm having a similar issue with Vue and Vite. After every HMR update the layout resets without the polyfill. Then I have to refresh the page for the polyfill to kick in.

martinszeltins avatar Apr 25 '22 06:04 martinszeltins

Same issue with Next.js router

kafkas avatar May 24 '22 08:05 kafkas

Same issue with Next.js router

You could try to hook into the router's navigation and re-initialize the polyfill on each navigation.

martinszeltins avatar May 24 '22 08:05 martinszeltins

Seems like there's no exported function to re-initialize

yuuk avatar Aug 06 '22 07:08 yuuk

Seems like there's no exported function to re-initialize

You can fork the lib and add a re-initialize function. That's what I did.

martinszeltins avatar Aug 06 '22 07:08 martinszeltins

Seems like there's no exported function to re-initialize

You can fork the lib and add a re-initialize function. That's what I did.

I'm not really sure how to go on about that.. I'm still a fresh developer with not so much experience. Could you maybe show me how you did it?

Enebz avatar Aug 06 '22 13:08 Enebz

This should be addressed by the latest version of the polyfill (currently in alpha). Please give it a try. I'm going to close this issue for now, but I would be happy to reopen it if you're still running into problems.

Thank you!

devknoll avatar Aug 16 '22 16:08 devknoll

I am not able to make this work, I am trying to find the re initialization function, but I have no idea where to start.

Enebz avatar Sep 10 '22 12:09 Enebz

I am not able to make this work, I am trying to find the re initialization function, but I have no idea where to start.

@Enebz there is no re-initialization function, you have to create it yourself.

martinszeltins avatar Sep 12 '22 05:09 martinszeltins

I realized that, but as devknoll said, it was "addressed" in the alpha version.. I thought addressed meant "explained how to do", but it meant that it is now a feature. So I made it work.

Enebz avatar Sep 17 '22 20:09 Enebz