Skip to content
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

feat(viewer): Add focus trap for fullscreen #1439

Merged
merged 6 commits into from
Dec 16, 2021

Conversation

ConradJChan
Copy link
Contributor

@ConradJChan ConradJChan commented Dec 15, 2021

Adds FocusTrap class which takes an element and allows you to enable or disable focus trapping.

This class is used for when Preview enters fullscreen to constrain tab navigation to within the visible previewed content. This can also be applied to the print modal.

TODO:

  • add/update unit tests
  • cross-browser testing

@ConradJChan ConradJChan requested a review from a team as a code owner December 15, 2021 01:30
src/lib/viewers/BaseViewer.js Outdated Show resolved Hide resolved
src/lib/viewers/BaseViewer.js Outdated Show resolved Hide resolved
src/lib/FocusTrap.ts Outdated Show resolved Hide resolved
src/lib/FocusTrap.ts Show resolved Hide resolved
src/lib/FocusTrap.ts Outdated Show resolved Hide resolved
src/lib/FocusTrap.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@jstoffan jstoffan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM apart from some super minor comments, just needs tests.

src/lib/FocusTrap.ts Outdated Show resolved Hide resolved
src/lib/FocusTrap.ts Outdated Show resolved Hide resolved
src/lib/FocusTrap.ts Outdated Show resolved Hide resolved
src/lib/FocusTrap.ts Outdated Show resolved Hide resolved
} else {
this.focusLastElement();
}
event.stopPropagation();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to stop propagation of the tab keydown event here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like if we're controlling the focus because of the tab key we should stop propagation. This is also how it's done in box-ui-elements

src/lib/FocusTrap.ts Outdated Show resolved Hide resolved
src/lib/__tests__/FocusTrap-test.ts Outdated Show resolved Hide resolved
src/lib/types/global.ts Outdated Show resolved Hide resolved
@@ -534,6 +540,28 @@ describe('lib/viewers/BaseViewer', () => {
expect(base.disableAnnotationControls).toBeCalled();
expect(base.processAnnotationModeChange).toBeCalledWith(AnnotationMode.NONE);
});

test('should enable the focus trap', () => {
jest.spyOn(FocusTrap.prototype, 'constructor');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little unusual to spy on the prototype. Can we instead make sure that base.focusTrap is defined, then use jest.spyOn(base.focusTrap, 'enable')? That way we're testing the instance rather than the prototype.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't prefer spying the prototype but I went this route because I wanted to test the case where we actually create the FocusTrap vs reuse it. I went with the approach you suggested below when reusing the existing focus trap

src/lib/__tests__/FocusTrap-test.ts Outdated Show resolved Hide resolved
src/lib/__tests__/FocusTrap-test.ts Outdated Show resolved Hide resolved
src/lib/FocusTrap.ts Outdated Show resolved Hide resolved
src/lib/FocusTrap.ts Show resolved Hide resolved
@ConradJChan ConradJChan merged commit b09b349 into box:master Dec 16, 2021
@ConradJChan ConradJChan deleted the add_focus_trap_fullscreen branch December 16, 2021 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants