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

Hooks for when color scheme changes #105

Open
ImpossibleReality opened this issue Aug 10, 2021 · 3 comments
Open

Hooks for when color scheme changes #105

ImpossibleReality opened this issue Aug 10, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@ImpossibleReality
Copy link

I want to be able to apply a css transition when the color mode changes, but there doesn't seem to be an easy way to do that without forking the color mode module. I have already considered doing the css transitions in the button that changes the color mode, but then it will not transition if the system theme changes.

I think it would be nice if there was some way of running code before the color scheme changes.

@ImpossibleReality ImpossibleReality added the enhancement New feature or request label Aug 10, 2021
Copy link
Contributor

atinux commented Aug 10, 2021

Cannot you apply the transition using CSS directly?

@ImpossibleReality
Copy link
Author

Let me clarify what I want to do. I currently have something like this in my global css:

* {
  transition: background-color 0.5s;
}
.preload * {
  transition: none !important;
}

The preload class is given to <body> on ssr, then removed after the client mounts.

This isn't really ideal, since if I don't want an element to have a background transition then I have to explicitly specify that, or if I want an element to have a transition when the page is mounting (ie a loader/transition) then I have to override the !important, which soon becomes a mess.

It would be nice if I could apply a transition class to <body> only when the color mode changes and remove it after the transition is finished.

A way I could do this is by applying the class when someone clicks on the button to change the scheme, but then you don't get a transition when the color mode is system, and the user changes their default color mode.

Copy link
Contributor

atinux commented Aug 24, 2021

I am using a transition for the example and does not have any issue:

https://codesandbox.io/s/github/nuxt-community/color-mode-module?file=/example/assets/main.css:657-693

You can create a plugin colorMode.client.js and use $colorMode.watch if you want advanced usage:

export default function ({ $colorMode }) {
  $colorMode.$watch("value", (c) => {
    console.log("$colorMode.value is", c);
  });
}

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