Skip to content

Commit

Permalink
Force tabindex="0" in Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic committed Sep 16, 2024
1 parent 462a397 commit 3fb972e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/safari-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dnd-kit/dom': patch
---

**AccessibilityPlugin**: Force `tabindex="0"` in Safari even for natively focusable elements as they are not always focusable by default.
6 changes: 5 additions & 1 deletion packages/dom/src/core/plugins/accessibility/Accessibility.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {effects} from '@dnd-kit/state';
import {Plugin} from '@dnd-kit/abstract';
import {isSafari} from '@dnd-kit/dom/utilities';

import type {DragDropManager} from '../../manager/index.ts';
import {
Expand Down Expand Up @@ -83,7 +84,10 @@ export class Accessibility extends Plugin<DragDropManager> {
initialize();
}

if (!isFocusable(activator) && !activator.hasAttribute('tabindex')) {
if (
(!isFocusable(activator) || isSafari()) &&
!activator.hasAttribute('tabindex')
) {
activator.setAttribute('tabindex', '0');
}

Expand Down
3 changes: 3 additions & 0 deletions packages/dom/src/utilities/execution-context/isSafari.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isSafari() {
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
}
1 change: 1 addition & 0 deletions packages/dom/src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
export {getVisibleBoundingRectangle} from './bounding-rectangle/getVisibleBoundingRectangle.ts';

export {canUseDOM, getDocument, getWindow} from './execution-context/index.ts';
export {isSafari} from './execution-context/isSafari.ts';

export {cloneElement, ProxiedElements} from './element/index.ts';

Expand Down

0 comments on commit 3fb972e

Please sign in to comment.