Sha256: 7ac925cc436b2f248035a25503253e0d9882aa77426799bc6069edb681f1b7a8

Contents?: true

Size: 1.33 KB

Versions: 28

Compression:

Stored size: 1.33 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.defaultState === 'collapsed') {
      return this.userState === '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

28 entries across 28 versions & 1 rubygems

Version Path
avo-2.13.2.pre.2 app/javascript/js/controllers/menu_controller.js
avo-2.13.2.pre.1 app/javascript/js/controllers/menu_controller.js
avo-2.12.1.pre.1 app/javascript/js/controllers/menu_controller.js
avo-2.12.0 app/javascript/js/controllers/menu_controller.js
avo-2.11.3.pre.3 app/javascript/js/controllers/menu_controller.js
avo-2.11.3.pre.2 app/javascript/js/controllers/menu_controller.js
avo-2.11.3.pre.1 app/javascript/js/controllers/menu_controller.js
avo-2.11.2.pre.3 app/javascript/js/controllers/menu_controller.js
avo-2.11.2.pre.2 app/javascript/js/controllers/menu_controller.js
avo-2.11.2.pre.1 app/javascript/js/controllers/menu_controller.js
avo-2.11.1.pre.3 app/javascript/js/controllers/menu_controller.js
avo-2.11.1 app/javascript/js/controllers/menu_controller.js
avo-2.11.1.pre.2 app/javascript/js/controllers/menu_controller.js
avo-2.11.1.pre.1 app/javascript/js/controllers/menu_controller.js
avo-2.11.0 app/javascript/js/controllers/menu_controller.js
avo-2.10.3.pre.1 app/javascript/js/controllers/menu_controller.js
avo-2.10.2 app/javascript/js/controllers/menu_controller.js
avo-2.9.2.pre1 app/javascript/js/controllers/menu_controller.js
avo-2.10.0 app/javascript/js/controllers/menu_controller.js
avo-2.9.1.pre7 app/javascript/js/controllers/menu_controller.js