Skip to content

Commit 7b7b8eb

Browse files
committed
feat(mosaic): theme the combined Switcher through .cl-switcher-* slots
1 parent 81b7956 commit 7b7b8eb

7 files changed

Lines changed: 92 additions & 63 deletions

File tree

‎.changeset/mosaic-switcher.md‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
**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

7-
| Before | After |
8-
| --------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- |
9-
| `<UserButton />` (accounts and organizations) | `<Switcher />` |
10-
| `<UserButton combined={false} />` | `<UserButton />` or `<Switcher mode='user' />` |
11-
| `<UserButton triggerVariant='condensed' />` | `<Switcher triggerVariant='condensed' />` |
12-
| Organization props on `UserButton` (`hidePersonal`, `afterSelectOrganizationUrl`, `createOrganizationUrl`, ...) | The same props on `Switcher` |
13-
| `UserButtonProps` (combined menu) | `CombinedSwitcherProps` |
14-
| `UserButtonProps` (user-only menu) | `UserButtonProps`, which now takes account options only |
7+
| Before | After |
8+
| --------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------- |
9+
| `<UserButton />` (accounts and organizations) | `<Switcher />` |
10+
| `<UserButton combined={false} />` | `<UserButton />` or `<Switcher mode='user' />` |
11+
| `<UserButton triggerVariant='condensed' />` | `<Switcher triggerVariant='condensed' />` |
12+
| Organization props on `UserButton` (`hidePersonal`, `afterSelectOrganizationUrl`, `createOrganizationUrl`, ...) | The same props on `Switcher` |
13+
| `UserButtonProps` (combined menu) | `CombinedSwitcherProps` |
14+
| `UserButtonProps` (user-only menu) | `UserButtonProps`, which now takes account options only |
15+
| `.cl-user-button-*` classes on the combined menu | `.cl-switcher-*`; `UserButton` keeps `.cl-user-button-*` |
1516

16-
Find usages with `git grep -n "UserButton"`.
17+
Find usages with `git grep -n -e "UserButton" -e "cl-user-button-"`.

