Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/afraid-bobcats-mate.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Improve accessibility of `<UserButton />` and `<OrganizationSwitcher />` by using `aria-*` attributes (where appropriate) and roles like `menu` and `menuitem`.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { withOrganizationsEnabledGuard } from '../../common';
import { withCoreUserGuard } from '../../contexts';
import { Flow } from '../../customizables';
Expand All@@ -12,19 +14,23 @@ const _OrganizationSwitcher = withFloatingTree(() => {
offset: 8,
});

const switcherButtonMenuId = useId();

return (
<Flow.Root flow='organizationSwitcher'>
<OrganizationSwitcherTrigger
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={switcherButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<OrganizationSwitcherPopover
id={switcherButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,6 +133,8 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
<PopoverCard.Root
elementDescriptor={descriptors.organizationSwitcherPopoverCard}
ref={ref}
role='dialog'
aria-label={`${currentOrg?.name} is active`}
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.organizationSwitcherPopoverMain}>
Expand All@@ -145,7 +147,7 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
user={user}
sx={theme => t => ({ padding: `0 ${theme.space.$6}`, marginBottom: t.space.$2 })}
/>
<Actions>
<Actions role='menu'>
{manageOrganizationButton}
{__unstable_manageBillingUrl && billingOrganizationButton}
</Actions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,6 +33,9 @@ export const OrganizationSwitcherTrigger = withAvatarShimmer(
colorScheme='neutral'
sx={[t => ({ minHeight: 0, padding: `0 ${t.space.$2} 0 0`, position: 'relative' }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} organization switcher`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
Comment thread
LekoArts marked this conversation as resolved.
{...rest}
>
{organization && (
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,7 +44,10 @@ export const OrganizationActionList = (props: OrganizationActionListProps) => {
return (
<>
<UserInvitationSuggestionList />
<SecondaryActions elementDescriptor={descriptors.organizationSwitcherPopoverActions}>
<SecondaryActions
elementDescriptor={descriptors.organizationSwitcherPopoverActions}
role='menu'
>
<UserMembershipList {...{ onPersonalWorkspaceClick, onOrganizationClick }} />
<CreateOrganizationButton {...{ onCreateOrganizationClick }} />
</SecondaryActions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -139,6 +139,7 @@ const SwitcherInvitationActions = (props: PropsOfComponent<typeof Flex> & { show
sx={t => ({
borderTop: showBorder ? `${t.borders.$normal} ${t.colors.$blackAlpha200}` : 'none',
})}
role='menu'
{...restProps}
/>
);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,13 +36,16 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
overflowY: 'auto',
...common.unstyledScrollbar(t),
})}
role='group'
aria-label={hidePersonal ? 'List of all organization memberships' : 'List of all accounts'}
>
{currentOrg && !hidePersonal && (
<PreviewButton
elementDescriptor={descriptors.organizationSwitcherPreviewButton}
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={onPersonalWorkspaceClick}
role='menuitem'
>
<PersonalWorkspacePreview
user={userWithoutIdentifiers}
Expand All@@ -59,6 +62,7 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={() => onOrganizationClick(organization)}
role='menuitem'
>
<OrganizationPreview
elementId='organizationSwitcher'
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
import type { MembershipRole } from '@clerk/types';
import { describe } from '@jest/globals';

import { render, runFakeTimers, waitFor } from '../../../../testUtils';
import { act, render, runFakeTimers, waitFor } from '../../../../testUtils';
import { bindCreateFixtures } from '../../../utils/test/createFixtures';
import { OrganizationSwitcher } from '../OrganizationSwitcher';
import { createFakeUserOrganizationInvitation, createFakeUserOrganizationSuggestion } from './utlis';
Expand All@@ -14,7 +14,7 @@ describe('OrganizationSwitcher', () => {
f.withOrganizations();
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button')).toBeDefined();
});

Expand All@@ -25,7 +25,7 @@ describe('OrganizationSwitcher', () => {
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
props.setProps({ hidePersonal: false });
const { getByText } = render(<OrganizationSwitcher />, { wrapper });
const { getByText } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(getByText('Personal account')).toBeDefined();
});

Expand DownExpand Up@@ -168,7 +168,7 @@ describe('OrganizationSwitcher', () => {
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Manage Organization' }));
await userEvent.click(getByRole('menuitem', { name: 'Manage Organization' }));
expect(fixtures.clerk.openOrganizationProfile).toHaveBeenCalled();
});

Expand All@@ -183,8 +183,8 @@ describe('OrganizationSwitcher', () => {
});
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Create Organization' }));
await userEvent.click(getByRole('button', { name: 'Open organization switcher' }));
await userEvent.click(getByRole('menuitem', { name: 'Create Organization' }));
expect(fixtures.clerk.openCreateOrganization).toHaveBeenCalled();
});

Expand All@@ -198,7 +198,7 @@ describe('OrganizationSwitcher', () => {
});
});
props.setProps({ hidePersonal: true });
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button', { name: 'Create Organization' })).not.toBeInTheDocument();
});

Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { getFullName, getIdentifier } from '../../../utils/user';
import { useCoreUser, useUserButtonContext, withCoreUserGuard } from '../../contexts';
import { descriptors, Flex, Flow, Text } from '../../customizables';
Expand All@@ -14,6 +16,8 @@ const _UserButton = withFloatingTree(() => {
offset: 8,
});

const userButtonMenuId = useId();

return (
<Flow.Root flow='userButton'>
<Flex
Expand All@@ -27,13 +31,15 @@ const _UserButton = withFloatingTree(() => {
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={userButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<UserButtonPopover
id={userButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,13 +35,14 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo

const sessionActions = authConfig.singleSessionMode ? null : otherSessions.length > 0 ? (
<>
<SecondaryActions>
<SecondaryActions role='menu'>
{otherSessions.map(session => (
<PreviewButton
key={session.id}
icon={SwitchArrows}
sx={t => ({ height: t.sizes.$14, borderRadius: 0 })}
onClick={handleSessionClicked(session)}
role='menuitem'
>
<UserPreview
user={session.user}
Expand All@@ -52,7 +53,7 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
))}
{addAccountButton}
</SecondaryActions>
<Actions>
<Actions role='menu'>
<Action
icon={SignOutDouble}
label={localizationKeys('userButton.action__signOutAll')}
Expand All@@ -61,14 +62,16 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
</Actions>
</>
) : (
<SecondaryActions>{addAccountButton}</SecondaryActions>
<SecondaryActions role='menu'>{addAccountButton}</SecondaryActions>
);

return (
<RootBox elementDescriptor={descriptors.userButtonPopoverRootBox}>
<PopoverCard.Root
elementDescriptor={descriptors.userButtonPopoverCard}
ref={ref}
role='dialog'
Comment thread
LekoArts marked this conversation as resolved.
aria-label='User button popover'
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.userButtonPopoverMain}>
Expand All@@ -77,7 +80,10 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
user={user}
sx={theme => ({ padding: `0 ${theme.space.$6}`, marginBottom: theme.space.$2 })}
/>
<Actions elementDescriptor={descriptors.userButtonPopoverActions}>
<Actions
role='menu'
elementDescriptor={descriptors.userButtonPopoverActions}
>
<Action
elementDescriptor={descriptors.userButtonPopoverActionButton}
elementId={descriptors.userButtonPopoverActionButton.setId('manageAccount')}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,9 @@ export const UserButtonTrigger = withAvatarShimmer(
variant='roundWrapper'
sx={[theme => ({ borderRadius: theme.radii.$circle }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} user button`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
{...rest}
>
<UserAvatar
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,7 +31,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('Manage account')).not.toBeNull();
});

Expand All@@ -45,7 +45,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Manage account'));
expect(fixtures.clerk.openUserProfile).toHaveBeenCalled();
});
Expand All@@ -60,7 +60,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalled();
});
Expand DownExpand Up@@ -96,7 +96,7 @@ describe('UserButton', () => {
it('renders all sessions', async () => {
const { wrapper } = await createFixtures(initConfig);
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('First1 Last1')).toBeDefined();
expect(getByText('First2 Last2')).toBeDefined();
expect(getByText('First3 Last3')).toBeDefined();
Expand All@@ -106,7 +106,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.setActive.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('First3 Last3'));
expect(fixtures.clerk.setActive).toHaveBeenCalledWith(
expect.objectContaining({ session: expect.objectContaining({ user: expect.objectContaining({ id: '3' }) }) }),
Expand All@@ -117,7 +117,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.signOut.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalledWith(expect.any(Function), { sessionId: '0' });
});
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/Actions.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,6 +86,7 @@ export const Action = (props: ActionProps) => {
]}
isDisabled={card.isLoading}
onClick={onClick}
role='menuitem'
{...rest}
>
<Flex
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/PoweredByClerk.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,6 +94,7 @@ const LogoMarkIconLink = () => {
'&:hover': { color: 'inherit' },
}}
isExternal
aria-label='Clerk logo'
>
<Icon
icon={LogoMark}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
chore(clerk-js): Improve accessibility in UserButton and OrganizationSwitcher by panteliselef · Pull Request #1826 · clerk/javascript · GitHub
Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/afraid-bobcats-mate.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Improve accessibility of `<UserButton />` and `<OrganizationSwitcher />` by using `aria-*` attributes (where appropriate) and roles like `menu` and `menuitem`.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { withOrganizationsEnabledGuard } from '../../common';
import { withCoreUserGuard } from '../../contexts';
import { Flow } from '../../customizables';
Expand All@@ -12,19 +14,23 @@ const _OrganizationSwitcher = withFloatingTree(() => {
offset: 8,
});

