Skip to content

Commit 3d122ee

Browse files
committed
fix(mosaic): let Switcher take a mode chosen at runtime
1 parent 4439b3a commit 3d122ee

3 files changed

Lines changed: 50 additions & 44 deletions

File tree

‎.changeset/mosaic-switcher.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'@clerk/mosaic': minor
33
---
44

5-
**Breaking:** Add `Switcher`, the account and organization menu, and make `UserButton` list accounts only. `Switcher` takes `mode`: `'combined'` (the default) lists accounts and organizations, `'user'` lists accounts only, and `'organization'` lists organizations. `<UserButton />` is now `<Switcher mode='user' />` and `<OrganizationSwitcher />` is `<Switcher mode='organization' />`. Each mode takes only its own props, so TypeScript flags organization options on `UserButton` and `triggerVariant` outside `combined`.
5+
**Breaking:** Add `Switcher`, the account and organization menu, and make `UserButton` list accounts only. `Switcher` takes `mode`: `'combined'` (the default) lists accounts and organizations, `'user'` lists accounts only, and `'organization'` lists organizations. `<UserButton />` is now `<Switcher mode='user' />` and `<OrganizationSwitcher />` is `<Switcher mode='organization' />`. Each mode takes only its own props, so TypeScript flags organization options on `UserButton` and `triggerVariant` outside `combined`. A `mode` chosen at runtime takes only the props every mode shares.
66

77
| Before | After |
88
| --------------------------------------------------------------------------------------------------------------- | ---------------------------------------------- |

‎packages/mosaic/src/features/switcher/switcher.test-d.tsx‎

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { describe, test } from 'vitest';
22

33
import { OrganizationSwitcher } from '../organization-switcher/organization-switcher';
44
import { UserButton } from '../user-button/user-button';
5+
import type { SwitcherProps } from './switcher';
56
import { Switcher } from './switcher';
7+
import type { SwitcherMode } from './switcher.types';
68

