Sha256: e94ec0f5faf30103c98b4902cb043979fdd2618c4d8de38e7d327401522f20bb

Contents?: true

Size: 1.32 KB

Versions: 213

Compression:

Stored size: 1.32 KB

Contents

import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
  static targets = ['svg', 'items', 'self'];

  collapsed = true;

  get key() {
    return this.selfTarget.getAttribute('data-menu-key-param')
  }

  get defaultState() {
    return this.selfTarget.getAttribute('data-menu-default-collapsed-state')
  }

  get userState() {
    return window.localStorage.getItem(this.key)
  }

  set userState(payload) {
    window.localStorage.setItem(this.key, payload)
  }

  get initiallyCollapsed() {
    if (!this.userState) {
      return this.defaultState === 'collapsed'
    }

    return this.userState === 'collapsed'
  }

  connect() {
    if (this.initiallyCollapsed) {
      this.collapsed = true
      this.markCollapsed()
    } else {
      this.collapsed = false
      this.markExpanded()
    }
  }

  triggerCollapse() {
    this.collapsed = !this.collapsed
    this.userState = this.collapsed ? 'collapsed' : 'expanded'

    this.updateDom()
  }

  updateDom() {
    if (this.collapsed) {
      this.markCollapsed()
    } else {
      this.markExpanded()
    }
  }

  markCollapsed() {
    this.svgTarget.classList.add('rotate-90')
    this.itemsTarget.classList.add('hidden')
  }

  markExpanded() {
    this.svgTarget.classList.remove('rotate-90')
    this.itemsTarget.classList.remove('hidden')
  }
}

Version data entries

213 entries across 213 versions & 1 rubygems

Version Path
avo-2.17.1.pre.1.zeitwerk.eager.load.dir app/javascript/js/controllers/menu_controller.js
avo-2.17.0 app/javascript/js/controllers/menu_controller.js
avo-2.16.1.pre.1.nativefields app/javascript/js/controllers/menu_controller.js
avo-2.16.0 app/javascript/js/controllers/menu_controller.js
avo-2.15.3 app/javascript/js/controllers/menu_controller.js
avo-2.15.3.pre.1.data.attrs.to.sidebar.items app/javascript/js/controllers/menu_controller.js
avo-2.15.2 app/javascript/js/controllers/menu_controller.js
avo-2.15.2.pre.1 app/javascript/js/controllers/menu_controller.js
avo-2.15.1 app/javascript/js/controllers/menu_controller.js
avo-2.15.0 app/javascript/js/controllers/menu_controller.js
avo-2.14.3.pre.7.polytranslations1 app/javascript/js/controllers/menu_controller.js
avo-2.14.3.pre.6.nosprockets app/javascript/js/controllers/menu_controller.js
avo-2.14.3.pre.5.nosprockets app/javascript/js/controllers/menu_controller.js
avo-2.14.3.pre.4.tosqlfix app/javascript/js/controllers/menu_controller.js
avo-2.14.3.pre.3.jsbundling app/javascript/js/controllers/menu_controller.js
avo-2.14.3.pre.2.tailwindcss app/javascript/js/controllers/menu_controller.js
avo-2.14.3.pre.1.branding app/javascript/js/controllers/menu_controller.js
avo-2.14.2 app/javascript/js/controllers/menu_controller.js
avo-2.14.2.pre.1 app/javascript/js/controllers/menu_controller.js
avo-2.14.1 app/javascript/js/controllers/menu_controller.js