-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2555 from Shopify/trapping-update
[TrapFocus] Fix tab focusing
- Loading branch information
Showing
12 changed files
with
447 additions
and
100 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 +1,5 @@ | ||
export {KeypressListener, KeypressListenerProps} from './KeypressListener'; | ||
export { | ||
KeypressListener, | ||
KeypressListenerProps, | ||
KeyEvent, | ||
} from './KeypressListener'; |
This file contains 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 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 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 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,99 +1,109 @@ | ||
import React from 'react'; | ||
import {closest} from '@shopify/javascript-utilities/dom'; | ||
import { | ||
focusFirstFocusableNode, | ||
findFirstFocusableNode, | ||
focusLastFocusableNode, | ||
} from '@shopify/javascript-utilities/focus'; | ||
import {write} from '@shopify/javascript-utilities/fastdom'; | ||
import React, {useState, useRef} from 'react'; | ||
import {focusFirstFocusableNode} from '@shopify/javascript-utilities/focus'; | ||
import {Key} from '../../types'; | ||
|
||
import {useComponentDidMount} from '../../utilities/use-component-did-mount'; | ||
import {EventListener} from '../EventListener'; | ||
import {KeypressListener, KeyEvent} from '../KeypressListener'; | ||
import {Focus} from '../Focus'; | ||
|
||
import { | ||
findFirstKeyboardFocusableNode, | ||
focusFirstKeyboardFocusableNode, | ||
findLastKeyboardFocusableNode, | ||
focusLastKeyboardFocusableNode, | ||
} from '../../utilities/focus'; | ||
|
||
export interface TrapFocusProps { | ||
trapping?: boolean; | ||
children?: React.ReactNode; | ||
} | ||
|
||
interface State { | ||
shouldFocusSelf: boolean | undefined; | ||
} | ||
|
||
export class TrapFocus extends React.PureComponent<TrapFocusProps, State> { | ||
state: State = { | ||
shouldFocusSelf: undefined, | ||
}; | ||
|
||
private focusTrapWrapper: HTMLElement | null = null; | ||
export function TrapFocus({trapping = true, children}: TrapFocusProps) { | ||
const [shouldFocusSelf, setFocusSelf] = useState<boolean | undefined>( | ||
undefined, | ||
); | ||
|
||
componentDidMount() { | ||
this.setState(this.handleTrappingChange()); | ||
} | ||
|
||
handleTrappingChange() { | ||
const {trapping = true} = this.props; | ||
const focusTrapWrapper = useRef<HTMLDivElement>(null); | ||
|
||
const handleTrappingChange = () => { | ||
if ( | ||
this.focusTrapWrapper && | ||
this.focusTrapWrapper.contains(document.activeElement) | ||
focusTrapWrapper.current && | ||
focusTrapWrapper.current.contains(document.activeElement) | ||
) { | ||
return {shouldFocusSelf: false}; | ||
return false; | ||
} | ||
return trapping; | ||
}; | ||
|
||
return {shouldFocusSelf: trapping}; | ||
} | ||
|
||
render() { | ||
const {children} = this.props; | ||
|
||
return ( | ||
<Focus disabled={this.shouldDisable()} root={this.focusTrapWrapper}> | ||
<div ref={this.setFocusTrapWrapper}> | ||
<EventListener event="focusout" handler={this.handleBlur} /> | ||
{children} | ||
</div> | ||
</Focus> | ||
); | ||
} | ||
|
||
private shouldDisable() { | ||
const {trapping = true} = this.props; | ||
const {shouldFocusSelf} = this.state; | ||
useComponentDidMount(() => setFocusSelf(handleTrappingChange())); | ||
|
||
const shouldDisableFirstElementFocus = () => { | ||
if (shouldFocusSelf === undefined) { | ||
return true; | ||
} | ||
|
||
return shouldFocusSelf ? !trapping : !shouldFocusSelf; | ||
} | ||
|
||
private setFocusTrapWrapper = (node: HTMLDivElement) => { | ||
this.focusTrapWrapper = node; | ||
}; | ||
|
||
private handleBlur = (event: FocusEvent) => { | ||
const {relatedTarget} = event; | ||
const {focusTrapWrapper} = this; | ||
const {trapping = true} = this.props; | ||
const handleFocusIn = (event: FocusEvent) => { | ||
const containerContentsHaveFocus = | ||
focusTrapWrapper.current && | ||
focusTrapWrapper.current.contains(document.activeElement); | ||
|
||
if (relatedTarget == null || trapping === false) { | ||
if ( | ||
trapping === false || | ||
!focusTrapWrapper.current || | ||
containerContentsHaveFocus | ||
) { | ||
return; | ||
} | ||
|
||
if ( | ||
focusTrapWrapper && | ||
!focusTrapWrapper.contains(relatedTarget as HTMLElement) && | ||
(!relatedTarget || | ||
!closest(relatedTarget as HTMLElement, '[data-polaris-overlay]')) | ||
focusTrapWrapper.current !== event.target && | ||
!focusTrapWrapper.current.contains(event.target as Node) | ||
) { | ||
focusFirstFocusableNode(focusTrapWrapper.current); | ||
} | ||
}; | ||
|
||
const handleTab = (event: KeyboardEvent) => { | ||
if (trapping === false || !focusTrapWrapper.current) { | ||
return; | ||
} | ||
|
||
const firstFocusableNode = findFirstKeyboardFocusableNode( | ||
focusTrapWrapper.current, | ||
); | ||
const lastFocusableNode = findLastKeyboardFocusableNode( | ||
focusTrapWrapper.current, | ||
); | ||
|
||
if (event.target === lastFocusableNode && !event.shiftKey) { | ||
event.preventDefault(); | ||
focusFirstKeyboardFocusableNode(focusTrapWrapper.current); | ||
} | ||
|
||
if (event.srcElement === findFirstFocusableNode(focusTrapWrapper)) { | ||
return write(() => focusLastFocusableNode(focusTrapWrapper)); | ||
} | ||
const firstNode = | ||
findFirstFocusableNode(focusTrapWrapper) || focusTrapWrapper; | ||
write(() => focusFirstFocusableNode(firstNode)); | ||
if (event.target === firstFocusableNode && event.shiftKey) { | ||
event.preventDefault(); | ||
focusLastKeyboardFocusableNode(focusTrapWrapper.current); | ||
} | ||
}; | ||
|
||
return ( | ||
<Focus | ||
disabled={shouldDisableFirstElementFocus()} | ||
root={focusTrapWrapper.current} | ||
> | ||
<div ref={focusTrapWrapper}> | ||
<EventListener event="focusin" handler={handleFocusIn} /> | ||
<KeypressListener | ||
keyCode={Key.Tab} | ||
keyEvent={KeyEvent.KeyDown} | ||
handler={handleTab} | ||
/> | ||
{children} | ||
</div> | ||
</Focus> | ||
); | ||
} |
Oops, something went wrong.