Tips for dynamically modifying style mode in Vue.js

I am looking to implement a feature where the style can be changed upon clicking a button.

In my scenario, I am using Sass/SCSS for styling.

For example, I have three different styles: default.scss, dark.scss, and system.scss. The code for dark.scss looks like this:

// Mode
$mode: dark;

// Initialize
@import "init";

// Components
@import "./core/components/components";
@import "components/components";

// Layout
@import "layout/layout";
@import "./core/layout/docs/layout";

And in app.vue:

<style lang="scss">
@import "assets/sass/dark";
</style>

In my test.vue file, I have created buttons to switch between styles:

<v-btn @click="light" />
<v-btn @click="dark" />

Is it possible to change the style with a button click? How can I achieve this, for instance, changing @import "assets/sass/dark"; to @import "assets/sass/light"; from the file app.vue?

Answer №1

You have the option to conditionally import using sass

<style lang="scss">
.dark-theme {
    @import "assets/sass/dark";
}
.light-theme {
    @import "assets/sass/light";
}
</style>

Apply the class to your main App div

<div :class="selectedTheme">
    
</div>

There are various ways to manage the selectedTheme class, but you probably already know how to do it

Answer №2

This problem was resolved by implementing the following steps:

  1. Set the mode to store in localstorage and then reload the page
const changeMode = (mode) => {
  localStorage.setItem("mode", mode);
  window.location.reload();
};
  1. Verify the mode stored in localstorage and load the appropriate style
if (localStorage.getItem("mode") && localStorage.getItem("mode") == "dark") {
  require("@/assets/sass/plugins.dark.scss");
  require("@/assets/sass/style.dark.scss");
} else {
  require("@/assets/sass/plugins.scss");
  require("@/assets/sass/style.scss");
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adding an existing element to the DOM using Javascript/jQuery

I am facing an issue in my DOM where I have an element with two children, a div block and a button. My goal is to add a new copy of the same div block to the parentNode whenever the button is clicked. Currently, I am using the following code in the button ...

Adding information into sqlite from a json document

There seems to be an issue here, I'm unable to make an insertion: $.getJSON('file.json', function(data) { tx.executeSql("INSERT INTO table (DATE, LIB, BID) VALUES("+data.date+","+data.Lib+","+data.BID+")"); }); ...

Angular does not display HTML content until the requested http data has been fully loaded

I am experiencing an issue where certain HTML content does not load until the component has received data from an API call through a service. Below is the relevant code snippet: import { ApiService } from './services/api.service'; @Component({ ...

Ensure that the background div extends all the way to the bottom of the page

Is there a clever CSS technique that allows a div to extend all the way to the bottom of the screen without displaying any visible content inside? Or am I better off using JavaScript for this purpose? Thank you, Richard ...

IntelliJ is indicating a typescript error related to react-bootstrap-table-next

Working with react-bootstrap-table-next (also known as react-bootstrap-table2) has been causing a Typescript error in my IntelliJ environment, specifically on the validator field within my column definition. Despite trying various solutions, such as adding ...

Using TypeScript and webpack, include the access_token in the http header when making requests with axios

I need to include an access_token in the header of axios, following this example: https://github.com/axios/axios#global-axios-defaults Currently, I am fetching the access_token using razor syntax, which is only accessible in CSHTML files. https://github ...

What is the significance of static constants?

In my TypeScript project, I'm working on creating a constants file that will provide an Opaque token object when ServiceConstants.AUTH_SERVICE_TOKEN is called. I've experimented with two approaches: Using ServiceConstants.ts as a namespace exp ...

Dropdown does not populate with data

HTML content <select class="form-control" tabindex="-1" id="superId" name="superId[]" multiple="multiple" required="required" data-bind="options: SupArray, optionsText: ' ...

How can I modify a dynamically generated table to include rowspan and colspan attributes in the rows?

My table was automatically created using data from the database. var rows = ""; rows += "<tr class='row_primary'>"; rows += "<td>COL 1</td>"; rows += "<td>COL 2</td>"; rows += "<td> ...

Disregard any unrecognized variables associated with the third-party package

I've been working on integrating the bluesnap payment gateway into a react/ts project. I added their hosted javascript code to my public/index.html and started the integration within a component. However, when compiling, an error pops up saying ' ...

Creating a grid layout similar to Tumblr using HTML and CSS

Struggling all day to figure out how to create a Tumblr-style grid for my website, I'm reaching out here for some assistance. Here is the page: I have a grid of images on my project page that initially looked fine, but as I try to add new elements, i ...

Position an element in the middle of the range between the tallest and shortest characters

Is there a way to vertically center an element between the tallest and shortest characters of another element (preferably using just CSS, but JavaScript is also acceptable)? I want it to be aligned with the actual text content rather than the line height, ...

Issue with displaying ng-repeat data in AngularJS tbody

I am experiencing an issue with using ng-repeat inside a tbody element. Here is the code snippet that is causing trouble: <tbody> <tr ng-repeat="group in groups"> <td>{{ group.name }}</td> </tr> </tbody> Wh ...

`How can I manage my electron.js application effectively?`

As a newcomer to electron.js, I have successfully created a game using html, css, and javascript that currently runs offline on the client side. However, I am now looking for a way to access, analyze, and make changes to this app. One solution could be lo ...

Steps for showcasing the data entered in the input box

I am looking to enhance my skills in working with Angular and JavaScript. I would greatly appreciate any feedback on the issue I am facing here. My goal is for the text box input to display: "hello {name} , would you like to play a game? However, curr ...

The NestJS framework encountered an error due to a method being undefined in the

Encountering Error with NestJS Function create123: TypeError - Cannot read properties of undefined (reading 'create123') The constructor is displayed below \`export class AuthenticationService { constructor( private readonly usersServ ...

What are some ways to customize the appearance of VueJS components?

I am new to Vue, so I apologize if this question seems silly or if I have overlooked something. I believed that you could target a custom element like this: my-element { styles go here} However, when I created an element, it appears that I can only targe ...

Displaying updated information in Angular

I recently developed a chat application using Angular that utilizes the stomp socket from @stomp/ng2-stompjs. To display all messages, I am leveraging *ngFor. <p *ngFor="let item of messages" style="padding: 5px; font-size: 18px"> <span style ...

When an event is triggered in Angular Component, the object is not properly defined in the handler causing errors

While experimenting with createjs and angular, I encountered an issue when trying to update my stage after an image loads. It seems like there might be a problem related to Angular in this situation. Upon completion of the load event on the Bitmap object a ...

Error: Javascript unable to generate video thumbnail using ffmpeg

I'm having trouble generating a thumbnail from a video file using JavaScript. I keep encountering the error below: ChildProcessError: `/workspace/node_modules/@ffmpeg-installer/linux-x64/ffmpeg -ss 0 -i valid-video-link -f image2 -vframes 1 -vf scale= ...