Last active
November 9, 2017 11:29
-
-
Save yuschick/e622b4e1744e53f7ec8cb32f3419aa84 to your computer and use it in GitHub Desktop.
Accessible Web Apps with React, TypeScript & Ally.js
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
class Dialog extends React.Component<Props> { | |
dialog: HTMLElement | null; | |
disabledHandle: Handle; | |
focusHandle: Handle; | |
keyHandle: Handle; | |
focusedElementBeforeDialogOpened: HTMLInputElement | HTMLButtonElement; | |
componentDidMount() { | |
if (document.activeElement instanceof HTMLInputElement || | |
document.activeElement instanceof HTMLButtonElement) { | |
this.focusedElementBeforeDialogOpened = document.activeElement; | |
} | |
this.disabledHandle = Disabled({ | |
filter: this.dialog, | |
}); | |
this.focusHandle = TabFocus({ | |
context: this.dialog, | |
}); | |
let element = FirstTab({ | |
context: this.dialog, | |
defaultToContext: true, | |
}); | |
this.keyHandle = Key({ | |
escape: () => { this.props.close(); }, | |
}); | |
element.focus(); | |
} | |
componentWillUnmount() { | |
this.disabledHandle.disengage(); | |
this.focusHandle.disengage(); | |
this.keyHandle.disengage(); | |
this.focusedElementBeforeDialogOpened.focus(); | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment