Skip to content

Commit 241b8a6

Browse files
ianprime0509diasbruno
authored andcommitted
[chore] Move API documentation from README to gitbook
1 parent 4c1e590 commit 241b8a6

File tree

5 files changed

+91
-226
lines changed

5 files changed

+91
-226
lines changed

README.md

+18-218
Original file line numberDiff line numberDiff line change
@@ -25,201 +25,16 @@ To install, you can use [npm](https://npmjs.org/) or [yarn](https://yarnpkg.com)
2525
$ yarn add react-modal
2626

2727

28-
## Usage
28+
## API documentation
2929

30-
The Modal object has one required prop:
31-
32-
- `isOpen` to render its children.
33-
34-
Example:
35-
36-
```jsx
37-
<Modal
38-
isOpen={bool}
39-
onAfterOpen={afterOpenFn}
40-
onRequestClose={requestCloseFn}
41-
closeTimeoutMS={n}
42-
style={customStyle}
43-
contentLabel="Modal"
44-
>
45-
<h1>Modal Content</h1>
46-
<p>Etc.</p>
47-
</Modal>
48-
```
49-
50-
> Use the prop `contentLabel` which adds `aria-label` to the modal if there is no label text visible on the screen, otherwise specify the element including the label text using `aria-labelledby`
51-
52-
### App Element
53-
54-
The app element allows you to specify the portion
55-
of your app that should be hidden (via aria-hidden)
56-
to prevent assistive technologies such as screenreaders
57-
from reading content outside of the content of
58-
your modal.
59-
60-
If you are doing server-side rendering, you should use
61-
this property.
62-
63-
It can be specified in the following ways:
64-
65-
- DOMElement
66-
67-
```js
68-
Modal.setAppElement(appElement);
69-
```
70-
71-
- query selector - uses the first element found if you pass in a class.
72-
73-
```js
74-
Modal.setAppElement('#your-app-element');
75-
```
76-
77-
### Additional Aria Attributes
78-
79-
Use the property `aria` to pass any additional aria attributes. It accepts
80-
an object where the keys are the names of the attributes without the prefix
81-
`aria-`.
82-
83-
Example:
84-
85-
```jsx
86-
<Modal
87-
isOpen={modalIsOpen}
88-
aria={{
89-
labelledby: "heading",
90-
describedby: "full_description"
91-
}}>
92-
<h1 id="heading">H1</h1>
93-
<div id="full_description">
94-
<p>Description goes here.</p>
95-
</div>
96-
</Modal>
97-
```
98-
99-
## Styles
100-
101-
Styles are passed with the `style` prop, an object with 2 keys, 'overlay' and 'content' like so
102-
103-
```jsx
104-
<Modal
105-
...
106-
style={{
107-
overlay: {
108-
position: 'fixed',
109-
top: 0,
110-
left: 0,
111-
right: 0,
112-
bottom: 0,
113-
backgroundColor: 'rgba(255, 255, 255, 0.75)'
114-
},
115-
content: {
116-
position: 'absolute',
117-
top: '40px',
118-
left: '40px',
119-
right: '40px',
120-
bottom: '40px',
121-
border: '1px solid #ccc',
122-
background: '#fff',
123-
overflow: 'auto',
124-
WebkitOverflowScrolling: 'touch',
125-
borderRadius: '4px',
126-
outline: 'none',
127-
padding: '20px'
128-
}
129-
}}
130-
...
131-
>
132-
```
133-
134-
Styles passed to the modal are merged in with the above defaults and applied to their respective elements.
135-
At this time, media queries will need to be handled by the consumer.
136-
137-
### Using CSS Classes
138-
139-
If you prefer not to use inline styles or are unable to do so in your project,
140-
you can pass `className` and `overlayClassName` props to the Modal. If you do
141-
this then none of the default styles will apply and you will have full control
142-
over styling via CSS.
143-
144-
If you want to override default content and overlay classes you can pass object
145-
with three required properties: `base`, `afterOpen`, `beforeClose`.
146-
147-
```jsx
148-
<Modal
149-
...
150-
className={{
151-
base: 'myClass',
152-
afterOpen: 'myClass_after-open',
153-
beforeClose: 'myClass_before-close'
154-
}}
155-
overlayClassName={{
156-
base: 'myOverlayClass',
157-
afterOpen: 'myOverlayClass_after-open',
158-
beforeClose: 'myOverlayClass_before-close'
159-
}}
160-
...
161-
>
162-
```
163-
164-
You can also pass a `portalClassName` to change the wrapper's class (*ReactModalPortal*).
165-
This doesn't affect styling as no styles are applied to this element by default.
166-
167-
### Overriding styles globally
168-
169-
The default styles above are available on `Modal.defaultStyles`. Changes to this
170-
object will apply to all instances of the modal.
171-
172-
### Appended to custom node
173-
174-
You can choose an element for the modal to be appended to, rather than using
175-
body tag. To do this, provide a function to `parentSelector` prop that return
176-
the element to be used.
177-
178-
```jsx
179-
180-
function getParent() {
181-
return document.querySelector('#root');
182-
}
183-
184-
<Modal
185-
...
186-
parentSelector={getParent}
187-
...
188-
>
189-
<p>Modal Content.</p>
190-
</Modal>
191-
```
192-
193-
### Body class
194-
195-
When the modal is opened a `ReactModal__Body--open` class is added to the `body` tag.
196-
You can use this to remove scrolling on the body while the modal is open.
197-
198-
```CSS
199-
/* Remove scroll on the body when react-modal is open */
200-
.ReactModal__Body--open {
201-
overflow: hidden;
202-
}
203-
```
204-
205-
### Refs
206-
207-
You can use ref callbacks to get overlay and content DOM nodes:
208-
209-
```jsx
210-
<Modal
211-
...
212-
overlayRef={node => this.overlayRef = node}
213-
contentRef={node => this.contentRef = node}
214-
...
215-
>
216-
<p>Modal Content.</p>
217-
</Modal>
218-
```
30+
The primary documentation for react-modal is the
31+
[reference book](https://reactjs.github.io/react-modal), which describes the API
32+
and gives examples of its usage.
21933

22034
## Examples
22135

222-
Inside an app:
36+
Here is a simple example of react-modal being used in an app with some custom
37+
styles and focusable input elements within the modal content:
22338

22439
```jsx
22540
import React from 'react';
@@ -294,33 +109,18 @@ class App extends React.Component {
294109
ReactDOM.render(<App />, appElement);
295110
```
296111

297-
## Testing
298-
299-
When using React Test Utils with this library, here are some things to keep in mind:
300-
301-
- You need to set `isOpen={true}` on the modal component for it to render its children.
302-
- You need to use the `.portal` property, as in `ReactDOM.findDOMNode(renderedModal.portal)` or `TestUtils.scryRenderedDOMComponentsWithClass(Modal.portal, 'my-modal-class')` to acquire a handle to the inner contents of your modal.
303-
304-
By default the modal is closed when clicking outside of it (the overlay area). If you want to prevent this behavior you can
305-
pass the 'shouldCloseOnOverlayClick' prop with 'false' value.
306-
307-
```jsx
308-
<Modal
309-
isOpen={bool}
310-
onAfterOpen={afterOpenFn}
311-
onRequestClose={requestCloseFn}
312-
closeTimeoutMS={n}
313-
shouldCloseOnOverlayClick={false}
314-
style={customStyle}
315-
contentLabel="No Overlay Click Modal"
316-
>
317-
318-
<h1>Force Modal</h1>
319-
<p>Modal cannot be closed when clicking the overlay area</p>
320-
<button onClick={handleCloseFunc}>Close Modal...</button>
321-
</Modal>
322-
```
112+
You can find more examples in the `examples` directory, which you can run in a
113+
local development server using `npm start` or `yarn run start`.
323114

324115
## Demos
325116

326-
* http://reactjs.github.io/react-modal/
117+
There are several demos hosted on [CodePen](https://codepen.io) which
118+
demonstrate various features of react-modal:
119+
120+
* [Minimal example](https://codepen.io/claydiffrient/pen/KNxgav)
121+
* [Using setAppElement](https://codepen.io/claydiffrient/pen/ENegGJ)
122+
* [Using onRequestClose](https://codepen.io/claydiffrient/pen/KNjVBx)
123+
* [Using shouldCloseOnOverlayClick](https://codepen.io/claydiffrient/pen/woLzwo)
124+
* [Using inline styles](https://codepen.io/claydiffrient/pen/ZBmyKz)
125+
* [Using CSS classes for styling](https://codepen.io/claydiffrient/pen/KNjVrG)
126+
* [Customizing the default styles](https://codepen.io/claydiffrient/pen/pNXgqQ)

docs/README.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ To install the stable version you can use [npm](https://npmjs.org/) or [yarn](ht
1515

1616
## General Usage {#usage}
1717

18-
The following is an example of using react-modal specifying all the possible props and options:
18+
The only required prop for the modal object is `isOpen`, which indicates
19+
whether the modal should be displayed. The following is an example of using
20+
react-modal specifying all the possible props and options:
1921

2022
```js
2123
import ReactModal from 'react-modal';
@@ -115,6 +117,46 @@ import ReactModal from 'react-modal';
115117
/>
116118
```
117119

120+
## Using a custom parent node {#custom-parent}
121+
122+
By default, the modal portal will be appended to the document's body. You can
123+
choose a different parent element by providing a function to the
124+
`parentSelector` prop that returns the element to be used:
125+
126+
```jsx
127+
function getParent() {
128+
return document.querySelector('#root');
129+
}
130+
131+
<Modal
132+
...
133+
parentSelector={getParent}
134+
...
135+
>
136+
<p>Modal Content.</p>
137+
</Modal>
138+
```
139+
140+
If you do this, please ensure that your
141+
[app element](accessibility/README.md#app-element) is set correctly. The app
142+
element should not be a parent of the modal, to prevent modal content from
143+
being hidden to screenreaders while it is open.
144+
145+
## Refs {#refs}
146+
147+
You can use ref callbacks to get the overlay and content DOM nodes directly:
148+
149+
```jsx
150+
<Modal
151+
...
152+
overlayRef={node => this.overlayRef = node}
153+
contentRef={node => this.contentRef = node}
154+
...
155+
>
156+
<p>Modal Content.</p>
157+
</Modal>
158+
```
159+
118160
## License {#license}
119161

120162
MIT

docs/SUMMARY.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
* [Overview](/README.md)
44
* [Installation](/README.md#installation)
5-
* [Usage](/README.md#usage)
5+
* [Usage summary](/README.md#usage)
6+
* [Using a custom parent node](/README.md#custom-parent)
7+
* [Refs](/README.md#refs)
68
* [License](/README.md#license)
79

810
* [Accessibility](accessibility/README.md)
911
* [The app element](accessibility/README.md#app-element)
1012
* [Keyboard navigation](accessibility/README.md#keyboard)
11-
* [Custom ARIA attributes](accessibility/README.md#aria)
13+
* [ARIA attributes](accessibility/README.md#aria)
1214

1315
* [Styles](styles/README.md)
1416
* [Using CSS Classes](styles/classes.md)

docs/accessibility/README.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,22 @@ The modal can be closed using the escape key, unless the
4444
`shouldCloseOnEsc={false}` prop is passed. Disabling this behavior may cause
4545
accessibility issues for keyboard users, however, so it is not recommended.
4646

47-
### Custom ARIA attributes {#aria}
47+
### ARIA attributes {#aria}
4848

49-
To pass custom ARIA attributes to your modal, you can use the `aria` prop,
50-
which accepts an object whose keys are the attributes you want to set (without
51-
the leading `aria-` prefix). For example, you could have an alert modal with a
49+
Besides the `aria-hidden` attribute which is applied to the app element when
50+
the modal is shown, there are many other ARIA attributes which you can use to
51+
make your app more accessible. A complete list of ARIA attributes can be found
52+
in the [ARIA specification](https://www.w3.org/TR/wai-aria-1.1/#state_prop_def).
53+
54+
One ARIA attribute is given a dedicated prop by react-modal: you should use the
55+
`contentLabel` prop to provide a label for the modal content (via `aria-label`)
56+
if there is no visible label on the screen. If the modal is already labeled
57+
with visible text, you should specify the element including the label with the
58+
`aria-labelledby` attribute using the `aria` prop described below.
59+
60+
To pass other ARIA attributes to your modal, you can use the `aria` prop, which
61+
accepts an object whose keys are the attributes you want to set (without the
62+
leading `aria-` prefix). For example, you could have an alert modal with a
5263
title as well as a longer description:
5364

5465
```jsx

docs/styles/classes.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,21 @@ modal is open by defining a property `bodyOpenClassName`.
4646
The `bodyOpenClassName` prop must be a *constant string*; otherwise, we would
4747
require a complex system to manage which class name should be added to or
4848
removed from `document.body` from which modal (if using multiple modals
49-
simultaneously).
49+
simultaneously). The default value is `ReactModal__Body--open`.
5050

5151
`bodyOpenClassName` can support adding multiple classes to `document.body` when
5252
the modal is open. Add as many class names as you desire, delineated by spaces.
5353

54+
One potential application for the body class is to remove scrolling on the body
55+
when the modal is open. To do this for all modals (except those that specify a
56+
non-default `bodyOpenClassName`), you could use the following CSS:
57+
58+
```CSS
59+
.ReactModal__Body--open {
60+
overflow: hidden;
61+
}
62+
```
63+
5464
#### For the entire portal
5565

5666
To specify a class to be applied to the entire portal, you may use the

0 commit comments

Comments
 (0)