Skip to content

Commit 710f86b

Browse files
aldenquimbyHaroenv
andauthored
fix(js): allow body scroll when detached mode responsively disabled (#1251)
* fix(js): allow body scroll when detached mode responsively disabled. fixes #1250 * refactor(js): add unit test * move test --------- Co-authored-by: Haroen Viaene <[email protected]>
1 parent e0187cd commit 710f86b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

packages/autocomplete-js/src/__tests__/detached.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,38 @@ describe('detached', () => {
466466
).toHaveAttribute('hidden');
467467
});
468468
});
469+
470+
test('removes aa-Detached when no longer matching', async () => {
471+
Object.defineProperty(window, 'matchMedia', {
472+
writable: true,
473+
value: createMatchMedia({ matches: true }),
474+
});
475+
476+
const container = document.createElement('div');
477+
478+
document.body.appendChild(container);
479+
const { update } = autocomplete<{ label: string }>({
480+
container,
481+
detachedMediaQuery: 'something',
482+
});
483+
484+
const searchButton = container.querySelector<HTMLButtonElement>(
485+
'.aa-DetachedSearchButton'
486+
)!;
487+
488+
// Open detached overlay
489+
searchButton.click();
490+
491+
await waitFor(() => expect(document.body).toHaveClass('aa-Detached'));
492+
493+
Object.defineProperty(window, 'matchMedia', {
494+
writable: true,
495+
value: createMatchMedia({ matches: false }),
496+
});
497+
498+
// schedule a render (normally this is done by the matchMedia listener, but that's not accessible here)
499+
update({ detachedMediaQuery: 'something' });
500+
501+
await waitFor(() => expect(document.body).not.toHaveClass('aa-Detached'));
502+
});
469503
});

packages/autocomplete-js/src/autocomplete.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ export function autocomplete<TItem extends BaseItem>(
217217
return () => {
218218
if (panelContainerElement.contains(panelElement)) {
219219
panelContainerElement.removeChild(panelElement);
220+
panelContainerElement.classList.remove('aa-Detached');
220221
}
221222
};
222223
});

0 commit comments

Comments
 (0)