I have a Vuejs single page application that I have built, and I would like to add user-switchable dynamic themes. Something where the user has a button somewhere or something and they can click a 'light' or 'dark' theme option and the whole app updates right away. I would also like to have all of my 'theme' scss files be local.
This answer here is the closest I've found so far, but the themes are not local.
const themes = {
flatly: "https://bootswatch.com/4/flatly/bootstrap.min.css",
materia: "https://bootswatch.com/4/materia/bootstrap.min.css",
solar: "https://bootswatch.com/4/solar/bootstrap.min.css"
};
If I could figure out how to get this answer to work with local theme files that would be great, but switching the link paths out with a local path doesn't work.
const themes = {
dark: "dark-theme.scss",
light: "light-theme.scss"
};
My suspicion is that this doesn't work because of Webpack or some other compiling problem where the scss files are not included.
I have found I can get my theme files to appear in the client source if I add something like the following to my index.html
<link rel="stylesheet" type="text/css" href="<%= BASE_URL %>dark-theme.scss">
But then I don't know how to reference this in my app. Maybe a link like http://localhost:8080/dark-theme.scss
? But then I don't think that would work in production (and it doesn't work locally anyway).
I have also looked at:
- this which is a similar idea to the answer linked above and runs into similar problems
- this which only has scss variables and wouldn't be able to add attributes to a class for example
- this which I don't find to be a very clean solution as it requires all components to know about themes and such
I've also tried doing something like import "@/path/to/theme.scss";
in my main.ts
file which does work, but I don't know how I'd leverage that to make the themes switchable. It just adds an element like the following to my head, which has no id or class so I can't effectively query it to switch it out. Using require
does something similar as well.
<style type="text/css" >
...
</style>