‎packages/mosaic/src/features/switcher/__tests__/switcher-avatar.feature.test.tsx‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,31 @@ const alice = fapiUser({
2929

3030
describe('Switcher badged avatar customization', () => {
3131
it('keeps the gap around a resized badge in the trigger', async () => {
32-
customize(`.cl-user-button-avatar { --cl-avatar-badge-size: 24px; --cl-avatar-badge-gap: ${GAP}px; }`);
32+
customize(`.cl-switcher-avatar { --cl-avatar-badge-size: 24px; --cl-avatar-badge-gap: ${GAP}px; }`);
3333
await renderSignedIn();
3434

3535
await expectGapAroundBadge(await triggerAvatar());
3636
});
3737

3838
it('keeps the gap when the trigger avatar is resized', async () => {
3939
customize(
40-
`.cl-user-button-avatar { width: 64px !important; height: 64px !important; --cl-avatar-badge-gap: ${GAP}px; }`,
40+
`.cl-switcher-avatar { width: 64px !important; height: 64px !important; --cl-avatar-badge-gap: ${GAP}px; }`,
4141
);
4242
await renderSignedIn();
4343

4444
await expectGapAroundBadge(await triggerAvatar());
4545
});
4646

4747
it('keeps the gap around a resized badge in the header', async () => {
48-
customize(`.cl-user-button-avatar { --cl-avatar-badge-size: 24px; --cl-avatar-badge-gap: ${GAP}px; }`);
48+
customize(`.cl-switcher-avatar { --cl-avatar-badge-size: 24px; --cl-avatar-badge-gap: ${GAP}px; }`);
4949
await renderSignedIn();
5050
await userEvent.setup().click(screen.getByRole('button', { name: /Open account menu/ }));
5151

5252
await expectGapAroundBadge(await headerAvatar());
5353
});
5454

5555
it('keeps the gap around a resized badge in right-to-left layouts', async () => {
56-
customize(`.cl-user-button-avatar { --cl-avatar-badge-size: 24px; --cl-avatar-badge-gap: ${GAP}px; }`);
56+
customize(`.cl-switcher-avatar { --cl-avatar-badge-size: 24px; --cl-avatar-badge-gap: ${GAP}px; }`);
5757
document.documentElement.dir = 'rtl';
5858
onTestFinished(() => document.documentElement.removeAttribute('dir'));
5959
await renderSignedIn();
@@ -64,7 +64,7 @@ describe('Switcher badged avatar customization', () => {
6464

6565
function customize(css: string) {
6666
const sheet = document.createElement('style');
67-
sheet.textContent = `.cl-user-button-avatar { --cl-color-neutral-alpha-200: #000; } ${css}`;
67+
sheet.textContent = `.cl-switcher-avatar { --cl-color-neutral-alpha-200: #000; } ${css}`;
6868
document.head.append(sheet);
6969
onTestFinished(() => sheet.remove());
7070
}
@@ -84,23 +84,23 @@ async function triggerAvatar(): Promise<HTMLElement> {
8484

8585
async function headerAvatar(): Promise<HTMLElement> {
8686
const dialog = await screen.findByRole('dialog');
87-
const header = dialog.querySelector<HTMLElement>('.cl-user-button-header');
87+
const header = dialog.querySelector<HTMLElement>('.cl-switcher-header');
8888
if (!header) {
8989
throw new Error('No header in the popup');
9090
}
9191
return requiredAvatar(header);
9292
}
9393

9494
function requiredAvatar(container: HTMLElement): HTMLElement {
95-
const avatar = container.querySelector<HTMLElement>('.cl-user-button-avatar');
95+
const avatar = container.querySelector<HTMLElement>('.cl-switcher-avatar');
9696
if (!avatar) {
9797
throw new Error('No badged avatar');
9898
}
9999
return avatar;
100100
}
101101

102102
async function expectGapAroundBadge(avatar: HTMLElement) {
103-
const badge = avatar.querySelector<HTMLElement>('.cl-user-button-avatar-badge');
103+
const badge = avatar.querySelector<HTMLElement>('.cl-switcher-avatar-badge');
104104
if (!badge) {
105105
throw new Error('No badge');
106106
}

‎packages/mosaic/src/features/switcher/__tests__/switcher.feature.test.tsx‎

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function requiredPopup() {
109109
const reading = (...names: string[]) =>
110110
within(requiredPopup())
111111
.queryAllByText(new RegExp(`^(${names.join('|')})$`))
112-
.filter(node => !node.closest('.cl-user-button-header'))
112+
.filter(node => !node.closest('.cl-switcher-header'))
113113
.map(node => node.textContent);
114114

115115
const current = () =>
@@ -214,7 +214,7 @@ describe('Switcher', () => {
214214
await open();
215215

216216
expect(await screen.findByRole('button', { name: 'Other' })).toBeInTheDocument();
217-
expect(requiredPopup().querySelector('.cl-user-button-header-description')).toHaveTextContent('alice');
217+
expect(requiredPopup().querySelector('.cl-switcher-header-description')).toHaveTextContent('alice');
218218
});
219219

220220
describe('switching organization', () => {
@@ -904,6 +904,16 @@ describe('Switcher', () => {
904904
});
905905

906906
describe('in user mode', () => {
907+
it('themes its parts through user-button slots', async () => {
908+
await renderSwitcher({ mode: 'user' });
909+
await open();
910+
911+
for (const slot of ['trigger', 'popover', 'header', 'item', 'group']) {
912+
expect(document.querySelector(`.cl-user-button-${slot}`)).not.toBeNull();
913+
}
914+
expect(document.querySelector('[class*="cl-switcher"]')).toBeNull();
915+
});
916+
907917
it('heads the surface with the account, never the active organization', async () => {
908918
await renderSwitcher({ mode: 'user' });
909919
await open();
@@ -964,21 +974,28 @@ describe('Switcher', () => {
964974
});
965975

966976
describe('in combined mode', () => {
977+
it('themes its parts through switcher slots', async () => {
978+
await renderSwitcher();
979+
await openWithList();
980+
981+
for (const slot of ['trigger', 'trigger-label', 'popover', 'header', 'item', 'group', 'separator']) {
982+
expect(document.querySelector(`.cl-switcher-${slot}`)).not.toBeNull();
983+
}
984+
expect(document.querySelector('[class*="cl-user-button"]')).toBeNull();
985+
});
986+
967987
it('heads the surface with the active organization, badged with the account, managing both', async () => {
968988
const { clerk } = await renderSwitcher();
969989
const openOrganizationProfile = vi.spyOn(clerk, 'openOrganizationProfile').mockImplementation(() => {});
970990

971991
expect(trigger()).toHaveAccessibleName('Open account menu for Alice Smith');
972992
const user = await open();
973993

974-
const header = requiredPopup().querySelector('.cl-user-button-header');
975-
expect(header?.querySelector('.cl-user-button-header-title')).toHaveTextContent('Acme');
976-
expect(header?.querySelector('.cl-user-button-header-description')).toHaveTextContent('alice');
977-
expect(header?.querySelector('.cl-user-button-avatar > [data-shape]')).toHaveAttribute('data-shape', 'square');
978-
expect(header?.querySelector('.cl-user-button-avatar-badge > [data-shape]')).toHaveAttribute(
979-
'data-shape',
980-
'circle',
981-
);
994+
const header = requiredPopup().querySelector('.cl-switcher-header');
995+
expect(header?.querySelector('.cl-switcher-header-title')).toHaveTextContent('Acme');
996+
expect(header?.querySelector('.cl-switcher-header-description')).toHaveTextContent('alice');
997+
expect(header?.querySelector('.cl-switcher-avatar > [data-shape]')).toHaveAttribute('data-shape', 'square');
998+
expect(header?.querySelector('.cl-switcher-avatar-badge > [data-shape]')).toHaveAttribute('data-shape', 'circle');
982999
expect(within(header ?? requiredPopup()).queryAllByRole('button')).toEqual([]);
9831000
expect(reading('Add organization', 'Settings', 'Invite members', 'Switch account', 'Sign out')).toEqual([
9841001
'Add organization',
@@ -998,8 +1015,8 @@ describe('Switcher', () => {
9981015
expect(trigger()).toHaveAccessibleName('Open account menu for alice');
9991016
await open();
10001017

1001-
expect(requiredPopup().querySelector('.cl-user-button-header-title')).toHaveTextContent('Acme');
1002-
expect(requiredPopup().querySelector('.cl-user-button-header-description')).toHaveTextContent('alice');
1018+
expect(requiredPopup().querySelector('.cl-switcher-header-title')).toHaveTextContent('Acme');
1019+
expect(requiredPopup().querySelector('.cl-switcher-header-description')).toHaveTextContent('alice');
10031020
});
10041021

10051022
it('trails the foot with sign-out, behind the flyout of accounts', async () => {
@@ -1171,7 +1188,7 @@ describe('Switcher', () => {
11711188

11721189
expect(await screen.findByText('Loading organizations…')).toBeInTheDocument();
11731190
expect(reading('Personal account', 'Other')).toEqual([]);
1174-
expect(requiredPopup().querySelector('.cl-user-button-header-title')).toHaveTextContent('Acme');
1191+
expect(requiredPopup().querySelector('.cl-switcher-header-title')).toHaveTextContent('Acme');
11751192

11761193
invitations.release();
11771194
await waitFor(() => expect(screen.queryByText('Loading organizations…')).toBeNull());
@@ -1183,7 +1200,7 @@ describe('Switcher', () => {
11831200
const terms = { id: 'terms', label: 'Terms of service', onClick: () => {} };
11841201
const support = { id: 'support', label: 'Support', href: '/support' };
11851202
const group = (name: string) =>
1186-
screen.getByRole(name === 'Support' ? 'link' : 'button', { name }).closest('.cl-user-button-group');
1203+
screen.getByRole(name === 'Support' ? 'link' : 'button', { name }).closest('.cl-switcher-group');
11871204

11881205
it('follow Settings and Invite members in their group, above the foot', async () => {
11891206
await renderSwitcher({ customMenuItems: [terms, support] });
@@ -1351,8 +1368,8 @@ describe('Switcher', () => {
13511368
expect(within(trigger()).getByText('Acme')).toBeInTheDocument();
13521369
expect(within(trigger()).queryByText('alice')).toBeNull();
13531370
expect(within(trigger()).queryByText('Alice Smith')).toBeNull();
1354-
expect(trigger().querySelector('.cl-user-button-avatar > [data-shape]')).toHaveAttribute('data-shape', 'square');
1355-
expect(trigger().querySelector('.cl-user-button-avatar-badge > [data-shape]')).toHaveAttribute(
1371+
expect(trigger().querySelector('.cl-switcher-avatar > [data-shape]')).toHaveAttribute('data-shape', 'square');
1372+
expect(trigger().querySelector('.cl-switcher-avatar-badge > [data-shape]')).toHaveAttribute(
13561373
'data-shape',
13571374
'circle',
13581375
);
@@ -1363,7 +1380,7 @@ describe('Switcher', () => {
13631380

13641381
expect(trigger()).toHaveAttribute('data-variant', 'condensed');
13651382
expect(within(trigger()).getByText('Acme')).toBeInTheDocument();
1366-
expect(trigger().querySelector('.cl-user-button-avatar-badge')).toBeNull();
1383+
expect(trigger().querySelector('.cl-switcher-avatar-badge')).toBeNull();
13671384
});
13681385

13691386
it('leads a full trigger with the account alone when no organization is active', async () => {
@@ -1372,7 +1389,7 @@ describe('Switcher', () => {
13721389
expect(trigger()).toHaveAttribute('data-variant', 'full');
13731390
expect(within(trigger()).getByText('Alice Smith')).toBeInTheDocument();
13741391
expect(trigger().querySelector('[data-shape]')).toHaveAttribute('data-shape', 'circle');
1375-
expect(trigger().querySelector('.cl-user-button-avatar-badge')).toBeNull();
1392+
expect(trigger().querySelector('.cl-switcher-avatar-badge')).toBeNull();
13761393
});
13771394

13781395
it('keeps the condensed trigger in user mode', async () => {
@@ -1388,7 +1405,7 @@ describe('Switcher', () => {
13881405

13891406
expect(trigger()).toHaveAccessibleName('Open account menu for Alice Smith');
13901407
expect(trigger().querySelector('[data-shape]')).toHaveAttribute('data-shape', 'square');
1391-
expect(trigger().querySelector('.cl-user-button-avatar-badge')).toBeNull();
1408+
expect(trigger().querySelector('.cl-switcher-avatar-badge')).toBeNull();
13921409
});
13931410

13941411
it('names no organization beneath the account in user mode', async () => {

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@ import type { MosaicMessages } from '../../localization';
44
import { useMessages } from '../../localization';
55
import type { SwitcherMode } from './switcher.types';
66

7-
export type SwitcherSurface = 'user-button' | 'organization-switcher';
7+
export type SwitcherSurface = 'switcher' | 'user-button' | 'organization-switcher';
88

99
export function surfaceForMode(mode: SwitcherMode): SwitcherSurface {
10-
return mode === 'organization' ? 'organization-switcher' : 'user-button';
10+
switch (mode) {
11+
case 'combined':
12+
return 'switcher';
13+
case 'user':
14+
return 'user-button';
15+
case 'organization':
16+
return 'organization-switcher';
17+
}
1118
}
1219

13-
const SurfaceContext = React.createContext<SwitcherSurface>('user-button');
20+
const SurfaceContext = React.createContext<SwitcherSurface>('switcher');
1421

1522
export const SwitcherSurfaceProvider = SurfaceContext.Provider;
1623

‎packages/swingset/src/stories/switcher.mdx‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -255,30 +255,33 @@ than an error, so one order can cover every mode.
255255

256256
## Styling
257257

258-
| Slot | Attributes |
259-
| ------------------------------------ | --------------------------------------- |
260-
| `.cl-user-button-trigger` | |
261-
| `.cl-user-button-popover` | |
262-
| `.cl-user-button-header` | |
263-
| `.cl-user-button-header-content` | |
264-
| `.cl-user-button-header-title` | |
265-
| `.cl-user-button-header-description` | |
266-
| `.cl-user-button-avatar` | |
267-
| `.cl-user-button-avatar-badge` | |
268-
| `.cl-user-button-group` | |
269-
| `.cl-user-button-separator` | |
270-
| `.cl-user-button-item` | `data-interactive` |
271-
| `.cl-user-button-item-media` | |
272-
| `.cl-user-button-item-content` | |
273-
| `.cl-user-button-item-label` | `data-variant="default \| interactive"` |
274-
| `.cl-user-button-item-description` | |
275-
| `.cl-user-button-item-trailing` | |
258+
These are the `combined` mode's slots. `UserButton` and `OrganizationSwitcher` have the same parts
259+
under their own prefixes, `.cl-user-button-*` and `.cl-organization-switcher-*`.
260+
261+
| Slot | Attributes |
262+
| --------------------------------- | --------------------------------------- |
263+
| `.cl-switcher-trigger` | |
264+
| `.cl-switcher-popover` | |
265+
| `.cl-switcher-header` | |
266+
| `.cl-switcher-header-content` | |
267+
| `.cl-switcher-header-title` | |
268+
| `.cl-switcher-header-description` | |
269+
| `.cl-switcher-avatar` | |
270+
| `.cl-switcher-avatar-badge` | |
271+
| `.cl-switcher-group` | |
272+
| `.cl-switcher-separator` | |
273+
| `.cl-switcher-item` | `data-interactive` |
274+
| `.cl-switcher-item-media` | |
275+
| `.cl-switcher-item-content` | |
276+
| `.cl-switcher-item-label` | `data-variant="default \| interactive"` |
277+
| `.cl-switcher-item-description` | |
278+
| `.cl-switcher-item-trailing` | |
276279

277280
### Avatar
278281

279-
`.cl-user-button-avatar` holds the account's badge on the organization's avatar. The cutout behind
282+
`.cl-switcher-avatar` holds the account's badge on the organization's avatar. The cutout behind
280283
the badge follows its size, so resizing either keeps the gap even. Set `width` and `height` on
281-
`.cl-user-button-avatar` to resize it, and these variables on it to place the badge:
284+
`.cl-switcher-avatar` to resize it, and these variables on it to place the badge:
282285

283286
| Variable | Default | Description |
284287
| -------------------------- | ----------------------------- | --------------------------------------------------------------- |
@@ -287,7 +290,7 @@ the badge follows its size, so resizing either keeps the gap even. Set `width` a
287290
| `--cl-avatar-badge-offset` | `2px` trigger, `4px` header | How far the badge sits past the corner of the avatar it badges. |
288291

289292
```css
290-
.cl-user-button-avatar {
293+
.cl-switcher-avatar {
291294
width: 48px;
292295
height: 48px;
293296
--cl-avatar-badge-size: 20px;

‎packages/swingset/src/stories/switcher.stories.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export function CustomizedAvatar(_args: Record<string, unknown>) {
131131

132132
return (
133133
<div className='flex w-full flex-col gap-6'>
134-
<style>{`@scope { .cl-user-button-avatar { ${declarations} } }`}</style>
134+
<style>{`@scope { .cl-switcher-avatar { ${declarations} } }`}</style>
135135
<div className='flex h-28 flex-wrap items-center gap-8'>
136136
<SwitcherView
137137
{...prototype}

0 commit comments

Comments
 (0)