79
describe('Switcher props', () => {
810
test('takes every option in combined mode, with or without the mode', () => {
@@ -31,27 +33,12 @@ describe('Switcher props', () => {
3133
signInUrl='/sign-in'
3234
/>
3335
);
34-
void (
35-
<Switcher
36-
mode='user'
37-
// @ts-expect-error -- the trigger variant only applies to the combined menu
38-
triggerVariant='full'
39-
/>
40-
);
41-
void (
42-
<Switcher
43-
mode='user'
44-
// @ts-expect-error -- a user menu lists no organizations
45-
hidePersonal
46-
/>
47-
);
48-
void (
49-
<Switcher
50-
mode='user'
51-
// @ts-expect-error -- a user menu lists no organizations
52-
createOrganizationUrl='/orgs/new'
53-
/>
54-
);
36+
// @ts-expect-error -- the trigger variant only applies to the combined menu
37+
void ({ mode: 'user', triggerVariant: 'full' } satisfies SwitcherProps);
38+
// @ts-expect-error -- a user menu lists no organizations
39+
void ({ mode: 'user', hidePersonal: true } satisfies SwitcherProps);
40+
// @ts-expect-error -- a user menu lists no organizations
41+
void ({ mode: 'user', createOrganizationUrl: '/orgs/new' } satisfies SwitcherProps);
5542
});
5643

5744
test('takes organization options in organization mode and rejects the rest', () => {
@@ -62,37 +49,55 @@ describe('Switcher props', () => {
6249
createOrganizationUrl='/orgs/new'
6350
/>
6451
);
52+
// @ts-expect-error -- the trigger variant only applies to the combined menu
53+
void ({ mode: 'organization', triggerVariant: 'full' } satisfies SwitcherProps);
54+
// @ts-expect-error -- an organization menu lists no accounts
55+
void ({ mode: 'organization', afterSwitchSessionUrl: '/dashboard' } satisfies SwitcherProps);
56+
});
57+
58+
test('requires a URL to navigate to the create-organization page', () => {
59+
// @ts-expect-error -- navigation needs a URL
60+
void ({ createOrganizationMode: 'navigation' } satisfies SwitcherProps);
61+
// @ts-expect-error -- navigation needs a URL
62+
void ({ mode: 'organization', createOrganizationMode: 'navigation' } satisfies SwitcherProps);
63+
});
64+
65+
test('takes a mode chosen at runtime with the props every mode shares', () => {
66+
const mode = pickMode();
6567
void (
6668
<Switcher
67-
mode='organization'
68-
// @ts-expect-error -- the trigger variant only applies to the combined menu
69-
triggerVariant='full'
70-
/>
71-
);
72-
void (
73-
<Switcher
74-
mode='organization'
75-
// @ts-expect-error -- an organization menu lists no accounts
76-
afterSwitchSessionUrl='/dashboard'
69+
mode={mode}
70+
showTriggerLabel={false}
71+
customMenuItems={[]}
7772
/>
7873
);
74+
// @ts-expect-error -- only a static combined mode takes the trigger variant
75+
void ({ mode, triggerVariant: 'full' } satisfies SwitcherProps);
76+
// @ts-expect-error -- only a static mode takes that mode's options
77+
void ({ mode, hidePersonal: true } satisfies SwitcherProps);
78+
// @ts-expect-error -- only a static mode takes that mode's options
79+
void ({ mode, createOrganizationMode: 'navigation' } satisfies SwitcherProps);
7980
});
8081
});
8182

8283
describe('UserButton and OrganizationSwitcher props', () => {
8384
test('match their Switcher mode and take no mode of their own', () => {
8485
void (<UserButton afterSwitchSessionUrl='/dashboard' />);
85-
// @ts-expect-error a user menu lists no organizations
86+
// @ts-expect-error -- a user menu lists no organizations
8687
void (<UserButton hidePersonal />);
87-
// @ts-expect-error the trigger variant only applies to the combined menu
88+
// @ts-expect-error -- the trigger variant only applies to the combined menu
8889
void (<UserButton triggerVariant='full' />);
89-
// @ts-expect-error the mode is fixed
90+
// @ts-expect-error -- the mode is fixed
9091
void (<UserButton mode='combined' />);
9192

9293
void (<OrganizationSwitcher hidePersonal />);
93-
// @ts-expect-error an organization menu lists no accounts
94+
// @ts-expect-error -- an organization menu lists no accounts
9495
void (<OrganizationSwitcher afterSwitchSessionUrl='/dashboard' />);
95-
// @ts-expect-error the mode is fixed
96+
// @ts-expect-error -- the mode is fixed
9697
void (<OrganizationSwitcher mode='combined' />);
9798
});
9899
});
100+
101+
function pickMode(): SwitcherMode {
102+
return 'user';
103+
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
} from './switcher.model';
1212
import { useSwitcherModel } from './switcher.model';
1313
import { useCustomPages, useOrganizationProfilePages, useUserProfilePages } from './switcher.pages';
14-
import type { SwitcherMenuProps } from './switcher.types';
14+
import type { SwitcherMenuProps, SwitcherMode } from './switcher.types';
1515
import type { SwitcherTriggerProps, SwitcherTriggerVariant } from './switcher.view';
1616
import { SwitcherView } from './switcher.view';
1717

@@ -47,13 +47,14 @@ export type OrganizationSwitcherProps = SwitcherBaseProps & OrganizationSwitcher
4747
/**
4848
* What the menu lists, and the options that apply to it. `combined` lists accounts and organizations,
4949
* `user` lists accounts alone and never an organization, even when one is active, and `organization`
50-
* lists organizations and the personal account. The profile's shape is set by `MosaicProvider`'s
51-
* `profile`.
50+
* lists organizations and the personal account. A mode chosen at runtime takes only the props every
51+
* mode shares. The profile's shape is set by `MosaicProvider`'s `profile`.
5252
*/
5353
export type SwitcherProps =
5454
| ({ mode?: 'combined' } & CombinedSwitcherProps)
55-
| ({ mode: 'user' } & UserButtonProps)
56-
| ({ mode: 'organization' } & OrganizationSwitcherProps);
55+
| ({ mode: 'user'; triggerVariant?: never } & UserButtonProps)
56+
| ({ mode: 'organization'; triggerVariant?: never } & OrganizationSwitcherProps)
57+
| ({ mode: SwitcherMode } & SwitcherBaseProps & { [K in keyof SwitcherModelOptions | 'triggerVariant']?: never });
5758

5859
/**
5960
* The signed-in user's avatar, and the menu behind it: switch organization, switch or add an account,
@@ -123,13 +124,13 @@ export function Switcher(props: SwitcherProps = {}): ReactElement | null {
123124
showTriggerLabel,
124125
triggerBadge,
125126
mode = 'combined',
127+
triggerVariant: requestedTriggerVariant,
126128
customMenuItems,
127129
menuItemOrder,
128130
fallback,
129131
...options
130132
} = props;
131-
const triggerVariant =
132-
props.mode === 'user' || props.mode === 'organization' ? 'condensed' : (props.triggerVariant ?? 'full');
133+
const triggerVariant = mode === 'combined' ? (requestedTriggerVariant ?? 'full') : 'condensed';
133134
const profile = useMosaicProfile();
134135
const { userProfileProps, organizationProfileProps } = profile;
135136
// The portals must outlive the popover, so custom pages are bridged here rather than inside it.

0 commit comments

Comments
 (0)