Skip to content

The open alternative to Salesforce, designed for AI.

fix(front): keep disabled sortable widgets accessible (#27015)

Félix Malfaitd6f80966a89e
diff --git a/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/components/DragDropItemSortableCell.tsx b/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/components/DragDropItemSortableCell.tsx
index a5d5bb5a13b..49ae574ee77 100644
--- a/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/components/DragDropItemSortableCell.tsx
+++ b/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/components/DragDropItemSortableCell.tsx
@@ -6,7 +6,7 @@ import {
} from '@dnd-kit/abstract/modifiers';
import { type UseSortableInput, useSortable } from '@dnd-kit/react/sortable';
import { styled } from '@linaria/react';
-import { type ReactNode } from 'react';
+import { type ReactNode, useCallback, useState } from 'react';
import { isDefined } from 'twenty-shared/utils';
import { themeCssVariables } from 'twenty-ui/theme';
@@ -15,6 +15,8 @@ import { DRAG_SOURCE_OPACITY } from '@/ui/utilities/drag-and-drop/constants/Drag
import { DragDropItemSortableHandleRefContext } from '@/ui/utilities/drag-and-drop/context/DragDropItemSortableHandleRefContext';
import { type DragDropItemDropTargetOrientation } from '@/ui/utilities/drag-and-drop/types/DragDropItemDropTargetOrientation';
import { preventNativeDragStart } from '@/ui/utilities/drag-and-drop/utils/preventNativeDragStart';
+import { getOwnDndKitAccessibilityAttributes } from '@/ui/utilities/drag-and-drop/utils/getOwnDndKitAccessibilityAttributes';
+import { removeDndKitAccessibilityAttributes } from '@/ui/utilities/drag-and-drop/utils/removeDndKitAccessibilityAttributes';
const SORTABLE_COLLISION_PRIORITY = 3;
@@ -117,17 +119,59 @@ export const DragDropItemSortableCell = ({
feedback: 'clone',
});
+ const [ownAccessibilityAttributesByElement] = useState(
+ () => new WeakMap<Element, Set<string>>(),
+ );
+
+ // A disabled sortable stays unregistered so dnd-kit cannot mark its
+ // activator, and everything inside it, as a disabled button.
+ const connectToDndKit = useCallback(
+ (element: Element | null, connect: (element: Element | null) => void) => {
+ if (!disabled) {
+ if (
+ isDefined(element) &&
+ !ownAccessibilityAttributesByElement.has(element)
+ ) {
+ ownAccessibilityAttributesByElement.set(
+ element,
+ getOwnDndKitAccessibilityAttributes(element),
+ );
+ }
+ connect(element);
+ return;
+ }
+
+ connect(null);
+
+ const ownAttributes = isDefined(element)
+ ? ownAccessibilityAttributesByElement.get(element)
+ : undefined;
+
+ if (isDefined(element) && isDefined(ownAttributes)) {
+ removeDndKitAccessibilityAttributes({ element, ownAttributes });
+ }
+ },
+ [disabled, ownAccessibilityAttributesByElement],
+ );
+
+ const setSortableRef = useCallback(
+ (element: Element | null) => connectToDndKit(element, ref),
+ [connectToDndKit, ref],
+ );
+
+ const setSortableHandleRef = useCallback(
+ (element: Element | null) => connectToDndKit(element, handleRef),
+ [connectToDndKit, handleRef],
+ );
+
return (
- <DragDropItemSortableHandleRefContext.Provider value={handleRef}>
+ <DragDropItemSortableHandleRefContext.Provider value={setSortableHandleRef}>
<StyledSortableRoot
- ref={ref}
+ ref={setSortableRef}
$disabled={disabled}
$fill={fill}
$isDragSourceFaded={fadeSourceWhileDragging && isDragSource}
$isDraggingHighlighted={highlightWhileDragging && isDragging}
- // dnd-kit stamps role="button" and tabindex unless both are declared, putting disabled cells in the tab order and breaking automation clicks
- role={disabled ? 'none' : undefined}
- tabIndex={disabled ? -1 : undefined}
onDragStart={
disabled && allowNativeDragWhenDisabled
? undefined
diff --git a/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/components/__tests__/DragDropItemSortableCell.test.tsx b/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/components/__tests__/DragDropItemSortableCell.test.tsx
index ae304a46db8..4adf630302a 100644
--- a/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/components/__tests__/DragDropItemSortableCell.test.tsx
+++ b/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/components/__tests__/DragDropItemSortableCell.test.tsx
@@ -1,76 +1,229 @@
import { DragDropItemSortableCell } from '@/ui/utilities/drag-and-drop/components/DragDropItemSortableCell';
-import { DragDropProvider } from '@dnd-kit/react';
+import { DragDropItemSortableHandle } from '@/ui/utilities/drag-and-drop/components/DragDropItemSortableHandle';
+import { DragDropItemSortableHandleRefContext } from '@/ui/utilities/drag-and-drop/context/DragDropItemSortableHandleRefContext';
+import { type DragDropEvents, DragDropProvider } from '@dnd-kit/react';
import { render, screen, waitFor } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { useContext } from 'react';
-// Playwright resolves clicks to the closest button-like ancestor, so a disabled-button wrapper makes its content unclickable
-const CLICK_TARGET_ANCESTOR_SELECTOR =
- 'button, [role=button], [role=checkbox], [role=radio]';
-
-const renderSortableCell = ({ disabled }: { disabled: boolean }) =>
- render(
- <DragDropProvider>
- <DragDropItemSortableCell
- id="widget-id"
- index={0}
- group="tab-id"
- disabled={disabled}
- >
- <div data-testid="widget-content">Emails</div>
- </DragDropItemSortableCell>
- </DragDropProvider>,
+const TabHandle = ({ disabled }: { disabled: boolean }) => {
+ const handleRef = useContext(DragDropItemSortableHandleRefContext);
+
+ return (
+ <button
+ ref={handleRef}
+ type="button"
+ role="tab"
+ tabIndex={-1}
+ aria-disabled={disabled || undefined}
+ >
+ Overview
+ </button>
);
+};
+
+const renderTab = (disabled: boolean) => (
+ <DragDropProvider>
+ <DragDropItemSortableCell
+ id="tab-id"
+ index={0}
+ group="tab-list"
+ disabled={disabled}
+ >
+ <TabHandle disabled={disabled} />
+ </DragDropItemSortableCell>
+ </DragDropProvider>
+);
+
+const expectOwnTabSemantics = (disabled: boolean) => {
+ const tab = screen.getByRole('tab', { name: 'Overview' });
+ expect(tab).toHaveAttribute('tabindex', '-1');
+ if (disabled) {
+ expect(tab).toHaveAttribute('aria-disabled', 'true');
+ } else {
+ expect(tab).not.toHaveAttribute('aria-disabled', 'true');
+ }
+ return tab;
+};
+
+const renderContent = ({
+ disabled,
+ withHandle = false,
+ onBeforeDragStart,
+}: {
+ disabled: boolean;
+ withHandle?: boolean;
+ onBeforeDragStart?: DragDropEvents['beforedragstart'];
+}) => (
+ <DragDropProvider onBeforeDragStart={onBeforeDragStart}>
+ <DragDropItemSortableCell
+ id="widget-id"
+ index={0}
+ group="tab-id"
+ disabled={disabled}
+ >
+ {withHandle && (
+ <DragDropItemSortableHandle disabled={disabled}>
+ Widget title
+ </DragDropItemSortableHandle>
+ )}
+ <input aria-label="Message" />
+ <button type="button">Send</button>
+ </DragDropItemSortableCell>
+ </DragDropProvider>
+);
-const getSortableRoot = () =>
- screen.getByTestId('widget-content').parentElement;
+const getSortableRoot = () => screen.getByRole('textbox').parentElement;
+
+const expectAccessibleContent = () => {
+ for (const element of [
+ screen.getByRole('textbox', { name: 'Message' }),
+ screen.getByRole('button', { name: 'Send' }),
+ ]) {
+ expect(element.closest('[aria-disabled="true"]')).toBeNull();
+ }
+ expect(getSortableRoot()).not.toHaveAttribute('role', 'button');
+ expect(getSortableRoot()).not.toHaveAttribute('tabindex', '0');
+ expect(getSortableRoot()).not.toHaveAttribute('aria-roledescription');
+ expect(getSortableRoot()).not.toHaveAttribute('aria-describedby');
+};
describe('DragDropItemSortableCell', () => {
- it('keeps content clickable when dragging is disabled', async () => {
- renderSortableCell({ disabled: true });
+ // jsdom lacks the Web Animations API that dnd-kit's keyboard sensor queries
+ // before activating a drag.
+ beforeAll(() => {
+ for (const target of [document, Element.prototype]) {
+ Object.defineProperty(target, 'getAnimations', {
+ configurable: true,
+ value: () => [],
+ });
+ }
+ });
- await waitFor(() => {
- expect(getSortableRoot()).toHaveAttribute('aria-disabled', 'true');
- });
+ afterAll(() => {
+ for (const target of [document, Element.prototype]) {
+ Reflect.deleteProperty(target, 'getAnimations');
+ }
+ });
+
+ it('keeps inputs and buttons accessible when dragging is disabled', async () => {
+ const user = userEvent.setup();
+ render(renderContent({ disabled: true }));
- expect(
- screen
- .getByTestId('widget-content')
- .closest(CLICK_TARGET_ANCESTOR_SELECTOR),
- ).toBeNull();
- expect(getSortableRoot()).not.toHaveAttribute('tabindex', '0');
+ await user.tab();
+ expect(screen.getByRole('textbox')).toHaveFocus();
+ await user.keyboard('Hello');
+ await user.tab();
+ expect(screen.getByRole('button', { name: 'Send' })).toHaveFocus();
+ expect(screen.getByRole('textbox')).toHaveValue('Hello');
+ expectAccessibleContent();
});
- it('exposes the sortable root as a button when dragging is enabled', async () => {
- renderSortableCell({ disabled: false });
+ it('exposes keyboard drag affordances when dragging is enabled', async () => {
+ const user = userEvent.setup();
+ render(renderContent({ disabled: false }));
await waitFor(() => {
expect(getSortableRoot()).toHaveAttribute('role', 'button');
+ expect(getSortableRoot()).toHaveAttribute('tabindex', '0');
+ expect(getSortableRoot()).toHaveAttribute('aria-disabled', 'false');
});
+ await user.tab();
+ expect(getSortableRoot()).toHaveFocus();
});
- it('restores the drag affordances when dragging is enabled again', async () => {
- const { rerender } = renderSortableCell({ disabled: true });
+ it('preserves the editor when dragging is repeatedly enabled and disabled', async () => {
+ const user = userEvent.setup();
+ const { rerender } = render(renderContent({ disabled: true }));
+ const editor = screen.getByRole('textbox');
+ await user.tab();
+ await user.keyboard('Unsaved message');
+
+ for (let transition = 0; transition < 2; transition++) {
+ rerender(renderContent({ disabled: false }));
+ await waitFor(() => {
+ expect(getSortableRoot()).toHaveAttribute('role', 'button');
+ expect(getSortableRoot()).toHaveAttribute('aria-disabled', 'false');
+ });
+ rerender(renderContent({ disabled: true }));
+ await user.tab();
+ expectAccessibleContent();
+ expect(screen.getByRole('textbox')).toBe(editor);
+ expect(editor).toHaveValue('Unsaved message');
+ }
+ });
+
+ it('starts a keyboard drag after dragging is enabled again', async () => {
+ const user = userEvent.setup();
+ const handleDragActivation = jest.fn();
+ // Cancelling right after activation keeps the drag out of jsdom's missing
+ // layout observers.
+ const onBeforeDragStart: DragDropEvents['beforedragstart'] = (event) => {
+ handleDragActivation();
+ event.preventDefault();
+ };
+ const { rerender } = render(
+ renderContent({ disabled: true, onBeforeDragStart }),
+ );
+
+ rerender(renderContent({ disabled: false, onBeforeDragStart }));
+ await waitFor(() => {
+ expect(getSortableRoot()).toHaveAttribute('tabindex', '0');
+ });
+ await user.tab();
+ expect(getSortableRoot()).toHaveFocus();
+ await user.keyboard(' ');
await waitFor(() => {
- expect(getSortableRoot()).toHaveAttribute('aria-disabled', 'true');
+ expect(handleDragActivation).toHaveBeenCalledTimes(1);
});
+ });
- rerender(
- <DragDropProvider>
- <DragDropItemSortableCell
- id="widget-id"
- index={0}
- group="tab-id"
- disabled={false}
- >
- <div data-testid="widget-content">Emails</div>
- </DragDropItemSortableCell>
- </DragDropProvider>,
+ it('removes drag affordances from an explicit handle when dragging is disabled', async () => {
+ const user = userEvent.setup();
+ const { rerender } = render(
+ renderContent({ disabled: false, withHandle: true }),
);
+ await waitFor(() => {
+ expect(
+ screen.getByRole('button', { name: 'Widget title' }),
+ ).toHaveAttribute('aria-disabled', 'false');
+ });
+ expect(getSortableRoot()).not.toHaveAttribute('role', 'button');
+
+ rerender(renderContent({ disabled: true, withHandle: true }));
+ await user.tab();
+ expect(screen.getByRole('textbox')).toHaveFocus();
+ expect(screen.queryByRole('button', { name: 'Widget title' })).toBeNull();
+ expect(screen.getByText('Widget title')).not.toHaveAttribute(
+ 'aria-disabled',
+ );
+ expectAccessibleContent();
+ });
+
+ it('keeps the semantics a handle renders itself when dragging is disabled', async () => {
+ const { rerender } = render(renderTab(true));
+ expect(expectOwnTabSemantics(true)).not.toHaveAttribute(
+ 'aria-roledescription',
+ );
+
+ rerender(renderTab(false));
await waitFor(() => {
- expect(getSortableRoot()).toHaveAttribute('role', 'button');
+ expect(expectOwnTabSemantics(false)).toHaveAttribute(
+ 'aria-roledescription',
+ 'draggable',
+ );
+ });
+
+ rerender(renderTab(true));
+ await waitFor(() => {
+ expect(expectOwnTabSemantics(true)).not.toHaveAttribute(
+ 'aria-roledescription',
+ );
});
- expect(getSortableRoot()).toHaveAttribute('tabindex', '0');
- expect(getSortableRoot()).toHaveAttribute('aria-disabled', 'false');
+ const tab = screen.getByRole('tab', { name: 'Overview' });
+ expect(tab).not.toHaveAttribute('aria-describedby');
+ expect(tab).not.toHaveAttribute('aria-pressed');
});
});
diff --git a/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/constants/DndKitAccessibilityAttributes.ts b/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/constants/DndKitAccessibilityAttributes.ts
new file mode 100644
index 00000000000..be640eddda8
--- /dev/null
+++ b/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/constants/DndKitAccessibilityAttributes.ts
@@ -0,0 +1,11 @@
+// Attributes dnd-kit's accessibility plugin sets on a drag activator and
+// never removes once the activator is unregistered.
+export const DND_KIT_ACCESSIBILITY_ATTRIBUTES = [
+ 'role',
+ 'tabindex',
+ 'aria-roledescription',
+ 'aria-describedby',
+ 'aria-pressed',
+ 'aria-grabbed',
+ 'aria-disabled',
+];
diff --git a/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/constants/DndKitAccessibilityStateAttributes.ts b/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/constants/DndKitAccessibilityStateAttributes.ts
new file mode 100644
index 00000000000..d20bd48639d
--- /dev/null
+++ b/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/constants/DndKitAccessibilityStateAttributes.ts
@@ -0,0 +1,7 @@
+// dnd-kit keeps these in sync with the drag state, so once a drag settles on
+// an enabled activator they hold 'false'.
+export const DND_KIT_ACCESSIBILITY_STATE_ATTRIBUTES = [
+ 'aria-pressed',
+ 'aria-grabbed',
+ 'aria-disabled',
+];
diff --git a/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/utils/getOwnDndKitAccessibilityAttributes.ts b/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/utils/getOwnDndKitAccessibilityAttributes.ts
new file mode 100644
index 00000000000..4de9eab53d8
--- /dev/null
+++ b/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/utils/getOwnDndKitAccessibilityAttributes.ts
@@ -0,0 +1,8 @@
+import { DND_KIT_ACCESSIBILITY_ATTRIBUTES } from '@/ui/utilities/drag-and-drop/constants/DndKitAccessibilityAttributes';
+
+export const getOwnDndKitAccessibilityAttributes = (element: Element) =>
+ new Set(
+ DND_KIT_ACCESSIBILITY_ATTRIBUTES.filter((attribute) =>
+ element.hasAttribute(attribute),
+ ),
+ );
diff --git a/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/utils/removeDndKitAccessibilityAttributes.ts b/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/utils/removeDndKitAccessibilityAttributes.ts
new file mode 100644
index 00000000000..96a1937b582
--- /dev/null
+++ b/packages/twenty-front/src/modules/ui/utilities/drag-and-drop/utils/removeDndKitAccessibilityAttributes.ts
@@ -0,0 +1,27 @@
+import { DND_KIT_ACCESSIBILITY_ATTRIBUTES } from '@/ui/utilities/drag-and-drop/constants/DndKitAccessibilityAttributes';
+import { DND_KIT_ACCESSIBILITY_STATE_ATTRIBUTES } from '@/ui/utilities/drag-and-drop/constants/DndKitAccessibilityStateAttributes';
+
+export const removeDndKitAccessibilityAttributes = ({
+ element,
+ ownAttributes,
+}: {
+ element: Element;
+ ownAttributes: Set<string>;
+}) => {
+ for (const attribute of DND_KIT_ACCESSIBILITY_ATTRIBUTES) {
+ if (ownAttributes.has(attribute)) {
+ continue;
+ }
+
+ // A state the element renders itself since registration, like a tab
+ // becoming aria-disabled, differs from the idle value dnd-kit leaves.
+ if (
+ DND_KIT_ACCESSIBILITY_STATE_ATTRIBUTES.includes(attribute) &&
+ element.getAttribute(attribute) !== 'false'
+ ) {
+ continue;
+ }
+
+ element.removeAttribute(attribute);
+ }
+};