Skip to content

Commit 7ffbf74

Browse files
feat(mosaic): add Combobox.Clear and consistent trigger interactions (#10689)
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 4be1859 commit 7ffbf74

18 files changed

Lines changed: 286 additions & 32 deletions
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
space,
1212
typeScaleVars,
1313
} from '../../tokens.stylex';
14+
import { inputGroupScope } from '../input-group/input-group.markers.stylex';
1415

1516
const popupRadius = radiusVars['--cl-radius-xl'];
1617
const inset = space['1.5'];
@@ -122,3 +123,27 @@ export const styles = stylex.create({
122123
width: space['4'],
123124
},
124125
});
126+
127+
export const trigger = stylex.create({
128+
base: {
129+
backgroundColor: 'transparent',
130+
color: {
131+
default: colorVars['--cl-color-input-placeholder'],
132+
'@media (hover: hover)': {
133+
default: null,
134+
[stylex.when.ancestor(':hover', inputGroupScope)]: colorVars['--cl-color-foreground'],
135+
},
136+
},
137+
},
138+
clear: {
139+
color: {
140+
default: colorVars['--cl-color-input-placeholder'],
141+
'@media (hover: hover)': {
142+
default: null,
143+
':not([data-disabled]):hover': colorVars['--cl-color-foreground'],
144+
},
145+
},
146+
marginInlineEnd: `calc(-1 * ${space['2']})`,
147+
width: space['5.5'],
148+
},
149+
});

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ describe('Mosaic Combobox', () => {
212212
expect(screen.getByRole('option', { name: 'Apple' }).closest('.cl-combobox-viewport')).toBe(viewport);
213213
});
214214

215+
it('opens its options when the input is clicked', async () => {
216+
const user = userEvent.setup();
217+
render(<FloatingCombobox />);
218+
219+
await user.click(screen.getByRole('combobox', { name: 'Fruit' }));
220+
221+
expect(screen.getByRole('listbox')).toBeInTheDocument();
222+
});
223+
215224
it('selects an option and restores its label to the input', async () => {
216225
const onValueChange = vi.fn();
217226
const user = userEvent.setup();
@@ -346,4 +355,74 @@ describe('Mosaic Combobox', () => {
346355

347356
expect(screen.getByText('No matches')).toHaveClass('cl-combobox-empty');
348357
});
358+
359+
function ClearableCombobox(props?: { onValueChange?: (value: string | null) => void }) {
360+
return (
361+
<Combobox.Root
362+
defaultValue='apple'
363+
defaultInputValue='Apple'
364+
onValueChange={props?.onValueChange}
365+
>
366+
<InputGroup.Root>
367+
<Combobox.Input aria-label='Fruit' />
368+
<InputGroup.End>
369+
<Combobox.Clear />
370+
<Combobox.Trigger aria-label='Toggle fruit options' />
371+
</InputGroup.End>
372+
</InputGroup.Root>
373+
<Combobox.Popup>
374+
<Combobox.Option
375+
value='apple'
376+
label='Apple'
377+
>
378+
Apple
379+
</Combobox.Option>
380+
</Combobox.Popup>
381+
</Combobox.Root>
382+
);
383+
}
384+
385+
it('clears the input and selection from the Clear button and keeps focus on the input', async () => {
386+
const user = userEvent.setup();
387+
const onValueChange = vi.fn();
388+
render(<ClearableCombobox onValueChange={onValueChange} />);
389+
390+
await user.click(screen.getByRole('button', { name: 'Clear' }));
391+
392+
expect(onValueChange).toHaveBeenCalledWith(null);
393+
expect(screen.getByRole('combobox', { name: 'Fruit' })).toHaveValue('');
394+
expect(screen.getByRole('combobox', { name: 'Fruit' })).toHaveFocus();
395+
});
396+
397+
it('renders Clear only while there is an input value or a selection', async () => {
398+
const user = userEvent.setup();
399+
render(
400+
<Combobox.Root>
401+
<InputGroup.Root>
402+
<Combobox.Input aria-label='Fruit' />
403+
<InputGroup.End>
404+
<Combobox.Clear />
405+
</InputGroup.End>
406+
</InputGroup.Root>
407+
</Combobox.Root>,
408+
);
409+
410+
expect(screen.queryByRole('button', { name: 'Clear' })).not.toBeInTheDocument();
411+
await user.type(screen.getByRole('combobox', { name: 'Fruit' }), 'a');
412+
expect(screen.getByRole('button', { name: 'Clear' })).toBeInTheDocument();
413+
});
414+
415+
it('keeps the trigger toggling the list with its own name while there is text', async () => {
416+
const user = userEvent.setup();
417+
render(<ClearableCombobox />);
418+
419+
const trigger = screen.getByRole('button', { name: 'Toggle fruit options' });
420+
expect(trigger).toHaveAttribute('aria-expanded', 'false');
421+
422+
await user.click(trigger);
423+
expect(trigger).toHaveAttribute('aria-expanded', 'true');
424+
425+
await user.click(trigger);
426+
expect(trigger).toHaveAttribute('aria-expanded', 'false');
427+
});
349428
});

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

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,99 @@ import * as stylex from '@stylexjs/stylex';
44
import React from 'react';
55

