Sha256: 46d3be1beea8d4c4f27f374a2e1e7e97a18fac2ee931c847f43d0653527f04f7

Contents?: true

Size: 1.24 KB

Versions: 15

Compression:

Stored size: 1.24 KB

Contents

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  connect() {
    this.checkUrlForDarkMode();
    this.toggleDarkMode();
  }

  checkUrlForDarkMode() {
    const urlParams = new URLSearchParams(window.location.search);
    const dark = urlParams.get('dark');

    if (dark === 'true') {
      document.documentElement.classList.add('dark');
      localStorage.setItem('darkMode', 'true');
    } else if (dark === 'false') {
      document.documentElement.classList.remove('dark');
      localStorage.setItem('darkMode', 'false');
    }
  }

  toggleDarkMode() {
    if (localStorage.getItem('darkMode') === 'true') {
      document.documentElement.classList.add('dark');
      this.element.innerHTML = 'Light Mode';
    } else {
      document.documentElement.classList.remove('dark');
      this.element.innerHTML = 'Dark Mode';
    }
  }

  toggle() {
    let htmlClasses = document.documentElement.classList;
    if (htmlClasses.contains('dark')) {
      htmlClasses.remove('dark');
      localStorage.setItem('darkMode', 'false');
      this.element.innerHTML = 'Dark Mode';
    } else {
      htmlClasses.add('dark');
      localStorage.setItem('darkMode', 'true');
      this.element.innerHTML = 'Light Mode';
    }
  }
}

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
mission_control-servers-0.4.0 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.3.3 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.3.2 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.3.1 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.3.0 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.2.8 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.2.6 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.2.5 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.2.4 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.2.3 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.2.2 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.2.1 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.2.0 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.1.6 app/javascript/mission_control/servers/controllers/dark_mode_controller.js
mission_control-servers-0.1.5 app/javascript/mission_control/servers/controllers/dark_mode_controller.js