Skip to content

Commit 4cf1302

Browse files
feat(mosaic): add combobox option parts and center phone input countries (#10764)
1 parent eb6cb09 commit 4cf1302

11 files changed

Lines changed: 213 additions & 48 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': minor
3+
---
4+
5+
Add `Combobox.Media`, `Combobox.Label` and `Combobox.Detail` for composing a combobox option, styled through `.cl-combobox-option-media`, `.cl-combobox-option-label` and `.cl-combobox-option-detail`. `PhoneInput` builds its country options from these parts, which centers each country's name in its row and gives the flag, name and calling code targetable classes.

‎packages/mosaic/src/components/combobox/combobox.styles.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ export const styles = stylex.create({
121121
fontSize: typeScaleVars['--cl-text-sm-size'],
122122
textAlign: 'center',
123123
},
124+
media: {
125+
alignItems: 'center',
126+
aspectRatio: '1/1',
127+
display: 'flex',
128+
flexShrink: 0,
129+
justifyContent: 'center',
130+
width: space['4'],
131+
},
132+
label: {
133+
flexGrow: 1,
134+
minWidth: 0,
135+
},
136+
detail: {
137+
color: colorVars['--cl-color-foreground-secondary'],
138+
flexShrink: 0,
139+
fontVariantNumeric: 'tabular-nums',
140+
whiteSpace: 'nowrap',
141+
},
124142
indicator: {
125143
alignItems: 'center',
126144
display: 'inline-flex',

‎packages/mosaic/src/components/combobox/combobox.test.tsx‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,32 @@ describe('Mosaic Combobox', () => {
350350
expect(screen.getByRole('option', { name: 'United States' })).toHaveClass('cl-combobox-option');
351351
});
352352

353+
it('composes an option from media, label and detail parts', async () => {
354+
const user = userEvent.setup();
355+
render(
356+
<Combobox.Root>
357+
<Combobox.Input aria-label='Country' />
358+
<Combobox.Popup>
359+
<Combobox.Option
360+
value='dz'
361+
label='Algeria'
362+
>
363+
<Combobox.Media data-testid='flag'>🇩🇿</Combobox.Media>
364+
<Combobox.Label>Algeria</Combobox.Label>
365+
<Combobox.Detail>+213</Combobox.Detail>
366+
</Combobox.Option>
367+
</Combobox.Popup>
368+
</Combobox.Root>,
369+
);
370+
371+
await user.click(screen.getByRole('combobox', { name: 'Country' }));
372+
373+
const option = screen.getByRole('option', { name: /Algeria/ });
374+
expect(screen.getByTestId('flag')).toHaveClass('cl-combobox-option-media');
375+
expect(option).toContainElement(screen.getByText('Algeria', { selector: '.cl-combobox-option-label' }));
376+
expect(option).toContainElement(screen.getByText('+213', { selector: '.cl-combobox-option-detail' }));
377+
});
378+
353379
it('renders a reusable empty state', () => {
354380
render(<Combobox.Empty>No matches</Combobox.Empty>);
355381

‎packages/mosaic/src/components/combobox/combobox.tsx‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useRender } from '../../primitives/utils';
1212
import type { MosaicComponentProps } from '../../props';
1313
import { mergeStyleProps, themeProps } from '../../props';
1414
import { reset } from '../../styles/reset.styles';
15+
import { truncationStyles } from '../../styles/typography.styles';
1516
import { Button } from '../button';
1617
import { Icon } from '../icon';
1718
import { Input, type InputVariant } from '../input';
@@ -213,6 +214,56 @@ export const ComboboxOption = React.forwardRef<HTMLDivElement, ComboboxOptionPro
213214
);
214215
});
215216

217+
/** `span`, not `div`: the option's children are phrasing content. */
218+
export type ComboboxMediaProps = MosaicComponentProps<'span'>;
219+
220+
/** Square leading column that centers an icon, flag or avatar, so every option's label starts on one line. */
221+
export const ComboboxMedia = React.forwardRef<HTMLSpanElement, ComboboxMediaProps>(function MosaicComboboxMedia(
222+
{ render, xstyle, ...rest },
223+
ref,
224+
) {
225+
return useRender({
226+
defaultTagName: 'span',
227+
render,
228+
ref,
229+
props: mergeStyleProps(themeProps('combobox-option-media'), stylex.props(reset.base, styles.media, xstyle), rest),
230+
});
231+
});
232+
233+
export type ComboboxLabelProps = MosaicComponentProps<'span'>;
234+
235+
/** The option's name. Takes the space between the media and whatever trails it, truncating to one line. */
236+
export const ComboboxLabel = React.forwardRef<HTMLSpanElement, ComboboxLabelProps>(function MosaicComboboxLabel(
237+
{ render, xstyle, ...rest },
238+
ref,
239+
) {
240+
return useRender({
241+
defaultTagName: 'span',
242+
render,
243+
ref,
244+
props: mergeStyleProps(
245+
themeProps('combobox-option-label'),
246+
stylex.props(reset.base, styles.label, truncationStyles.singleLine, xstyle),
247+
rest,
248+
),
249+
});
250+
});
251+
252+
export type ComboboxDetailProps = MosaicComponentProps<'span'>;
253+
254+
/** Secondary trailing text, such as a code or count. Stays whole while the label truncates. */
255+
export const ComboboxDetail = React.forwardRef<HTMLSpanElement, ComboboxDetailProps>(function MosaicComboboxDetail(
256+
{ render, xstyle, ...rest },
257+
ref,
258+
) {
259+
return useRender({
260+
defaultTagName: 'span',
261+
render,
262+
ref,
263+
props: mergeStyleProps(themeProps('combobox-option-detail'), stylex.props(reset.base, styles.detail, xstyle), rest),
264+
});
265+
});
266+
216267
export type ComboboxEmptyProps = MosaicComponentProps<'p'>;
217268

218269
export type ComboboxOptionIndicatorProps = MosaicComponentProps<'span'>;
@@ -258,6 +309,9 @@ export const Combobox = {
258309
Popup: ComboboxPopup,
259310
List: ComboboxList,
260311
Option: ComboboxOption,
312+
Media: ComboboxMedia,
313+
Label: ComboboxLabel,
314+
Detail: ComboboxDetail,
261315
OptionIndicator: ComboboxOptionIndicator,
262316
Empty: ComboboxEmpty,
263317
};

‎packages/mosaic/src/components/combobox/index.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ export {
22
Combobox,
33
ComboboxClear,
44
ComboboxCollection,
5+
ComboboxDetail,
56
ComboboxEmpty,
67
ComboboxInput,
8+
ComboboxLabel,
79
ComboboxList,
10+
ComboboxMedia,
811
ComboboxOption,
912
ComboboxOptionIndicator,
1013
ComboboxPopup,
@@ -13,9 +16,12 @@ export {
1316
} from './combobox';
1417
export type {
1518
ComboboxClearProps,
19+
ComboboxDetailProps,
1620
ComboboxEmptyProps,
1721
ComboboxInputProps,
22+
ComboboxLabelProps,
1823
ComboboxListProps,
24+
ComboboxMediaProps,
1925
ComboboxOptionProps,
2026
ComboboxOptionIndicatorProps,
2127
ComboboxPopupProps,

‎packages/mosaic/src/components/phone-input/phone-input.styles.ts‎

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -174,34 +174,12 @@ export const styles = stylex.create({
174174
textAlign: 'center',
175175
},
176176
optionFlag: {
177-
alignItems: 'center',
178-
display: 'flex',
179-
flexShrink: 0,
180-
justifyContent: 'center',
181-
height: space['4'],
182-
width: space['4'],
183-
},
184-
optionName: {
185-
overflow: 'hidden',
186-
flexGrow: 1,
187-
textOverflow: 'ellipsis',
188-
whiteSpace: 'nowrap',
189-
height: space['6'],
190-
minWidth: 0,
177+
fontSize: '1rem',
178+
lineHeight: 1,
191179
},
192180
optionCode: {
193-
alignItems: 'center',
194-
color: colorVars['--cl-color-foreground-secondary'],
195-
display: 'flex',
196-
flexShrink: 0,
197181
fontSize: typeScaleVars['--cl-text-xs-size'],
198-
fontVariantNumeric: 'tabular-nums',
199182
fontWeight: fontWeightVars['--cl-font-medium'],
200-
justifyContent: 'flex-end',
201183
lineHeight: typeScaleVars['--cl-text-xs-leading'],
202-
whiteSpace: 'nowrap',
203-
height: space['5'],
204-
minWidth: space['5'],
205-
width: 'max-content',
206184
},
207185
});

‎packages/mosaic/src/components/phone-input/phone-input.test.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,25 @@ describe('Mosaic PhoneInput', () => {
368368
expect(popup).toContainElement(list);
369369
});
370370

371+
it('builds each country option from the combobox option parts', async () => {
372+
const user = userEvent.setup();
373+
render(
374+
<PhoneInput
375+
{...labels}
376+
aria-label='Phone number'
377+
/>,
378+
);
379+
380+
await user.click(screen.getByRole('button', { name: 'Country, United States' }));
381+
await user.type(screen.getByRole('combobox', { name: 'Search countries' }), 'Algeria');
382+
383+
const option = await screen.findByRole('option', { name: /Algeria/ });
384+
expect(option).toHaveClass('cl-combobox-option', 'cl-phone-input-country-option');
385+
expect(option.querySelector('.cl-combobox-option-media')).toHaveTextContent('🇩🇿');
386+
expect(option.querySelector('.cl-combobox-option-label')).toHaveTextContent('Algeria');
387+
expect(option.querySelector('.cl-combobox-option-detail')).toHaveTextContent('+213');
388+
});
389+
371390
it('keeps the country search in its own Field scope', async () => {
372391
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
373392
const user = userEvent.setup();

‎packages/mosaic/src/components/phone-input/phone-input.tsx‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { mergeStyleProps, themeProps } from '../../props';
1717
import { focusOutline } from '../../styles/focus-outline.styles';
1818
import { reset } from '../../styles/reset.styles';
1919
import { styles as buttonStyles } from '../button/button.styles';
20+
import { Combobox } from '../combobox';
2021
import { Field } from '../field';
2122
import { useOptionalFieldContext } from '../field/field.context';
2223
import { Icon } from '../icon';
@@ -305,25 +306,22 @@ export const PhoneInput = React.forwardRef<HTMLInputElement, PhoneInputProps>(fu
305306
>
306307
{filteredCountries.length > 0 ? (
307308
filteredCountries.map(option => (
308-
<HeadlessCombobox.Option
309+
<Combobox.Option
309310
key={option.iso}
310311
value={option.iso}
311312
label={option.name}
312-
{...mergeStyleProps(
313-
themeProps('combobox-option'),
314-
themeProps('phone-input-country-option'),
315-
stylex.props(reset.base, styles.option),
316-
)}
313+
{...themeProps('phone-input-country-option')}
314+
xstyle={styles.option}
317315
>
318-
<span
316+
<Combobox.Media
319317
aria-hidden='true'
320-
{...stylex.props(reset.base, styles.optionFlag)}
318+
xstyle={styles.optionFlag}
321319
>
322-
<span {...stylex.props(reset.base, styles.flag)}>{getFlagEmojiFromCountryIso(option.iso)}</span>
323-
</span>
324-
<span {...stylex.props(reset.base, styles.optionName)}>{option.name}</span>
325-
<span {...stylex.props(reset.base, styles.optionCode)}>+{option.code}</span>
326-
</HeadlessCombobox.Option>
320+
{getFlagEmojiFromCountryIso(option.iso)}
321+
</Combobox.Media>
322+
<Combobox.Label>{option.name}</Combobox.Label>
323+
<Combobox.Detail xstyle={styles.optionCode}>+{option.code}</Combobox.Detail>
324+
</Combobox.Option>
327325
))
328326
) : (
329327
<p {...mergeStyleProps(themeProps('combobox-empty'), stylex.props(reset.base, styles.empty))}>

‎packages/mosaic/src/styles/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ export type {
3636
} from '../components/empty-state';
3737
export { Combobox } from '../components/combobox';
3838
export type {
39+
ComboboxDetailProps,
3940
ComboboxEmptyProps,
4041
ComboboxInputProps,
42+
ComboboxLabelProps,
4143
ComboboxListProps,
44+
ComboboxMediaProps,
4245
ComboboxOptionProps,
4346
ComboboxPopupProps,
4447
ComboboxRootProps,

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,30 @@ so closing resets the search, not the selection.
4343
storyModule={ComboboxStories}
4444
/>
4545

46+
## Option parts
47+
48+
Compose an option from `Media`, `Label` and `Detail` so every row lines up. The label truncates to
49+
one line; the detail stays whole.
50+
51+
<Story
52+
name='OptionParts'
53+
storyModule={ComboboxStories}
54+
/>
55+
4656
## Parts
4757

48-
| Part | Description |
49-
| --------------------- | --------------------------------------------------------------------------- |
50-
| `Combobox.Root` | Owns input, selection, open state, and keyboard navigation. |
51-
| `Combobox.Input` | Search input; renders Mosaic `Input` unless composed through another input. |
52-
| `Combobox.Trigger` | Opens and closes the option list while keeping focus on the input. |
53-
| `Combobox.Clear` | Empties the input and selection; shown only while there is a value. |
54-
| `Combobox.Popup` | Portals, positions, surfaces, and scrolls a floating option list. |
55-
| `Combobox.List` | Scrollable inline listbox. |
56-
| `Combobox.Collection` | Filters items and renders each option, with an optional empty state. |
57-
| `Combobox.Option` | Selectable option with active, selected, and disabled states. |
58-
| `Combobox.Empty` | Empty result message. |
58+
| Part | Description |
59+
| -------------------------- | --------------------------------------------------------------------------- |
60+
| `Combobox.Root` | Owns input, selection, open state, and keyboard navigation. |
61+
| `Combobox.Input` | Search input; renders Mosaic `Input` unless composed through another input. |
62+
| `Combobox.Trigger` | Opens and closes the option list while keeping focus on the input. |
63+
| `Combobox.Clear` | Empties the input and selection; shown only while there is a value. |
64+
| `Combobox.Popup` | Portals, positions, surfaces, and scrolls a floating option list. |
65+
| `Combobox.List` | Scrollable inline listbox. |
66+
| `Combobox.Collection` | Filters items and renders each option, with an optional empty state. |
67+
| `Combobox.Option` | Selectable option with active, selected, and disabled states. |
68+
| `Combobox.Media` | Square leading column for an icon, flag, or avatar. |
69+
| `Combobox.Label` | The option's name; truncates to one line. |
70+
| `Combobox.Detail` | Secondary trailing text, such as a code; never truncates. |
71+
| `Combobox.OptionIndicator` | Check shown on the selected option. |
72+
| `Combobox.Empty` | Empty result message. |

0 commit comments

Comments
 (0)