const switcherButtonMenuId = useId();

return (
<Flow.Root flow='organizationSwitcher'>
<OrganizationSwitcherTrigger
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={switcherButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<OrganizationSwitcherPopover
id={switcherButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,6 +133,8 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
<PopoverCard.Root
elementDescriptor={descriptors.organizationSwitcherPopoverCard}
ref={ref}
role='dialog'
aria-label={`${currentOrg?.name} is active`}
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.organizationSwitcherPopoverMain}>
Expand All@@ -145,7 +147,7 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
user={user}
sx={theme => t => ({ padding: `0 ${theme.space.$6}`, marginBottom: t.space.$2 })}
/>
<Actions>
<Actions role='menu'>
{manageOrganizationButton}
{__unstable_manageBillingUrl && billingOrganizationButton}
</Actions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,6 +33,9 @@ export const OrganizationSwitcherTrigger = withAvatarShimmer(
colorScheme='neutral'
sx={[t => ({ minHeight: 0, padding: `0 ${t.space.$2} 0 0`, position: 'relative' }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} organization switcher`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
Comment thread
LekoArts marked this conversation as resolved.
{...rest}
>
{organization && (
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,7 +44,10 @@ export const OrganizationActionList = (props: OrganizationActionListProps) => {
return (
<>
<UserInvitationSuggestionList />
<SecondaryActions elementDescriptor={descriptors.organizationSwitcherPopoverActions}>
<SecondaryActions
elementDescriptor={descriptors.organizationSwitcherPopoverActions}
role='menu'
>
<UserMembershipList {...{ onPersonalWorkspaceClick, onOrganizationClick }} />
<CreateOrganizationButton {...{ onCreateOrganizationClick }} />
</SecondaryActions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -139,6 +139,7 @@ const SwitcherInvitationActions = (props: PropsOfComponent<typeof Flex> & { show
sx={t => ({
borderTop: showBorder ? `${t.borders.$normal} ${t.colors.$blackAlpha200}` : 'none',
})}
role='menu'
{...restProps}
/>
);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,13 +36,16 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
overflowY: 'auto',
...common.unstyledScrollbar(t),
})}
role='group'
aria-label={hidePersonal ? 'List of all organization memberships' : 'List of all accounts'}
>
{currentOrg && !hidePersonal && (
<PreviewButton
elementDescriptor={descriptors.organizationSwitcherPreviewButton}
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={onPersonalWorkspaceClick}
role='menuitem'
>
<PersonalWorkspacePreview
user={userWithoutIdentifiers}
Expand All@@ -59,6 +62,7 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={() => onOrganizationClick(organization)}
role='menuitem'
>
<OrganizationPreview
elementId='organizationSwitcher'
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
import type { MembershipRole } from '@clerk/types';
import { describe } from '@jest/globals';

import { render, runFakeTimers, waitFor } from '../../../../testUtils';
import { act, render, runFakeTimers, waitFor } from '../../../../testUtils';
import { bindCreateFixtures } from '../../../utils/test/createFixtures';
import { OrganizationSwitcher } from '../OrganizationSwitcher';
import { createFakeUserOrganizationInvitation, createFakeUserOrganizationSuggestion } from './utlis';
Expand All@@ -14,7 +14,7 @@ describe('OrganizationSwitcher', () => {
f.withOrganizations();
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button')).toBeDefined();
});

Expand All@@ -25,7 +25,7 @@ describe('OrganizationSwitcher', () => {
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
props.setProps({ hidePersonal: false });
const { getByText } = render(<OrganizationSwitcher />, { wrapper });
const { getByText } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(getByText('Personal account')).toBeDefined();
});

Expand DownExpand Up@@ -168,7 +168,7 @@ describe('OrganizationSwitcher', () => {
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Manage Organization' }));
await userEvent.click(getByRole('menuitem', { name: 'Manage Organization' }));
expect(fixtures.clerk.openOrganizationProfile).toHaveBeenCalled();
});

Expand All@@ -183,8 +183,8 @@ describe('OrganizationSwitcher', () => {
});
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Create Organization' }));
await userEvent.click(getByRole('button', { name: 'Open organization switcher' }));
await userEvent.click(getByRole('menuitem', { name: 'Create Organization' }));
expect(fixtures.clerk.openCreateOrganization).toHaveBeenCalled();
});

Expand All@@ -198,7 +198,7 @@ describe('OrganizationSwitcher', () => {
});
});
props.setProps({ hidePersonal: true });
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button', { name: 'Create Organization' })).not.toBeInTheDocument();
});

Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { getFullName, getIdentifier } from '../../../utils/user';
import { useCoreUser, useUserButtonContext, withCoreUserGuard } from '../../contexts';
import { descriptors, Flex, Flow, Text } from '../../customizables';
Expand All@@ -14,6 +16,8 @@ const _UserButton = withFloatingTree(() => {
offset: 8,
});

const userButtonMenuId = useId();

return (
<Flow.Root flow='userButton'>
<Flex
Expand All@@ -27,13 +31,15 @@ const _UserButton = withFloatingTree(() => {
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={userButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<UserButtonPopover
id={userButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,13 +35,14 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo

const sessionActions = authConfig.singleSessionMode ? null : otherSessions.length > 0 ? (
<>
<SecondaryActions>
<SecondaryActions role='menu'>
{otherSessions.map(session => (
<PreviewButton
key={session.id}
icon={SwitchArrows}
sx={t => ({ height: t.sizes.$14, borderRadius: 0 })}
onClick={handleSessionClicked(session)}
role='menuitem'
>
<UserPreview
user={session.user}
Expand All@@ -52,7 +53,7 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
))}
{addAccountButton}
</SecondaryActions>
<Actions>
<Actions role='menu'>
<Action
icon={SignOutDouble}
label={localizationKeys('userButton.action__signOutAll')}
Expand All@@ -61,14 +62,16 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
</Actions>
</>
) : (
<SecondaryActions>{addAccountButton}</SecondaryActions>
<SecondaryActions role='menu'>{addAccountButton}</SecondaryActions>
);

return (
<RootBox elementDescriptor={descriptors.userButtonPopoverRootBox}>
<PopoverCard.Root
elementDescriptor={descriptors.userButtonPopoverCard}
ref={ref}
role='dialog'
Comment thread
LekoArts marked this conversation as resolved.
aria-label='User button popover'
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.userButtonPopoverMain}>
Expand All@@ -77,7 +80,10 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
user={user}
sx={theme => ({ padding: `0 ${theme.space.$6}`, marginBottom: theme.space.$2 })}
/>
<Actions elementDescriptor={descriptors.userButtonPopoverActions}>
<Actions
role='menu'
elementDescriptor={descriptors.userButtonPopoverActions}
>
<Action
elementDescriptor={descriptors.userButtonPopoverActionButton}
elementId={descriptors.userButtonPopoverActionButton.setId('manageAccount')}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,9 @@ export const UserButtonTrigger = withAvatarShimmer(
variant='roundWrapper'
sx={[theme => ({ borderRadius: theme.radii.$circle }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} user button`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
{...rest}
>
<UserAvatar
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,7 +31,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('Manage account')).not.toBeNull();
});

Expand All@@ -45,7 +45,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Manage account'));
expect(fixtures.clerk.openUserProfile).toHaveBeenCalled();
});
Expand All@@ -60,7 +60,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalled();
});
Expand DownExpand Up@@ -96,7 +96,7 @@ describe('UserButton', () => {
it('renders all sessions', async () => {
const { wrapper } = await createFixtures(initConfig);
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('First1 Last1')).toBeDefined();
expect(getByText('First2 Last2')).toBeDefined();
expect(getByText('First3 Last3')).toBeDefined();
Expand All@@ -106,7 +106,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.setActive.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('First3 Last3'));
expect(fixtures.clerk.setActive).toHaveBeenCalledWith(
expect.objectContaining({ session: expect.objectContaining({ user: expect.objectContaining({ id: '3' }) }) }),
Expand All@@ -117,7 +117,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.signOut.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalledWith(expect.any(Function), { sessionId: '0' });
});
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/Actions.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,6 +86,7 @@ export const Action = (props: ActionProps) => {
]}
isDisabled={card.isLoading}
onClick={onClick}
role='menuitem'
{...rest}
>
<Flex
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/PoweredByClerk.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,6 +94,7 @@ const LogoMarkIconLink = () => {
'&:hover': { color: 'inherit' },
}}
isExternal
aria-label='Clerk logo'
>
<Icon
icon={LogoMark}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' chore(clerk-js): Improve accessibility in UserButton and OrganizationSwitcher by panteliselef · Pull Request #1826 · clerk/javascript · GitHub
Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/afraid-bobcats-mate.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Improve accessibility of `<UserButton />` and `<OrganizationSwitcher />` by using `aria-*` attributes (where appropriate) and roles like `menu` and `menuitem`.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { withOrganizationsEnabledGuard } from '../../common';
import { withCoreUserGuard } from '../../contexts';
import { Flow } from '../../customizables';
Expand All@@ -12,19 +14,23 @@ const _OrganizationSwitcher = withFloatingTree(() => {
offset: 8,
});

