I have a VueJS app that will come with many different themes (at least 20 or so). Each theme stylesheet not only changes things like color and font size, but also the position and layout of some elements as well.
I want the user to be able to switch between these themes dynamically. So, at runtime, the user will be able to open an Options menu and select from a dropdown.
What is the cleanest way to have many dynamic user-selectable themes in VueJS?
I've thought of a couple of ways, such as:
- Dynamically inserting a
<link>
or<style>
tag. While this might work, I don't really see it as particularly "clean", and if I'm loading from AJAX, then oftentimes I'll see a FOUC. - Simply changing the Vue class bindings through a computed property. Something like having a
if-else
chain for every supported theme in every component. I don't particularly like this solution, as then every component I make will need to be updated every time I add a new theme later on.
In React, I think there's a plugin or something that has a <ThemeProvider>
component, where adding a theme is as simple as wrapping it, i.e. <ThemeProvider theme={themeProp}><MyComponent></ThemeProvider>
, and all styles in that theme will apply to that component and all child components.
Does VueJS have something similar, or is there a way to implement it?