Skip to content

Commit

Permalink
fix(js): allow body scroll when detached mode responsively disabled (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
aldenquimby and Haroenv authored May 14, 2024
1 parent e0187cd commit 710f86b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/autocomplete-js/src/__tests__/detached.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,38 @@ describe('detached', () => {
).toHaveAttribute('hidden');
});
});

test('removes aa-Detached when no longer matching', async () => {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: createMatchMedia({ matches: true }),
});

const container = document.createElement('div');

document.body.appendChild(container);
const { update } = autocomplete<{ label: string }>({
container,
detachedMediaQuery: 'something',
});

const searchButton = container.querySelector<HTMLButtonElement>(
'.aa-DetachedSearchButton'
)!;

// Open detached overlay
searchButton.click();

await waitFor(() => expect(document.body).toHaveClass('aa-Detached'));

Object.defineProperty(window, 'matchMedia', {
writable: true,
value: createMatchMedia({ matches: false }),
});

// schedule a render (normally this is done by the matchMedia listener, but that's not accessible here)
update({ detachedMediaQuery: 'something' });

await waitFor(() => expect(document.body).not.toHaveClass('aa-Detached'));
});
});
1 change: 1 addition & 0 deletions packages/autocomplete-js/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export function autocomplete<TItem extends BaseItem>(
return () => {
if (panelContainerElement.contains(panelElement)) {
panelContainerElement.removeChild(panelElement);
panelContainerElement.classList.remove('aa-Detached');
}
};
});
Expand Down

0 comments on commit 710f86b

Please sign in to comment.