Skip to content

Commit a47c268

Browse files
authored
Add docs for easier to use overlay hooks (#3347)
1 parent a9dea8a commit a47c268

File tree

29 files changed

+984
-899
lines changed

29 files changed

+984
-899
lines changed

packages/@react-aria/aria-modal-polyfill/docs/watchModals.mdx

Lines changed: 0 additions & 51 deletions
This file was deleted.

packages/@react-aria/autocomplete/docs/useSearchAutocomplete.mdx

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,11 @@ This example uses an `<input>` element for the search field.
113113
A "contains" filter function is obtained from <TypeLink links={i18nDocs.links} type={i18nDocs.exports.useFilter} /> and is passed to <TypeLink links={statelyDocs.links} type={statelyDocs.exports.useComboBoxState} /> so
114114
that the list box can be filtered based on the option text and the current input text.
115115

116-
The list box popup should use the same `Popover` and `ListBox` components created with [useOverlay](useOverlay.html)
116+
The list box popup should use the same `Popover` and `ListBox` components created with [usePopover](usePopover.html)
117117
and [useListBox](useListBox.html) that you may already have in your component library or application. These can be shared with other
118118
components such as a `Select` created with [useSelect](useSelect.html) or a `Dialog` popover created with [useDialog](useDialog.html).
119119
The code for these components is also included below in the collapsed sections.
120120

121-
This example does not do any advanced popover positioning or portaling to escape its visual container.
122-
See [useOverlayTrigger](useOverlayTrigger.html) for an example of how to implement this
123-
using <TypeLink links={overlaysDocs.links} type={overlaysDocs.exports.useOverlayPosition} />.
124-
125121
In addition, see [useListBox](useListBox.html) for examples of sections (option groups), and more complex
126122
options.
127123

@@ -174,7 +170,7 @@ function SearchAutocomplete(props) {
174170
<button {...buttonProps}>❎</button>
175171
}
176172
{state.isOpen &&
177-
<Popover popoverRef={popoverRef} isOpen={state.isOpen} onClose={state.close}>
173+
<Popover state={state} triggerRef={inputRef} popoverRef={popoverRef} isNonModal placement="bottom start">
178174
<ListBox
179175
{...listBoxProps}
180176
listBoxRef={listBoxRef}
@@ -200,52 +196,37 @@ function SearchAutocomplete(props) {
200196

201197
The `Popover` component is used to contain the popup listbox for the SearchAutocomplete.
202198
It can be shared between many other components, including [Select](useSelect.html),
203-
[Menu](useMenu.html), [Dialog](useOverlayTrigger.html), and others.
204-
See [useOverlayTrigger](useOverlayTrigger.html) for more examples of popovers.
199+
[Menu](useMenu.html), and others.
200+
See [usePopover](usePopover.html) for more examples of popovers.
205201

206202
<details>
207203
<summary style={{fontWeight: 'bold'}}><ChevronRight size="S" /> Show code</summary>
208204

209205
```tsx example export=true render=false
210-
import {useOverlay, DismissButton} from '@react-aria/overlays';
211-
import {FocusScope} from '@react-aria/focus';
206+
import {usePopover, Overlay, DismissButton} from '@react-aria/overlays';
212207

213-
function Popover(props) {
208+
function Popover({children, state, ...props}) {
214209
let ref = React.useRef();
215-
let {
216-
popoverRef = ref,
217-
isOpen,
218-
onClose,
219-
children
220-
} = props;
221-
222-
// Handle events that should cause the popup to close,
223-
// e.g. blur, clicking outside, or pressing the escape key.
224-
let {overlayProps} = useOverlay({
225-
isOpen,
226-
onClose,
227-
shouldCloseOnBlur: true,
228-
isDismissable: true
229-
}, popoverRef);
230-
231-
// Add a hidden <DismissButton> component at the end of the popover
232-
// to allow screen reader users to dismiss the popup easily.
210+
let {popoverRef = ref} = props;
211+
let {popoverProps} = usePopover({
212+
...props,
213+
popoverRef
214+
}, state);
215+
233216
return (
234-
<FocusScope restoreFocus>
217+
<Overlay>
235218
<div
236-
{...overlayProps}
219+
{...popoverProps}
237220
ref={popoverRef}
238221
style={{
239-
position: "absolute",
240-
width: "100%",
241-
border: "1px solid gray",
242-
background: "lightgray",
243-
marginTop: 4
222+
...popoverProps.style,
223+
background: 'lightgray',
224+
border: '1px solid gray'
244225
}}>
245226
{children}
246-
<DismissButton onDismiss={onClose} />
227+
<DismissButton onDismiss={state.close} />
247228
</div>
248-
</FocusScope>
229+
</Overlay>
249230
);
250231
}
251232
```
@@ -277,8 +258,9 @@ function ListBox(props) {
277258
margin: 0,
278259
padding: 0,
279260
listStyle: "none",
280-
maxHeight: "150px",
281-
overflow: "auto"
261+
maxHeight: 150,
262+
overflow: "auto",
263+
minWidth: 200
282264
}}>
283265
{[...state.collection].map(item => (
284266
<Option

packages/@react-aria/combobox/docs/useComboBox.mdx

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,11 @@ is included within the `<button>` to display the dropdown arrow icon (hidden fro
129129
A "contains" filter function is obtained from <TypeLink links={i18nDocs.links} type={i18nDocs.exports.useFilter} /> and is passed to <TypeLink links={statelyDocs.links} type={statelyDocs.exports.useComboBoxState} /> so
130130
that the list box can be filtered based on the option text and the current input text.
131131

132-
The same `Popover`, `ListBox`, and `Button` components created with [useOverlay](useOverlay.html), [useListBox](useListBox.html),
132+
The same `Popover`, `ListBox`, and `Button` components created with [usePopover](usePopover.html), [useListBox](useListBox.html),
133133
and [useButton](useButton.html) that you may already have in your component library or application should be reused. These can be shared with other
134134
components such as a `Select` created with [useSelect](useSelect.html) or a `Dialog` popover created with [useDialog](useDialog.html).
135135
The code for these components is also included below in the collapsed sections.
136136

137-
This example does not do any advanced popover positioning or portaling to escape its visual container.
138-
See [useOverlayTrigger](useOverlayTrigger.html) for an example of how to implement this
139-
using <TypeLink links={overlaysDocs.links} type={overlaysDocs.exports.useOverlayPosition} />.
140-
141137
In addition, see [useListBox](useListBox.html) for examples of sections (option groups), and more complex
142138
options. For an example of the description and error message elements, see [useTextField](useTextField.html).
143139

@@ -176,7 +172,7 @@ function ComboBox(props) {
176172
return (
177173
<div style={{display: 'inline-flex', flexDirection: 'column'}}>
178174
<label {...labelProps}>{props.label}</label>
179-
<div style={{position: 'relative', display: 'inline-block'}}>
175+
<div>
180176
<input
181177
{...inputProps}
182178
ref={inputRef}
@@ -200,7 +196,7 @@ function ComboBox(props) {
200196
</span>
201197
</Button>
202198
{state.isOpen &&
203-
<Popover popoverRef={popoverRef} isOpen={state.isOpen} onClose={state.close}>
199+
<Popover state={state} triggerRef={inputRef} popoverRef={popoverRef} isNonModal placement="bottom start">
204200
<ListBox
205201
{...listBoxProps}
206202
listBoxRef={listBoxRef}
@@ -226,52 +222,37 @@ function ComboBox(props) {
226222

227223
The `Popover` component is used to contain the popup listbox for the ComboBox.
228224
It can be shared between many other components, including [Select](useSelect.html),
229-
[Menu](useMenu.html), [Dialog](useOverlayTrigger.html), and others.
230-
See [useOverlayTrigger](useOverlayTrigger.html) for more examples of popovers.
225+
[Menu](useMenu.html), and others.
226+
See [usePopover](usePopover.html) for more examples of popovers.
231227

232228
<details>
233229
<summary style={{fontWeight: 'bold'}}><ChevronRight size="S" /> Show code</summary>
234230

235231
```tsx example export=true render=false
236-
import {useOverlay, DismissButton} from '@react-aria/overlays';
237-
import {FocusScope} from '@react-aria/focus';
232+
import {usePopover, Overlay, DismissButton} from '@react-aria/overlays';
238233

239-
function Popover(props) {
234+
function Popover({children, state, ...props}) {
240235
let ref = React.useRef();
241-
let {
242-
popoverRef = ref,
243-
isOpen,
244-
onClose,
245-
children
246-
} = props;
247-
248-
// Handle events that should cause the popup to close,
249-
// e.g. blur, clicking outside, or pressing the escape key.
250-
let {overlayProps} = useOverlay({
251-
isOpen,
252-
onClose,
253-
shouldCloseOnBlur: true,
254-
isDismissable: true
255-
}, popoverRef);
256-
257-
// Add a hidden <DismissButton> component at the end of the popover
258-
// to allow screen reader users to dismiss the popup easily.
236+
let {popoverRef = ref} = props;
237+
let {popoverProps} = usePopover({
238+
...props,
239+
popoverRef
240+
}, state);
241+
259242
return (
260-
<FocusScope restoreFocus>
243+
<Overlay>
261244
<div
262-
{...overlayProps}
245+
{...popoverProps}
263246
ref={popoverRef}
264247
style={{
265-
position: "absolute",
266-
width: "100%",
267-
border: "1px solid gray",
268-
background: "lightgray",
269-
marginTop: 4
248+
...popoverProps.style,
249+
background: 'lightgray',
250+
border: '1px solid gray'
270251
}}>
271252
{children}
272-
<DismissButton onDismiss={onClose} />
253+
<DismissButton onDismiss={state.close} />
273254
</div>
274-
</FocusScope>
255+
</Overlay>
275256
);
276257
}
277258
```
@@ -303,8 +284,9 @@ function ListBox(props) {
303284
margin: 0,
304285
padding: 0,
305286
listStyle: "none",
306-
maxHeight: "150px",
307-
overflow: "auto"
287+
maxHeight: 150,
288+
overflow: "auto",
289+
minWidth: 200
308290
}}>
309291
{[...state.collection].map(item => (
310292
<Option

0 commit comments

Comments
 (0)