const switcherButtonMenuId = useId();

return (
<Flow.Root flow='organizationSwitcher'>
<OrganizationSwitcherTrigger
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={switcherButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<OrganizationSwitcherPopover
id={switcherButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,6 +133,8 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
<PopoverCard.Root
elementDescriptor={descriptors.organizationSwitcherPopoverCard}
ref={ref}
role='dialog'
aria-label={`${currentOrg?.name} is active`}
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.organizationSwitcherPopoverMain}>
Expand All@@ -145,7 +147,7 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
user={user}
sx={theme => t => ({ padding: `0 ${theme.space.$6}`, marginBottom: t.space.$2 })}
/>
<Actions>
<Actions role='menu'>
{manageOrganizationButton}
{__unstable_manageBillingUrl && billingOrganizationButton}
</Actions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,6 +33,9 @@ export const OrganizationSwitcherTrigger = withAvatarShimmer(
colorScheme='neutral'
sx={[t => ({ minHeight: 0, padding: `0 ${t.space.$2} 0 0`, position: 'relative' }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} organization switcher`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
Comment thread
LekoArts marked this conversation as resolved.
{...rest}
>
{organization && (
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,7 +44,10 @@ export const OrganizationActionList = (props: OrganizationActionListProps) => {
return (
<>
<UserInvitationSuggestionList />
<SecondaryActions elementDescriptor={descriptors.organizationSwitcherPopoverActions}>
<SecondaryActions
elementDescriptor={descriptors.organizationSwitcherPopoverActions}
role='menu'
>
<UserMembershipList {...{ onPersonalWorkspaceClick, onOrganizationClick }} />
<CreateOrganizationButton {...{ onCreateOrganizationClick }} />
</SecondaryActions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -139,6 +139,7 @@ const SwitcherInvitationActions = (props: PropsOfComponent<typeof Flex> & { show
sx={t => ({
borderTop: showBorder ? `${t.borders.$normal} ${t.colors.$blackAlpha200}` : 'none',
})}
role='menu'
{...restProps}
/>
);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,13 +36,16 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
overflowY: 'auto',
...common.unstyledScrollbar(t),
})}
role='group'
aria-label={hidePersonal ? 'List of all organization memberships' : 'List of all accounts'}
>
{currentOrg && !hidePersonal && (
<PreviewButton
elementDescriptor={descriptors.organizationSwitcherPreviewButton}
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={onPersonalWorkspaceClick}
role='menuitem'
>
<PersonalWorkspacePreview
user={userWithoutIdentifiers}
Expand All@@ -59,6 +62,7 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={() => onOrganizationClick(organization)}
role='menuitem'
>
<OrganizationPreview
elementId='organizationSwitcher'
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
import type { MembershipRole } from '@clerk/types';
import { describe } from '@jest/globals';

import { render, runFakeTimers, waitFor } from '../../../../testUtils';
import { act, render, runFakeTimers, waitFor } from '../../../../testUtils';
import { bindCreateFixtures } from '../../../utils/test/createFixtures';
import { OrganizationSwitcher } from '../OrganizationSwitcher';
import { createFakeUserOrganizationInvitation, createFakeUserOrganizationSuggestion } from './utlis';
Expand All@@ -14,7 +14,7 @@ describe('OrganizationSwitcher', () => {
f.withOrganizations();
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button')).toBeDefined();
});

Expand All@@ -25,7 +25,7 @@ describe('OrganizationSwitcher', () => {
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
props.setProps({ hidePersonal: false });
const { getByText } = render(<OrganizationSwitcher />, { wrapper });
const { getByText } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(getByText('Personal account')).toBeDefined();
});

Expand DownExpand Up@@ -168,7 +168,7 @@ describe('OrganizationSwitcher', () => {
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Manage Organization' }));
await userEvent.click(getByRole('menuitem', { name: 'Manage Organization' }));
expect(fixtures.clerk.openOrganizationProfile).toHaveBeenCalled();
});

Expand All@@ -183,8 +183,8 @@ describe('OrganizationSwitcher', () => {
});
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Create Organization' }));
await userEvent.click(getByRole('button', { name: 'Open organization switcher' }));
await userEvent.click(getByRole('menuitem', { name: 'Create Organization' }));
expect(fixtures.clerk.openCreateOrganization).toHaveBeenCalled();
});

Expand All@@ -198,7 +198,7 @@ describe('OrganizationSwitcher', () => {
});
});
props.setProps({ hidePersonal: true });
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button', { name: 'Create Organization' })).not.toBeInTheDocument();
});

Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { getFullName, getIdentifier } from '../../../utils/user';
import { useCoreUser, useUserButtonContext, withCoreUserGuard } from '../../contexts';
import { descriptors, Flex, Flow, Text } from '../../customizables';
Expand All@@ -14,6 +16,8 @@ const _UserButton = withFloatingTree(() => {
offset: 8,
});

const userButtonMenuId = useId();

return (
<Flow.Root flow='userButton'>
<Flex
Expand All@@ -27,13 +31,15 @@ const _UserButton = withFloatingTree(() => {
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={userButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<UserButtonPopover
id={userButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,13 +35,14 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo

const sessionActions = authConfig.singleSessionMode ? null : otherSessions.length > 0 ? (
<>
<SecondaryActions>
<SecondaryActions role='menu'>
{otherSessions.map(session => (
<PreviewButton
key={session.id}
icon={SwitchArrows}
sx={t => ({ height: t.sizes.$14, borderRadius: 0 })}
onClick={handleSessionClicked(session)}
role='menuitem'
>
<UserPreview
user={session.user}
Expand All@@ -52,7 +53,7 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
))}
{addAccountButton}
</SecondaryActions>
<Actions>
<Actions role='menu'>
<Action
icon={SignOutDouble}
label={localizationKeys('userButton.action__signOutAll')}
Expand All@@ -61,14 +62,16 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
</Actions>
</>
) : (
<SecondaryActions>{addAccountButton}</SecondaryActions>
<SecondaryActions role='menu'>{addAccountButton}</SecondaryActions>
);

return (
<RootBox elementDescriptor={descriptors.userButtonPopoverRootBox}>
<PopoverCard.Root
elementDescriptor={descriptors.userButtonPopoverCard}
ref={ref}
role='dialog'
Comment thread
LekoArts marked this conversation as resolved.
aria-label='User button popover'
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.userButtonPopoverMain}>
Expand All@@ -77,7 +80,10 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
user={user}
sx={theme => ({ padding: `0 ${theme.space.$6}`, marginBottom: theme.space.$2 })}
/>
<Actions elementDescriptor={descriptors.userButtonPopoverActions}>
<Actions
role='menu'
elementDescriptor={descriptors.userButtonPopoverActions}
>
<Action
elementDescriptor={descriptors.userButtonPopoverActionButton}
elementId={descriptors.userButtonPopoverActionButton.setId('manageAccount')}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,9 @@ export const UserButtonTrigger = withAvatarShimmer(
variant='roundWrapper'
sx={[theme => ({ borderRadius: theme.radii.$circle }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} user button`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
{...rest}
>
<UserAvatar
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,7 +31,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('Manage account')).not.toBeNull();
});

Expand All@@ -45,7 +45,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Manage account'));
expect(fixtures.clerk.openUserProfile).toHaveBeenCalled();
});
Expand All@@ -60,7 +60,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalled();
});
Expand DownExpand Up@@ -96,7 +96,7 @@ describe('UserButton', () => {
it('renders all sessions', async () => {
const { wrapper } = await createFixtures(initConfig);
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('First1 Last1')).toBeDefined();
expect(getByText('First2 Last2')).toBeDefined();
expect(getByText('First3 Last3')).toBeDefined();
Expand All@@ -106,7 +106,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.setActive.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('First3 Last3'));
expect(fixtures.clerk.setActive).toHaveBeenCalledWith(
expect.objectContaining({ session: expect.objectContaining({ user: expect.objectContaining({ id: '3' }) }) }),
Expand All@@ -117,7 +117,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.signOut.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalledWith(expect.any(Function), { sessionId: '0' });
});
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/Actions.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,6 +86,7 @@ export const Action = (props: ActionProps) => {
]}
isDisabled={card.isLoading}
onClick={onClick}
role='menuitem'
{...rest}
>
<Flex
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/PoweredByClerk.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,6 +94,7 @@ const LogoMarkIconLink = () => {
'&:hover': { color: 'inherit' },
}}
isExternal
aria-label='Clerk logo'
>
<Icon
icon={LogoMark}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' chore(clerk-js): Improve accessibility in UserButton and OrganizationSwitcher by panteliselef · Pull Request #1826 · clerk/javascript · GitHub
Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/afraid-bobcats-mate.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Improve accessibility of `<UserButton />` and `<OrganizationSwitcher />` by using `aria-*` attributes (where appropriate) and roles like `menu` and `menuitem`.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { withOrganizationsEnabledGuard } from '../../common';
import { withCoreUserGuard } from '../../contexts';
import { Flow } from '../../customizables';
Expand All@@ -12,19 +14,23 @@ const _OrganizationSwitcher = withFloatingTree(() => {
offset: 8,
});

