Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useFocus): resolve focus trap issue #6835

Merged
merged 20 commits into from
Feb 10, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions packages/ibm-products/src/global/js/hooks/useFocus.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import { usePrefix } from '@carbon/react';
import { pkg } from '../../../settings';
import { useCallback, useEffect } from 'react';
import wait from '../utils/wait';

export const getSpecificElement = (parentEl, elementId) => {
const element = parentEl?.querySelector(elementId);
Expand Down Expand Up @@ -70,18 +69,14 @@
// Checking whether the key is tab or not
if (event.key === 'Tab') {
// updating the focusable elements list
const { first, last, all } = getFocusable();
const { first, last } = getFocusable();

Check warning on line 72 in packages/ibm-products/src/global/js/hooks/useFocus.js

View check run for this annotation

Codecov / codecov/patch

packages/ibm-products/src/global/js/hooks/useFocus.js#L72

Added line #L72 was not covered by tests

await wait(1);
if (
event.shiftKey &&
!Array.prototype.includes.call(all, document?.activeElement)
) {
if (event.shiftKey && document?.activeElement === first) {

Check warning on line 74 in packages/ibm-products/src/global/js/hooks/useFocus.js

View check run for this annotation

Codecov / codecov/patch

packages/ibm-products/src/global/js/hooks/useFocus.js#L74

Added line #L74 was not covered by tests
// Prevents the default "Tab" behavior
event.preventDefault();
// if the user press shift+tab and the current element not in focusable items
last?.focus();
} else if (!Array.prototype.includes.call(all, document?.activeElement)) {
} else if (!event.shiftKey && document?.activeElement === last) {

Check warning on line 79 in packages/ibm-products/src/global/js/hooks/useFocus.js

View check run for this annotation

Codecov / codecov/patch

packages/ibm-products/src/global/js/hooks/useFocus.js#L79

Added line #L79 was not covered by tests
event.preventDefault();
// user pressing tab key only then
// focusing the first element if the current element is not in focusable items
Expand Down
Loading