components icon indicating copy to clipboard operation
components copied to clipboard

feat(cdk/drag-drop): Proposal for Keyboard and Screenreader Accessibility

Open forsti0506 opened this issue 3 years ago • 2 comments

Feature Description

Hello everyone,

Accessibility is nowadays a very important topic and therefore I think it is essential to get as much work as possible done directly in the framework. Therefore is my proposal to make the Angular CDK Drag and Drop accessible to everyone! My ideas so far

  • Reusing as much code as possible (maybe renaming the functions if selected)
  • Adding aria roles if necessary
  • tabindex to all grabbable items
  • Space to activate moving by keyboard (then activly moving by arrow keys with small movement). Further ideas/improvement: movement to fix containers or movement by bigger (user defined) steps!
  • Escape to quit movement
  • Implementation according to https://www.w3.org/wiki/PF/ARIA/BestPractices/DragDrop

First Code proposal (Please don't look to specific, is my first proposal, Improvements needed): https://github.com/angular/components/commit/1c50475095a7ecf3c1f142c820d3f5a71f0070b8

Is is meaningful to do more work on it? Please be so kind and share your meanings with me!

Best regards,

Martin

Use Case

Drag and Drop Accessibility!

forsti0506 avatar Aug 15 '22 19:08 forsti0506

@crisbeto (marking you because you reviewed my last PR) - Do you think it is useful to do more work on providing accessible drag and drop?

forsti0506 avatar Dec 21 '22 18:12 forsti0506

Hello,

Is there any update on this feature ?

Thanks

vwasteels avatar Sep 30 '24 12:09 vwasteels

a simple workaround for my case, just sharing an idea.

@Directive({
  selector: '[cdkDrag]',
  standalone: true
})
export class KeyboardDragDirective {

  readonly disabled = input(false, { alias: 'cdkDragDisabled' });
  
  constructor() {
    const cdkDropList = inject(CdkDropList, { optional: true });
    if (cdkDropList === null) return; 

    const cdkDrag = inject(CdkDrag, { self: true });
    const hostElement: HTMLElement = inject(ElementRef).nativeElement;
    const focusMonitor = inject(FocusMonitor);

    afterNextRender(() => {
      const dragHandle = hostElement.querySelector<HTMLElement>('[cdkDragHandle]') ?? hostElement;

      dragHandle.addEventListener('keydown', e => {
        if (this.disabled()) return; 
        if (!e.altKey) return;  
        if(!['ArrowUp', 'ArrowRight', 'ArrowDown', 'ArrowLeft'].includes(e.key)) return;  
        if (e.target !== e.currentTarget) return;  

        const previousIndex = cdkDropList.getSortedItems().indexOf(cdkDrag); 
        let currentIndex = previousIndex + (['ArrowDown', 'ArrowRight'].includes(e.key) ? 1 : -1);
        cdkDropList.dropped.emit({ previousIndex, currentIndex } as CdkDragDrop<unknown>);
        window.requestAnimationFrame(() => focusMonitor.focusVia((e.target as HTMLElement), 'keyboard'));
      });
    });
  }
}

keatkeat87 avatar Nov 06 '24 18:11 keatkeat87

Hi Team, Any update on this issue?

msftedad avatar Sep 26 '25 09:09 msftedad

Hi Team, Any update on this issue?

msftedad avatar Dec 18 '25 05:12 msftedad