Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically switch theme with vmware clarity

Vmware Clarity 0.10.16 just released the new dark theme. This is great!

They described how to add that new theme, but nothing about the possibility to dynamically change it inside the page. Is it because it is not doable?

If it is, how can I do it with Angular 4+? Any site that could help me explaining how to realize that?

Thanks in advance!

like image 369
David Avatar asked Dec 01 '17 23:12

David


2 Answers

Clarity now ships with stylesheets for both light and dark themes. We document how to consume them here with both angular-cli or webpack build configurations. That means that once the app is built, that is only style it has.

I have some ideas about how to implement a theme switcher for toggling between the two. Here is a rough idea I might start my prototype with:

  1. Build the app without either of the theme css files (no clarity style at all)
  2. Copy both css files to the assets folder (during the build)
  3. Write a directive or component that can take an @Input of the the src for a stylesheet in the <head>
  4. Store the both paths /path/to/light.css and /path/to/dark.css in a service so the app could pass the active theme value around and modify it when needed.
  5. Add the service to the app components where we want to let users update the theme.

Does this give you some ideas for your app?

I'll update here after I have a prototype working so you can see it in action and find the source code.

like image 168
hippeelee Avatar answered Sep 25 '22 14:09

hippeelee


I've built a proof of concept how you could do this, but it does have some limitations. Since you can only include one theme file at a time, there can be some lag for the correct theme to render since it happens later in the Angular rendering cycle. It would only big a big deal when the cache is empty for a browser, as subsequent visits would render quickly, but that is a major consideration here. Its a start that you might be able to build something more robust upon.

https://stackblitz.com/edit/clarity-theme-switcher?file=app%2Fapp.component.ts

like image 20
Jeremy Wilken Avatar answered Sep 24 '22 14:09

Jeremy Wilken