const switcherButtonMenuId = useId();

return (
<Flow.Root flow='organizationSwitcher'>
<OrganizationSwitcherTrigger
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={switcherButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<OrganizationSwitcherPopover
id={switcherButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,6 +133,8 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
<PopoverCard.Root
elementDescriptor={descriptors.organizationSwitcherPopoverCard}
ref={ref}
role='dialog'
aria-label={`${currentOrg?.name} is active`}
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.organizationSwitcherPopoverMain}>
Expand All@@ -145,7 +147,7 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
user={user}
sx={theme => t => ({ padding: `0 ${theme.space.$6}`, marginBottom: t.space.$2 })}
/>
<Actions>
<Actions role='menu'>
{manageOrganizationButton}
{__unstable_manageBillingUrl && billingOrganizationButton}
</Actions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,6 +33,9 @@ export const OrganizationSwitcherTrigger = withAvatarShimmer(
colorScheme='neutral'
sx={[t => ({ minHeight: 0, padding: `0 ${t.space.$2} 0 0`, position: 'relative' }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} organization switcher`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
Comment thread
LekoArts marked this conversation as resolved.
{...rest}
>
{organization && (
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,7 +44,10 @@ export const OrganizationActionList = (props: OrganizationActionListProps) => {
return (
<>
<UserInvitationSuggestionList />
<SecondaryActions elementDescriptor={descriptors.organizationSwitcherPopoverActions}>
<SecondaryActions
elementDescriptor={descriptors.organizationSwitcherPopoverActions}
role='menu'
>
<UserMembershipList {...{ onPersonalWorkspaceClick, onOrganizationClick }} />
<CreateOrganizationButton {...{ onCreateOrganizationClick }} />
</SecondaryActions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -139,6 +139,7 @@ const SwitcherInvitationActions = (props: PropsOfComponent<typeof Flex> & { show
sx={t => ({
borderTop: showBorder ? `${t.borders.$normal} ${t.colors.$blackAlpha200}` : 'none',
})}
role='menu'
{...restProps}
/>
);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,13 +36,16 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
overflowY: 'auto',
...common.unstyledScrollbar(t),
})}
role='group'
aria-label={hidePersonal ? 'List of all organization memberships' : 'List of all accounts'}
>
{currentOrg && !hidePersonal && (
<PreviewButton
elementDescriptor={descriptors.organizationSwitcherPreviewButton}
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={onPersonalWorkspaceClick}
role='menuitem'
>
<PersonalWorkspacePreview
user={userWithoutIdentifiers}
Expand All@@ -59,6 +62,7 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={() => onOrganizationClick(organization)}
role='menuitem'
>
<OrganizationPreview
elementId='organizationSwitcher'
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
import type { MembershipRole } from '@clerk/types';
import { describe } from '@jest/globals';

import { render, runFakeTimers, waitFor } from '../../../../testUtils';
import { act, render, runFakeTimers, waitFor } from '../../../../testUtils';
import { bindCreateFixtures } from '../../../utils/test/createFixtures';
import { OrganizationSwitcher } from '../OrganizationSwitcher';
import { createFakeUserOrganizationInvitation, createFakeUserOrganizationSuggestion } from './utlis';
Expand All@@ -14,7 +14,7 @@ describe('OrganizationSwitcher', () => {
f.withOrganizations();
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button')).toBeDefined();
});

Expand All@@ -25,7 +25,7 @@ describe('OrganizationSwitcher', () => {
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
props.setProps({ hidePersonal: false });
const { getByText } = render(<OrganizationSwitcher />, { wrapper });
const { getByText } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(getByText('Personal account')).toBeDefined();
});

Expand DownExpand Up@@ -168,7 +168,7 @@ describe('OrganizationSwitcher', () => {
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Manage Organization' }));
await userEvent.click(getByRole('menuitem', { name: 'Manage Organization' }));
expect(fixtures.clerk.openOrganizationProfile).toHaveBeenCalled();
});

Expand All@@ -183,8 +183,8 @@ describe('OrganizationSwitcher', () => {
});
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Create Organization' }));
await userEvent.click(getByRole('button', { name: 'Open organization switcher' }));
await userEvent.click(getByRole('menuitem', { name: 'Create Organization' }));
expect(fixtures.clerk.openCreateOrganization).toHaveBeenCalled();
});

Expand All@@ -198,7 +198,7 @@ describe('OrganizationSwitcher', () => {
});
});
props.setProps({ hidePersonal: true });
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button', { name: 'Create Organization' })).not.toBeInTheDocument();
});

Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { getFullName, getIdentifier } from '../../../utils/user';
import { useCoreUser, useUserButtonContext, withCoreUserGuard } from '../../contexts';
import { descriptors, Flex, Flow, Text } from '../../customizables';
Expand All@@ -14,6 +16,8 @@ const _UserButton = withFloatingTree(() => {
offset: 8,
});

const userButtonMenuId = useId();

return (
<Flow.Root flow='userButton'>
<Flex
Expand All@@ -27,13 +31,15 @@ const _UserButton = withFloatingTree(() => {
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={userButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<UserButtonPopover
id={userButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,13 +35,14 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo

const sessionActions = authConfig.singleSessionMode ? null : otherSessions.length > 0 ? (
<>
<SecondaryActions>
<SecondaryActions role='menu'>
{otherSessions.map(session => (
<PreviewButton
key={session.id}
icon={SwitchArrows}
sx={t => ({ height: t.sizes.$14, borderRadius: 0 })}
onClick={handleSessionClicked(session)}
role='menuitem'
>
<UserPreview
user={session.user}
Expand All@@ -52,7 +53,7 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
))}
{addAccountButton}
</SecondaryActions>
<Actions>
<Actions role='menu'>
<Action
icon={SignOutDouble}
label={localizationKeys('userButton.action__signOutAll')}
Expand All@@ -61,14 +62,16 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
</Actions>
</>
) : (
<SecondaryActions>{addAccountButton}</SecondaryActions>
<SecondaryActions role='menu'>{addAccountButton}</SecondaryActions>
);

return (
<RootBox elementDescriptor={descriptors.userButtonPopoverRootBox}>
<PopoverCard.Root
elementDescriptor={descriptors.userButtonPopoverCard}
ref={ref}
role='dialog'
Comment thread
LekoArts marked this conversation as resolved.
aria-label='User button popover'
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.userButtonPopoverMain}>
Expand All@@ -77,7 +80,10 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
user={user}
sx={theme => ({ padding: `0 ${theme.space.$6}`, marginBottom: theme.space.$2 })}
/>
<Actions elementDescriptor={descriptors.userButtonPopoverActions}>
<Actions
role='menu'
elementDescriptor={descriptors.userButtonPopoverActions}
>
<Action
elementDescriptor={descriptors.userButtonPopoverActionButton}
elementId={descriptors.userButtonPopoverActionButton.setId('manageAccount')}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,9 @@ export const UserButtonTrigger = withAvatarShimmer(
variant='roundWrapper'
sx={[theme => ({ borderRadius: theme.radii.$circle }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} user button`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
{...rest}
>
<UserAvatar
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,7 +31,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('Manage account')).not.toBeNull();
});

Expand All@@ -45,7 +45,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Manage account'));
expect(fixtures.clerk.openUserProfile).toHaveBeenCalled();
});
Expand All@@ -60,7 +60,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalled();
});
Expand DownExpand Up@@ -96,7 +96,7 @@ describe('UserButton', () => {
it('renders all sessions', async () => {
const { wrapper } = await createFixtures(initConfig);
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('First1 Last1')).toBeDefined();
expect(getByText('First2 Last2')).toBeDefined();
expect(getByText('First3 Last3')).toBeDefined();
Expand All@@ -106,7 +106,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.setActive.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('First3 Last3'));
expect(fixtures.clerk.setActive).toHaveBeenCalledWith(
expect.objectContaining({ session: expect.objectContaining({ user: expect.objectContaining({ id: '3' }) }) }),
Expand All@@ -117,7 +117,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.signOut.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalledWith(expect.any(Function), { sessionId: '0' });
});
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/Actions.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,6 +86,7 @@ export const Action = (props: ActionProps) => {
]}
isDisabled={card.isLoading}
onClick={onClick}
role='menuitem'
{...rest}
>
<Flex
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/PoweredByClerk.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,6 +94,7 @@ const LogoMarkIconLink = () => {
'&:hover': { color: 'inherit' },
}}
isExternal
aria-label='Clerk logo'
>
<Icon
icon={LogoMark}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' chore(clerk-js): Improve accessibility in UserButton and OrganizationSwitcher by panteliselef · Pull Request #1826 · clerk/javascript · GitHub
Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/afraid-bobcats-mate.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Improve accessibility of `<UserButton />` and `<OrganizationSwitcher />` by using `aria-*` attributes (where appropriate) and roles like `menu` and `menuitem`.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { withOrganizationsEnabledGuard } from '../../common';
import { withCoreUserGuard } from '../../contexts';
import { Flow } from '../../customizables';
Expand All@@ -12,19 +14,23 @@ const _OrganizationSwitcher = withFloatingTree(() => {
offset: 8,
});

