Skip to content

Commit 35ab2bd

Browse files
committed
fix: remove invalid hook usage in wrapFocus utils
1 parent 5a86693 commit 35ab2bd

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

packages/react/src/internal/__tests__/wrapFocus-test.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { wrapFocus } from '../wrapFocus';
8+
import { wrapFocus, wrapFocusWithoutSentinels } from '../wrapFocus';
99

1010
describe('wrapFocus', () => {
1111
let node;
@@ -146,3 +146,54 @@ describe('wrapFocus', () => {
146146
expect(spyInnerModal).toHaveBeenCalled();
147147
});
148148
});
149+
150+
describe('wrapFocusWithoutSentinels', () => {
151+
let container;
152+
let button1;
153+
154+
beforeEach(() => {
155+
container = document.createElement('div');
156+
container.innerHTML = `
157+
<button id="button-0">Button 0</button>
158+
<button id="button-1">Button 1</button>
159+
`;
160+
document.body.appendChild(container);
161+
button1 = container.querySelector('#button-1');
162+
});
163+
164+
afterEach(() => {
165+
document.body.removeChild(container);
166+
});
167+
168+
it('should not throw an error when called during keydown event', () => {
169+
const event = new KeyboardEvent('keydown', { key: 'Tab' });
170+
Object.defineProperty(event, 'shiftKey', { value: false });
171+
172+
expect(() =>
173+
wrapFocusWithoutSentinels({
174+
containerNode: container,
175+
currentActiveNode: button1,
176+
event,
177+
})
178+
).not.toThrow();
179+
});
180+
181+
it('should throw an error when called during unsupported event', () => {
182+
const event = new FocusEvent('focusin');
183+
const originalEnv = process.env.NODE_ENV;
184+
185+
process.env.NODE_ENV = 'development';
186+
187+
expect(() =>
188+
wrapFocusWithoutSentinels({
189+
containerNode: container,
190+
currentActiveNode: button1,
191+
event,
192+
})
193+
).toThrow(
194+
/^Error: wrapFocusWithoutSentinels\(\.\.\.\) called in unsupported focusin event\.\s+Call wrapFocusWithoutSentinels\(\.\.\.\) from onKeyDown instead\.$/
195+
);
196+
197+
process.env.NODE_ENV = originalEnv;
198+
});
199+
});

packages/react/src/internal/wrapFocus.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { useEffect } from 'react';
98
import { tabbable } from 'tabbable';
109
import { selectorTabbable } from './keyboard/navigation';
1110

@@ -143,16 +142,14 @@ export const wrapFocusWithoutSentinels = ({
143142
}) => {
144143
if (!containerNode) return;
145144

146-
useEffect(() => {
147-
if (
148-
['blur', 'focusout', 'focusin', 'focus'].includes(event.type) &&
149-
process.env.NODE_ENV !== 'production'
150-
) {
151-
throw new Error(
152-
`Error: wrapFocusWithoutSentinels(...) called in unsupported ${event.type} event.\n\nCall wrapFocusWithoutSentinels(...) from onKeyDown instead.`
153-
);
154-
}
155-
}, []);
145+
if (
146+
['blur', 'focusout', 'focusin', 'focus'].includes(event.type) &&
147+
process.env.NODE_ENV !== 'production'
148+
) {
149+
throw new Error(
150+
`Error: wrapFocusWithoutSentinels(...) called in unsupported ${event.type} event.\n\nCall wrapFocusWithoutSentinels(...) from onKeyDown instead.`
151+
);
152+
}
156153

157154
// Use `tabbable` to get the focusable elements in tab order.
158155
// `selectorTabbable` returns elements in DOM order which is why it's not

0 commit comments

Comments
 (0)