Skip to content

Commit

Permalink
document preventFocusOnOpen prop
Browse files Browse the repository at this point in the history
  • Loading branch information
ktravers authored Feb 6, 2025
1 parent 4f249d4 commit 9d4229b
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions content/guides/react/hooks/use-open-and-close-focus.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ description: A utility Hook that focuses an element when a component is first mo

`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.

If no ref is passed to `initialFocusRef` , the hook focuses the first focusable element inside of the container.
If no ref is passed to `initialFocusRef`, the hook focuses the first focusable element inside of the container.

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.

### Usage

```javascript live noinline
const Overlay = ({returnFocusRef, initialFocusRef, children}) => {
const containerRef = React.useRef(null)
useOpenAndCloseFocus({containerRef, returnFocusRef, initialFocusRef})
useOpenAndCloseFocus({containerRef, returnFocusRef, initialFocusRef, preventFocusOnOpen: true})
return (
<Box height="200px" ref={containerRef}>
{children}
Expand Down Expand Up @@ -44,8 +46,9 @@ render(<Component />)

#### useOpenAndCloseFocus settings

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

0 comments on commit 9d4229b

Please sign in to comment.