Customizing Bootstrap styles when toggling

I have been working on incorporating a dark/light theme into my project. By creating a separate SCSS file that is imported after the main Bootstrap file, I was able to override certain core Bootstrap styles to achieve a dark/light mode. Now, I am facing the challenge of enabling the user to switch between these two modes while the project is running.

One approach I tried was to use a toggle button that adds the 'dark-mode' class to the body. However, this method seems to neglect all the Bootstrap-specific styles:

Here is a snippet of my custom SCSS:

.dark-mode{
    
    $body-bg: rgb(41, 41, 41);
    $card-bg: rgb(78, 78, 78);

    $primary: rgb(0, 183, 255);
    $secondary: white;
    $danger: rgb(255, 60, 60);
    $success: rgb(1, 197, 1);

    h6, h1, h4, h5, p{
        color: white;
    }

    $dark: white;
    $light: $body-bg;
    $modal-content-bg: $card-bg;

    $carousel-indicator-height: 0px;

}@import '../../node_modules/bootstrap/scss/bootstrap.scss';

Javascript/Vue:

toggleDarkMode(){
    const el = document.body
    el.classList.toggle("dark-mode");
  }

Although I can observe the addition of the 'dark-mode' class to the body upon clicking the toggle button, and see the changes in the headings (h6, h1, etc.), the Bootstrap-specific styles remain unaffected. I initially assumed this process would suffice since removing the 'dark-mode' class parentheses from the overrides in the SCSS file and reloading the browser successfully activates dark mode.

It appears there might be a more appropriate method to achieve this, but I lack expertise in SCSS. Any guidance on this matter would be greatly appreciated.

Thank you!

Answer №1

Just like the issue addressed in this particular query, it is essential to include the @import statement for "bootstrap" within the .dark-mode class definition to effectively reset the Bootstrap variables. Additionally, it is important to note that there might be a necessity to redefine the CSS variables...

@import "functions";
@import "variables";
@import "mixins";

.dark-mode{
    
    $body-bg: rgb(41, 41, 41);
    $card-bg: rgb(78, 78, 78);

    $primary: rgb(0, 183, 255);
    $secondary: white;
    $danger: rgb(255, 60, 60);
    $success: rgb(1, 197, 1);
    
    $theme-colors: (
        "primary": $primary,
        "secondary": $secondary,
        "success": $success,
        "danger": $danger,
        "info": $indigo,
        "dark": $dark,
        "light": $light,
    );


    h6, h1, h2, h4, h5, p {
        color: white;
    }

    $dark: white;
    $light: $body-bg;
    $modal-content-bg: $card-bg;

    $carousel-indicator-height: 0px;
    
    --#{$variable-prefix}body-color: #{$body-color};
    --#{$variable-prefix}body-bg: #{$body-bg};

    @import "bootstrap";

}

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

Unable to find the matching closing parenthesis ')" before reaching the end

I've been struggling to create a regular expression that can identify strings like the ones below: var p1=@VAL([Test1Q1].[Bandwidth]) var p2=@VAL([Test1Q1].[Usages (KB)]) The criteria is to find strings starting with @VAL( and ending before the firs ...

Activate jqGrid's navigation bar save icon when the ondblClickRow event is triggered

