(function () {
var drawerIds = (sessionStorage.getItem('theme-drawer-open') || '').split(',').filter(Boolean);
if (!drawerIds.length) return;
// Only restore on wide viewports (squeeze mode). On narrow viewports the
// drawer is a modal overlay — let it start closed after navigation rather
// than re-opening a fullscreen dialog the user may have left behind.
if (!window.matchMedia('(min-width: 990px)').matches) return;
var dialogs = [];
for (var i = 0; i < drawerIds.length; i++) {
var drawer = document.getElementById(drawerIds[i]);
if (!drawer) continue;
drawer.setAttribute('open', '');
// Set [open] directly so the CSS slide-in applies without triggering
// the browser's dialog focusing steps — on wide viewports the drawer is
// a sidebar, not a modal, so focus should stay where it is.
// The instant class suppresses the slide-in transition for the initial restore.
// Set --drawer-stack-order so z-index layering matches the original order.
drawer.style.setProperty('--drawer-stack-order', String(i + 1));
var dialog = drawer.querySelector('dialog');
if (dialog) {
dialog.setAttribute('open', '');
dialog.classList.add('theme-drawer__dialog-instant');
dialogs.push(dialog);
}
// Let each drawer type handle its own restore (e.g. shopify-chat mode).
// Synchronous dispatch — listeners attached during earlier parsing run immediately.
drawer.dispatchEvent(new CustomEvent('theme-drawer:restoring'));
}
// Apply squeeze immediately, bypassing the margin transition.
// Only squeeze if at least one drawer was actually restored — saved
// drawer IDs may not exist on every page (e.g. cart-drawer on /cart).
var pageWrapper = document.querySelector('.page-wrapper');
if (pageWrapper && dialogs.length > 0) {
pageWrapper.style.transition = 'none';
pageWrapper.classList.add('page-wrapper--drawer-open');
}
// Re-enable transitions after one paint so the instant state is rendered first.
requestAnimationFrame(function () {
requestAnimationFrame(function () {
if (pageWrapper) pageWrapper.style.transition = '';
for (var j = 0; j < dialogs.length; j++) {
dialogs[j].classList.remove('theme-drawer__dialog-instant');
}
});
});
})();