Skip to content

Commit b0ba7fd

Browse files
feat(mosaic): add full trigger variant to UserButton (#10279)
1 parent 431fd89 commit b0ba7fd

9 files changed

Lines changed: 181 additions & 42 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/mosaic': patch
3+
---
4+
5+
`<UserButton />` in `combined` mode now defaults to a larger trigger that badges the active organization with the user's avatar. Pass `triggerVariant='condensed'` to keep the previous, smaller trigger.
Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,78 @@
11
import * as stylex from '@stylexjs/stylex';
22

3-
import { space, spacingVars } from '../../tokens.stylex';
3+
import { spacingVars } from '../../tokens.stylex';
44

55
const GAP_PX = 1;
66

77
const step = (multiple: number) => `calc(${spacingVars['--cl-spacing']} * ${multiple})`;
88

99
const cutout = `url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2 2'><circle cx='1' cy='1' r='1'/></svg>")`;
1010

11-
const square = (multiple: number) => ({
11+
const dimensions = (multiple: number) => ({
1212
height: step(multiple),
1313
width: step(multiple),
14+
});
15+
16+
const square = (multiple: number) => ({
17+
...dimensions(multiple),
1418
fontSize: `calc(${step(multiple)} * 0.4)`,
1519
});
1620

17-
const cutoutOffset = (box: number, badgeSize: number) => `calc(${step(box - badgeSize)} - ${GAP_PX}px)`;
21+
const cutoutOffset = (leadSize: number, badgeSize: number, overhang: number) =>
22+
`calc(${step(leadSize - badgeSize + overhang)} - ${GAP_PX}px)`;
1823

1924
const cutoutSize = (badgeSize: number) => `calc(${step(badgeSize)} + ${GAP_PX * 2}px)`;
2025

21-
const lead = (box: number, badgeSize: number) => ({
22-
...square(box - 1),
26+
const lead = (leadSize: number, badgeSize: number, overhang: number) => ({
27+
...square(leadSize),
2328
maskImage: `linear-gradient(#000 0 0), ${cutout}`,
2429
maskPosition: {
25-
default: `0 0, ${cutoutOffset(box, badgeSize)} ${cutoutOffset(box, badgeSize)}`,
26-
':is([dir="rtl"] *)': `0 0, right ${cutoutOffset(box, badgeSize)} top ${cutoutOffset(box, badgeSize)}`,
30+
default: `0 0, ${cutoutOffset(leadSize, badgeSize, overhang)} ${cutoutOffset(leadSize, badgeSize, overhang)}`,
31+
':is([dir="rtl"] *)': `0 0, right ${cutoutOffset(leadSize, badgeSize, overhang)} top ${cutoutOffset(leadSize, badgeSize, overhang)}`,
2732
},
2833
maskSize: `100% 100%, ${cutoutSize(badgeSize)} ${cutoutSize(badgeSize)}`,
2934
});
3035

31-
const BOX = 10.5;
32-
const BADGE = 4;
36+
const badgeInset = (rootSize: number, leadSize: number, overhang: number) => ({
37+
insetBlockEnd: step(rootSize - leadSize - overhang),
38+
insetInlineEnd: step(rootSize - leadSize - overhang),
39+
});
3340

3441
export const styles = stylex.create({
3542
root: {
3643
alignItems: 'flex-start',
3744
display: 'inline-flex',
3845
flexShrink: 0,
3946
position: 'relative',
40-
height: space['10.5'],
41-
width: space['10.5'],
4247
},
4348

4449
lead: {
45-
...lead(BOX, BADGE),
4650
maskComposite: 'exclude',
4751
maskRepeat: 'no-repeat',
4852
},
4953

5054
badge: {
5155
display: 'flex',
52-
insetBlockEnd: 0,
53-
insetInlineEnd: 0,
5456
position: 'absolute',
5557
},
58+
});
59+
60+
export const rootSizes = stylex.create({
61+
sm: dimensions(8),
62+
md: dimensions(10.5),
63+
});
64+
65+
export const leadSizes = stylex.create({
66+
sm: lead(8, 3.5, 0.5),
67+
md: lead(9.5, 4, 1),
68+
});
69+
70+
export const badgeInsets = stylex.create({
71+
sm: badgeInset(8, 8, 0.5),
72+
md: badgeInset(10.5, 9.5, 1),
73+
});
5674

57-
badgeAvatar: square(BADGE),
75+
export const badgeSizes = stylex.create({
76+
sm: square(3.5),
77+
md: square(4),
5878
});

‎packages/mosaic/src/features/switcher/switcher-avatar.view.tsx‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { AvatarProps } from '../../components/avatar';
55
import { Avatar } from '../../components/avatar';
66
import { mergeStyleProps, themeProps } from '../../props';
77
import { reset } from '../../styles/reset.styles';
8-
import { styles } from './switcher-avatar.styles';
8+
import { badgeInsets, badgeSizes, leadSizes, rootSizes, styles } from './switcher-avatar.styles';
99
import { useSlot } from './switcher-surface';
1010

1111
function initials(name: string): string {
@@ -46,29 +46,35 @@ export interface BadgedAvatarProps {
4646
name: string;
4747
imageUrl?: string;
4848
badge: { name: string; imageUrl?: string };
49+
size: 'sm' | 'md';
4950
}
5051

51-
export function BadgedAvatar({ name, imageUrl, badge }: BadgedAvatarProps): ReactElement {
52+
export function BadgedAvatar({ name, imageUrl, badge, size }: BadgedAvatarProps): ReactElement {
5253
const slot = useSlot();
5354
return (
5455
<span
5556
aria-hidden
56-
{...mergeStyleProps(themeProps(slot('avatar')), stylex.props(reset.base, styles.root))}
57+
{...mergeStyleProps(themeProps(slot('avatar')), stylex.props(reset.base, styles.root, rootSizes[size]))}
5758
>
5859
<RowAvatar
5960
name={name}
6061
imageUrl={imageUrl}
6162
shape='square'
6263
size='fit'
63-
xstyle={styles.lead}
64+
xstyle={[styles.lead, leadSizes[size]]}
6465
/>
65-
<span {...mergeStyleProps(themeProps(slot('avatar-badge')), stylex.props(reset.base, styles.badge))}>
66+
<span
67+
{...mergeStyleProps(
68+
themeProps(slot('avatar-badge')),
69+
stylex.props(reset.base, styles.badge, badgeInsets[size]),
70+
)}
71+
>
6672
<RowAvatar
6773
name={badge.name}
6874
imageUrl={badge.imageUrl}
6975
shape='circle'
7076
size='fit'
71-
xstyle={styles.badgeAvatar}
77+
xstyle={badgeSizes[size]}
7278
/>
7379
</span>
7480
</span>

‎packages/mosaic/src/features/switcher/switcher.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { useSwitcherModel } from './switcher.model';
1010
import type { OrganizationProfilePageId } from './switcher.pages';
1111
import { useCustomPages, useOrganizationProfilePages, useUserProfilePages } from './switcher.pages';
1212
import type { SwitcherMenuProps, SwitcherMode } from './switcher.types';
13-
import type { SwitcherTriggerProps } from './switcher.view';
13+
import type { SwitcherTriggerProps, SwitcherTriggerVariant } from './switcher.view';
1414
import { SwitcherView } from './switcher.view';
1515

1616
/** What a profile opened from the switcher takes beyond the profile component's own props. */
@@ -61,10 +61,13 @@ export type SwitcherProps = SwitcherModelOptions &
6161
fallback?: ReactNode;
6262
};
6363

64-
export function Switcher(props: SwitcherProps & { mode: SwitcherMode }): ReactElement | null {
64+
export function Switcher(
65+
props: SwitcherProps & { mode: SwitcherMode; triggerVariant?: SwitcherTriggerVariant },
66+
): ReactElement | null {
6567
const {
6668
renderTriggerLabel,
6769
renderTriggerBadge,
70+
triggerVariant,
6871
mode,
6972
userProfileProps,
7073
organizationProfileProps,
@@ -128,6 +131,7 @@ export function Switcher(props: SwitcherProps & { mode: SwitcherMode }): ReactEl
128131
{...viewController}
129132
renderTriggerLabel={renderTriggerLabel}
130133
renderTriggerBadge={renderTriggerBadge}
134+
triggerVariant={triggerVariant}
131135
/>
132136
{portals}
133137
</>

‎packages/mosaic/src/features/switcher/switcher.view.tsx‎

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ function Header() {
342342
name={lead.name}
343343
imageUrl={lead.imageUrl}
344344
badge={user}
345+
size='md'
345346
/>
346347
) : (
347348
<RowAvatar
@@ -1027,11 +1028,14 @@ export interface SwitcherTriggerProps {
10271028
renderTriggerBadge?: boolean;
10281029
}
10291030

1031+
export type SwitcherTriggerVariant = 'full' | 'condensed';
1032+
10301033
/** The trigger: the lead's avatar, and what it is called. */
10311034
export function SwitcherTrigger({
10321035
renderTriggerLabel = true,
10331036
renderTriggerBadge = true,
1034-
}: SwitcherTriggerProps = {}): ReactElement {
1037+
triggerVariant = 'condensed',
1038+
}: SwitcherTriggerProps & { triggerVariant?: SwitcherTriggerVariant } = {}): ReactElement {
10351039
const m = useSwitcherMessages();
10361040
const slot = useSlot();
10371041
const data = useSwitcherContext();
@@ -1042,7 +1046,7 @@ export function SwitcherTrigger({
10421046

10431047
return (
10441048
<Popover.Trigger
1045-
{...themeProps(slot('trigger'))}
1049+
{...themeProps(slot('trigger'), { variant: triggerVariant })}
10461050
aria-label={fill(m.trigger.open, { name: user?.name ?? name })}
10471051
xstyle={[
10481052
focusOutline.visible,
@@ -1051,12 +1055,9 @@ export function SwitcherTrigger({
10511055
!renderTriggerLabel && shape === 'circle' ? styles.triggerRound : null,
10521056
]}
10531057
>
1054-
<RowAvatar
1055-
name={lead.name}
1056-
imageUrl={lead.imageUrl}
1057-
shape={shape}
1058-
size={renderTriggerLabel ? 'fit' : 'sm'}
1059-
xstyle={renderTriggerLabel ? styles.rowAvatar : undefined}
1058+
<TriggerAvatar
1059+
lead={lead}
1060+
variant={renderTriggerLabel ? triggerVariant : 'avatarOnly'}
10601061
/>
10611062
{renderTriggerLabel ? (
10621063
<>
@@ -1080,6 +1081,35 @@ export function SwitcherTrigger({
10801081
);
10811082
}
10821083

1084+
function TriggerAvatar({
1085+
lead,
1086+
variant,
1087+
}: {
1088+
lead: LeadSelection;
1089+
variant: SwitcherTriggerVariant | 'avatarOnly';
1090+
}): ReactElement {
1091+
const user = leadUser(lead);
1092+
if (variant === 'full' && user) {
1093+
return (
1094+
<BadgedAvatar
1095+
name={lead.name}
1096+
imageUrl={lead.imageUrl}
1097+
badge={user}
1098+
size='sm'
1099+
/>
1100+
);
1101+
}
1102+
return (
1103+
<RowAvatar
1104+
name={lead.name}
1105+
imageUrl={lead.imageUrl}
1106+
shape={lead.shape}
1107+
size={variant === 'condensed' ? 'fit' : 'sm'}
1108+
xstyle={variant === 'condensed' ? styles.rowAvatar : undefined}
1109+
/>
1110+
);
1111+
}
1112+
10831113
/** The popover surface: header, organizations, what can be done about the lead, and footer. */
10841114
export function SwitcherPopup(): ReactElement {
10851115
const m = useSwitcherMessages();
@@ -1102,18 +1132,25 @@ export function SwitcherPopup(): ReactElement {
11021132
);
11031133
}
11041134

1105-
export type SwitcherViewProps = Omit<SwitcherRootProps, 'children'> & SwitcherTriggerProps;
1135+
export type SwitcherViewProps = Omit<SwitcherRootProps, 'children'> &
1136+
SwitcherTriggerProps & { triggerVariant?: SwitcherTriggerVariant };
11061137

11071138
/**
11081139
* Presentational all-in-one: renders the trigger + popup from a single prop-driven call. The
11091140
* connected, Clerk-backed `Switcher` lives in `switcher.tsx` and wraps this view.
11101141
*/
1111-
export function SwitcherView({ renderTriggerLabel, renderTriggerBadge, ...root }: SwitcherViewProps): ReactElement {
1142+
export function SwitcherView({
1143+
renderTriggerLabel,
1144+
renderTriggerBadge,
1145+
triggerVariant,
1146+
...root
1147+
}: SwitcherViewProps): ReactElement {
11121148
return (
11131149
<SwitcherRoot {...root}>
11141150
<SwitcherTrigger
11151151
renderTriggerLabel={renderTriggerLabel}
11161152
renderTriggerBadge={renderTriggerBadge}
1153+
triggerVariant={triggerVariant}
11171154
/>
11181155
<SwitcherPopup />
11191156
</SwitcherRoot>

‎packages/mosaic/src/features/user-button/__tests__/user-button.feature.test.tsx‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,16 +1168,44 @@ describe('UserButton', () => {
11681168
expect(popup()).toBeNull();
11691169
});
11701170

1171-
it('names the active organization alone, and opens as the account', async () => {
1171+
it('names the active organization alone, badged with the account, and opens as the account', async () => {
11721172
await renderUserButton();
11731173

11741174
expect(trigger()).toHaveAccessibleName('Open account menu for Alice Smith');
1175+
expect(trigger()).toHaveAttribute('data-variant', 'full');
11751176
expect(within(trigger()).getByText('Acme')).toBeInTheDocument();
11761177
expect(within(trigger()).queryByText('alice')).toBeNull();
11771178
expect(within(trigger()).queryByText('Alice Smith')).toBeNull();
1179+
expect(trigger().querySelector('.cl-user-button-avatar > [data-shape]')).toHaveAttribute('data-shape', 'square');
1180+
expect(trigger().querySelector('.cl-user-button-avatar-badge > [data-shape]')).toHaveAttribute(
1181+
'data-shape',
1182+
'circle',
1183+
);
1184+
});
1185+
1186+
it('drops the account badge from a condensed trigger', async () => {
1187+
await renderUserButton({ triggerVariant: 'condensed' });
1188+
1189+
expect(trigger()).toHaveAttribute('data-variant', 'condensed');
1190+
expect(within(trigger()).getByText('Acme')).toBeInTheDocument();
11781191
expect(trigger().querySelector('.cl-user-button-avatar-badge')).toBeNull();
11791192
});
11801193

1194+
it('leads a full trigger with the account alone when no organization is active', async () => {
1195+
await renderUserButton({}, signedIn({ client: fapiClient([personalSession]) }));
1196+
1197+
expect(trigger()).toHaveAttribute('data-variant', 'full');
1198+
expect(within(trigger()).getByText('Alice Smith')).toBeInTheDocument();
1199+
expect(trigger().querySelector('[data-shape]')).toHaveAttribute('data-shape', 'circle');
1200+
expect(trigger().querySelector('.cl-user-button-avatar-badge')).toBeNull();
1201+
});
1202+
1203+
it('keeps the condensed trigger in user mode', async () => {
1204+
await renderUserButton({ mode: 'user', triggerVariant: 'full' });
1205+
1206+
expect(trigger()).toHaveAttribute('data-variant', 'condensed');
1207+
});
1208+
11811209
it('leads the avatar alone with the organization, unbadged', async () => {
11821210
await renderUserButton({ renderTriggerLabel: false });
11831211

‎packages/mosaic/src/features/user-button/user-button.tsx‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ReactElement } from 'react';
55
import type { SwitcherProps } from '../switcher/switcher';
66
import { Switcher } from '../switcher/switcher';
77
import type { SwitcherMode } from '../switcher/switcher.types';
8+
import type { SwitcherTriggerVariant } from '../switcher/switcher.view';
89

910
/**
1011
* Which switchers the surface carries. `combined` is both; `user` is an account switcher that never
@@ -20,6 +21,14 @@ export interface UserButtonModeProps {
2021
* @default 'combined'
2122
*/
2223
mode?: UserButtonMode;
24+
/**
25+
* How much the labelled trigger carries in `combined` mode: `full` badges the active organization
26+
* with the account at a larger size, `condensed` shows the lead's avatar alone. `user` mode is
27+
* always `condensed`.
28+
*
29+
* @default 'full'
30+
*/
31+
triggerVariant?: SwitcherTriggerVariant;
2332
}
2433

2534
/** Everything `<UserButton />` takes: profile routing, trigger content, the app's own menu rows, and the profiles it opens. */
@@ -47,6 +56,12 @@ export type UserButtonProps = SwitcherProps & UserButtonModeProps;
4756
* ```
4857
*
4958
* @example
59+
* `triggerVariant` trades the badged trigger for the lead's avatar alone.
60+
* ```tsx
61+
* <UserButton triggerVariant='condensed' />
62+
* ```
63+
*
64+
* @example
5065
* Passing a URL routes to a page of your own instead of opening Clerk's modal; that is the whole
5166
* opt-in. `afterSelectOrganizationUrl` is where switching organization lands, and takes a `:param`
5267
* template, a plain path, or a function. `afterSwitchSessionUrl` is where switching account lands.
@@ -87,11 +102,13 @@ export type UserButtonProps = SwitcherProps & UserButtonModeProps;
87102
* />
88103
* ```
89104
*/
90-
export function UserButton(props: UserButtonProps = {}): ReactElement {
105+
export function UserButton({ mode, triggerVariant = 'full', ...props }: UserButtonProps = {}): ReactElement {
106+
const combined = mode !== 'user';
91107
return (
92108
<Switcher
93109
{...props}
94-
mode={props.mode === 'user' ? 'user' : 'combined'}
110+
mode={combined ? 'combined' : 'user'}
111+
triggerVariant={combined ? triggerVariant : 'condensed'}
95112
/>
96113
);
97114
}

0 commit comments

Comments
 (0)