Skip to content

Fix modal spa lock #1061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-laws-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@qwik-ui/headless': patch
---

fix: cleanup scroll locking
5 changes: 5 additions & 0 deletions apps/component-tests/src/routes/test-route/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { component$ } from '@builder.io/qwik';

export default component$(() => {
return <div>Test Route</div>;
});
37 changes: 37 additions & 0 deletions apps/website/src/routes/docs/headless/modal/auto-api/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const api = {
modal: [
{
'modal-close': [],
},
{
'modal-content': [],
},
{
'modal-context': [],
},
{
'modal-description': [],
},
{
'modal-footer': [],
},
{
'modal-header': [],
},
{
'modal-panel': [],
},
{
'modal-root': [],
},
{
'modal-title': [],
},
{
'modal-trigger': [],
},
{
'use-modal': [],
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { component$, useStyles$ } from '@builder.io/qwik';
import { Modal, Label } from '@qwik-ui/headless';

export default component$(() => {
useStyles$(styles);

return (
<Modal.Root>
<Modal.Trigger class="modal-trigger">Open Modal</Modal.Trigger>
<Modal.Panel class="modal-panel">
<Modal.Title>Edit Profile</Modal.Title>
<Modal.Description>
You can update your profile here. Hit the save button when finished.
</Modal.Description>
<Label>
Name
<input type="text" placeholder="John Doe" />
</Label>
<Label>
Email
<input type="text" placeholder="johndoe@gmail.com" />
</Label>
<footer>
<Modal.Close class="modal-close">Cancel</Modal.Close>
<Modal.Close class="modal-close">Save Changes</Modal.Close>
</footer>
<Link href="../../../test-route">SPA NAVIGATION</Link>
</Modal.Panel>
</Modal.Root>
);
});

// internal
import styles from '../snippets/modal.css?inline';
import { Link } from '@builder.io/qwik-city';
5 changes: 5 additions & 0 deletions packages/kit-headless/src/components/modal/modal-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { modalContextId } from './modal-context';

import styles from './modal.css?inline';
import { useModal } from './use-modal';
import { isServer } from '@builder.io/qwik/build';
import { enableBodyScroll } from 'body-scroll-lock-upgrade';

export type ModalProps = Omit<PropsOf<'dialog'>, 'open'> & {
onShow$?: QRL<() => void>;
Expand Down Expand Up @@ -59,7 +61,10 @@ export const HModalPanel = component$((props: PropsOf<'dialog'>) => {
}

cleanup(async () => {
if (isServer) return;
await deactivateFocusTrap(focusTrap);
if (!panelRef.value) return;
enableBodyScroll(panelRef.value);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const HModalTrigger = component$((props: PropsOf<'button'>) => {
return (
<button
aria-haspopup="dialog"
aria-label="Open Theme Customization Panel"
aria-expanded={context.showSig.value}
data-open={context.showSig.value ? '' : undefined}
data-closed={!context.showSig.value ? '' : undefined}
Expand Down
16 changes: 16 additions & 0 deletions packages/kit-headless/src/components/modal/modal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ test.describe('Scroll locking', () => {
}),
).toBe(true);
});

test(`GIVEN a modal
WHEN navigating to another page in SPA
THEN the body should not have overflow hidden`, async ({ page }) => {
const { driver: d } = await setup(page, 'test-spa-scroll');

await expect(page.locator('body')).not.toHaveCSS('overflow', 'hidden');

await d.openModal();

await expect(page.locator('body')).toHaveCSS('overflow', 'hidden');

await page.click('[q\\:link]');

await expect(page.locator('body')).not.toHaveCSS('overflow', 'hidden');
});
});

test.describe('Focus Trap', () => {
Expand Down