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.

fix dropdowns overflow issues

Damian Legawiecd35cb1401596
diff --git a/admin/app/assets/stylesheets/spree/admin/components/_dropdowns.scss b/admin/app/assets/stylesheets/spree/admin/components/_dropdowns.scss
index 4e4d6a00d6..6b863026e0 100644
--- a/admin/app/assets/stylesheets/spree/admin/components/_dropdowns.scss
+++ b/admin/app/assets/stylesheets/spree/admin/components/_dropdowns.scss
@@ -32,6 +32,7 @@
padding: $dropdown-padding-y $dropdown-padding-x;
font-size: $dropdown-font-size;
color: $dropdown-color;
+ max-width: 20rem;
.dropdown-item, .dropdown-header {
padding-top: $dropdown-item-padding-y;
diff --git a/admin/app/helpers/spree/admin/dropdown_helper.rb b/admin/app/helpers/spree/admin/dropdown_helper.rb
index ad8270acc4..bd6ec26256 100644
--- a/admin/app/helpers/spree/admin/dropdown_helper.rb
+++ b/admin/app/helpers/spree/admin/dropdown_helper.rb
@@ -16,10 +16,18 @@ module Spree
options[:placement] || 'bottom-start'
end
- options[:data] = {
+ # Extract portal option
+ portal = options.delete(:portal)
+
+ data_attrs = {
controller: 'dropdown',
dropdown_placement_value: placement
- }.merge(options[:data] || {})
+ }
+
+ # Add portal value if explicitly set
+ data_attrs[:dropdown_portal_value] = portal unless portal.nil?
+
+ options[:data] = data_attrs.merge(options[:data] || {})
content_tag(:div, options, &block)
end
diff --git a/admin/app/javascript/spree/admin/controllers/dropdown_controller.js b/admin/app/javascript/spree/admin/controllers/dropdown_controller.js
index 664dfd03f0..d2b7c6bdd5 100644
--- a/admin/app/javascript/spree/admin/controllers/dropdown_controller.js
+++ b/admin/app/javascript/spree/admin/controllers/dropdown_controller.js
@@ -9,24 +9,44 @@ import {
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 },
+ portal: { type: Boolean, default: true },
}
connect() {
+ // Find menu and toggle elements by CSS class instead of Stimulus target
+ // Try both .dropdown-menu and .dropdown-container for backward compatibility
+ this.menu = this.element.querySelector('.dropdown-menu') ||
+ this.element.querySelector('.dropdown-container')
+ this.toggleBtn = this.element.querySelector('.dropdown-toggle')
+
+ // Early return if no menu element exists
+ if (!this.menu) {
+ return
+ }
+
this._cleanup = null
this.boundUpdate = this.update.bind(this)
this._isOpen = false
this._toggleElement = null
+ this._originalParent = null
+ this._originalNextSibling = null
+ this._movedToBody = false
}
disconnect() {
+ if (!this.menu) return
this.stopAutoUpdate()
+ this.restoreMenuPosition()
}
toggle(event) {
+ if (!this.menu) {
+ return
+ }
+
event.preventDefault()
event.stopPropagation()
@@ -41,9 +61,19 @@ export default class extends Controller {
}
open() {
- if (this._isOpen) return
+ if (!this.menu || this._isOpen) {
+ return
+ }
- this.menuTarget.classList.remove("hidden")
+ // Move menu to body on first open to prevent clipping by sidebar overflow
+ // Skip if portal is disabled or if inside bulk panel (to preserve Stimulus controller context)
+ if (!this._movedToBody && this.shouldPortalToBody()) {
+ this.moveMenuToBody()
+ this._movedToBody = true
+ }
+
+ this.menu.classList.remove("hidden")
+ this.menu.style.display = "block"
this._isOpen = true
// Start automatic positioning
@@ -59,9 +89,10 @@ export default class extends Controller {
}
close() {
- if (!this._isOpen) return
+ if (!this.menu || !this._isOpen) return
- this.menuTarget.classList.add("hidden")
+ this.menu.classList.add("hidden")
+ this.menu.style.display = ""
this._isOpen = false
// Stop automatic positioning
@@ -101,11 +132,11 @@ export default class extends Controller {
}
startAutoUpdate() {
- if (!this._cleanup) {
- const referenceElement = this.hasToggleTarget ? this.toggleTarget : (this._toggleElement || this.element)
+ if (!this._cleanup && this.menu) {
+ const referenceElement = this.toggleBtn || this._toggleElement || this.element
this._cleanup = autoUpdate(
referenceElement,
- this.menuTarget,
+ this.menu,
this.boundUpdate,
)
}
@@ -119,10 +150,12 @@ export default class extends Controller {
}
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)
+ if (!this.menu) return
+
+ // Use the toggle button if available, or the stored toggle element, or fall back to the controller element
+ const referenceElement = this.toggleBtn || this._toggleElement || this.element
- computePosition(referenceElement, this.menuTarget, {
+ computePosition(referenceElement, this.menu, {
placement: this.placementValue,
middleware: [
offset(this.offsetValue),
@@ -133,9 +166,18 @@ export default class extends Controller {
shift({ padding: 8 }),
size({
apply({ availableWidth, availableHeight, elements }) {
- // Ensure dropdown doesn't exceed viewport
+ // Get the element's computed max-width
+ const computedStyle = window.getComputedStyle(elements.floating)
+ const originalMaxWidth = parseFloat(computedStyle.maxWidth)
+
+ // Use the smaller of availableWidth or original max-width
+ const maxWidth = originalMaxWidth && !isNaN(originalMaxWidth)
+ ? Math.min(availableWidth, originalMaxWidth)
+ : availableWidth
+
+ // Ensure dropdown doesn't exceed viewport or original constraints
Object.assign(elements.floating.style, {
- maxWidth: `${availableWidth}px`,
+ maxWidth: `${maxWidth}px`,
maxHeight: `${availableHeight}px`,
overflow: "auto",
})
@@ -144,11 +186,40 @@ export default class extends Controller {
}),
],
}).then(({ x, y }) => {
- Object.assign(this.menuTarget.style, {
+ Object.assign(this.menu.style, {
left: `${x}px`,
top: `${y}px`,
position: "absolute",
})
})
}
+
+ shouldPortalToBody() {
+ // Don't portal if explicitly disabled via data attribute
+ return this.portalValue
+ }
+
+ moveMenuToBody() {
+ if (this.menu && this.menu.parentNode !== document.body) {
+ // Save original position for restoration
+ this._originalParent = this.menu.parentNode
+ this._originalNextSibling = this.menu.nextSibling
+
+ // Move menu to body to prevent clipping by sidebar overflow
+ document.body.appendChild(this.menu)
+ }
+ }
+
+ restoreMenuPosition() {
+ if (this.menu && this._originalParent) {
+ // Restore menu to original position
+ if (this._originalNextSibling) {
+ this._originalParent.insertBefore(this.menu, this._originalNextSibling)
+ } else {
+ this._originalParent.appendChild(this.menu)
+ }
+ this._originalParent = null
+ this._originalNextSibling = null
+ }
+ }
}
diff --git a/admin/app/views/spree/admin/gift_cards/index.html.erb b/admin/app/views/spree/admin/gift_cards/index.html.erb
index 1ccc4197df..bcff2abf2e 100644
--- a/admin/app/views/spree/admin/gift_cards/index.html.erb
+++ b/admin/app/views/spree/admin/gift_cards/index.html.erb
@@ -12,7 +12,7 @@
<%= icon 'plus' %>
<%= Spree.t(:new_gift_card) %>
<% end %>
- <%= dropdown_menu class: 'dropdown-menu-right' do %>
+ <%= dropdown_menu do %>
<%= link_to spree.new_admin_gift_card_path,
class: 'text-left dropdown-item' do %>
<%= icon 'add' %>
diff --git a/admin/app/views/spree/admin/orders/_customer.html.erb b/admin/app/views/spree/admin/orders/_customer.html.erb
index fc39289544..f9811ff8f2 100644
--- a/admin/app/views/spree/admin/orders/_customer.html.erb
+++ b/admin/app/views/spree/admin/orders/_customer.html.erb
@@ -8,7 +8,7 @@
<%= dropdown_toggle class: 'btn-light btn-sm px-1', data: { test_id: 'dropdown-toggle' } do %>
<%= icon 'dots-vertical', class: 'mr-0' %>
<% end %>
- <%= dropdown_menu class: 'dropdown-menu-right' do %>
+ <%= dropdown_menu do %>
<% if can?(:update_customer, @order) && [email protected]? %>
<%= link_to Spree.t('admin.edit_contact_information'), spree.edit_admin_order_contact_information_path(@order), class: 'dropdown-item', data: { turbo_frame: 'dialog', action: 'dialog#open' } %>
<% end %>
diff --git a/admin/app/views/spree/admin/orders/_table_filter_dropdown.html.erb b/admin/app/views/spree/admin/orders/_table_filter_dropdown.html.erb
index 750277e62e..5478017efb 100644
--- a/admin/app/views/spree/admin/orders/_table_filter_dropdown.html.erb
+++ b/admin/app/views/spree/admin/orders/_table_filter_dropdown.html.erb
@@ -6,7 +6,7 @@
</span>
<%= order_filter_dropdown_value %>
<% end %>
- <%= dropdown_menu class: 'dropdown-menu-right w-100', style: 'min-width: 200px' do %>
+ <%= dropdown_menu do %>
<%= active_link_to Spree.t('admin.orders.all_orders'), params.to_unsafe_h.deep_merge({q: {shipment_state_eq: '', shipment_state_not_in: '', state_eq: '', state_in: '', refunded: '', partially_refunded: '' }}), class: 'dropdown-item', active: (params[:q] || {}).values_at(*%w[payment_state_not_eq shipment_state_not_in shipment_state_eq state_eq state_in refunded partially_refunded] ).all?(&:blank?) %>
<%= link_to params.to_unsafe_h.deep_merge({q: {shipment_state_not_in: [:shipped, :canceled], shipment_state_eq: '', state_eq: '', state_in: '', refunded: '', partially_refunded: ''}}),
class: "dropdown-item d-flex align-items-center justify-content-between #{'active' if params[:q][:shipment_state_not_in] == ['shipped', 'canceled']}" do %>
diff --git a/admin/app/views/spree/admin/products/_bulk_operations.html.erb b/admin/app/views/spree/admin/products/_bulk_operations.html.erb
index 799a22cebc..716a30cea2 100644
--- a/admin/app/views/spree/admin/products/_bulk_operations.html.erb
+++ b/admin/app/views/spree/admin/products/_bulk_operations.html.erb
@@ -18,7 +18,7 @@
url: spree.bulk_status_update_admin_products_path(status: :draft)
) %>
- <%= dropdown direction: 'top' do %>
+ <%= dropdown direction: 'top', portal: false do %>
<%= dropdown_toggle do %>
<%= icon('dots-vertical', class: "mr-0") %>
<% end %>
diff --git a/admin/app/views/spree/admin/products/_table_filter_dropdown.html.erb b/admin/app/views/spree/admin/products/_table_filter_dropdown.html.erb
index fab1f080fc..d0c263a6f6 100644
--- a/admin/app/views/spree/admin/products/_table_filter_dropdown.html.erb
+++ b/admin/app/views/spree/admin/products/_table_filter_dropdown.html.erb
@@ -13,7 +13,7 @@
<%= product_filter_stock_dropdown_value if product_filter_stock_dropdown_value != Spree.t('admin.products.any_stock') %>
<% end %>
<% end %>
- <%= dropdown_menu class: 'w-100', style: 'min-width: 200px' do %>
+ <%= dropdown_menu do %>
<h6 class="dropdown-header"><%= Spree.t('admin.products.by_status') %>:</h6>
<%= active_link_to Spree.t('admin.products.all_statuses'), params.to_unsafe_h.deep_merge({page: nil, q: {status_eq: nil}}), class: 'dropdown-item', active: (params[:q] || {}).values_at(*%w[status_eq in_stock_items out_of_stock_items] ).all?(&:blank?) %>
diff --git a/admin/app/views/spree/admin/promotions/_table_filter_dropdown.html.erb b/admin/app/views/spree/admin/promotions/_table_filter_dropdown.html.erb
index 4f44dae2f9..a499e739e2 100644
--- a/admin/app/views/spree/admin/promotions/_table_filter_dropdown.html.erb
+++ b/admin/app/views/spree/admin/promotions/_table_filter_dropdown.html.erb
@@ -5,7 +5,7 @@
</span>
<%= promotion_filter_dropdown_value %>
<% end %>
- <%= dropdown_menu class: 'dropdown-menu-right w-100', style: 'min-width: 200px' do %>
+ <%= dropdown_menu do %>
<%= active_link_to 'All Promotions', params.to_unsafe_h.deep_merge({page: nil, q: {active: nil, expired: nil}}), class: 'dropdown-item', active: (params[:q] || {}).values_at(*%w[expired active] ).all?(&:blank?) %>
<%= link_to Spree.t(:expired),
params.to_unsafe_h.deep_merge({page: nil, q: {active: nil, expired: true}}),
diff --git a/admin/app/views/spree/admin/return_authorizations/filters.html.erb b/admin/app/views/spree/admin/return_authorizations/filters.html.erb
index ebf8d11213..b01d426ee4 100644
--- a/admin/app/views/spree/admin/return_authorizations/filters.html.erb
+++ b/admin/app/views/spree/admin/return_authorizations/filters.html.erb
@@ -13,7 +13,7 @@
<%= Spree.t(:all) %>
<% end %>
<% end %>
- <%= dropdown_menu class: 'dropdown-menu-right w-100', style: 'min-width: 200px' do %>
+ <%= dropdown_menu do %>
<%= active_link_to Spree.t(:all),
params.to_unsafe_h.deep_merge({ q: { state_eq: "" } }),
class: "dropdown-item",
diff --git a/admin/app/views/spree/admin/shared/sortable_tree/_taxonomy.html.erb b/admin/app/views/spree/admin/shared/sortable_tree/_taxonomy.html.erb
index 359234a947..c1c1c17979 100644
--- a/admin/app/views/spree/admin/shared/sortable_tree/_taxonomy.html.erb
+++ b/admin/app/views/spree/admin/shared/sortable_tree/_taxonomy.html.erb
@@ -33,7 +33,7 @@
<%= dropdown_toggle class: 'btn-sm px-1 h-100' do %>
<%= icon('dots-vertical', class: 'mr-0') %>
<% end %>
- <%= dropdown_menu class: 'dropdown-menu-right' do %>
+ <%= dropdown_menu do %>
<% if can?(:create, Spree::Taxon) %>
<%= link_to_with_icon 'plus', Spree.t(:new_nested_taxon), spree.new_admin_taxonomy_taxon_path(taxonomy, taxon: { parent_id: taxon.id }), class: 'dropdown-item' %>
<div class="dropdown-divider"></div>
diff --git a/admin/app/views/spree/admin/stock_locations/_table_row.html.erb b/admin/app/views/spree/admin/stock_locations/_table_row.html.erb
index c19a2c8456..5e3c1e7bb4 100644
--- a/admin/app/views/spree/admin/stock_locations/_table_row.html.erb
+++ b/admin/app/views/spree/admin/stock_locations/_table_row.html.erb
@@ -20,7 +20,7 @@
<%= dropdown_toggle class: 'btn-sm h-100' do %>
<%= icon('dots-vertical', class: 'mr-0') %>
<% end %>
- <%= dropdown_menu class: 'dropdown-menu-right' do %>
+ <%= dropdown_menu do %>
<%= link_to_with_icon 'check',
Spree.t(:mark_as_default),
spree.mark_as_default_admin_stock_location_path(stock_location),