const switcherButtonMenuId = useId();

return (
<Flow.Root flow='organizationSwitcher'>
<OrganizationSwitcherTrigger
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={switcherButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<OrganizationSwitcherPopover
id={switcherButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,6 +133,8 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
<PopoverCard.Root
elementDescriptor={descriptors.organizationSwitcherPopoverCard}
ref={ref}
role='dialog'
aria-label={`${currentOrg?.name} is active`}
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.organizationSwitcherPopoverMain}>
Expand All@@ -145,7 +147,7 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
user={user}
sx={theme => t => ({ padding: `0 ${theme.space.$6}`, marginBottom: t.space.$2 })}
/>
<Actions>
<Actions role='menu'>
{manageOrganizationButton}
{__unstable_manageBillingUrl && billingOrganizationButton}
</Actions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,6 +33,9 @@ export const OrganizationSwitcherTrigger = withAvatarShimmer(
colorScheme='neutral'
sx={[t => ({ minHeight: 0, padding: `0 ${t.space.$2} 0 0`, position: 'relative' }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} organization switcher`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
Comment thread
LekoArts marked this conversation as resolved.
{...rest}
>
{organization && (
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,7 +44,10 @@ export const OrganizationActionList = (props: OrganizationActionListProps) => {
return (
<>
<UserInvitationSuggestionList />
<SecondaryActions elementDescriptor={descriptors.organizationSwitcherPopoverActions}>
<SecondaryActions
elementDescriptor={descriptors.organizationSwitcherPopoverActions}
role='menu'
>
<UserMembershipList {...{ onPersonalWorkspaceClick, onOrganizationClick }} />
<CreateOrganizationButton {...{ onCreateOrganizationClick }} />
</SecondaryActions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -139,6 +139,7 @@ const SwitcherInvitationActions = (props: PropsOfComponent<typeof Flex> & { show
sx={t => ({
borderTop: showBorder ? `${t.borders.$normal} ${t.colors.$blackAlpha200}` : 'none',
})}
role='menu'
{...restProps}
/>
);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,13 +36,16 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
overflowY: 'auto',
...common.unstyledScrollbar(t),
})}
role='group'
aria-label={hidePersonal ? 'List of all organization memberships' : 'List of all accounts'}
>
{currentOrg && !hidePersonal && (
<PreviewButton
elementDescriptor={descriptors.organizationSwitcherPreviewButton}
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={onPersonalWorkspaceClick}
role='menuitem'
>
<PersonalWorkspacePreview
user={userWithoutIdentifiers}
Expand All@@ -59,6 +62,7 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={() => onOrganizationClick(organization)}
role='menuitem'
>
<OrganizationPreview
elementId='organizationSwitcher'
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
import type { MembershipRole } from '@clerk/types';
import { describe } from '@jest/globals';

import { render, runFakeTimers, waitFor } from '../../../../testUtils';
import { act, render, runFakeTimers, waitFor } from '../../../../testUtils';
import { bindCreateFixtures } from '../../../utils/test/createFixtures';
import { OrganizationSwitcher } from '../OrganizationSwitcher';
import { createFakeUserOrganizationInvitation, createFakeUserOrganizationSuggestion } from './utlis';
Expand All@@ -14,7 +14,7 @@ describe('OrganizationSwitcher', () => {
f.withOrganizations();
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button')).toBeDefined();
});

Expand All@@ -25,7 +25,7 @@ describe('OrganizationSwitcher', () => {
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
props.setProps({ hidePersonal: false });
const { getByText } = render(<OrganizationSwitcher />, { wrapper });
const { getByText } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(getByText('Personal account')).toBeDefined();
});

Expand DownExpand Up@@ -168,7 +168,7 @@ describe('OrganizationSwitcher', () => {
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Manage Organization' }));
await userEvent.click(getByRole('menuitem', { name: 'Manage Organization' }));
expect(fixtures.clerk.openOrganizationProfile).toHaveBeenCalled();
});

Expand All@@ -183,8 +183,8 @@ describe('OrganizationSwitcher', () => {
});
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Create Organization' }));
await userEvent.click(getByRole('button', { name: 'Open organization switcher' }));
await userEvent.click(getByRole('menuitem', { name: 'Create Organization' }));
expect(fixtures.clerk.openCreateOrganization).toHaveBeenCalled();
});

Expand All@@ -198,7 +198,7 @@ describe('OrganizationSwitcher', () => {
});
});
props.setProps({ hidePersonal: true });
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button', { name: 'Create Organization' })).not.toBeInTheDocument();
});

Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { getFullName, getIdentifier } from '../../../utils/user';
import { useCoreUser, useUserButtonContext, withCoreUserGuard } from '../../contexts';
import { descriptors, Flex, Flow, Text } from '../../customizables';
Expand All@@ -14,6 +16,8 @@ const _UserButton = withFloatingTree(() => {
offset: 8,
});

const userButtonMenuId = useId();

return (
<Flow.Root flow='userButton'>
<Flex
Expand All@@ -27,13 +31,15 @@ const _UserButton = withFloatingTree(() => {
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={userButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<UserButtonPopover
id={userButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,13 +35,14 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo

const sessionActions = authConfig.singleSessionMode ? null : otherSessions.length > 0 ? (
<>
<SecondaryActions>
<SecondaryActions role='menu'>
{otherSessions.map(session => (
<PreviewButton
key={session.id}
icon={SwitchArrows}
sx={t => ({ height: t.sizes.$14, borderRadius: 0 })}
onClick={handleSessionClicked(session)}
role='menuitem'
>
<UserPreview
user={session.user}
Expand All@@ -52,7 +53,7 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
))}
{addAccountButton}
</SecondaryActions>
<Actions>
<Actions role='menu'>
<Action
icon={SignOutDouble}
label={localizationKeys('userButton.action__signOutAll')}
Expand All@@ -61,14 +62,16 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
</Actions>
</>
) : (
<SecondaryActions>{addAccountButton}</SecondaryActions>
<SecondaryActions role='menu'>{addAccountButton}</SecondaryActions>
);

return (
<RootBox elementDescriptor={descriptors.userButtonPopoverRootBox}>
<PopoverCard.Root
elementDescriptor={descriptors.userButtonPopoverCard}
ref={ref}
role='dialog'
Comment thread
LekoArts marked this conversation as resolved.
aria-label='User button popover'
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.userButtonPopoverMain}>
Expand All@@ -77,7 +80,10 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
user={user}
sx={theme => ({ padding: `0 ${theme.space.$6}`, marginBottom: theme.space.$2 })}
/>
<Actions elementDescriptor={descriptors.userButtonPopoverActions}>
<Actions
role='menu'
elementDescriptor={descriptors.userButtonPopoverActions}
>
<Action
elementDescriptor={descriptors.userButtonPopoverActionButton}
elementId={descriptors.userButtonPopoverActionButton.setId('manageAccount')}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,9 @@ export const UserButtonTrigger = withAvatarShimmer(
variant='roundWrapper'
sx={[theme => ({ borderRadius: theme.radii.$circle }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} user button`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
{...rest}
>
<UserAvatar
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,7 +31,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('Manage account')).not.toBeNull();
});

Expand All@@ -45,7 +45,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Manage account'));
expect(fixtures.clerk.openUserProfile).toHaveBeenCalled();
});
Expand All@@ -60,7 +60,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalled();
});
Expand DownExpand Up@@ -96,7 +96,7 @@ describe('UserButton', () => {
it('renders all sessions', async () => {
const { wrapper } = await createFixtures(initConfig);
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('First1 Last1')).toBeDefined();
expect(getByText('First2 Last2')).toBeDefined();
expect(getByText('First3 Last3')).toBeDefined();
Expand All@@ -106,7 +106,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.setActive.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('First3 Last3'));
expect(fixtures.clerk.setActive).toHaveBeenCalledWith(
expect.objectContaining({ session: expect.objectContaining({ user: expect.objectContaining({ id: '3' }) }) }),
Expand All@@ -117,7 +117,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.signOut.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalledWith(expect.any(Function), { sessionId: '0' });
});
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/Actions.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,6 +86,7 @@ export const Action = (props: ActionProps) => {
]}
isDisabled={card.isLoading}
onClick={onClick}
role='menuitem'
{...rest}
>
<Flex
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/PoweredByClerk.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,6 +94,7 @@ const LogoMarkIconLink = () => {
'&:hover': { color: 'inherit' },
}}
isExternal
aria-label='Clerk logo'
>
<Icon
icon={LogoMark}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' chore(clerk-js): Improve accessibility in UserButton and OrganizationSwitcher by panteliselef · Pull Request #1826 · clerk/javascript · GitHub
Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/afraid-bobcats-mate.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Improve accessibility of `<UserButton />` and `<OrganizationSwitcher />` by using `aria-*` attributes (where appropriate) and roles like `menu` and `menuitem`.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { withOrganizationsEnabledGuard } from '../../common';
import { withCoreUserGuard } from '../../contexts';
import { Flow } from '../../customizables';
Expand All@@ -12,19 +14,23 @@ const _OrganizationSwitcher = withFloatingTree(() => {
offset: 8,
});

const switcherButtonMenuId = useId();

