Sha256: 0936760d5ce32cb3c5e1a3e35071753c852fa68e0f287820f13176c6ee26bcd0

Contents?: true

Size: 1.26 KB

Versions: 66

Compression:

Stored size: 1.26 KB

Contents

// hello_controller.js
import { Controller } from "stimulus"
export default class extends Controller {
  static targets = [ "dropdown", "button" ]

  connect() {
    window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
      const theme = localStorage.getItem('good_job-theme');
      if (!["light", "dark"].includes(theme)) {
        this.setTheme(this.autoTheme());
      }
    });

    this.setTheme(this.getStoredTheme() || 'light');
  }

  change(event) {
    const theme = event.params.value;
    localStorage.setItem('good_job-theme', theme);
    this.setTheme(theme);
  }

  setTheme(theme) {
    document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? this.autoTheme() : theme);

    this.buttonTargets.forEach((button) => {
      button.classList.remove('active');
      if (button.dataset.themeValueParam === theme) {
        button.classList.add('active');
      }
    });

    const svg = this.buttonTargets.filter(b => b.matches(".active"))[0]?.querySelector('svg');
    this.dropdownTarget.querySelector('svg').outerHTML = svg.outerHTML;
  }

  getStoredTheme() {
    return localStorage.getItem('good_job-theme');
  }

  autoTheme() {
    return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
  }
}

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
good_job-3.26.0 app/frontend/good_job/modules/theme_controller.js
good_job-3.25.0 app/frontend/good_job/modules/theme_controller.js
good_job-3.24.0 app/frontend/good_job/modules/theme_controller.js
good_job-3.23.0 app/frontend/good_job/modules/theme_controller.js
good_job-3.22.0 app/frontend/good_job/modules/theme_controller.js
good_job-3.21.5 app/frontend/good_job/modules/theme_controller.js
good_job-3.21.4 app/frontend/good_job/modules/theme_controller.js
good_job-3.21.3 app/frontend/good_job/modules/theme_controller.js
good_job-3.21.2 app/frontend/good_job/modules/theme_controller.js
good_job-3.21.1 app/frontend/good_job/modules/theme_controller.js
good_job-3.21.0 app/frontend/good_job/modules/theme_controller.js
good_job-3.20.0 app/frontend/good_job/modules/theme_controller.js
good_job-3.19.4 app/frontend/good_job/modules/theme_controller.js
good_job-3.19.3 app/frontend/good_job/modules/theme_controller.js
good_job-3.19.2 app/frontend/good_job/modules/theme_controller.js
good_job-3.19.1 app/frontend/good_job/modules/theme_controller.js
good_job-3.19.0 app/frontend/good_job/modules/theme_controller.js
good_job-3.18.3 app/frontend/good_job/modules/theme_controller.js
good_job-3.18.2 app/frontend/good_job/modules/theme_controller.js
good_job-3.18.1 app/frontend/good_job/modules/theme_controller.js