66
import { Checkmark } from '../../icons/glyphs/checkmark';
7+
import { ChevronDown } from '../../icons/glyphs/chevron-down';
8+
import { X } from '../../icons/glyphs/x';
79
import type { ComboboxProps } from '../../primitives/combobox';
810
import { Combobox as HeadlessCombobox } from '../../primitives/combobox';
911
import { useRender } from '../../primitives/utils';
1012
import type { MosaicComponentProps } from '../../props';
1113
import { mergeStyleProps, themeProps } from '../../props';
1214
import { reset } from '../../styles/reset.styles';
15+
import { Button } from '../button';
1316
import { Icon } from '../icon';
1417
import { Input, type InputVariant } from '../input';
1518
import { useOptionalInputGroupContext } from '../input-group/input-group.context';
1619
import { scrollAreaRoot, scrollAreaViewport } from '../scroll-area';
17-
import { styles } from './combobox.styles';
20+
import { styles, trigger } from './combobox.styles';
1821

1922
export type ComboboxRootProps = ComboboxProps;
2023
export type ComboboxSize = 'sm' | 'md' | 'lg';
2124
export type ComboboxTriggerProps = MosaicComponentProps<'button'>;
25+
export type ComboboxClearProps = MosaicComponentProps<'button'>;
2226
export const ComboboxCollection = HeadlessCombobox.Collection;
2327

2428
const ComboboxAnchorContext = React.createContext<{
2529
anchor: HTMLElement | null;
2630
setAnchor: React.Dispatch<React.SetStateAction<HTMLElement | null>>;
2731
} | null>(null);
2832

