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

Navigate overlay dropdowns via arrow keys #2626

Closed
pinussilvestrus opened this issue Dec 10, 2021 · 2 comments · Fixed by #2679
Closed

Navigate overlay dropdowns via arrow keys #2626

pinussilvestrus opened this issue Dec 10, 2021 · 2 comments · Fixed by #2679
Assignees
Labels
enhancement New feature or request spring cleaning Could be cleaned up one day ux

Comments

@pinussilvestrus
Copy link
Contributor

Is your feature request related to a problem? Please describe.

Any overlay dropdown component is accessible via TAB & SHIFT + TAB to navigate through the options via keyboard.

Kapture 2021-12-08 at 18 39 31

That might not be intuitive enough, since any kind of dropdowns and select shall be navigatable via arrow keys as well. That's currently not possible.

Describe the solution you'd like

Make navigation via arrow keys possible.

Describe alternatives you've considered

/

Additional context

This is a follow up of #2616.

@pinussilvestrus pinussilvestrus added enhancement New feature or request ux backlog Queued in backlog labels Dec 10, 2021
@pinussilvestrus
Copy link
Contributor Author

I sketched how this could be possible given the existing option group structures we have in place with this commit.

function Options(props) {
  const { items, onSelect } = props;

  const focusRef = useArrowNavigation();

  return (
    <Section.Body>
      <ul ref={ focusRef }>
        // ...
      </ul>
    </Section.Body>
  );
}

function useArrowNavigation() {
  const ref = React.createRef(0);

  const handleKeydown = (event) => {
    const {
      key,
      keyCode,
      currentTarget
    } = event;

    if (key === 'ArrowDown' && keyCode == 40) {
      focusNext(currentTarget);
    } else if (key === 'ArrowUp' && keyCode == 38) {
      focusPrevious(currentTarget);
    }
  };

  React.useEffect(() => {
    if (!ref.current) {
      return;
    }

    const items = ref.current.querySelectorAll('li');

    items.forEach(i => i.addEventListener('keydown', handleKeydown));

    return () => {
      items.forEach(i => i.removeEventListener('keydown', handleKeydown));
    };

  }, [ ref.current ]);

  return ref;
}

/**
 *
 * @param {Node} focusElement
 */
function focusNext(focusElement) {
  const nextSibling = focusElement.nextSibling;

  // (1) focus immediate neighbor
  if (nextSibling) {
    return nextSibling.querySelector('button').focus();
  }

  // (2) try to find neighbor in other section
  const nextSection = focusElement.closest('section').nextElementSibling;
  return nextSection && nextSection.querySelector('li button').focus();
}

/**
 *
 * @param {Node} focusElement
 */
function focusPrevious(focusElement) {
  const previousSibling = focusElement.previousSibling;

  // (1) focus immediate neighbor
  if (previousSibling) {
    return previousSibling.querySelector('button').focus();
  }

  // (2) try to find neighbor in other section
  const previousSection = focusElement.closest('section').previousElementSibling;
  return previousSection && previousSection.querySelector('li:last-child button').focus();
}

Kapture 2021-12-10 at 09 01 32

branch: https://github.com/camunda/camunda-modeler/tree/navigate-with-arrow-keys

@pinussilvestrus
Copy link
Contributor Author

Regarding the possible keyboard interaction improvements, we could take into consideration the WAI-ARIA recommendations for the menu component: https://www.w3.org/TR/wai-aria-practices/#keyboard-interaction-12

Originally posted: #2616 (comment)

@MaxTru MaxTru added the spring cleaning Could be cleaned up one day label Jan 11, 2022
@pinussilvestrus pinussilvestrus self-assigned this Jan 11, 2022
@pinussilvestrus pinussilvestrus added the ready Ready to be worked on label Jan 14, 2022 — with bpmn-io-tasks
@pinussilvestrus pinussilvestrus removed the backlog Queued in backlog label Jan 14, 2022
@pinussilvestrus pinussilvestrus added the in progress Currently worked on label Jan 17, 2022 — with bpmn-io-tasks
@pinussilvestrus pinussilvestrus removed the ready Ready to be worked on label Jan 17, 2022
@bpmn-io-tasks bpmn-io-tasks bot added needs review Review pending and removed in progress Currently worked on labels Jan 17, 2022
@bpmn-io-tasks bpmn-io-tasks bot removed the needs review Review pending label Jan 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request spring cleaning Could be cleaned up one day ux
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants