I have a custom-built Vue.js single page application and I am interested in implementing user-switchable dynamic themes. I envision a scenario where users can easily toggle between 'light' and 'dark' themes by clicking a button, and have the entire app update instantaneously to reflect their selection. Additionally, I want all the theme scss files to be stored locally for better control.
While I came across this resource, it falls short as it does not address the issue of using local theme files.
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"
};
I attempted to modify the answer provided by replacing the external links with local paths:
const themes = {
dark: "dark-theme.scss",
light: "light-theme.scss"
};
However, this approach seems to be ineffective due to potential Webpack or compilation issues that prevent the inclusion of scss files in the project.
In my exploration, I discovered that manually linking the theme files in the index.html file allows them to appear in the client source:
<link rel="stylesheet" type="text/css" href="<%= BASE_URL %>dark-theme.scss">
Yet, I struggle with understanding how to reference these files within the application itself. For instance, referencing
http://localhost:8080/dark-theme.scss
might not work in production.
I also investigated other resources like:
- this post, which encounters similar hurdles to the one mentioned above
- this thread, focusing on scss variables without accommodating attribute modifications
- this article, proposing an approach involving theme-aware components
Moreover, I tested importing the scss files directly in the main.ts
file, which successfully added them to the head section but did not enable seamless switching between themes. The lack of identifiable classes or IDs made it challenging to manipulate them effectively.
<style type="text/css" >
...
</style>