Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/mosaic-button-icon-padding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@clerk/mosaic': minor
---

**Breaking:** Remove the `placement` prop from `Icon`, along with the `data-icon` attribute it rendered. `Button` now tightens its padding on the side of an `Icon` placed directly before or after the label, read from its position. An icon inside a wrapper element no longer counts as leading or trailing, so the button keeps its text padding. Delete `placement` from your `Icon`s; TypeScript flags the ones left.

| Before | After |
| ------------------------------------------- | ------------------------------------------- |
| `<Icon glyph={Plus} placement='inline-start' />` | `<Icon glyph={Plus} />` |

Find usages with `git grep -nE "placement=\{?['\"]inline-|data-icon"`.

The text label inside `Button` and `SubmitButton` now carries a `cl-button-label` class you can target in your styles.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { cleanup, render, screen } from '@testing-library/react';
import React from 'react';
import { afterEach, describe, expect, it } from 'vitest';

import { Plus } from '../../../icons/glyphs/plus';
import { Icon } from '../../icon/icon';
import { Button } from '../button';

afterEach(() => cleanup());

function padding(ui: React.ReactElement) {
render(ui);
const style = getComputedStyle(screen.getByRole('button'));
return { start: style.paddingInlineStart, end: style.paddingInlineEnd };
}

