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

feat(action-popover): allow menu button to be focused programmatically #7128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nuria1110
Copy link
Contributor

fix #5589

Proposed behaviour

Expose focusButton method to programmatically focus on the menu button using ref.

Current behaviour

ActionPopover menu button cannot be focused programmatically.

Checklist

  • Commits follow our style guide
  • Related issues linked in commit messages if required
  • Screenshots are included in the PR if useful
  • All themes are supported if required
  • Unit tests added or updated if required
  • Playwright automation tests added or updated if required
  • Storybook added or updated if required
  • Translations added or updated (including creating or amending translation keys table in storybook) if required
  • Typescript d.ts file added or updated if required
  • Related docs have been updated if required

QA

  • Tested in provided StackBlitz sandbox/Storybook
  • Add new Playwright test coverage if required
  • Carbon implementation matches Design System/designs
  • UI Tests GitHub check reviewed if required

Additional context

Testing instructions

Copy link
Contributor

@damienrobson-sage damienrobson-sage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor suggestion (more from a cleanliness perspective) but works great 👍

Comment on lines 201 to 221
// Keyboard commands implemented as recommended by WAI-ARIA best practices
// https://www.w3.org/TR/wai-aria-practices/examples/menu-button/menu-button-actions.html

const onButtonKeyDown = useCallback(
(e) => {
if (
Events.isSpaceKey(e) ||
Events.isDownKey(e) ||
Events.isEnterKey(e)
) {
e.preventDefault();
e.stopPropagation();
setFocusIndex(firstFocusableItem);
setOpen(true);
} else if (Events.isUpKey(e)) {
e.preventDefault();
e.stopPropagation();
setFocusIndex(lastFocusableItem);
setOpen(true);
}
},
[firstFocusableItem, lastFocusableItem, setOpen],
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): Feels like this can be better written to remove some of the duplicated lines (just for neatness and readability, though - it's functionally fine). Something like this (via Co-Pilot):

const onButtonKeyDown = useCallback(
  (e) => {
    if (
      Events.isSpaceKey(e) ||
      Events.isDownKey(e) ||
      Events.isEnterKey(e) ||
      Events.isUpKey(e)
    ) {
      e.preventDefault();
      e.stopPropagation();
      setFocusIndex(
        Events.isUpKey(e) ? lastFocusableItem : firstFocusableItem
      );
      setOpen(true);
    }
  },
  [firstFocusableItem, lastFocusableItem, setOpen],
);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, a re-read indicates the original code was already there and this "change" was just because it moved, so feel free to completely ignore this 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add this in for some cleanliness

@DipperTheDan DipperTheDan self-requested a review December 16, 2024 13:46
DipperTheDan
DipperTheDan previously approved these changes Dec 16, 2024
DipperTheDan
DipperTheDan previously approved these changes Dec 16, 2024
Exposes `focusButton` method to allow consumers to programmatically focus on the menu button using
ref.

fix #5589
@nuria1110 nuria1110 marked this pull request as ready for review December 17, 2024 10:28
@nuria1110 nuria1110 requested review from a team as code owners December 17, 2024 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Allow for ActionPopover to be focusable
4 participants