jQuery(document).ready(function ($) {
var POPUP_ID = 'YOUR_POPUP_ID'; // ← replace this
// Broader selector: targets any button inside any off-canvas widget
$(document).on('click', '[data-widget_type="off-canvas.default"] .elementor-button', function (e) {
e.preventDefault();
e.stopPropagation();
// --- Method 1: Force-close via all known Elementor v4 off-canvas states ---
var $offCanvas = $(this).closest('[data-widget_type="off-canvas.default"]');
var $toggle = $offCanvas.find('[data-elementor-open-lightbox]');
// Remove every active/open state class Elementor v4 uses
$('body')
.removeClass('e-off-canvas--active')
.removeClass('elementor-off-canvas--active');
$offCanvas
.removeClass('e-off-canvas--open')
.removeClass('elementor-off-canvas--show')
.attr('aria-hidden', 'true');
// Also click the native toggle to let Elementor clean up its own state
var $closeBtn = $offCanvas.find('.e-off-canvas__close-button, [data-e-off-canvas-close]');
if ($closeBtn.length) {
$closeBtn[0].click();
}
// --- Method 2: Remove the overlay backdrop ---
$('.e-off-canvas__overlay, .elementor-off-canvas__overlay').remove();
// --- Open the popup after animation settles ---
setTimeout(function () {
if (
typeof elementorProFrontend !== 'undefined' &&
elementorProFrontend.modules &&
elementorProFrontend.modules.popup
) {
elementorProFrontend.modules.popup.showPopup({ id: POPUP_ID });
} else {
// Fallback: trigger via Elementor's action system
$(document).trigger('elementor/popup/show', [POPUP_ID]);
}
}, 400);
});
});