Skip to content

Commit 7326474

Browse files
authored
document preventFocusOnOpen prop
Related: primer/react#5636
1 parent 4f249d4 commit 7326474

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

content/guides/react/hooks/use-open-and-close-focus.mdx

+12-9
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ description: A utility Hook that focuses an element when a component is first mo
55

66
`useOpenAndCloseFocus` is a utility Hook that manages focusing an element when a component is first mounted, and returns focus to an element on the page when that component unmounts.
77

8-
If no ref is passed to `initialFocusRef` , the hook focuses the first focusable element inside of the container.
8+
If no ref is passed to `initialFocusRef`, the hook focuses the first focusable element inside of the container.
9+
10+
If `preventFocusOnOpen` prop is passed, then no focus will be applied when component mounts, even if `initialFocusRef` prop is included. Only initial focus is prevented; focus will still be returned to `returnFocusRef` when component unmounts.
911

1012
### Usage
1113

1214
```javascript live noinline
13-
const Overlay = ({returnFocusRef, initialFocusRef, children}) => {
15+
const Overlay = ({returnFocusRef, initialFocusRef, preventFocusOnOpen, children}) => {
1416
const containerRef = React.useRef(null)
15-
useOpenAndCloseFocus({containerRef, returnFocusRef, initialFocusRef})
17+
useOpenAndCloseFocus({containerRef, returnFocusRef, initialFocusRef, preventFocusOnOpen})
1618
return (
1719
<Box height="200px" ref={containerRef}>
1820
{children}
@@ -30,7 +32,7 @@ function Component() {
3032
toggle
3133
</Button>
3234
{isOpen && (
33-
<Overlay returnFocusRef={returnFocusRef} initialFocusRef={initialFocusRef}>
35+
<Overlay returnFocusRef={returnFocusRef} initialFocusRef={initialFocusRef} preventFocusOnOpen={true}>
3436
<Button>Button One</Button>
3537
<Button ref={initialFocusRef}>Button Two</Button>
3638
</Overlay>
@@ -44,8 +46,9 @@ render(<Component />)
4446

4547
#### useOpenAndCloseFocus settings
4648

47-
| Name | Type | Default | Description |
48-
| :-------------- | :----------------------------- | :-----: | :------------------------------------------------------------------------ |
49-
| initialFocusRef | `React.RefObject<HTMLElement>` | | Optional. The element to focus when the container is mounted on the page. |
50-
| returnFocusRef | `React.RefObject<HTMLElement>` | | Required. The element to focus when the container is unmounted. |
51-
| containerRef | `React.RefObject<HTMLElement>` | | Required. A ref for the containing element. |
49+
| Name | Type | Default | Description |
50+
| :----------------- | :----------------------------- | :-----: | :------------------------------------------------------------------------ |
51+
| initialFocusRef | `React.RefObject<HTMLElement>` | | Optional. The element to focus when the container is mounted on the page. |
52+
| returnFocusRef | `React.RefObject<HTMLElement>` | | Required. The element to focus when the container is unmounted. |
53+
| containerRef | `React.RefObject<HTMLElement>` | | Required. A ref for the containing element. |
54+
| preventFocusOnOpen | `React.RefObject<HTMLElement>` | | Optional. When true, prevents focus when container is mounted. |

0 commit comments

Comments
 (0)