-
Notifications
You must be signed in to change notification settings - Fork 166
LG-3476: Upgrade focus-trap from ^2.3.0 (2.4.6) to 6.0.1 #4211
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,4 @@ | ||
| import focusTrapProxy from './focus-trap-proxy'; | ||
| import modal from './modal'; | ||
| import Modal from './modal'; | ||
|
|
||
| window.LoginGov = window.LoginGov || {}; | ||
| const { LoginGov } = window; | ||
| const trapModal = modal(focusTrapProxy); | ||
|
|
||
| LoginGov.Modal = trapModal; | ||
|
|
||
| export { trapModal as Modal }; | ||
| window.LoginGov.Modal = Modal; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,47 @@ | ||
| import 'classlist.js'; | ||
| import { createFocusTrap } from 'focus-trap'; | ||
| import Events from '../utils/events'; | ||
|
|
||
| const STATES = { | ||
| HIDE: 'hide', | ||
| SHOW: 'show', | ||
| }; | ||
|
|
||
| function modal(focusTrap) { | ||
| return class extends Events { | ||
| constructor(options) { | ||
| super(); | ||
| class Modal extends Events { | ||
| constructor(options) { | ||
| super(); | ||
|
|
||
| this.el = document.querySelector(options.el); | ||
| this.shown = false; | ||
| this.trap = focusTrap(this.el, { escapeDeactivates: false }); | ||
| } | ||
|
|
||
| toggle() { | ||
| if (this.shown) { | ||
| this.hide(); | ||
| } else { | ||
| this.show(); | ||
| } | ||
| } | ||
|
|
||
| show(target) { | ||
| this.setElementVisibility(target, true); | ||
| this.emit(STATES.SHOW); | ||
| } | ||
|
|
||
| hide(target) { | ||
| this.setElementVisibility(target, false); | ||
| this.emit(STATES.HIDE); | ||
| } | ||
|
|
||
| setElementVisibility(target = null, showing) { | ||
| const el = target || this.el; | ||
| this.el = document.querySelector(options.el); | ||
| this.shown = false; | ||
| this.trap = createFocusTrap(this.el, { escapeDeactivates: false }); | ||
| } | ||
|
|
||
| this.shown = showing; | ||
| el.classList[showing ? 'remove' : 'add']('display-none'); | ||
| document.body.classList[showing ? 'add' : 'remove']('modal-open'); | ||
| this.trap[showing ? 'activate' : 'deactivate'](); | ||
| toggle() { | ||
| if (this.shown) { | ||
| this.hide(); | ||
| } else { | ||
| this.show(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| show(target) { | ||
| this.setElementVisibility(target, true); | ||
| this.emit(STATES.SHOW); | ||
| } | ||
|
|
||
| hide(target) { | ||
| this.setElementVisibility(target, false); | ||
| this.emit(STATES.HIDE); | ||
| } | ||
|
|
||
| setElementVisibility(target = null, showing) { | ||
| const el = target || this.el; | ||
|
|
||
| this.shown = showing; | ||
| el.classList[showing ? 'remove' : 'add']('display-none'); | ||
| document.body.classList[showing ? 'add' : 'remove']('modal-open'); | ||
| this.trap[showing ? 'activate' : 'deactivate'](); | ||
| } | ||
| } | ||
|
|
||
| export default modal; | ||
| export default Modal; | ||
2 changes: 1 addition & 1 deletion
2
app/javascript/packages/document-capture/components/full-screen.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { waitFor } from '@testing-library/dom'; | ||
| import BaseModal from '../../../../app/javascript/app/components/modal'; | ||
| import { useCleanDOM } from '../../support/dom'; | ||
| import { useSandbox } from '../../support/sinon'; | ||
|
|
||
| describe('components/modal', () => { | ||
| useCleanDOM(); | ||
| const sandbox = useSandbox(); | ||
|
|
||
| class Modal extends BaseModal { | ||
| static instances = []; | ||
|
|
||
| constructor(...args) { | ||
| super(...args); | ||
| Modal.instances.push(this); | ||
| } | ||
| } | ||
|
|
||
| function createModalContainer(id = 'modal') { | ||
| const container = document.createElement('div'); | ||
| container.id = id; | ||
| container.className = 'modal display-none'; | ||
| container.innerHTML = ` | ||
| <div class="modal-backdrop"> | ||
| <div class="px2 py4 modal" role="dialog"> | ||
| <div class="modal-center"> | ||
| <div class="modal-content"> | ||
| <p>Do action?</p> | ||
| <button type="button">Yes</button> | ||
| <button type="button">No</button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| `; | ||
|
|
||
| return container; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| document.body.appendChild(createModalContainer()); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| Modal.instances.forEach((instance) => { | ||
| instance.off(); | ||
| if (instance.shown) { | ||
| instance.hide(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it('shows with initial focus', async () => { | ||
| const onShow = sandbox.stub(); | ||
| const modal = new Modal({ el: '#modal' }); | ||
| modal.on('show', onShow); | ||
| modal.show(); | ||
|
|
||
| await waitFor(() => expect(document.activeElement.nodeName).to.equal('BUTTON')); | ||
| expect(onShow.called).to.be.true(); | ||
| expect(document.activeElement.textContent).to.equal('Yes'); | ||
| const container = document.activeElement.closest('#modal'); | ||
| expect(container.classList.contains('display-none')).to.be.false(); | ||
| expect(document.body.classList.contains('modal-open')).to.be.true(); | ||
| }); | ||
|
|
||
| it('allows interaction in most recently activated focus trap', async () => { | ||
| document.body.appendChild(createModalContainer('modal2')); | ||
| const modal = new Modal({ el: '#modal' }); | ||
| const modal2 = new Modal({ el: '#modal2' }); | ||
|
|
||
| modal.show(); | ||
|
|
||
| await waitFor(() => expect(document.activeElement.closest('#modal')).to.be.ok()); | ||
|
|
||
| modal2.show(); | ||
|
|
||
| await waitFor(() => expect(document.activeElement.closest('#modal2')).to.be.ok()); | ||
|
|
||
| await new Promise((resolve) => { | ||
| document.activeElement.addEventListener('click', (event) => { | ||
| if (!event.defaultPrevented) { | ||
| resolve(); | ||
| } | ||
| }); | ||
|
|
||
| document.activeElement.click(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is all indentation changes. Suggest to review with "Hide whitespace changes":