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

Document how to force color mode dynamically (e.g. from a useFetch response) #242

Open
plushdohn opened this issue Mar 22, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@plushdohn
Copy link

Is your feature request related to a problem? Please describe.

Related to #100. In that issue @Atinux said it's not possible with Nuxt 2, but I still couldn't figure out how to do it with Nuxt 3.

Describe the solution you'd like

I think this might be a good addition to the Force a Color mode section of the docs.

Describe alternatives you've considered

I tried overriding during SSR both useColorMode().preference and useColorMode().value but none of them work, the page still flashes.

Additional context

I'd gladly make a PR to document this if I knew how to do it 😄 Maybe the server plugin can be modified to not only check the meta of the target page during routing but also check if a colorMode.value was manually set?

@plushdohn plushdohn added the enhancement New feature or request label Mar 22, 2024
Copy link
Contributor

Atinux commented Mar 22, 2024

What is your use case to depend on a fetch call for forcing the color mode?

@plushdohn
Copy link
Author

What is your use case to depend on a fetch call for forcing the color mode?

In my specific case we are using Nuxt as a sort of site builder, and each site can customise its theme which includes light or dark color schemes. I'm aware it's not the best case for a11y as you would probably still want to follow the user's preference, but sites are design-heavy and highly customisable so it's a tradeoff we accept. For now we have to tolerate a light mode flash on dark sites.

The linked issue had another use-case which is forcing the color mode for a specific article in a blog with a call to a CMS.

@Atinux
Copy link
Contributor

Atinux commented Mar 22, 2024

Is it for specific pages or for the whole site @plushdohn ?

@plushdohn
Copy link
Author

Is it for specific pages or for the whole site @plushdohn ?

Specific for the "public sites" part of the app. The landing page and the dashboard for the site builder obey the user's preferences for color mode.

Copy link
Contributor

Atinux commented May 22, 2024

One solution would be to use a page middleware @plushdohn that fetch the pages that need to force a specific mode.

See example on https://stackblitz.com/~/edit/github-qvp2nk?file=middleware/color-mode.global.ts

@plushdohn
Copy link
Author

plushdohn commented May 22, 2024

Thanks @Atinux, that seems to work but not in our case since our fetch call may fetch thousands of articles, not just a few routes. That said, we might be able to just fetch the next route's info and keep it in cache as it SSRs after middleware.

Although I do wonder if there's a way to simply set the related attribute on the html element during SSR? Since we can already manually set the preference, the problem is just the flash during SSR. What's the blocker for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants