Skip to content

Commit

Permalink
feat: add aria-modal and onClose event to <Modal>
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 20, 2018
1 parent 19029eb commit eea744c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions docs/en/Modal.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import {Modal} from 'libreact/lib/Modal';
</Modal>
```

Set aria title and description.
Set aria title, description, and close button.

```jsx
<Modal>{({idTitle, idDescription}) =>
<Modal>{({idTitle, idDescription, close}) =>
<div>
<h1 id={idTitle}>My title</h1>
<p id={idDescription}>This is description.</p>

<button onClick={close}>Cancel</button>
</div>
}</Modal>
```
Expand All @@ -43,11 +45,13 @@ Track when user intends to close the the modal.
Accepts all [`<Overlay>`](./Overlay.md) props in addition to:

- `onEsc` &mdash; optional, callback, called when user presses `Esc` button.
- `onClose` &mdash; optional, callback, called when `close()` is executed.


## State

`<Modal>` is a render prop that injects its state into the render function. State has the following keys.

- `close()` &mdash; method to calle `onClose` event.
- `idTitle` &mdash; id to set for aria title element.
- `idDescription` &mdash; id to set for aria description element.
4 changes: 4 additions & 0 deletions src/Modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ const ESC = 27;

export interface IModalProps extends IOverlayProps {
onEsc?: (event: KeyboardEvent) => void;
onClose?: () => void;
}

export interface IModalState {
close: () => void;
idTitle: string;
idDescription: string;
}
Expand All @@ -28,6 +30,7 @@ export class Modal extends Component<IModalProps, IModalState> {
this.id = id++;

this.state = {
close: () => (this.props.onClose || noop)(),
idTitle: 'dialog-title-' + this.id,
idDescription: 'dialog-descr-' + this.id
};
Expand Down Expand Up @@ -74,6 +77,7 @@ export class Modal extends Component<IModalProps, IModalState> {
el.setAttribute('role', 'dialog');
el.classList.add('dialog');

el.setAttribute('aria-modal', 'true');
el.setAttribute('aria-labelledby', 'dialog-title-' + this.id);
el.setAttribute('aria-describedby', 'dialog-descr-' + this.id);

Expand Down

0 comments on commit eea744c

Please sign in to comment.