describe('icon padding', () => {
it('tightens the start side for a leading icon without placement', () => {
expect(
padding(
<Button>
<Icon glyph={Plus} />
Add
</Button>,
),
).toEqual({ start: '8px', end: '12px' });
});

it('tightens the end side for a trailing icon without placement', () => {
expect(
padding(
<Button>
Next
<Icon glyph={Plus} />
</Button>,
),
).toEqual({ start: '12px', end: '8px' });
});

it('keeps the default padding for text alone', () => {
expect(padding(<Button>Add</Button>)).toEqual({ start: '12px', end: '12px' });
});

it('zeroes the padding for an icon-only button', () => {
expect(
padding(
<Button
shape='square'
aria-label='Add'
>
<Icon glyph={Plus} />
</Button>,
),
).toEqual({ start: '0px', end: '0px' });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ describe('Mosaic Button', () => {
expect(button).toHaveAttribute('type', 'button');
});

it('gives the text label a class consumers can target', () => {
render(<Button>Save</Button>);
expect(screen.getByText('Save')).toHaveClass('cl-button-label');
});

it('omits the boolean modifiers when they are off', () => {
render(<Button>Hi</Button>);
const button = screen.getByRole('button');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const atoms = (el: Element | null | undefined) => el?.className.split(' ').filte
const isSupersetOf = (all: string[], some: string[]) => some.every(atom => all.includes(atom));

describe('Mosaic SubmitButton', () => {
it('gives the text label a class consumers can target', () => {
render(<SubmitButton>Save</SubmitButton>);
expect(screen.getByText('Save')).toHaveClass('cl-button-label');
});

it('renders a submit button with its children', () => {
render(<SubmitButton>Save</SubmitButton>);
const button = screen.getByRole('button', { name: 'Save' });
Expand Down
16 changes: 8 additions & 8 deletions packages/mosaic/src/components/button/button.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,11 @@ export const sizes = stylex.create({
lineHeight: typeScaleVars['--cl-text-xs-leading'],
paddingInlineEnd: {
default: space['2'],
[stylex.when.descendant("[data-icon='inline-end']", iconScope)]: space['1.5'],
[stylex.when.descendant(':last-child:not(:first-child)', iconScope)]: space['1.5'],
},
paddingInlineStart: {
default: space['2'],
[stylex.when.descendant("[data-icon='inline-start']", iconScope)]: space['1.5'],
[stylex.when.descendant(':first-child:not(:last-child)', iconScope)]: space['1.5'],
},
height: space['6'],
},
Expand All @@ -347,11 +347,11 @@ export const sizes = stylex.create({
lineHeight: typeScaleVars['--cl-text-xs-leading'],
paddingInlineEnd: {
default: space['2.5'],
[stylex.when.descendant("[data-icon='inline-end']", iconScope)]: space['2'],
[stylex.when.descendant(':last-child:not(:first-child)', iconScope)]: space['2'],
},
paddingInlineStart: {
default: space['2.5'],
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
[stylex.when.descendant("[data-icon='inline-start']", iconScope)]: space['2'],
[stylex.when.descendant(':first-child:not(:last-child)', iconScope)]: space['2'],
},
height: space['7'],
},
Expand All @@ -361,11 +361,11 @@ export const sizes = stylex.create({
lineHeight: typeScaleVars['--cl-text-sm-leading'],
paddingInlineEnd: {
default: space['3'],
[stylex.when.descendant("[data-icon='inline-end']", iconScope)]: space['2'],
[stylex.when.descendant(':last-child:not(:first-child)', iconScope)]: space['2'],
},
paddingInlineStart: {
default: space['3'],
[stylex.when.descendant("[data-icon='inline-start']", iconScope)]: space['2'],
[stylex.when.descendant(':first-child:not(:last-child)', iconScope)]: space['2'],
},
height: space['8'],
},
Expand All @@ -375,11 +375,11 @@ export const sizes = stylex.create({
lineHeight: typeScaleVars['--cl-text-base-leading'],
paddingInlineEnd: {
default: space['3'],
[stylex.when.descendant("[data-icon='inline-end']", iconScope)]: space['2'],
[stylex.when.descendant(':last-child:not(:first-child)', iconScope)]: space['2'],
},
paddingInlineStart: {
default: space['3'],
[stylex.when.descendant("[data-icon='inline-start']", iconScope)]: space['2'],
[stylex.when.descendant(':first-child:not(:last-child)', iconScope)]: space['2'],
},
height: space['9'],
},
Expand Down
2 changes: 1 addition & 1 deletion packages/mosaic/src/components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(function
rest,
)}
>
{withTruncatableLabel(children)}
{withTruncatableLabel(children, { slot: 'button-label' })}
</HeadlessButton>
);
});
2 changes: 1 addition & 1 deletion packages/mosaic/src/components/button/submit-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const SubmitButton = React.forwardRef<HTMLButtonElement, SubmitButtonProp
stylex.props(reset.base, styles.content, showPending && styles.contentPending),
)}
>
{withTruncatableLabel(children)}
{withTruncatableLabel(children, { slot: 'button-label' })}
</span>
{isPending || showPending ? (
<Spinner
Expand Down
21 changes: 3 additions & 18 deletions packages/mosaic/src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ export const ComboboxTrigger = React.forwardRef<HTMLButtonElement, ComboboxTrigg
}
{...mergeStyleProps(themeProps('combobox-trigger'), stylex.props(xstyle), rest)}
>
{children ?? (
<Icon
glyph={ChevronDown}
size='md'
/>
)}
{children ?? <Icon glyph={ChevronDown} />}
</HeadlessCombobox.Trigger>
);
});
Expand All @@ -101,12 +96,7 @@ export const ComboboxClear = React.forwardRef<HTMLButtonElement, ComboboxClearPr
}
{...mergeStyleProps(themeProps('combobox-clear'), stylex.props(xstyle), rest)}
>
{children ?? (
<Icon
glyph={X}
size='md'
/>
)}
{children ?? <Icon glyph={X} />}
</HeadlessCombobox.Clear>
);
});
Expand Down Expand Up @@ -278,12 +268,7 @@ export const ComboboxOptionIndicator = React.forwardRef<HTMLSpanElement, Combobo
ref={ref}
{...mergeStyleProps(themeProps('combobox-option-indicator'), stylex.props(styles.indicator, xstyle), rest)}
>
{children ?? (
<Icon
glyph={Checkmark}
size='md'
/>
)}
{children ?? <Icon glyph={Checkmark} />}
</HeadlessCombobox.OptionIndicator>
);
},
Expand Down
10 changes: 2 additions & 8 deletions packages/mosaic/src/components/copy-button/copy-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ const CopyButtonTrigger = React.forwardRef<HTMLButtonElement, Omit<CopyButtonPro
onClick={handleClick}
{...rest}
>
<Icon
glyph={Clipboard}
size='md'
/>
<Icon glyph={Clipboard} />
</Button>
<Toast.Portal>
<Toast.Viewport aria-label={label}>
Expand All @@ -113,10 +110,7 @@ const CopyButtonTrigger = React.forwardRef<HTMLButtonElement, Omit<CopyButtonPro
<Toast.Title
{...mergeStyleProps(themeProps('copy-button-toast-title'), stylex.props(styles.title))}
/>
<Icon
glyph={Checkmark}
size='md'
/>
<Icon glyph={Checkmark} />
</Toast.Root>
</Toast.Positioner>
))}
Expand Down
38 changes: 0 additions & 38 deletions packages/mosaic/src/components/icon/__tests__/icon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,44 +248,6 @@ describe('Mosaic Icon', () => {
expect(svg).not.toHaveClass(stylex.props(containerStyles.mdHeight).className ?? '');
});

it('emits no placement attribute when the icon is not placed', () => {
const { container } = wrap(<Icon glyph={ChevronRight} />);
expect(container.querySelector('svg')).not.toHaveAttribute('data-icon');
});

it('reflects placement as data-icon so a container can select on it', () => {
const { container } = wrap(
<Icon
glyph={ChevronRight}
placement='inline-end'
/>,
);
expect(container.querySelector('svg')).toHaveAttribute('data-icon', 'inline-end');
});

