Skip to content

Commit

Permalink
fix: replace references to global.document (#642)
Browse files Browse the repository at this point in the history
* remove unnecessary references to global document object

* Update src/tab.ts

Co-authored-by: Philipp Fritsche <ph.fritsche@gmail.com>

* Update src/utils/misc/isDocument.ts

Co-authored-by: Philipp Fritsche <ph.fritsche@gmail.com>

* Update src/tab.ts

Co-authored-by: Philipp Fritsche <ph.fritsche@gmail.com>

* refactor getNextElement()

* fix a linting issue

* refactor getNextElement()

Co-authored-by: Philipp Fritsche <ph.fritsche@gmail.com>
  • Loading branch information
loreanvictor and ph-fritsche committed Apr 9, 2021
1 parent 7143ae3 commit c69a74a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/keyboard/index.ts
Expand Up @@ -92,7 +92,7 @@ describe('error', () => {
// the catch will be asynchronous
await Promise.resolve()

expect(err).toHaveBeenCalledWith(expect.any(Error))
expect(err).toHaveBeenCalledWith(expect.any(Error) as unknown)
expect(err.mock.calls[0][0]).toHaveProperty(
'message',
'Expected key descriptor but found "!" in "{!"',
Expand Down
2 changes: 1 addition & 1 deletion src/click.ts
Expand Up @@ -109,7 +109,7 @@ function findClosest(element: Element, callback: (e: Element) => boolean) {
return el
}
el = el.parentElement
} while (el && el !== document.body)
} while (el && el !== element.ownerDocument.body)
return undefined
}

Expand Down
30 changes: 15 additions & 15 deletions src/tab.ts
Expand Up @@ -4,6 +4,7 @@ import {
FOCUSABLE_SELECTOR,
isVisible,
isDisabled,
isDocument,
} from './utils'
import {focus} from './focus'
import {blur} from './blur'
Expand All @@ -12,21 +13,19 @@ function getNextElement(
currentIndex: number,
shift: boolean,
elements: Element[],
focusTrap?: Document | Element,
focusTrap: Document | Element,
) {
if (focusTrap === document && currentIndex === 0 && shift) {
return document.body
} else if (
focusTrap === document &&
currentIndex === elements.length - 1 &&
!shift
if (
isDocument(focusTrap) &&
((currentIndex === 0 && shift) ||
(currentIndex === elements.length - 1 && !shift))
) {
return document.body
} else {
const nextIndex = shift ? currentIndex - 1 : currentIndex + 1
const defaultIndex = shift ? elements.length - 1 : 0
return elements[nextIndex] || elements[defaultIndex]
return focusTrap.body
}

const nextIndex = shift ? currentIndex - 1 : currentIndex + 1
const defaultIndex = shift ? elements.length - 1 : 0
return elements[nextIndex] || elements[defaultIndex]
}

interface tabOptions {
Expand All @@ -35,10 +34,11 @@ interface tabOptions {
}

function tab({shift = false, focusTrap}: tabOptions = {}) {
const previousElement = getActiveElement(focusTrap?.ownerDocument ?? document)
const doc = focusTrap?.ownerDocument ?? document
const previousElement = getActiveElement(doc)

if (!focusTrap) {
focusTrap = document
focusTrap = doc
}

const focusableElements = focusTrap.querySelectorAll(FOCUSABLE_SELECTOR)
Expand Down Expand Up @@ -142,7 +142,7 @@ function tab({shift = false, focusTrap}: tabOptions = {}) {
!continueToTab && previousElement ? previousElement : nextElement

if (continueToTab) {
if (nextElement === document.body) {
if (nextElement === doc.body) {
/* istanbul ignore else */
if (previousElement) {
blur(previousElement)
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Expand Up @@ -22,5 +22,6 @@ export * from './misc/isElementType'
export * from './misc/isLabelWithInternallyDisabledControl'
export * from './misc/isVisible'
export * from './misc/isDisabled'
export * from './misc/isDocument'
export * from './misc/wait'
export * from './misc/hasPointerEvents'
3 changes: 3 additions & 0 deletions src/utils/misc/isDocument.ts
@@ -0,0 +1,3 @@
export function isDocument(el: Document | Element): el is Document {
return el.nodeType === el.DOCUMENT_NODE
}

0 comments on commit c69a74a

Please sign in to comment.