|
| 1 | +:::note Before using FocusTrap |
| 2 | +`<FocusTrap>` is intended to prevent keyboard users from interacting with elements on the page that a mouse user cannot interact with either. An example of this is `<Modal>` where the user cannot click the items behind the modal. If you want to shift focus based on UI events, consider using the [.focus()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) method instead. |
| 3 | +::: |
| 4 | + |
| 5 | +:::warning Accessibility |
| 6 | +It is **required** that when using a `<FocusTrap>` there is logic using only key presses to escape the focus trap. Otherwise, keyboard users will be blocked after they enter a `<FocusTrap>`. |
| 7 | +::: |
| 8 | + |
| 9 | +## Basic example |
| 10 | + |
| 11 | +:::note |
| 12 | +All the examples have controls to enable / disable the focus trap so that page keyboard navigation isn't blocked. |
| 13 | +::: |
| 14 | + |
| 15 | +When enabled, only the children of the `<FocusTrap>` will be able to receive focus. Otherwise, standard DOM focus order follows. |
| 16 | + |
| 17 | +```jsx live |
| 18 | +function Example() { |
| 19 | + const [focusTrapEnabled, setFocusTrapEnabled] = useState(false); |
| 20 | + |
| 21 | + return ( |
| 22 | + <VStack gap={2}> |
| 23 | + <Checkbox |
| 24 | + checked={focusTrapEnabled} |
| 25 | + onChange={() => setFocusTrapEnabled((prev) => !prev)} |
| 26 | + label="Focus trap enabled" |
| 27 | + /> |
| 28 | + {focusTrapEnabled && ( |
| 29 | + <FocusTrap> |
| 30 | + <VStack gap={2} background="bgAlternate" padding={2}> |
| 31 | + <Text>Inside FocusTrap</Text> |
| 32 | + <HStack gap={1}> |
| 33 | + <Button>1</Button> |
| 34 | + <Button>2</Button> |
| 35 | + <Button>3</Button> |
| 36 | + </HStack> |
| 37 | + <Checkbox |
| 38 | + checked={focusTrapEnabled} |
| 39 | + onChange={() => setFocusTrapEnabled((prev) => !prev)} |
| 40 | + label="Focus trap enabled" |
| 41 | + /> |
| 42 | + </VStack> |
| 43 | + </FocusTrap> |
| 44 | + )} |
| 45 | + </VStack> |
| 46 | + ); |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Include trigger in FocusTrap |
| 51 | + |
| 52 | +The `includeTriggerInFocusTrap` prop includes the triggering element causing the `<FocusTrap>` to render as part of the focus order. |
| 53 | + |
| 54 | +```jsx live |
| 55 | +function Example() { |
| 56 | + const [focusTrapEnabled, setFocusTrapEnabled] = useState(false); |
| 57 | + |
| 58 | + return ( |
| 59 | + <VStack gap={2}> |
| 60 | + <Checkbox |
| 61 | + checked={focusTrapEnabled} |
| 62 | + onChange={() => setFocusTrapEnabled((prev) => !prev)} |
| 63 | + label="Focus trap enabled" |
| 64 | + /> |
| 65 | + {focusTrapEnabled && ( |
| 66 | + <FocusTrap includeTriggerInFocusTrap> |
| 67 | + <VStack gap={2} background="bgAlternate" padding={2}> |
| 68 | + <Text>Inside FocusTrap</Text> |
| 69 | + <HStack gap={1}> |
| 70 | + <Button>1</Button> |
| 71 | + <Button>2</Button> |
| 72 | + <Button>3</Button> |
| 73 | + </HStack> |
| 74 | + <Checkbox |
| 75 | + checked={focusTrapEnabled} |
| 76 | + onChange={() => setFocusTrapEnabled((prev) => !prev)} |
| 77 | + label="Focus trap enabled" |
| 78 | + /> |
| 79 | + </VStack> |
| 80 | + </FocusTrap> |
| 81 | + )} |
| 82 | + </VStack> |
| 83 | + ); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Restore focus on unmount |
| 88 | + |
| 89 | +The `restoreFocusOnUnmount` prop returns focus to the triggering element when the `<FocusTrap>` is unmounted. |
| 90 | + |
| 91 | +```jsx live |
| 92 | +function Example() { |
| 93 | + const [focusTrapEnabled, setFocusTrapEnabled] = useState(false); |
| 94 | + |
| 95 | + return ( |
| 96 | + <VStack gap={2}> |
| 97 | + <Checkbox |
| 98 | + checked={focusTrapEnabled} |
| 99 | + onChange={() => setFocusTrapEnabled((prev) => !prev)} |
| 100 | + label="Focus trap enabled" |
| 101 | + /> |
| 102 | + {focusTrapEnabled && ( |
| 103 | + <FocusTrap restoreFocusOnUnmount> |
| 104 | + <VStack gap={2} background="bgAlternate" padding={2}> |
| 105 | + <Text>Inside FocusTrap</Text> |
| 106 | + <HStack gap={1}> |
| 107 | + <Button>1</Button> |
| 108 | + <Button>2</Button> |
| 109 | + <Button>3</Button> |
| 110 | + </HStack> |
| 111 | + <Checkbox |
| 112 | + checked={focusTrapEnabled} |
| 113 | + onChange={() => setFocusTrapEnabled((prev) => !prev)} |
| 114 | + label="Focus trap enabled" |
| 115 | + /> |
| 116 | + </VStack> |
| 117 | + </FocusTrap> |
| 118 | + )} |
| 119 | + </VStack> |
| 120 | + ); |
| 121 | +} |
| 122 | +``` |
0 commit comments