it('does not leak the placement prop itself to the DOM', () => {
const { container } = wrap(
<Icon
glyph={ChevronRight}
placement='inline-start'
/>,
);
const svg = container.querySelector('svg');
expect(svg).toHaveAttribute('data-icon', 'inline-start');
expect(svg).not.toHaveAttribute('placement');
});

it('reflects placement on an override element too', () => {
const { getByTestId } = wrap(
<Icon
glyph={ChevronRight}
placement='inline-end'
/>,
override,
);
expect(getByTestId('override')).toHaveAttribute('data-icon', 'inline-end');
});

it('renders the override element instead of the default glyph', () => {
const { getByTestId, container } = wrap(<Icon glyph={ChevronRight} />, override);
expect(getByTestId('override')).not.toBeNull();
Expand Down
8 changes: 2 additions & 6 deletions packages/mosaic/src/components/icon/icon.doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
practices: [
{
do: true,
text: 'Set `placement` when the icon leads or trails a `Button` label, so the button tightens that side.',
text: 'Put the icon directly before or after a `Button` label, not inside a wrapper, so the button tightens that side.',
},
{ do: true, text: "Use `size='inherit'` for an icon in running text or a heading." },
{
Expand All @@ -35,10 +35,6 @@ export default {
doc: '`sm`, `md`, `lg`, or `inherit` to follow the font size.',
default: "'md'",
},
placement: {
type: "'inline-start' | 'inline-end'",
doc: 'Marks the icon as leading or trailing content, as `data-icon`. Applies no style of its own.',
},
},
},
IconFrame: {
Expand All @@ -57,7 +53,7 @@ export default {
code: `
<Button>
Next
<Icon glyph={ChevronRight} placement='inline-end' />
<Icon glyph={ChevronRight} />
</Button>
`,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/mosaic/src/components/icon/icon.markers.stylex.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as stylex from '@stylexjs/stylex';

/**
* Scopes a container's `data-icon` selectors to a Mosaic `Icon`, so a stray `data-icon` elsewhere
* in the subtree can't trigger them. `Icon` applies it; `Button`'s inline padding matches on it.
* Scopes a container's icon selectors to a Mosaic `Icon`, so another element in the subtree can't
* trigger them. `Icon` applies it; `Button`'s inline padding matches on it.
*
* Its own module because `@stylexjs/enforce-extension` requires the define-primitives to live in
* a `.stylex.ts` file.
Expand Down
12 changes: 2 additions & 10 deletions packages/mosaic/src/components/icon/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,21 @@ import { sizes, styles } from './icon.styles';
export interface IconProps extends MosaicElementProps<'svg'> {
glyph: IconComponent;
size?: 'sm' | 'md' | 'lg' | 'inherit';
placement?: 'inline-start' | 'inline-end';
}

/**
* Renders a Mosaic glyph. The glyph can be swapped by its name via the `icons` prop on
* `MosaicProvider`; Mosaic's sizing is applied to the override exactly as it is to the built-in
* glyph, so the two stay visually consistent.
*
* `placement` marks the icon as leading or trailing content within a container. It applies no
* style here — it reflects an attribute the container styles against, so a `Button` can tighten
* the padding on whichever side the icon sits. The values are the CSS logical directions, so they
* follow the writing mode rather than naming a physical edge.
*/
export const Icon = React.forwardRef<SVGSVGElement, IconProps>(function MosaicIcon(
{ glyph: Glyph, size = 'md', placement, xstyle, ...rest },
{ glyph: Glyph, size = 'md', xstyle, ...rest },
ref,
) {
const override = useMosaicIcons()[Glyph.iconName];
// The placement axis reflects as `data-icon`, not `data-placement`, so a container selects the
// child by what it is: `:has([data-icon='inline-end'])` can't match some other placed descendant.
const props = mergeStyleProps(
{ 'aria-hidden': true },
themeProps('icon', { size, icon: placement }),
themeProps('icon', { size }),
stylex.props(reset.base, styles.base, sizes[size], iconScope, xstyle),
rest,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/mosaic/src/components/link/link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Link', () => {
</>,
);
const external = screen.getByRole('link', { name: 'External' });
const arrow = external.querySelector('[data-icon="inline-end"]');
const arrow = external.querySelector('.cl-icon');
expect(arrow).toHaveAttribute('aria-hidden', 'true');
expect(external.lastElementChild?.contains(arrow)).toBe(true);
expect(screen.getByRole('link', { name: 'Internal' }).querySelector('svg')).toBeNull();
Expand Down
1 change: 0 additions & 1 deletion packages/mosaic/src/components/link/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(function Mosa
<span {...stylex.props(styles.iconSlot)}>
<Icon
glyph={External}
placement='inline-end'
size='inherit'
/>
</span>
Expand Down
Loading
Loading