Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fixed: auto-positioning admin dropdowns (#13248)

Damian Legawiec8a5b93588cf8
diff --git a/admin/app/assets/stylesheets/spree/admin/components/_dropdowns.scss b/admin/app/assets/stylesheets/spree/admin/components/_dropdowns.scss
index ecc00ec49c..67bb607876 100644
--- a/admin/app/assets/stylesheets/spree/admin/components/_dropdowns.scss
+++ b/admin/app/assets/stylesheets/spree/admin/components/_dropdowns.scss
@@ -108,20 +108,18 @@
}
// new non-bootstrap dropdowns
.dropdown-container {
+ // Position is handled by Floating UI via inline styles
+ // Keep position: absolute but let Floating UI set top/left/right/bottom
position: absolute;
- top: 100%;
- right: 0;
animation: fadeIn 0.15s ease-in-out;
- transform-origin: top right;
+ // Legacy classes kept for backward compatibility but no longer apply positioning
+ // Floating UI handles all positioning automatically
&.dropdown-container-left {
- left: 0;
- right: auto;
+ // No longer needed - Floating UI handles placement
}
&.dropdown-container-top {
- top: auto;
- bottom: 100%;
- height: auto;
+ // No longer needed - Floating UI handles placement
}
}
diff --git a/admin/app/helpers/spree/admin/dropdown_helper.rb b/admin/app/helpers/spree/admin/dropdown_helper.rb
index 6f6d475f34..a350ee2ca8 100644
--- a/admin/app/helpers/spree/admin/dropdown_helper.rb
+++ b/admin/app/helpers/spree/admin/dropdown_helper.rb
@@ -10,21 +10,33 @@ module Spree
def dropdown_toggle(options = {}, &block)
options[:type] = 'button'
options[:class] = ['btn'] + Array(options[:class])
- options[:data] = { action: 'dropdown#toggle click@window->dropdown#hide' }.merge(options[:data] || {})
+ options[:data] = {
+ action: 'dropdown#toggle click@window->dropdown#hide',
+ dropdown_target: 'toggle'
+ }.merge(options[:data] || {})
button_tag(options, &block)
end
def dropdown_menu(options = {}, &block)
options[:class] = ['dropdown-container hidden'] + Array(options[:class])
- if options[:direction] == 'left'
- options[:class] << 'dropdown-container-left'
- elsif options[:direction] == 'top'
- options[:class] << 'dropdown-container-top'
- elsif options[:direction] == 'top-left'
- options[:class] << 'dropdown-container-top dropdown-container-left'
+ # Extract direction option for backward compatibility and convert to Floating UI placement
+ placement = case options.delete(:direction)
+ when 'left'
+ 'bottom-end'
+ when 'top'
+ 'top-start'
+ when 'top-left'
+ 'top-end'
+ else
+ options[:placement] || 'bottom-start'
end
- options[:data] = { dropdown_target: 'menu' }.merge(options[:data] || {})
+
+ options[:data] = {
+ dropdown_target: 'menu',
+ dropdown_placement_value: placement
+ }.merge(options[:data] || {})
+
content_tag(:div, options, &block)
end
end
diff --git a/admin/app/javascript/spree/admin/application.js b/admin/app/javascript/spree/admin/application.js
index 7dc0fd1fcd..74b5027e60 100644
--- a/admin/app/javascript/spree/admin/application.js
+++ b/admin/app/javascript/spree/admin/application.js
@@ -35,7 +35,6 @@ if (typeof Stimulus === 'undefined') {
import AutoSubmit from '@stimulus-components/auto-submit'
import CheckboxSelectAll from 'stimulus-checkbox-select-all'
import Dialog from "@stimulus-components/dialog"
-import Dropdown from '@stimulus-components/dropdown'
import TextareaAutogrow from 'stimulus-textarea-autogrow'
import Notification from 'stimulus-notification'
import PasswordVisibility from 'stimulus-password-visibility'
@@ -56,6 +55,7 @@ import Clipboard from 'spree/admin/controllers/clipboard_controller'
import CodeMirrorController from 'spree/admin/controllers/codemirror_controller'
import ColorPaletteController from 'spree/admin/controllers/color_palette_controller'
import ColorPickerController from 'spree/admin/controllers/color_picker_controller'
+import DropdownController from 'spree/admin/controllers/dropdown_controller'
import FiltersController from 'spree/admin/controllers/filters_controller'
import FontPickerController from 'spree/admin/controllers/font_picker_controller'
import HighlightController from 'spree/admin/controllers/highlight_controller'
@@ -114,7 +114,7 @@ application.register('color-picker', ColorPickerController)
application.register('dialog', Dialog)
application.register('drawer', Dialog)
application.register('disable-submit-button', DisableSubmitButtonController)
-application.register('dropdown', Dropdown)
+application.register('dropdown', DropdownController)
application.register('enable-button', EnableButtonController)
application.register('export-dialog', Dialog)
application.register('filters', FiltersController)
diff --git a/admin/app/javascript/spree/admin/controllers/dropdown_controller.js b/admin/app/javascript/spree/admin/controllers/dropdown_controller.js
new file mode 100644
index 0000000000..664dfd03f0
--- /dev/null
+++ b/admin/app/javascript/spree/admin/controllers/dropdown_controller.js
@@ -0,0 +1,154 @@
+import {
+ autoUpdate,
+ computePosition,
+ flip,
+ offset,
+ shift,
+ size,
+} from "@floating-ui/dom"
+import { Controller } from "@hotwired/stimulus"
+
+export default class extends Controller {
+ static targets = ["menu", "toggle"]
+ static values = {
+ placement: { type: String, default: "bottom-start" },
+ offset: { type: Number, default: 4 },
+ }
+
+ connect() {
+ this._cleanup = null
+ this.boundUpdate = this.update.bind(this)
+ this._isOpen = false
+ this._toggleElement = null
+ }
+
+ disconnect() {
+ this.stopAutoUpdate()
+ }
+
+ toggle(event) {
+ event.preventDefault()
+ event.stopPropagation()
+
+ // Store the toggle element that triggered this action
+ this._toggleElement = event.currentTarget
+
+ if (this._isOpen) {
+ this.close()
+ } else {
+ this.open()
+ }
+ }
+
+ open() {
+ if (this._isOpen) return
+
+ this.menuTarget.classList.remove("hidden")
+ this._isOpen = true
+
+ // Start automatic positioning
+ this.startAutoUpdate()
+
+ // Add event listener to close on outside click
+ this._outsideClickHandler = this.handleOutsideClick.bind(this)
+ document.addEventListener("click", this._outsideClickHandler)
+
+ // Add event listener to close on escape key
+ this._escapeHandler = this.handleEscape.bind(this)
+ document.addEventListener("keydown", this._escapeHandler)
+ }
+
+ close() {
+ if (!this._isOpen) return
+
+ this.menuTarget.classList.add("hidden")
+ this._isOpen = false
+
+ // Stop automatic positioning
+ this.stopAutoUpdate()
+
+ // Remove event listeners
+ if (this._outsideClickHandler) {
+ document.removeEventListener("click", this._outsideClickHandler)
+ this._outsideClickHandler = null
+ }
+
+ if (this._escapeHandler) {
+ document.removeEventListener("keydown", this._escapeHandler)
+ this._escapeHandler = null
+ }
+ }
+
+ hide(event) {
+ // This method is called from the action: click@window->dropdown#hide
+ // Only close if clicking outside the dropdown element
+ if (!this.element.contains(event.target)) {
+ this.close()
+ }
+ }
+
+ handleOutsideClick(event) {
+ // Close dropdown if clicking outside
+ if (!this.element.contains(event.target)) {
+ this.close()
+ }
+ }
+
+ handleEscape(event) {
+ if (event.key === "Escape") {
+ this.close()
+ }
+ }
+
+ startAutoUpdate() {
+ if (!this._cleanup) {
+ const referenceElement = this.hasToggleTarget ? this.toggleTarget : (this._toggleElement || this.element)
+ this._cleanup = autoUpdate(
+ referenceElement,
+ this.menuTarget,
+ this.boundUpdate,
+ )
+ }
+ }
+
+ stopAutoUpdate() {
+ if (this._cleanup) {
+ this._cleanup()
+ this._cleanup = null
+ }
+ }
+
+ update() {
+ // Use the toggle target if available, or the stored toggle element, or fall back to the controller element
+ const referenceElement = this.hasToggleTarget ? this.toggleTarget : (this._toggleElement || this.element)
+
+ computePosition(referenceElement, this.menuTarget, {
+ placement: this.placementValue,
+ middleware: [
+ offset(this.offsetValue),
+ flip({
+ fallbackAxisSideDirection: "start",
+ padding: 8,
+ }),
+ shift({ padding: 8 }),
+ size({
+ apply({ availableWidth, availableHeight, elements }) {
+ // Ensure dropdown doesn't exceed viewport
+ Object.assign(elements.floating.style, {
+ maxWidth: `${availableWidth}px`,
+ maxHeight: `${availableHeight}px`,
+ overflow: "auto",
+ })
+ },
+ padding: 8,
+ }),
+ ],
+ }).then(({ x, y }) => {
+ Object.assign(this.menuTarget.style, {
+ left: `${x}px`,
+ top: `${y}px`,
+ position: "absolute",
+ })
+ })
+ }
+}
diff --git a/admin/config/importmap.rb b/admin/config/importmap.rb
index 94c5ec51bb..65dd641c4e 100644
--- a/admin/config/importmap.rb
+++ b/admin/config/importmap.rb
@@ -27,7 +27,6 @@ pin 'hotkeys-js', preload: ['application-spree-admin'] # @3.13.9
pin 'stimulus-use', preload: ['application-spree-admin'] # @0.51.3
pin 'stimulus-checkbox-select-all', preload: ['application-spree-admin'] # @5.3.0
pin 'stimulus-clipboard', preload: ['application-spree-admin'] # @4.0.1
-pin "@stimulus-components/dropdown", to: "@stimulus-components--dropdown.js", preload: ['application-spree-admin'] # @3.0.0
pin "@stimulus-components/dialog", to: "@stimulus-components--dialog.js", preload: ['application-spree-admin'] # @1.0.1
# We can't vendor some of the javascript libraries
diff --git a/admin/vendor/javascript/@stimulus-components--dropdown.js b/admin/vendor/javascript/@stimulus-components--dropdown.js
deleted file mode 100644
index 240ae9ea5a..0000000000
--- a/admin/vendor/javascript/@stimulus-components--dropdown.js
+++ /dev/null
@@ -1,4 +0,0 @@
-// @stimulus-components/[email protected] downloaded from https://ga.jspm.io/npm:@stimulus-components/[email protected]/dist/stimulus-dropdown.mjs
-
-import{Controller as t}from"@hotwired/stimulus";import{useTransition as e}from"stimulus-use";const s=class _Dropdown extends t{connect(){e(this,{element:this.menuTarget})}toggle(){this.toggleTransition()}hide(t){!this.element.contains(t.target)&&!this.menuTarget.classList.contains("hidden")&&this.leave()}};s.targets=["menu"];let i=s;export{i as default};
-