Skip to content

Commit

Permalink
[CHUX-295] Force ec-modal to trap focus (#1910)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Lazarev authored Apr 5, 2024
1 parent 6b6c6b8 commit b0c6022
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ebury/chameleon-components",
"version": "2.8.1",
"version": "2.8.2",
"main": "src/main.ts",
"sideEffects": false,
"author": "Ebury Team (http://labs.ebury.rocks/)",
Expand Down
16 changes: 16 additions & 0 deletions src/components/ec-modal/ec-modal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ describe('EcModal', () => {
expect(wrapper.element).toMatchSnapshot();
});

it('should remove focus trap activation interval on unmount', () => {
const wrapper = mountModal(
{ show: true },
{
slots: {
main: '<p>Before we can process your application we need you to upload your management accounts. You can do this now or leave it for later.</p>',
},
},
);

const clearIntervalSpy = vi.spyOn(global, 'clearInterval');
wrapper.unmount();

expect(clearIntervalSpy).toHaveBeenCalledTimes(1);
});

it('should render with custom attributes', () => {
const wrapper = mountModal({
show: true,
Expand Down
5 changes: 4 additions & 1 deletion src/components/ec-modal/ec-modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ function getFocusTrapOptions(): UseFocusTrapOptions {
return options;
}
const { deactivate } = useFocusTrap(focusTrapTarget, getFocusTrapOptions());
const { deactivate, activate } = useFocusTrap(focusTrapTarget, getFocusTrapOptions());
const focusTrapInterval = setInterval(() => activate(), 300);
watch(() => focusTrapTarget.value, (targetEl) => {
if (!targetEl) {
deactivate();
Expand Down Expand Up @@ -219,6 +221,7 @@ function onKeyUp(e: KeyboardEvent) {
}
onBeforeUnmount(() => {
document.removeEventListener('keyup', onKeyUp);
clearInterval(focusTrapInterval);
});
// modal footer
Expand Down

0 comments on commit b0c6022

Please sign in to comment.