After setting the jqGrid row edit on the ondblClickRow event, how can I enable the save icon on the navigation bar when a row is double clicked? ondblClickRow: function (rowid) { jQuery(this).jqGrid('editRow', rowid, true, null, null); ...

Tips for assigning unique non-changing keys to siblings in React components

I've been searching for a solution for more than an hour without success. The structure of the data is as follows: const arr = [ { id: 1, title: 'something', tags: ['first', 'second', 'third'] }, { id: 2, t ...

Utilizing jQuery to Sort Table Rows based on an Array of Class Names

I have a table containing values and a filter option where users can select multiple values to filter the table. I want to create a filter with numbers ranging from 1 to 10, and assign class names like filter_1, filter_2, filter_3, etc. to each table row ( ...

Live Node.js and Next.js apps experiencing issues with functioning websockets

Within my nodejs backend at https://backend.example.com, the following code resides in my server.js file: const WebSocket = require('ws'); const server = new WebSocket.Server({ port: 7500 }, () => { console.log('S ...

Setting up angular-cli project for rc5- Step by step guide

Trying to integrate angular-cli with angular 2 rc5 but facing challenges: The issue of 'Promise' not being found After attempting to install 'npm install -g angular-cli@webpack', typings did not get installed, resulting in WebStorm un ...

Sending an Angular scope variable to JavaScript

I am working with an angular scope variable that is being used in ng-repeat. My goal is to create a chart that starts from a specific date, ends at another date, and includes a marker for the current day. I am utilizing loader.js for drawing the charts in ...

Troubleshooting JavaScript AJAX: Challenges with the GET Method in Web API Core

Struggling with a basic ajax method and feeling lost without proper backend explanation in tutorials used for work. Seeking to display a message from the backend along with additional text and a corresponding message. Currently working with HTML/CSS/Java ...

Tips on implementing VueStripe in a vue2 Project

I recently integrated Vue Stripe into my vue2 Project and encountered 2 similar errors in my code : Property 'redirectToCheckout' does not exist on type 'Vue | Element | (Vue | Element)[]'. Property 'publishableKey' does ...

What is the process of extracting a utility function from a helper file on a node.js server?

I'm facing a challenge with my node/express server where I need to access a function from a helper file in my app.js. The function in the helper file looks like this: CC.CURRENT.unpack = function(value) { var valuesArray = value.split("~"); ...

Vue app experiencing issues with .env file not displaying all characters

My .env file has the following variable defined: VUE_APP_CLIENT_SECRET=$2a$10$tgYzYgMgTsdfLlRFtkd9IeEc2W4v/0EuG1uf05ztQARqhsf3esr9D5Zi When I try to access this variable using process.env.VUE_APP_CLIENT_ID, it only returns "/0EuG1uf05ztQARqhsf3esr9D5 ...

Ways to navigate to a different page in React when a user clicks?

When working on my react app, I encountered an issue where the useHistory hook was undefined. How can I troubleshoot this problem and ensure that useHistory is properly defined? App.js import 'bootstrap/dist/css/bootstrap.css' import React f ...

Combining d3 and vue.js for enhanced interactive visualizations

I am currently in the process of integrating D3 with vue.js and I am following a straightforward guide available here: The guide suggests appending a new div for each new bar, which I have successfully accomplished using a custom directive as shown in the ...

Is there a way to detect and handle errors triggered by a callback function?

My component has the following code snippet: this.loginService.login(this.user, () => { this.router.navigateByUrl('/'); }); Additionally, my service contains this method: login(credentials, callback) { co ...

Unlocking the potential of input values in Angular.jsDiscovering the secret to

I'm currently experimenting with the angular date picker directive. My goal is to retrieve the entered date value from the date picker and log it to the console. However, all of my attempts so far have been unsuccessful. Here's a snippet of my c ...

Tips for preloading a script in nextjs

I'm having trouble incorporating a script into my website. The issue is that the script doesn't load properly the first time the page loads, but after a few refreshes, it works and the responsible iFrame shows up. I've attempted several di ...

Try out a Vue.js Plugin - A Comprehensive Guide

Currently, I am delving into the world of Vue.js. In my journey, I have crafted a plugin that takes the form of: source/myPlugin.js const MyPlugin = { install: function(Vue, options) { console.log('installing my plugin'); Vue.myMetho ...

Infinite loop always occurs with Ui-router FromState being constantly reset

My page is experiencing continuous refreshing and calling $stateChangeStart after the first call to $state.go. I believe this issue may be related to the StateProvider configuration. Can anyone offer suggestions on what might be going wrong? Check out thi ...

transmit JSON formatted form data to an AngularJS platform

I have a webpage built on AngularJS with a login feature. I want to iframe this webpage onto my own page and automatically log in my users. Upon inspecting the AngularJS site, I noticed that the login procedure expects a json object. I have tried multipl ...

In WordPress, the magic_quotes feature is essential for json_decode to function properly

In my current project on the client side, I am dealing with an array of JavaScript objects. Upon submission, this array needs to be sent to PHP using a form and then further manipulated on the server-side. As I work on building or modifying the array of o ...