Sha256: e3a0825425ce3f3f93b14d7e38c07b1948e37bbffbe6f4894239b2c174571b1e

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

import { Controller } from "@hotwired/stimulus"

const BOTTOM_THRESHOLD = 0

export default class extends Controller {
  static targets = [ "button", "menu" ]
  static classes = [ "flip" ]

  #closeTimer

  show() {
    this.#resetTimer()
    this.menuTarget.show()
    this.#updateExpanded()
    this.#orient()
  }

  close() {
    this.menuTarget.close()
    this.#updateExpanded()
  }

  closeLater() {
    this.#closeTimer = setTimeout(() => this.close(), 300)
  }

  toggle() {
    this.menuTarget.open ? this.close() : this.show()
  }

  closeOnClickOutside({ target }) {
    !this.element.contains(target) && this.close()
  }

  #resetTimer() {
    clearTimeout(this.#closeTimer)
  }

  #orient() {
    this.menuTarget.classList.toggle(this.flipClass, this.#distanceToBottom < BOTTOM_THRESHOLD)
  }

  #updateExpanded() {
    this.buttonTarget.ariaExpanded = this.menuTarget.open
  }

  get #distanceToBottom() {
    return window.innerHeight - this.#boundingClientRect.bottom
  }

  get #boundingClientRect() {
    return this.menuTarget.getBoundingClientRect()
  }
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
css-zero-0.0.47 lib/generators/css_zero/add/templates/app/javascript/controllers/popover_controller.js
css-zero-0.0.46 lib/generators/css_zero/add/templates/app/javascript/controllers/popover_controller.js
css-zero-0.0.45 lib/generators/css_zero/add/templates/app/javascript/controllers/popover_controller.js