29-
export function ComboboxRoot({ sideOffset = 8, ...props }: ComboboxRootProps) {
33+
export function ComboboxRoot({ sideOffset = 8, openOnInputClick = true, ...props }: ComboboxRootProps) {
3034
const [anchor, setAnchor] = React.useState<HTMLElement | null>(null);
3135
const context = React.useMemo(() => ({ anchor, setAnchor }), [anchor]);
3236
return (
3337
<ComboboxAnchorContext.Provider value={context}>
3438
<HeadlessCombobox.Root
3539
sideOffset={sideOffset}
40+
openOnInputClick={openOnInputClick}
3641
{...props}
3742
/>
3843
</ComboboxAnchorContext.Provider>
3944
);
4045
}
4146

4247
export const ComboboxTrigger = React.forwardRef<HTMLButtonElement, ComboboxTriggerProps>(function MosaicComboboxTrigger(
43-
{ xstyle, ...rest },
48+
{ xstyle, render, children, ...rest },
4449
ref,
4550
) {
4651
return (
4752
<HeadlessCombobox.Trigger
4853
ref={ref}
54+
render={
55+
render ??
56+
(props => (
57+
<Button
58+
xstyle={trigger.base}
59+
{...props}
60+
/>
61+
))
62+
}
4963
{...mergeStyleProps(themeProps('combobox-trigger'), stylex.props(xstyle), rest)}
50-
/>
64+
>
65+
{children ?? (
66+
<Icon
67+
glyph={ChevronDown}
68+
size='md'
69+
/>
70+
)}
71+
</HeadlessCombobox.Trigger>
72+
);
73+
});
74+
75+
export const ComboboxClear = React.forwardRef<HTMLButtonElement, ComboboxClearProps>(function MosaicComboboxClear(
76+
{ xstyle, render, children, ...rest },
77+
ref,
78+
) {
79+
return (
80+
<HeadlessCombobox.Clear
81+
ref={ref}
82+
render={
83+
render ??
84+
(props => (
85+
<Button
86+
xstyle={[trigger.base, trigger.clear]}
87+
{...props}
88+
/>
89+
))
90+
}
91+
{...mergeStyleProps(themeProps('combobox-clear'), stylex.props(xstyle), rest)}
92+
>
93+
{children ?? (
94+
<Icon
95+
glyph={X}
96+
size='md'
97+
/>
98+
)}
99+
</HeadlessCombobox.Clear>
51100
);
52101
});
53102

@@ -205,6 +254,7 @@ export const Combobox = {
205254
Collection: ComboboxCollection,
206255
Input: ComboboxInput,
207256
Trigger: ComboboxTrigger,
257+
Clear: ComboboxClear,
208258
Popup: ComboboxPopup,
209259
List: ComboboxList,
210260
Option: ComboboxOption,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export {
22
Combobox,
3+
ComboboxClear,
34
ComboboxCollection,
45
ComboboxEmpty,
56
ComboboxInput,
@@ -11,6 +12,7 @@ export {
1112
ComboboxTrigger,
1213
} from './combobox';
1314
export type {
15+
ComboboxClearProps,
1416
ComboboxEmptyProps,
1517
ComboboxInputProps,
1618
ComboboxListProps,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as stylex from '@stylexjs/stylex';
2+
3+
export const inputGroupScope = stylex.defineMarker();

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { useOptionalFieldContext } from '../field/field.context';
1313
import { Input } from '../input';
1414
import type { InputGroupSize } from './input-group.context';
1515
import { InputGroupContext, useInputGroupContext } from './input-group.context';
16+
import { inputGroupScope } from './input-group.markers.stylex';
1617
import { sizes, styles, textSizes } from './input-group.styles';
1718

1819
export interface InputGroupRootProps extends MosaicComponentProps<'div'> {
@@ -44,7 +45,15 @@ const Root = React.forwardRef<HTMLDivElement, InputGroupRootProps>(function Mosa
4445
props: {
4546
...mergeStyleProps(
4647
themeProps('input-group', { size, disabled, invalid }),
47-
stylex.props(reset.base, inputStyles.group, styles.root, sizes[size], disabled && inputStyles.disabled, xstyle),
48+
stylex.props(
49+
reset.base,
50+
inputGroupScope,
51+
inputStyles.group,
52+
styles.root,
53+
sizes[size],
54+
disabled && inputStyles.disabled,
55+
xstyle,
56+
),
4857
otherProps,
4958
),
5059
onClick: (event: React.MouseEvent<HTMLDivElement>) => {

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@ const optionRadius = `max(0px, calc(${popupRadius} - ${listInset}))`;
1616

1717
export const styles = stylex.create({
1818
triggerStart: {
19-
paddingInlineStart: 'var(--_cl-input-group-inset)',
19+
paddingInlineStart: 0,
2020
},
2121
trigger: {
22-
'--_cl-icon-color': colorVars['--cl-color-foreground-secondary'],
23-
borderRadius: 'max(0px, calc(var(--_cl-input-group-radius) - var(--_cl-input-group-inset)))',
22+
'--_cl-icon-color': {
23+
default: colorVars['--cl-color-input-placeholder'],
24+
':not([data-disabled])[aria-expanded="true"]': colorVars['--cl-color-foreground'],
25+
'@media (hover: hover)': {
26+
default: null,
27+
':not([data-disabled]):hover': colorVars['--cl-color-foreground'],
28+
},
29+
},
30+
borderRadius: 'max(0px, calc(var(--_cl-input-group-radius) - 1px))',
2431
borderWidth: 0,
2532
alignItems: 'center',
33+
alignSelf: 'stretch',
2634
appearance: 'none',
2735
backgroundColor: 'transparent',
2836
cursor: 'pointer',
@@ -31,9 +39,8 @@ export const styles = stylex.create({
3139
justifyContent: 'center',
3240
outlineOffset: { default: null, ':focus-visible': 0 },
3341
paddingInlineEnd: 0,
34-
paddingInlineStart: space['2'],
42+
paddingInlineStart: `calc(${space['2']} + var(--_cl-input-group-inset))`,
3543
userSelect: 'none',
36-
height: 'calc(var(--_cl-input-group-height) - 2 * var(--_cl-input-group-inset))',
3744
},
3845
triggerContent: {
3946
gap: space['0.5'],

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const optionRadius = `max(0px, calc(${popupRadius} - ${inset}))`;
2424
// popup's 1px border, the viewport's 5px padding, and its own 6px padding, on both the text and
2525
// the icon's edge, at the same 32px height.
2626

27+
const hoverBorderColor = 'light-dark(#bebebe, #525252)';
28+
2729
export const trigger = stylex.create({
2830
base: {
2931
'--_cl-icon-color': {
@@ -33,6 +35,18 @@ export const trigger = stylex.create({
3335
':not([data-disabled]):hover': colorVars['--cl-color-foreground'],
3436
},
3537
},
38+
borderColor: {
39+
default: colorVars['--cl-color-border'],
40+
'@media (hover: hover)': {
41+
default: null,
42+
':not([data-disabled]):hover:not(:active):not([data-open])': hoverBorderColor,
43+
},
44+
},
45+
backgroundColor: {
46+
default: 'transparent',
47+
':not([data-disabled]):active': colorVars['--cl-color-neutral-alpha-200'],
48+
':not([data-disabled])[data-open]': colorVars['--cl-color-neutral-alpha-200'],
49+
},
3650
paddingInlineEnd: `calc(${space['3']} - 1px)`,
3751
paddingInlineStart: `calc(${space['3']} - 1px)`,
3852
},
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use client';
2+
3+
import React from 'react';
4+
5+
import { type ComponentProps, type DefaultProps, mergeProps, useRender } from '../utils';
6+
import { useComboboxContext } from './combobox-context';
7+
8+
export type ComboboxClearProps = ComponentProps<'button'>;
9+
10+
export const ComboboxClear = React.forwardRef<HTMLButtonElement, ComboboxClearProps>(
11+
function ComboboxClear(props, ref) {
12+
const { render, ...otherProps } = props;
13+
const { inputValue, selectedValue, clear } = useComboboxContext();
14+
15+
const defaultProps = {
16+
type: 'button',
17+
tabIndex: -1,
18+
'aria-label': 'Clear',
19+
onPointerDown(event: React.PointerEvent<HTMLButtonElement>) {
20+
event.preventDefault();
21+
},
22+
onClick() {
23+
clear();
24+
},
25+
} satisfies DefaultProps<'button'>;
26+
27+
const element = useRender({
28+
defaultTagName: 'button',
29+
render,
30+
ref,
31+
props: mergeProps<'button'>(defaultProps, otherProps),
32+
});
33+
34+
return inputValue !== '' || selectedValue !== null ? element : null;
35+
},
36+
);

0 commit comments

Comments
 (0)