return (
<Flow.Root flow='organizationSwitcher'>
<OrganizationSwitcherTrigger
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={switcherButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<OrganizationSwitcherPopover
id={switcherButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,6 +133,8 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
<PopoverCard.Root
elementDescriptor={descriptors.organizationSwitcherPopoverCard}
ref={ref}
role='dialog'
aria-label={`${currentOrg?.name} is active`}
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.organizationSwitcherPopoverMain}>
Expand All@@ -145,7 +147,7 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
user={user}
sx={theme => t => ({ padding: `0 ${theme.space.$6}`, marginBottom: t.space.$2 })}
/>
<Actions>
<Actions role='menu'>
{manageOrganizationButton}
{__unstable_manageBillingUrl && billingOrganizationButton}
</Actions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,6 +33,9 @@ export const OrganizationSwitcherTrigger = withAvatarShimmer(
colorScheme='neutral'
sx={[t => ({ minHeight: 0, padding: `0 ${t.space.$2} 0 0`, position: 'relative' }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} organization switcher`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
Comment thread
LekoArts marked this conversation as resolved.
{...rest}
>
{organization && (
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,7 +44,10 @@ export const OrganizationActionList = (props: OrganizationActionListProps) => {
return (
<>
<UserInvitationSuggestionList />
<SecondaryActions elementDescriptor={descriptors.organizationSwitcherPopoverActions}>
<SecondaryActions
elementDescriptor={descriptors.organizationSwitcherPopoverActions}
role='menu'
>
<UserMembershipList {...{ onPersonalWorkspaceClick, onOrganizationClick }} />
<CreateOrganizationButton {...{ onCreateOrganizationClick }} />
</SecondaryActions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -139,6 +139,7 @@ const SwitcherInvitationActions = (props: PropsOfComponent<typeof Flex> & { show
sx={t => ({
borderTop: showBorder ? `${t.borders.$normal} ${t.colors.$blackAlpha200}` : 'none',
})}
role='menu'
{...restProps}
/>
);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,13 +36,16 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
overflowY: 'auto',
...common.unstyledScrollbar(t),
})}
role='group'
aria-label={hidePersonal ? 'List of all organization memberships' : 'List of all accounts'}
>
{currentOrg && !hidePersonal && (
<PreviewButton
elementDescriptor={descriptors.organizationSwitcherPreviewButton}
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={onPersonalWorkspaceClick}
role='menuitem'
>
<PersonalWorkspacePreview
user={userWithoutIdentifiers}
Expand All@@ -59,6 +62,7 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={() => onOrganizationClick(organization)}
role='menuitem'
>
<OrganizationPreview
elementId='organizationSwitcher'
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
import type { MembershipRole } from '@clerk/types';
import { describe } from '@jest/globals';

import { render, runFakeTimers, waitFor } from '../../../../testUtils';
import { act, render, runFakeTimers, waitFor } from '../../../../testUtils';
import { bindCreateFixtures } from '../../../utils/test/createFixtures';
import { OrganizationSwitcher } from '../OrganizationSwitcher';
import { createFakeUserOrganizationInvitation, createFakeUserOrganizationSuggestion } from './utlis';
Expand All@@ -14,7 +14,7 @@ describe('OrganizationSwitcher', () => {
f.withOrganizations();
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button')).toBeDefined();
});

Expand All@@ -25,7 +25,7 @@ describe('OrganizationSwitcher', () => {
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
props.setProps({ hidePersonal: false });
const { getByText } = render(<OrganizationSwitcher />, { wrapper });
const { getByText } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(getByText('Personal account')).toBeDefined();
});

Expand DownExpand Up@@ -168,7 +168,7 @@ describe('OrganizationSwitcher', () => {
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Manage Organization' }));
await userEvent.click(getByRole('menuitem', { name: 'Manage Organization' }));
expect(fixtures.clerk.openOrganizationProfile).toHaveBeenCalled();
});

Expand All@@ -183,8 +183,8 @@ describe('OrganizationSwitcher', () => {
});
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Create Organization' }));
await userEvent.click(getByRole('button', { name: 'Open organization switcher' }));
await userEvent.click(getByRole('menuitem', { name: 'Create Organization' }));
expect(fixtures.clerk.openCreateOrganization).toHaveBeenCalled();
});

Expand All@@ -198,7 +198,7 @@ describe('OrganizationSwitcher', () => {
});
});
props.setProps({ hidePersonal: true });
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button', { name: 'Create Organization' })).not.toBeInTheDocument();
});

Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { getFullName, getIdentifier } from '../../../utils/user';
import { useCoreUser, useUserButtonContext, withCoreUserGuard } from '../../contexts';
import { descriptors, Flex, Flow, Text } from '../../customizables';
Expand All@@ -14,6 +16,8 @@ const _UserButton = withFloatingTree(() => {
offset: 8,
});

const userButtonMenuId = useId();

return (
<Flow.Root flow='userButton'>
<Flex
Expand All@@ -27,13 +31,15 @@ const _UserButton = withFloatingTree(() => {
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={userButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<UserButtonPopover
id={userButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,13 +35,14 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo

const sessionActions = authConfig.singleSessionMode ? null : otherSessions.length > 0 ? (
<>
<SecondaryActions>
<SecondaryActions role='menu'>
{otherSessions.map(session => (
<PreviewButton
key={session.id}
icon={SwitchArrows}
sx={t => ({ height: t.sizes.$14, borderRadius: 0 })}
onClick={handleSessionClicked(session)}
role='menuitem'
>
<UserPreview
user={session.user}
Expand All@@ -52,7 +53,7 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
))}
{addAccountButton}
</SecondaryActions>
<Actions>
<Actions role='menu'>
<Action
icon={SignOutDouble}
label={localizationKeys('userButton.action__signOutAll')}
Expand All@@ -61,14 +62,16 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
</Actions>
</>
) : (
<SecondaryActions>{addAccountButton}</SecondaryActions>
<SecondaryActions role='menu'>{addAccountButton}</SecondaryActions>
);

return (
<RootBox elementDescriptor={descriptors.userButtonPopoverRootBox}>
<PopoverCard.Root
elementDescriptor={descriptors.userButtonPopoverCard}
ref={ref}
role='dialog'
Comment thread
LekoArts marked this conversation as resolved.
aria-label='User button popover'
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.userButtonPopoverMain}>
Expand All@@ -77,7 +80,10 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
user={user}
sx={theme => ({ padding: `0 ${theme.space.$6}`, marginBottom: theme.space.$2 })}
/>
<Actions elementDescriptor={descriptors.userButtonPopoverActions}>
<Actions
role='menu'
elementDescriptor={descriptors.userButtonPopoverActions}
>
<Action
elementDescriptor={descriptors.userButtonPopoverActionButton}
elementId={descriptors.userButtonPopoverActionButton.setId('manageAccount')}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,9 @@ export const UserButtonTrigger = withAvatarShimmer(
variant='roundWrapper'
sx={[theme => ({ borderRadius: theme.radii.$circle }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} user button`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
{...rest}
>
<UserAvatar
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,7 +31,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('Manage account')).not.toBeNull();
});

Expand All@@ -45,7 +45,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Manage account'));
expect(fixtures.clerk.openUserProfile).toHaveBeenCalled();
});
Expand All@@ -60,7 +60,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalled();
});
Expand DownExpand Up@@ -96,7 +96,7 @@ describe('UserButton', () => {
it('renders all sessions', async () => {
const { wrapper } = await createFixtures(initConfig);
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('First1 Last1')).toBeDefined();
expect(getByText('First2 Last2')).toBeDefined();
expect(getByText('First3 Last3')).toBeDefined();
Expand All@@ -106,7 +106,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.setActive.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('First3 Last3'));
expect(fixtures.clerk.setActive).toHaveBeenCalledWith(
expect.objectContaining({ session: expect.objectContaining({ user: expect.objectContaining({ id: '3' }) }) }),
Expand All@@ -117,7 +117,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.signOut.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalledWith(expect.any(Function), { sessionId: '0' });
});
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/Actions.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,6 +86,7 @@ export const Action = (props: ActionProps) => {
]}
isDisabled={card.isLoading}
onClick={onClick}
role='menuitem'
{...rest}
>
<Flex
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/PoweredByClerk.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,6 +94,7 @@ const LogoMarkIconLink = () => {
'&:hover': { color: 'inherit' },
}}
isExternal
aria-label='Clerk logo'
>
<Icon
icon={LogoMark}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' chore(clerk-js): Improve accessibility in UserButton and OrganizationSwitcher by panteliselef · Pull Request #1826 · clerk/javascript · GitHub
Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/afraid-bobcats-mate.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Improve accessibility of `<UserButton />` and `<OrganizationSwitcher />` by using `aria-*` attributes (where appropriate) and roles like `menu` and `menuitem`.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { withOrganizationsEnabledGuard } from '../../common';
import { withCoreUserGuard } from '../../contexts';
import { Flow } from '../../customizables';
Expand All@@ -12,19 +14,23 @@ const _OrganizationSwitcher = withFloatingTree(() => {
offset: 8,
});

const switcherButtonMenuId = useId();

