This repository was archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Fix shift tab when currentIndex is 0 or -1 #49
Merged
Changes from all commits
Commits
Show all changes
5 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 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 |
---|---|---|
|
@@ -49,17 +49,22 @@ function restrictTabBehavior(event: KeyboardEvent): void { | |
if (elements.length === 0) return | ||
|
||
const movement = event.shiftKey ? -1 : 1 | ||
const currentFocus = elements.filter(el => el.matches(':focus'))[0] | ||
let targetIndex = 0 | ||
const currentFocus = dialog.contains(document.activeElement) ? document.activeElement : null | ||
let targetIndex = movement === -1 ? -1 : 0 | ||
|
||
if (currentFocus) { | ||
const currentIndex = elements.indexOf(currentFocus) | ||
if (currentIndex !== -1) { | ||
const newIndex = currentIndex + movement | ||
if (newIndex >= 0) targetIndex = newIndex % elements.length | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When |
||
targetIndex = currentIndex + movement | ||
} | ||
} | ||
|
||
if (targetIndex < 0) { | ||
targetIndex = elements.length - 1 | ||
} else { | ||
targetIndex = targetIndex % elements.length | ||
} | ||
|
||
elements[targetIndex].focus() | ||
} | ||
|
||
|
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 |
---|---|---|
|
@@ -81,17 +81,19 @@ describe('details-dialog-element', function() { | |
dialog.toggle(true) | ||
await waitForToggleEvent(details) | ||
assert(details.open) | ||
pressEscape(details) | ||
triggerKeydownEvent(details, 'Escape') | ||
assert(!details.open) | ||
}) | ||
|
||
it('manages focus', async function() { | ||
summary.click() | ||
await waitForToggleEvent(details) | ||
assert.equal(document.activeElement, dialog) | ||
pressTab(details) | ||
triggerKeydownEvent(details, 'Tab', true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New test for shift + tab here. |
||
assert.equal(document.activeElement, document.querySelector(`[${CLOSE_ATTR}]`)) | ||
triggerKeydownEvent(details, 'Tab') | ||
assert.equal(document.activeElement, document.querySelector(`[data-button]`)) | ||
pressTab(details) | ||
triggerKeydownEvent(details, 'Tab') | ||
assert.equal(document.activeElement, document.querySelector(`[${CLOSE_ATTR}]`)) | ||
}) | ||
|
||
|
@@ -122,7 +124,7 @@ describe('details-dialog-element', function() { | |
assert(details.open) | ||
assert.equal(closeRequestCount, 2) | ||
|
||
pressEscape(details) | ||
triggerKeydownEvent(details, 'Escape') | ||
assert(details.open) | ||
assert.equal(closeRequestCount, 3) | ||
|
||
|
@@ -163,7 +165,7 @@ describe('details-dialog-element', function() { | |
assert(details.open) | ||
assert.equal(closeRequestCount, 1) | ||
|
||
pressEscape(details) | ||
triggerKeydownEvent(details, 'Escape') | ||
assert(details.open) | ||
assert.equal(closeRequestCount, 2) | ||
|
||
|
@@ -197,7 +199,7 @@ describe('details-dialog-element', function() { | |
dialog.toggle(true) | ||
await waitForToggleEvent(details) | ||
assert(details.open) | ||
pressEscape(details) | ||
triggerKeydownEvent(details, 'Escape') | ||
assert(!details.open) | ||
}) | ||
}) | ||
|
@@ -230,7 +232,7 @@ describe('details-dialog-element', function() { | |
dialog.preload = true | ||
assert(dialog.hasAttribute('preload')) | ||
assert.notOk(includeFragment.getAttribute('src')) | ||
triggerEvent(details, 'mouseover') | ||
triggerMouseoverEvent(details) | ||
assert.equal(includeFragment.getAttribute('src'), '/404') | ||
}) | ||
}) | ||
|
@@ -260,7 +262,7 @@ describe('details-dialog-element', function() { | |
it('transfers src on mouseover', async function() { | ||
assert(!details.open) | ||
assert.notOk(includeFragment.getAttribute('src')) | ||
triggerEvent(details, 'mouseover') | ||
triggerMouseoverEvent(details) | ||
assert.equal(includeFragment.getAttribute('src'), '/404') | ||
}) | ||
}) | ||
|
@@ -279,17 +281,21 @@ function waitForToggleEvent(details) { | |
}) | ||
} | ||
|
||
function triggerEvent(element, name, key) { | ||
const event = document.createEvent('Event') | ||
event.initEvent(name, true, true) | ||
if (key) event.key = key | ||
element.dispatchEvent(event) | ||
} | ||
|
||
function pressEscape(details) { | ||
triggerEvent(details, 'keydown', 'Escape') | ||
function triggerMouseoverEvent(element) { | ||
element.dispatchEvent( | ||
new MouseEvent('mouseover', { | ||
bubbles: true, | ||
cancelable: true | ||
}) | ||
) | ||
} | ||
|
||
function pressTab(details) { | ||
triggerEvent(details, 'keydown', 'Tab') | ||
function triggerKeydownEvent(element, key, shiftKey) { | ||
element.dispatchEvent( | ||
new KeyboardEvent('keydown', { | ||
bubbles: true, | ||
cancelable: true, | ||
key, | ||
shiftKey | ||
}) | ||
) | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
When
currentFocus
does not exist,movement
was not apply at all.