Skip to content

Commit 904c07d

Browse files
committed
feat(useFocusTrap): add triggeredDefault prop
This allows to determine if the focus trap should be triggered by default
1 parent d8db45e commit 904c07d

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

packages/orbit-components/src/Dialog/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const Dialog = ({
4343
const ref = React.useRef<HTMLDivElement | null>(null);
4444
const theme = useTheme();
4545

46-
useFocusTrap(ref);
46+
useFocusTrap(ref, true);
4747

4848
React.useEffect(() => {
4949
const transitionLength = parseFloat(theme.orbit.durationFast) * 1000;

packages/orbit-components/src/hooks/useFocusTrap/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const Component = (props: Props) => {
2121

2222
The table below contains all parameters available to the `useFocusTrap` hook.
2323

24-
| Name | Type | Description |
25-
| :--- | :----- | :------------------ |
26-
| ref | `func` | Component reference |
24+
| Name | Type | Description |
25+
| :--------------- | :-------- | :-------------------------------------------- |
26+
| ref | `func` | Component reference |
27+
| triggeredDefault | `boolean` | Whether to trigger the focus trap by default. |

packages/orbit-components/src/hooks/useFocusTrap/index.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import * as React from "react";
22

33
import FOCUSABLE_ELEMENT_SELECTORS from "./consts";
4-
import KEY_CODE_MAP from "../../common/keyMaps";
54

65
interface FocusElements {
76
first: HTMLElement | null;
87
last: HTMLElement | null;
98
}
109

11-
type UseFocusTrap = <T extends HTMLElement>(ref: React.RefObject<T>) => void;
10+
type UseFocusTrap = <T extends HTMLElement>(
11+
ref: React.RefObject<T>,
12+
triggeredDefault?: boolean,
13+
) => void;
1214

1315
const manageFocus = (ref, triggered): FocusElements => {
1416
if (triggered && ref.current) {
@@ -25,12 +27,12 @@ const manageFocus = (ref, triggered): FocusElements => {
2527
return { first: null, last: null };
2628
};
2729

28-
const useFocusTrap: UseFocusTrap = ref => {
29-
const [triggered, setTriggered] = React.useState(false);
30+
const useFocusTrap: UseFocusTrap = (ref, triggeredDefault = false) => {
31+
const [triggered, setTriggered] = React.useState(triggeredDefault);
3032

3133
React.useEffect(() => {
3234
const handleKeyDown = (ev: KeyboardEvent) => {
33-
if (ev.keyCode === KEY_CODE_MAP.TAB) {
35+
if (ev.key === "Tab") {
3436
if (!triggered) {
3537
setTriggered(true);
3638
}

0 commit comments

Comments
 (0)