|
| 1 | +{/* Copyright 2025 Adobe. All rights reserved. |
| 2 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 4 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 6 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 7 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 8 | +governing permissions and limitations under the License. */} |
| 9 | + |
| 10 | +import {Layout} from '../../src/Layout'; |
| 11 | +export default Layout; |
| 12 | +import {GroupedPropTable} from '../../src/PropTable'; |
| 13 | +import {FunctionAPI} from '../../src/FunctionAPI'; |
| 14 | +import docs from 'docs:@react-aria/focus'; |
| 15 | + |
| 16 | +export const section = 'Focus'; |
| 17 | + |
| 18 | +# FocusScope |
| 19 | + |
| 20 | +<PageDescription>{docs.exports.FocusScope.description}</PageDescription> |
| 21 | + |
| 22 | +## Introduction |
| 23 | + |
| 24 | +`FocusScope` is a utility component that can be used to manage focus for its descendants. |
| 25 | +When the `contain` prop is set, focus is contained within the scope. This is useful when |
| 26 | +implementing overlays like modal dialogs, which should not allow focus to escape them while open. |
| 27 | +In addition, the `restoreFocus` prop can be used to restore focus back to the previously focused |
| 28 | +element when the focus scope unmounts, for example, back to a button which opened a dialog. |
| 29 | +A FocusScope can also optionally auto focus the first focusable element within it on mount |
| 30 | +when the `autoFocus` prop is set. |
| 31 | + |
| 32 | +The <TypeLink links={docs.links} type={docs.exports.useFocusManager} /> hook can also be used |
| 33 | +in combination with a FocusScope to programmatically move focus within the scope. For example, |
| 34 | +arrow key navigation could be implemented by handling keyboard events and using a focus manager |
| 35 | +to move focus to the next and previous elements. |
| 36 | + |
| 37 | +## Props |
| 38 | + |
| 39 | +<PropTable links={docs.links} component={docs.exports.FocusScope} /> |
| 40 | + |
| 41 | +## FocusManager Interface |
| 42 | + |
| 43 | +To get a focus manager, call the <TypeLink links={docs.links} type={docs.exports.useFocusManager} /> hook |
| 44 | +from a component within the FocusScope. A focus manager supports the following methods: |
| 45 | + |
| 46 | +<ClassAPI links={docs.links} class={docs.exports.FocusManager} /> |
| 47 | + |
| 48 | +## Example |
| 49 | + |
| 50 | +A basic example of a focus scope that contains focus within it is below. Clicking the "Open" |
| 51 | +button mounts a FocusScope, which auto focuses the first input inside it. Once open, you can |
| 52 | +press the <Keyboard>Tab</Keyboard> key to move within the scope, but focus is contained inside. Clicking the "Close" |
| 53 | +button unmounts the focus scope, which restores focus back to the button. |
| 54 | + |
| 55 | +{/* Not implemented yet */} |
| 56 | +{/* For a full example of building a modal dialog, see [useDialog](useDialog.html). */} |
| 57 | + |
| 58 | +```tsx render |
| 59 | +'use client'; |
| 60 | +import React from 'react'; |
| 61 | +import {FocusScope} from '@react-aria/focus'; |
| 62 | + |
| 63 | +function Example() { |
| 64 | + let [isOpen, setOpen] = React.useState(false); |
| 65 | + return ( |
| 66 | + <> |
| 67 | + <button onClick={() => setOpen(true)}>Open</button> |
| 68 | + {isOpen && |
| 69 | + <FocusScope contain restoreFocus autoFocus> |
| 70 | + <label htmlFor="first-input">First Input</label> |
| 71 | + <input id="first-input" /> |
| 72 | + <label htmlFor="second-input">Second Input</label> |
| 73 | + <input id="second-input" /> |
| 74 | + <button onClick={() => setOpen(false)}>Close</button> |
| 75 | + </FocusScope> |
| 76 | + } |
| 77 | + </> |
| 78 | + ); |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## useFocusManager Example |
| 83 | + |
| 84 | +This example shows how to use `useFocusManager` to programmatically move focus within a |
| 85 | +`FocusScope`. It implements a basic toolbar component, which allows using the left and |
| 86 | +right arrow keys to move focus to the previous and next buttons. The `wrap` option is |
| 87 | +used to make focus wrap around when it reaches the first or last button. |
| 88 | + |
| 89 | +```tsx render |
| 90 | +'use client'; |
| 91 | +import {FocusScope} from '@react-aria/focus'; |
| 92 | +import {useFocusManager} from '@react-aria/focus'; |
| 93 | + |
| 94 | +function Toolbar(props) { |
| 95 | + return ( |
| 96 | + <div role="toolbar"> |
| 97 | + <FocusScope> |
| 98 | + {props.children} |
| 99 | + </FocusScope> |
| 100 | + </div> |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +function ToolbarButton(props) { |
| 105 | + let focusManager = useFocusManager(); |
| 106 | + let onKeyDown = (e) => { |
| 107 | + switch (e.key) { |
| 108 | + case 'ArrowRight': |
| 109 | + focusManager.focusNext({wrap: true}); |
| 110 | + break; |
| 111 | + case 'ArrowLeft': |
| 112 | + focusManager.focusPrevious({wrap: true}); |
| 113 | + break; |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + return ( |
| 118 | + <button |
| 119 | + onKeyDown={onKeyDown}> |
| 120 | + {props.children} |
| 121 | + </button> |
| 122 | + ); |
| 123 | +} |
| 124 | + |
| 125 | +<Toolbar> |
| 126 | + <ToolbarButton>Cut</ToolbarButton> |
| 127 | + <ToolbarButton>Copy</ToolbarButton> |
| 128 | + <ToolbarButton>Paste</ToolbarButton> |
| 129 | +</Toolbar> |
| 130 | +``` |
0 commit comments