|
| 1 | +import { Controller } from '@hotwired/stimulus'; |
| 2 | + |
| 3 | +export default class extends Controller { |
| 4 | + static values = { |
| 5 | + delayDuration: Number, |
| 6 | + // Using targets does not work if the elements are moved in the DOM (document.body.appendChild) |
| 7 | + // and using outlets does not work either if elements are children of the controller element. |
| 8 | + wrapperSelector: String, |
| 9 | + contentSelector: String, |
| 10 | + arrowSelector: String, |
| 11 | + } |
| 12 | + static targets = ['trigger']; |
| 13 | + |
| 14 | + connect() { |
| 15 | + this.wrapperElement = document.querySelector(this.wrapperSelectorValue); |
| 16 | + this.contentElement = document.querySelector(this.contentSelectorValue); |
| 17 | + this.arrowElement = document.querySelector(this.arrowSelectorValue); |
| 18 | + |
| 19 | + this.side = this.wrapperElement.getAttribute('data-side') || 'top'; |
| 20 | + this.sideOffset = parseInt(this.wrapperElement.getAttribute('data-side-offset'), 10) || 0; |
| 21 | + |
| 22 | + this.showTimeout = null; |
| 23 | + this.hideTimeout = null; |
| 24 | + |
| 25 | + document.body.appendChild(this.wrapperElement); |
| 26 | + } |
| 27 | + |
| 28 | + disconnect() { |
| 29 | + this.clearTimeouts(); |
| 30 | + this.element.appendChild(this.wrapperElement); |
| 31 | + } |
| 32 | + |
| 33 | + clearTimeouts() { |
| 34 | + if (this.showTimeout) { |
| 35 | + clearTimeout(this.showTimeout); |
| 36 | + this.showTimeout = null; |
| 37 | + } |
| 38 | + if (this.hideTimeout) { |
| 39 | + clearTimeout(this.hideTimeout); |
| 40 | + this.hideTimeout = null; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + show() { |
| 45 | + this.clearTimeouts(); |
| 46 | + |
| 47 | + const delay = this.hasDelayDurationValue ? this.delayDurationValue : 0; |
| 48 | + |
| 49 | + this.showTimeout = setTimeout(() => { |
| 50 | + this.doShow(); |
| 51 | + this.showTimeout = null; |
| 52 | + }, delay); |
| 53 | + } |
| 54 | + |
| 55 | + hide() { |
| 56 | + this.clearTimeouts(); |
| 57 | + this.doHide(); |
| 58 | + } |
| 59 | + |
| 60 | + positionElements() { |
| 61 | + const triggerRect = this.triggerTarget.getBoundingClientRect(); |
| 62 | + const contentRect = this.contentElement.getBoundingClientRect(); |
| 63 | + const arrowRect = this.arrowElement.getBoundingClientRect(); |
| 64 | + |
| 65 | + let wrapperLeft = 0; |
| 66 | + let wrapperTop = 0; |
| 67 | + let arrowLeft = null; |
| 68 | + let arrowTop = null; |
| 69 | + switch (this.side) { |
| 70 | + case 'left': |
| 71 | + wrapperLeft = triggerRect.left - contentRect.width - (arrowRect.width / 2) - this.sideOffset; |
| 72 | + wrapperTop = triggerRect.top - (contentRect.height / 2) + (triggerRect.height / 2); |
| 73 | + arrowTop = contentRect.height / 2 - (arrowRect.height / 2); |
| 74 | + break; |
| 75 | + case 'top': |
| 76 | + wrapperLeft = triggerRect.left - (contentRect.width / 2) + (triggerRect.width / 2); |
| 77 | + wrapperTop = triggerRect.top - contentRect.height - (arrowRect.height / 2) - this.sideOffset; |
| 78 | + arrowLeft = contentRect.width / 2 - (arrowRect.width / 2); |
| 79 | + break; |
| 80 | + case 'right': |
| 81 | + wrapperLeft = triggerRect.right + (arrowRect.width / 2) + this.sideOffset; |
| 82 | + wrapperTop = triggerRect.top - (contentRect.height / 2) + (triggerRect.height / 2); |
| 83 | + arrowTop = contentRect.height / 2 - (arrowRect.height / 2); |
| 84 | + break; |
| 85 | + case 'bottom': |
| 86 | + wrapperLeft = triggerRect.left - (contentRect.width / 2) + (triggerRect.width / 2); |
| 87 | + wrapperTop = triggerRect.bottom + (arrowRect.height / 2) + this.sideOffset; |
| 88 | + arrowLeft = contentRect.width / 2 - (arrowRect.width / 2); |
| 89 | + break; |
| 90 | + } |
| 91 | + |
| 92 | + this.wrapperElement.style.transform = `translate3d(${wrapperLeft}px, ${wrapperTop}px, 0)`; |
| 93 | + if (arrowLeft !== null) { |
| 94 | + this.arrowElement.style.left = `${arrowLeft}px`; |
| 95 | + } |
| 96 | + if (arrowTop !== null) { |
| 97 | + this.arrowElement.style.top = `${arrowTop}px`; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + doShow() { |
| 102 | + this.wrapperElement.setAttribute('open', ''); |
| 103 | + this.contentElement.setAttribute('open', ''); |
| 104 | + this.arrowElement.setAttribute('open', ''); |
| 105 | + this.positionElements(); |
| 106 | + } |
| 107 | + |
| 108 | + doHide() { |
| 109 | + this.wrapperElement.removeAttribute('open'); |
| 110 | + this.contentElement.removeAttribute('open'); |
| 111 | + this.arrowElement.removeAttribute('open'); |
| 112 | + } |
| 113 | +} |
0 commit comments