Clean Markup

Accessible List Reordering without Drag-Only Interactions

Accessible List Reordering without Drag-Only Interactions

Sortable lists are a staple of task boards, playlist editors, and navigation builders, yet many implementations still depend exclusively on pointer dragging or the native HTML draggable="true" attribute. Drag-only interfaces lock out keyboard-only users, fail switch and voice-control workflows, and violate WCAG 2.2 Success Criterion 2.5.7 (Dragging Movements), which requires any dragging operation to have an equivalent single-pointer alternative such as simple button clicks. Even when developers bolt on custom ArrowUp and ArrowDown key handlers to a draggable card, they frequently omit visible affordances, lose focus when moving DOM nodes, or stay silent when an item changes rank.

An inclusive sortable list starts with semantic markup—an ordered <ol> where each <li> contains explicit, clearly labeled Move Up and Move Down <button type="button"> controls. Those native buttons work immediately with touch, mouse clicks, voice commands, and Tab/Enter/Space keyboard activation. To preserve flow when reordering, JavaScript should restore focus directly onto the activated button after moving its parent <li> in the DOM, dynamically update boundary disabled states, and write a concise confirmation into a polite aria-live="polite" status region. Optional pointer drag-and-drop can then be layered on top as a progressive convenience rather than the sole mechanism.

Example

<ol id="priority-list" aria-label="Task priorities">
  <li data-item-id="audit">
    <span>Run accessibility audit</span>
    <button type="button" data-move="up" aria-label="Move Run accessibility audit up" disabled>Up</button>
    <button type="button" data-move="down" aria-label="Move Run accessibility audit down">Down</button>
  </li>
  <li data-item-id="tokens">
    <span>Refine color contrast tokens</span>
    <button type="button" data-move="up" aria-label="Move Refine color contrast tokens up">Up</button>
    <button type="button" data-move="down" aria-label="Move Refine color contrast tokens down" disabled>Down</button>
  </li>
</ol>
<p id="reorder-status" class="sr-only" aria-live="polite" aria-atomic="true"></p>

Implementation

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Accessible Sortable List</title>
  <style>
    .sr-only {
      position: absolute;
      width: 1px;
      height: 1px;
      padding: 0;
      margin: -1px;
      overflow: hidden;
      clip-path: inset(50%);
      white-space: nowrap;
      border: 0;
    }

    .sortable-list {
      list-style: none;
      padding: 0;
      margin: 0;
      max-width: 34rem;
      display: grid;
      gap: 0.5rem;
      font-family: system-ui, sans-serif;
    }

    .sortable-item {
      display: grid;
      grid-template-columns: auto 1fr auto;
      align-items: center;
      gap: 0.75rem;
      padding: 0.75rem 1rem;
      border: 1px solid #d4d4d8;
      border-radius: 0.5rem;
      background: #ffffff;
    }

    .sortable-item.is-dragging {
      opacity: 0.6;
      border-style: dashed;
    }

    .item-rank {
      font-weight: 600;
      color: #52525b;
      min-width: 1.5rem;
    }

    .item-actions {
      display: inline-flex;
      gap: 0.375rem;
    }

    .move-btn {
      padding: 0.35rem 0.65rem;
      border: 1px solid #d4d4d8;
      border-radius: 0.375rem;
      background: #f4f4f5;
      color: #18181b;
      font: inherit;
      font-size: 0.875rem;
      cursor: pointer;
    }

    .move-btn:disabled {
      opacity: 0.45;
      cursor: not-allowed;
    }

    .move-btn:focus-visible {
      outline: 2px solid #2563eb;
      outline-offset: 2px;
    }
  </style>
