Skip to content

Commit

Permalink
fix(clerk-js): Add aria-label and aria-expanded in menu trigger (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef authored May 27, 2024
1 parent 0deaf07 commit d1c3c30
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-fans-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Add `aria-label` and `aria-expanded` in menu trigger to improve accessibility
10 changes: 7 additions & 3 deletions packages/clerk-js/src/ui/elements/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ export const Menu = withFloatingTree((props: MenuProps) => {
);
});

type MenuTriggerProps = React.PropsWithChildren<Record<never, never>>;
type MenuTriggerProps = React.PropsWithChildren<{ arialLabel?: string | ((open: boolean) => string) }>;

export const MenuTrigger = (props: MenuTriggerProps) => {
const { children } = props;
const { children, arialLabel } = props;
const { popoverCtx, elementId } = useMenuState();
const { reference, toggle } = popoverCtx;
const { reference, toggle, isOpen } = popoverCtx;

const normalizedAriaLabel = typeof arialLabel === 'function' ? arialLabel(isOpen) : arialLabel;

if (!isValidElement(children)) {
return null;
Expand All @@ -60,6 +62,8 @@ export const MenuTrigger = (props: MenuTriggerProps) => {
ref: reference,
elementDescriptor: children.props.elementDescriptor || descriptors.menuButton,
elementId: children.props.elementId || descriptors.menuButton.setId(elementId),
'aria-label': normalizedAriaLabel,
'aria-expanded': isOpen,
onClick: (e: React.MouseEvent) => {
children.props?.onClick?.(e);
toggle();
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk-js/src/ui/elements/ThreeDotsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const ThreeDotsMenu = (props: ThreeDotsMenuProps) => {
const { actions, elementId } = props;
return (
<Menu elementId={elementId}>
<MenuTrigger>
<MenuTrigger arialLabel={isOpen => `${isOpen ? 'Close' : 'Open'} menu`}>
<Button
sx={t => ({
padding: t.space.$0x5,
Expand Down

0 comments on commit d1c3c30

Please sign in to comment.