diff --git a/app/assets/tailwind/application.css b/app/assets/tailwind/application.css index 4af14181..c665a763 100644 --- a/app/assets/tailwind/application.css +++ b/app/assets/tailwind/application.css @@ -20,7 +20,6 @@ } .pcr-color-palette{ height: 12em !important; - width: 21.5rem !important; } .pcr-palette{ border-radius: 10px !important; diff --git a/app/javascript/controllers/category_controller.js b/app/javascript/controllers/category_controller.js index fdd47729..ec0ab645 100644 --- a/app/javascript/controllers/category_controller.js +++ b/app/javascript/controllers/category_controller.js @@ -15,6 +15,7 @@ export default class extends Controller { "validationMessage", "selection", "colorPickerRadioBtn", + "popup", ]; static values = { @@ -36,6 +37,7 @@ export default class extends Controller { this.colorInputTarget.reportValidity(); e.target.open = true; } + this.updatePopupPosition() }); this.selectedIcon = null; @@ -43,6 +45,8 @@ export default class extends Controller { if (!this.presetColorsValue.includes(this.colorInputTarget.value)) { this.colorPickerRadioBtnTarget.checked = true; } + + document.addEventListener("mousedown", this.handleOutsideClick); } initPicker() { @@ -209,6 +213,7 @@ export default class extends Controller { this.colorsSectionTarget.classList.add("hidden"); this.paletteSectionTarget.classList.remove("hidden"); this.pickerSectionTarget.classList.remove("hidden"); + this.updatePopupPosition(); this.picker.show(); } @@ -216,6 +221,7 @@ export default class extends Controller { this.colorsSectionTarget.classList.remove("hidden"); this.paletteSectionTarget.classList.add("hidden"); this.pickerSectionTarget.classList.add("hidden"); + this.updatePopupPosition() if (this.picker) { this.picker.destroyAndRemove(); } @@ -229,6 +235,27 @@ export default class extends Controller { } } + handleOutsideClick = (event) => { + if (this.detailsTarget.open && !this.detailsTarget.contains(event.target)) { + this.detailsTarget.open = false; + } + }; + + updatePopupPosition() { + const popup = this.popupTarget; + popup.style.top = ""; + popup.style.bottom = ""; + + const rect = popup.getBoundingClientRect(); + const overflow = rect.bottom > window.innerHeight; + + if (overflow) { + popup.style.bottom = "0px"; + } else { + popup.style.bottom = ""; + } + } + #backgroundColor(color) { return `color-mix(in oklab, ${color} 10%, transparent)`; } diff --git a/app/views/categories/_form.html.erb b/app/views/categories/_form.html.erb index 2c62d739..b0f75b8f 100644 --- a/app/views/categories/_form.html.erb +++ b/app/views/categories/_form.html.erb @@ -2,20 +2,20 @@