Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvertedColorScheme cannot be used with SSR #452

Open
arturmiglio opened this issue May 28, 2024 · 1 comment
Open

InvertedColorScheme cannot be used with SSR #452

arturmiglio opened this issue May 28, 2024 · 1 comment
Labels
bug Something isn't working conversation needed Issues that may need to go to a broader conversation

Comments

@arturmiglio
Copy link
Contributor

  • @freenow/wave version: v2 onwards

What was expected to happen?

Since InvertedColorScheme reads from window it fails when statically or server rendering.

@martimalek
Copy link
Contributor

I initially thought this would be simple, but after taking a better look it seems quite complex, I'll write a bit of context here so it's easier to pick up in the future...

The current InvertedColorScheme

Essentially it's a wrapper component that sets the color scheme of it's children to the inverse of the preferred color scheme of the user (e.g. if the user prefers light-scheme it will set it's children to dark-scheme)

How does it work?

It basically does 3 things:

  • Get and store the current preferred color scheme of the user, it does this by using window.matchMedia to check for matches with the prefers-color-scheme media query
  • Listen to changes on the prefers-color-scheme media query and set it's internal state based on that
  • Return a wrapper Box which has the class of the inverse color scheme set (effectively applying the inverse color scheme to it's children)

The problem

When rendering on the server side we don't have the window object and it's APIs, so we can't check media queries. We need a way to know which color scheme the user prefers, this article by Josh Comeau offers some insights into how this may be accomplished for a specific project and some more context around the problem.

@martimalek martimalek added bug Something isn't working conversation needed Issues that may need to go to a broader conversation labels Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working conversation needed Issues that may need to go to a broader conversation
Development

No branches or pull requests

2 participants