Sha256: 2b6440e71dad8eb0f83d18c7ab4329ee83ac8f2b4644158e9fcedb886d646fb4

Contents?: true

Size: 1.87 KB

Versions: 40

Compression:

Stored size: 1.87 KB

Contents

import ApplicationController from "satis/controllers/application_controller"
import { getInitialTheme } from "satis/utils"

/*
 * Theme controller
 *
 *   div data-controller="satis-appearance-switcher" data-action='click->satis-appearance-switcher#switch'
 *     i.fal.fa-sun data-satis-appearance-switcher-target="light"
 *     i.fal.fa-moon-stars data-satis-appearance-switcher-target="dark"
 *
 */
export default class AppearanceSwitcherComponentController extends ApplicationController {
  static targets = ["light", "dark"]

  connect() {
    super.connect()
    const theme = getInitialTheme()
    this.rawSetTheme(theme, false)
  }

  switch() {
    const theme = getInitialTheme()

    if (theme == "dark") {
      this.rawSetTheme("light", true)
    } else {
      this.rawSetTheme("dark", true)
    }
  }

  rawSetTheme(rawTheme, delay) {
    const root = window.document.documentElement
    const eventLight = new CustomEvent('theme-change', { detail: { theme: 'light' } });
    const eventDark = new CustomEvent('theme-change', { detail: { theme: 'dark' } });
    const isDark = rawTheme === "dark"

    if (delay == true) {
      this.lightTarget.classList.add("transition", "ease-in-out", "duration-1000")
      this.darkTarget.classList.add("transition", "ease-in-out", "duration-1000")
    }

    if (isDark) {
      window.dispatchEvent(eventDark);
      this.lightTarget.classList.add("transform", "translate-y-7")
      this.darkTarget.classList.remove("transform", "-translate-y-7")
    } else {
      window.dispatchEvent(eventLight);
      this.lightTarget.classList.remove("transform", "translate-y-7")
      this.darkTarget.classList.add("transform", "-translate-y-7")
    }

    localStorage.setItem("color-theme", rawTheme)
    setTimeout(
      () => {
        root.classList.remove(isDark ? "light" : "dark")
        root.classList.add(rawTheme)
      },
      delay ? 500 : 0
    )
  }
}

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
satis-2.1.47 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.46 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.45 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.44 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.43 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.42 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.41 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.40 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.39 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.38 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.37 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.36 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.35 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.33 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.31 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.30 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.29 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.28 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.27 app/components/satis/appearance_switcher/component_controller.js
satis-2.1.26 app/components/satis/appearance_switcher/component_controller.js