</head>
<body>
  <h2 id="list-heading">Release Checklist Priority</h2>

  <ol id="checklist" class="sortable-list" aria-labelledby="list-heading">
    <li class="sortable-item" draggable="true" data-label="Verify keyboard focus order">
      <span class="item-rank" aria-hidden="true">1.</span>
      <span class="item-title">Verify keyboard focus order</span>
      <div class="item-actions">
        <button class="move-btn" type="button" data-move="up">Up</button>
        <button class="move-btn" type="button" data-move="down">Down</button>
      </div>
    </li>
    <li class="sortable-item" draggable="true" data-label="Audit form error announcements">
      <span class="item-rank" aria-hidden="true">2.</span>
      <span class="item-title">Audit form error announcements</span>
      <div class="item-actions">
        <button class="move-btn" type="button" data-move="up">Up</button>
        <button class="move-btn" type="button" data-move="down">Down</button>
      </div>
    </li>
    <li class="sortable-item" draggable="true" data-label="Test reduced-motion stylesheets">
      <span class="item-rank" aria-hidden="true">3.</span>
      <span class="item-title">Test reduced-motion stylesheets</span>
      <div class="item-actions">
        <button class="move-btn" type="button" data-move="up">Up</button>
        <button class="move-btn" type="button" data-move="down">Down</button>
      </div>
    </li>
  </ol>

  <p id="sort-live-region" class="sr-only" aria-live="polite" aria-atomic="true"></p>

  <script>
    const list = document.getElementById('checklist');
    const liveRegion = document.getElementById('sort-live-region');
    let draggedItem = null;

    function syncListSemantics() {
      const items = Array.from(list.children);
      const total = items.length;

      items.forEach((item, index) => {
        const rank = index + 1;
        const label = item.dataset.label || `Item ${rank}`;
        const rankEl = item.querySelector('.item-rank');
        const upBtn = item.querySelector('[data-move="up"]');
        const downBtn = item.querySelector('[data-move="down"]');

        if (rankEl) rankEl.textContent = `${rank}.`;

        if (upBtn) {
          upBtn.disabled = index === 0;
          upBtn.setAttribute('aria-label', `Move ${label} up to position ${Math.max(1, rank - 1)}`);
        }

        if (downBtn) {
          downBtn.disabled = index === total - 1;
          downBtn.setAttribute('aria-label', `Move ${label} down to position ${Math.min(total, rank + 1)}`);
        }
      });
    }

    function announcePosition(item) {
      const items = Array.from(list.children);
      const position = items.indexOf(item) + 1;
      const label = item.dataset.label || `Item ${position}`;
      liveRegion.textContent = `${label} moved to position ${position} of ${items.length}.`;
    }

    function moveItem(item, direction, triggerBtn) {
      if (direction === 'up' && item.previousElementSibling) {
        list.insertBefore(item, item.previousElementSibling);
      } else if (direction === 'down' && item.nextElementSibling) {
        list.insertBefore(item.nextElementSibling, item);
      } else {
        return;
      }

      syncListSemantics();
      announcePosition(item);

      // Keep keyboard focus on the moved control or shift to sibling control at boundaries
      if (triggerBtn && !triggerBtn.disabled) {
        triggerBtn.focus();
      } else {
        const fallbackBtn = item.querySelector('.move-btn:not(:disabled)');
        fallbackBtn?.focus();
      }
    }

    list.addEventListener('click', (event) => {
      const button = event.target.closest('button[data-move]');
      if (!button) return;
      const item = button.closest('.sortable-item');
      if (item) {
        moveItem(item, button.dataset.move, button);
      }
    });

    // Progressive pointer drag-and-drop enhancement
    list.addEventListener('dragstart', (event) => {
      const item = event.target.closest('.sortable-item');
      if (!item) return;
      draggedItem = item;
      item.classList.add('is-dragging');
      event.dataTransfer.effectAllowed = 'move';
    });

    list.addEventListener('dragover', (event) => {
      event.preventDefault();
      const target = event.target.closest('.sortable-item');
      if (!target || !draggedItem || target === draggedItem) return;

      const rect = target.getBoundingClientRect();
      const insertAfter = event.clientY > rect.top + rect.height / 2;
      list.insertBefore(draggedItem, insertAfter ? target.nextElementSibling : target);
    });

    list.addEventListener('dragend', () => {
      if (!draggedItem) return;
      draggedItem.classList.remove('is-dragging');
      syncListSemantics();
      announcePosition(draggedItem);
      draggedItem = null;
    });

    syncListSemantics();
  </script>
</body>
</html>

Main Point: Building sortable lists around explicit Move Up and Move Down buttons, preserved focus targets, and polite aria-live rank announcements guarantees full keyboard and single-pointer compliance before layering on optional drag-and-drop.

Credits

Photo by Alessio Soggetti on Unsplash

← Home