return (
<Flow.Root flow='organizationSwitcher'>
<OrganizationSwitcherTrigger
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={switcherButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<OrganizationSwitcherPopover
id={switcherButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,6 +133,8 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
<PopoverCard.Root
elementDescriptor={descriptors.organizationSwitcherPopoverCard}
ref={ref}
role='dialog'
aria-label={`${currentOrg?.name} is active`}
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.organizationSwitcherPopoverMain}>
Expand All@@ -145,7 +147,7 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
user={user}
sx={theme => t => ({ padding: `0 ${theme.space.$6}`, marginBottom: t.space.$2 })}
/>
<Actions>
<Actions role='menu'>
{manageOrganizationButton}
{__unstable_manageBillingUrl && billingOrganizationButton}
</Actions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,6 +33,9 @@ export const OrganizationSwitcherTrigger = withAvatarShimmer(
colorScheme='neutral'
sx={[t => ({ minHeight: 0, padding: `0 ${t.space.$2} 0 0`, position: 'relative' }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} organization switcher`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
Comment thread
LekoArts marked this conversation as resolved.
{...rest}
>
{organization && (
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,7 +44,10 @@ export const OrganizationActionList = (props: OrganizationActionListProps) => {
return (
<>
<UserInvitationSuggestionList />
<SecondaryActions elementDescriptor={descriptors.organizationSwitcherPopoverActions}>
<SecondaryActions
elementDescriptor={descriptors.organizationSwitcherPopoverActions}
role='menu'
>
<UserMembershipList {...{ onPersonalWorkspaceClick, onOrganizationClick }} />
<CreateOrganizationButton {...{ onCreateOrganizationClick }} />
</SecondaryActions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -139,6 +139,7 @@ const SwitcherInvitationActions = (props: PropsOfComponent<typeof Flex> & { show
sx={t => ({
borderTop: showBorder ? `${t.borders.$normal} ${t.colors.$blackAlpha200}` : 'none',
})}
role='menu'
{...restProps}
/>
);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,13 +36,16 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
overflowY: 'auto',
...common.unstyledScrollbar(t),
})}
role='group'
aria-label={hidePersonal ? 'List of all organization memberships' : 'List of all accounts'}
>
{currentOrg && !hidePersonal && (
<PreviewButton
elementDescriptor={descriptors.organizationSwitcherPreviewButton}
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={onPersonalWorkspaceClick}
role='menuitem'
>
<PersonalWorkspacePreview
user={userWithoutIdentifiers}
Expand All@@ -59,6 +62,7 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={() => onOrganizationClick(organization)}
role='menuitem'
>
<OrganizationPreview
elementId='organizationSwitcher'
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
import type { MembershipRole } from '@clerk/types';
import { describe } from '@jest/globals';

import { render, runFakeTimers, waitFor } from '../../../../testUtils';
import { act, render, runFakeTimers, waitFor } from '../../../../testUtils';
import { bindCreateFixtures } from '../../../utils/test/createFixtures';
import { OrganizationSwitcher } from '../OrganizationSwitcher';
import { createFakeUserOrganizationInvitation, createFakeUserOrganizationSuggestion } from './utlis';
Expand All@@ -14,7 +14,7 @@ describe('OrganizationSwitcher', () => {
f.withOrganizations();
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button')).toBeDefined();
});

Expand All@@ -25,7 +25,7 @@ describe('OrganizationSwitcher', () => {
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
props.setProps({ hidePersonal: false });
const { getByText } = render(<OrganizationSwitcher />, { wrapper });
const { getByText } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(getByText('Personal account')).toBeDefined();
});

Expand DownExpand Up@@ -168,7 +168,7 @@ describe('OrganizationSwitcher', () => {
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Manage Organization' }));
await userEvent.click(getByRole('menuitem', { name: 'Manage Organization' }));
expect(fixtures.clerk.openOrganizationProfile).toHaveBeenCalled();
});

Expand All@@ -183,8 +183,8 @@ describe('OrganizationSwitcher', () => {
});
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Create Organization' }));
await userEvent.click(getByRole('button', { name: 'Open organization switcher' }));
await userEvent.click(getByRole('menuitem', { name: 'Create Organization' }));
expect(fixtures.clerk.openCreateOrganization).toHaveBeenCalled();
});

Expand All@@ -198,7 +198,7 @@ describe('OrganizationSwitcher', () => {
});
});
props.setProps({ hidePersonal: true });
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button', { name: 'Create Organization' })).not.toBeInTheDocument();
});

Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { getFullName, getIdentifier } from '../../../utils/user';
import { useCoreUser, useUserButtonContext, withCoreUserGuard } from '../../contexts';
import { descriptors, Flex, Flow, Text } from '../../customizables';
Expand All@@ -14,6 +16,8 @@ const _UserButton = withFloatingTree(() => {
offset: 8,
});

const userButtonMenuId = useId();

return (
<Flow.Root flow='userButton'>
<Flex
Expand All@@ -27,13 +31,15 @@ const _UserButton = withFloatingTree(() => {
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={userButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<UserButtonPopover
id={userButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,13 +35,14 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo

const sessionActions = authConfig.singleSessionMode ? null : otherSessions.length > 0 ? (
<>
<SecondaryActions>
<SecondaryActions role='menu'>
{otherSessions.map(session => (
<PreviewButton
key={session.id}
icon={SwitchArrows}
sx={t => ({ height: t.sizes.$14, borderRadius: 0 })}
onClick={handleSessionClicked(session)}
role='menuitem'
>
<UserPreview
user={session.user}
Expand All@@ -52,7 +53,7 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
))}
{addAccountButton}
</SecondaryActions>
<Actions>
<Actions role='menu'>
<Action
icon={SignOutDouble}
label={localizationKeys('userButton.action__signOutAll')}
Expand All@@ -61,14 +62,16 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
</Actions>
</>
) : (
<SecondaryActions>{addAccountButton}</SecondaryActions>
<SecondaryActions role='menu'>{addAccountButton}</SecondaryActions>
);

return (
<RootBox elementDescriptor={descriptors.userButtonPopoverRootBox}>
<PopoverCard.Root
elementDescriptor={descriptors.userButtonPopoverCard}
ref={ref}
role='dialog'
Comment thread
LekoArts marked this conversation as resolved.
aria-label='User button popover'
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.userButtonPopoverMain}>
Expand All@@ -77,7 +80,10 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
user={user}
sx={theme => ({ padding: `0 ${theme.space.$6}`, marginBottom: theme.space.$2 })}
/>
<Actions elementDescriptor={descriptors.userButtonPopoverActions}>
<Actions
role='menu'
elementDescriptor={descriptors.userButtonPopoverActions}
>
<Action
elementDescriptor={descriptors.userButtonPopoverActionButton}
elementId={descriptors.userButtonPopoverActionButton.setId('manageAccount')}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,9 @@ export const UserButtonTrigger = withAvatarShimmer(
variant='roundWrapper'
sx={[theme => ({ borderRadius: theme.radii.$circle }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} user button`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
{...rest}
>
<UserAvatar
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,7 +31,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('Manage account')).not.toBeNull();
});

Expand All@@ -45,7 +45,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Manage account'));
expect(fixtures.clerk.openUserProfile).toHaveBeenCalled();
});
Expand All@@ -60,7 +60,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalled();
});
Expand DownExpand Up@@ -96,7 +96,7 @@ describe('UserButton', () => {
it('renders all sessions', async () => {
const { wrapper } = await createFixtures(initConfig);
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('First1 Last1')).toBeDefined();
expect(getByText('First2 Last2')).toBeDefined();
expect(getByText('First3 Last3')).toBeDefined();
Expand All@@ -106,7 +106,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.setActive.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('First3 Last3'));
expect(fixtures.clerk.setActive).toHaveBeenCalledWith(
expect.objectContaining({ session: expect.objectContaining({ user: expect.objectContaining({ id: '3' }) }) }),
Expand All@@ -117,7 +117,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.signOut.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalledWith(expect.any(Function), { sessionId: '0' });
});
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/Actions.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,6 +86,7 @@ export const Action = (props: ActionProps) => {
]}
isDisabled={card.isLoading}
onClick={onClick}
role='menuitem'
{...rest}
>
<Flex
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/PoweredByClerk.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,6 +94,7 @@ const LogoMarkIconLink = () => {
'&:hover': { color: 'inherit' },
}}
isExternal
aria-label='Clerk logo'
>
<Icon
icon={LogoMark}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); chore(clerk-js): Improve accessibility in UserButton and OrganizationSwitcher by panteliselef · Pull Request #1826 · clerk/javascript · GitHub
Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/afraid-bobcats-mate.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Improve accessibility of `<UserButton />` and `<OrganizationSwitcher />` by using `aria-*` attributes (where appropriate) and roles like `menu` and `menuitem`.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { withOrganizationsEnabledGuard } from '../../common';
import { withCoreUserGuard } from '../../contexts';
import { Flow } from '../../customizables';
Expand All@@ -12,19 +14,23 @@ const _OrganizationSwitcher = withFloatingTree(() => {
offset: 8,
});

const switcherButtonMenuId = useId();

