Skip to content

Commit cd72a27

Browse files
alexcarpenteranagstef
andauthoredDec 9, 2024··
feat(clerk-js,types): Add navbar button text descriptor and localization key (#4635)
Co-authored-by: Stefanos Anagnostou <anagstef@users.noreply.github.com>
1 parent b10755e commit cd72a27

File tree

8 files changed

+42
-15
lines changed

8 files changed

+42
-15
lines changed
 

‎.changeset/young-deers-deliver.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
'@clerk/types': patch
4+
---
5+
6+
Introduce the `navbarButtonText` element descriptor.

‎packages/clerk-js/src/ui/components/UserProfile/__tests__/UserProfile.test.tsx

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ describe('UserProfile', () => {
1515
});
1616

1717
render(<UserProfile />, { wrapper });
18-
const profileElements = screen.getAllByText(/Profile/i);
19-
expect(profileElements.some(el => el.tagName.toUpperCase() === 'BUTTON')).toBe(true);
20-
const securityElements = screen.getAllByText(/Security/i);
21-
expect(securityElements.some(el => el.tagName.toUpperCase() === 'BUTTON')).toBe(true);
18+
const profileElements = screen.getAllByRole('button', { name: /Profile/i });
19+
expect(profileElements.length).toBeGreaterThan(0);
20+
const securityElements = screen.getAllByRole('button', { name: /Security/i });
21+
expect(securityElements.length).toBeGreaterThan(0);
2222
});
2323

2424
it('includes custom nav items', async () => {
@@ -45,14 +45,14 @@ describe('UserProfile', () => {
4545

4646
props.setProps({ customPages });
4747
render(<UserProfile />, { wrapper });
48-
const profileElements = screen.getAllByText(/Profile/i);
49-
expect(profileElements.some(el => el.tagName.toUpperCase() === 'BUTTON')).toBe(true);
50-
const securityElements = screen.getAllByText(/Security/i);
51-
expect(securityElements.some(el => el.tagName.toUpperCase() === 'BUTTON')).toBe(true);
52-
const customElements = screen.getAllByText(/Custom1/i);
53-
expect(customElements.some(el => el.tagName.toUpperCase() === 'BUTTON')).toBe(true);
54-
const externalElements = screen.getAllByText(/ExternalLink/i);
55-
expect(externalElements.some(el => el.tagName.toUpperCase() === 'BUTTON')).toBe(true);
48+
const profileElements = screen.getAllByRole('button', { name: /Profile/i });
49+
expect(profileElements.length).toBeGreaterThan(0);
50+
const securityElements = screen.getAllByRole('button', { name: /Security/i });
51+
expect(securityElements.length).toBeGreaterThan(0);
52+
const customElements = screen.getAllByRole('button', { name: /Custom1/i });
53+
expect(customElements.length).toBeGreaterThan(0);
54+
const externalElements = screen.getAllByRole('button', { name: /ExternalLink/i });
55+
expect(externalElements.length).toBeGreaterThan(0);
5656
});
5757
});
5858
});

‎packages/clerk-js/src/ui/customizables/elementDescriptors.ts

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ export const APPEARANCE_KEYS = containsAllElementsConfigKeys([
250250
'navbarButtons',
251251
'navbarButton',
252252
'navbarButtonIcon',
253+
'navbarButtonText',
253254
'navbarMobileMenuRow',
254255
'navbarMobileMenuButton',
255256
'navbarMobileMenuButtonIcon',

‎packages/clerk-js/src/ui/customizables/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ export const Tbody = makeCustomizable(sanitizeDomProps(Primitives.Tbody));
6060
export const Tr = makeCustomizable(sanitizeDomProps(Primitives.Tr));
6161
export const Th = makeCustomizable(makeLocalizable(sanitizeDomProps(Primitives.Th)));
6262
export const Td = makeCustomizable(makeLocalizable(sanitizeDomProps(Primitives.Td)));
63+
64+
export const Span = makeCustomizable(makeLocalizable(sanitizeDomProps(Primitives.Span)));

‎packages/clerk-js/src/ui/elements/Navbar.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createContextAndHook } from '@clerk/shared/react';
22
import React, { useCallback } from 'react';
33

44
import type { LocalizationKey } from '../customizables';
5-
import { Button, Col, descriptors, Flex, Heading, Icon, Text, useLocalizations } from '../customizables';
5+
import { Button, Col, descriptors, Flex, Heading, Icon, Span, Text, useLocalizations } from '../customizables';
66
import type { ElementDescriptor, ElementId } from '../customizables/elementDescriptors';
77
import { useNavigateToFlowStart, usePopover } from '../hooks';
88
import { Menu } from '../icons';
@@ -47,7 +47,6 @@ export const NavBar = (props: NavBarProps) => {
4747
const { close } = useNavbarContext();
4848
const { navigate } = useRouter();
4949
const { navigateToFlowStart } = useNavigateToFlowStart();
50-
const { t } = useLocalizations();
5150
const router = useRouter();
5251

5352
const handleNavigate = (route: NavbarRoute) => {
@@ -105,7 +104,11 @@ export const NavBar = (props: NavBarProps) => {
105104
minHeight: t.space.$8,
106105
})}
107106
>
108-
{t(r.name)}
107+
<Span
108+
elementDescriptor={descriptors.navbarButtonText}
109+
elementId={descriptors.navbarButtonText.setId(r.id as any)}
110+
localizationKey={r.name}
111+
/>
109112
</NavButton>
110113
))}
111114
</Col>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
3+
export const Span = React.forwardRef<
4+
HTMLSpanElement,
5+
React.DetailedHTMLProps<React.HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>
6+
>((props, ref) => {
7+
return (
8+
<span
9+
{...props}
10+
ref={ref}
11+
/>
12+
);
13+
});

‎packages/clerk-js/src/ui/primitives/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ export * from './Tr';
2626
export * from './Th';
2727
export * from './Td';
2828
export * from './NotificationBadge';
29+
export * from './Span';

‎packages/types/src/appearance.ts

+1
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ export type ElementsConfig = {
375375
navbarButtons: WithOptions<never, ActiveState>;
376376
navbarButton: WithOptions<string, ActiveState>;
377377
navbarButtonIcon: WithOptions<string, ActiveState>;
378+
navbarButtonText: WithOptions<string, ActiveState>;
378379
navbarMobileMenuRow: WithOptions;
379380
navbarMobileMenuButton: WithOptions;
380381
navbarMobileMenuButtonIcon: WithOptions;

0 commit comments

Comments
 (0)
Please sign in to comment.