Skip to content

Instantly share code, notes, and snippets.

@yuschick
Last active November 9, 2017 11:29
Show Gist options
  • Save yuschick/e622b4e1744e53f7ec8cb32f3419aa84 to your computer and use it in GitHub Desktop.
Save yuschick/e622b4e1744e53f7ec8cb32f3419aa84 to your computer and use it in GitHub Desktop.
Accessible Web Apps with React, TypeScript & Ally.js
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