return (
<Flow.Root flow='organizationSwitcher'>
<OrganizationSwitcherTrigger
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={switcherButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<OrganizationSwitcherPopover
id={switcherButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,6 +133,8 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
<PopoverCard.Root
elementDescriptor={descriptors.organizationSwitcherPopoverCard}
ref={ref}
role='dialog'
aria-label={`${currentOrg?.name} is active`}
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.organizationSwitcherPopoverMain}>
Expand All@@ -145,7 +147,7 @@ export const OrganizationSwitcherPopover = React.forwardRef<HTMLDivElement, Orga
user={user}
sx={theme => t => ({ padding: `0 ${theme.space.$6}`, marginBottom: t.space.$2 })}
/>
<Actions>
<Actions role='menu'>
{manageOrganizationButton}
{__unstable_manageBillingUrl && billingOrganizationButton}
</Actions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,6 +33,9 @@ export const OrganizationSwitcherTrigger = withAvatarShimmer(
colorScheme='neutral'
sx={[t => ({ minHeight: 0, padding: `0 ${t.space.$2} 0 0`, position: 'relative' }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} organization switcher`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
Comment thread
LekoArts marked this conversation as resolved.
{...rest}
>
{organization && (
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,7 +44,10 @@ export const OrganizationActionList = (props: OrganizationActionListProps) => {
return (
<>
<UserInvitationSuggestionList />
<SecondaryActions elementDescriptor={descriptors.organizationSwitcherPopoverActions}>
<SecondaryActions
elementDescriptor={descriptors.organizationSwitcherPopoverActions}
role='menu'
>
<UserMembershipList {...{ onPersonalWorkspaceClick, onOrganizationClick }} />
<CreateOrganizationButton {...{ onCreateOrganizationClick }} />
</SecondaryActions>
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -139,6 +139,7 @@ const SwitcherInvitationActions = (props: PropsOfComponent<typeof Flex> & { show
sx={t => ({
borderTop: showBorder ? `${t.borders.$normal} ${t.colors.$blackAlpha200}` : 'none',
})}
role='menu'
{...restProps}
/>
);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,13 +36,16 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
overflowY: 'auto',
...common.unstyledScrollbar(t),
})}
role='group'
aria-label={hidePersonal ? 'List of all organization memberships' : 'List of all accounts'}
>
{currentOrg && !hidePersonal && (
<PreviewButton
elementDescriptor={descriptors.organizationSwitcherPreviewButton}
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={onPersonalWorkspaceClick}
role='menuitem'
>
<PersonalWorkspacePreview
user={userWithoutIdentifiers}
Expand All@@ -59,6 +62,7 @@ export const UserMembershipList = (props: UserMembershipListProps) => {
icon={SwitchArrows}
sx={{ borderRadius: 0 }}
onClick={() => onOrganizationClick(organization)}
role='menuitem'
>
<OrganizationPreview
elementId='organizationSwitcher'
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
import type { MembershipRole } from '@clerk/types';
import { describe } from '@jest/globals';

import { render, runFakeTimers, waitFor } from '../../../../testUtils';
import { act, render, runFakeTimers, waitFor } from '../../../../testUtils';
import { bindCreateFixtures } from '../../../utils/test/createFixtures';
import { OrganizationSwitcher } from '../OrganizationSwitcher';
import { createFakeUserOrganizationInvitation, createFakeUserOrganizationSuggestion } from './utlis';
Expand All@@ -14,7 +14,7 @@ describe('OrganizationSwitcher', () => {
f.withOrganizations();
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button')).toBeDefined();
});

Expand All@@ -25,7 +25,7 @@ describe('OrganizationSwitcher', () => {
f.withUser({ email_addresses: ['test@clerk.dev'] });
});
props.setProps({ hidePersonal: false });
const { getByText } = render(<OrganizationSwitcher />, { wrapper });
const { getByText } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(getByText('Personal account')).toBeDefined();
});

Expand DownExpand Up@@ -168,7 +168,7 @@ describe('OrganizationSwitcher', () => {
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Manage Organization' }));
await userEvent.click(getByRole('menuitem', { name: 'Manage Organization' }));
expect(fixtures.clerk.openOrganizationProfile).toHaveBeenCalled();
});

Expand All@@ -183,8 +183,8 @@ describe('OrganizationSwitcher', () => {
});
props.setProps({ hidePersonal: true });
const { getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
await userEvent.click(getByRole('button'));
await userEvent.click(getByRole('button', { name: 'Create Organization' }));
await userEvent.click(getByRole('button', { name: 'Open organization switcher' }));
await userEvent.click(getByRole('menuitem', { name: 'Create Organization' }));
expect(fixtures.clerk.openCreateOrganization).toHaveBeenCalled();
});

Expand All@@ -198,7 +198,7 @@ describe('OrganizationSwitcher', () => {
});
});
props.setProps({ hidePersonal: true });
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
const { queryByRole } = await act(() => render(<OrganizationSwitcher />, { wrapper }));
expect(queryByRole('button', { name: 'Create Organization' })).not.toBeInTheDocument();
});

Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
import { useId } from 'react';

import { getFullName, getIdentifier } from '../../../utils/user';
import { useCoreUser, useUserButtonContext, withCoreUserGuard } from '../../contexts';
import { descriptors, Flex, Flow, Text } from '../../customizables';
Expand All@@ -14,6 +16,8 @@ const _UserButton = withFloatingTree(() => {
offset: 8,
});

const userButtonMenuId = useId();

return (
<Flow.Root flow='userButton'>
<Flex
Expand All@@ -27,13 +31,15 @@ const _UserButton = withFloatingTree(() => {
ref={reference}
onClick={toggle}
isOpen={isOpen}
aria-controls={userButtonMenuId}
/>
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
>
<UserButtonPopover
id={userButtonMenuId}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,13 +35,14 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo

const sessionActions = authConfig.singleSessionMode ? null : otherSessions.length > 0 ? (
<>
<SecondaryActions>
<SecondaryActions role='menu'>
{otherSessions.map(session => (
<PreviewButton
key={session.id}
icon={SwitchArrows}
sx={t => ({ height: t.sizes.$14, borderRadius: 0 })}
onClick={handleSessionClicked(session)}
role='menuitem'
>
<UserPreview
user={session.user}
Expand All@@ -52,7 +53,7 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
))}
{addAccountButton}
</SecondaryActions>
<Actions>
<Actions role='menu'>
<Action
icon={SignOutDouble}
label={localizationKeys('userButton.action__signOutAll')}
Expand All@@ -61,14 +62,16 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
</Actions>
</>
) : (
<SecondaryActions>{addAccountButton}</SecondaryActions>
<SecondaryActions role='menu'>{addAccountButton}</SecondaryActions>
);

return (
<RootBox elementDescriptor={descriptors.userButtonPopoverRootBox}>
<PopoverCard.Root
elementDescriptor={descriptors.userButtonPopoverCard}
ref={ref}
role='dialog'
Comment thread
LekoArts marked this conversation as resolved.
aria-label='User button popover'
{...rest}
>
<PopoverCard.Main elementDescriptor={descriptors.userButtonPopoverMain}>
Expand All@@ -77,7 +80,10 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
user={user}
sx={theme => ({ padding: `0 ${theme.space.$6}`, marginBottom: theme.space.$2 })}
/>
<Actions elementDescriptor={descriptors.userButtonPopoverActions}>
<Actions
role='menu'
elementDescriptor={descriptors.userButtonPopoverActions}
>
<Action
elementDescriptor={descriptors.userButtonPopoverActionButton}
elementId={descriptors.userButtonPopoverActionButton.setId('manageAccount')}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,9 @@ export const UserButtonTrigger = withAvatarShimmer(
variant='roundWrapper'
sx={[theme => ({ borderRadius: theme.radii.$circle }), sx]}
ref={ref}
aria-label={`${props.isOpen ? 'Close' : 'Open'} user button`}
aria-expanded={props.isOpen}
aria-haspopup='dialog'
{...rest}
>
<UserAvatar
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,7 +31,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('Manage account')).not.toBeNull();
});

Expand All@@ -45,7 +45,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Manage account'));
expect(fixtures.clerk.openUserProfile).toHaveBeenCalled();
});
Expand All@@ -60,7 +60,7 @@ describe('UserButton', () => {
});
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalled();
});
Expand DownExpand Up@@ -96,7 +96,7 @@ describe('UserButton', () => {
it('renders all sessions', async () => {
const { wrapper } = await createFixtures(initConfig);
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
expect(getByText('First1 Last1')).toBeDefined();
expect(getByText('First2 Last2')).toBeDefined();
expect(getByText('First3 Last3')).toBeDefined();
Expand All@@ -106,7 +106,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.setActive.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('First3 Last3'));
expect(fixtures.clerk.setActive).toHaveBeenCalledWith(
expect.objectContaining({ session: expect.objectContaining({ user: expect.objectContaining({ id: '3' }) }) }),
Expand All@@ -117,7 +117,7 @@ describe('UserButton', () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.signOut.mockReturnValueOnce(Promise.resolve());
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'FL' }));
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalledWith(expect.any(Function), { sessionId: '0' });
});
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/Actions.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,6 +86,7 @@ export const Action = (props: ActionProps) => {
]}
isDisabled={card.isLoading}
onClick={onClick}
role='menuitem'
{...rest}
>
<Flex
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/elements/PoweredByClerk.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,6 +94,7 @@ const LogoMarkIconLink = () => {
'&:hover': { color: 'inherit' },
}}
isExternal
aria-label='Clerk logo'
>
<Icon
icon={LogoMark}
Expand Down