Skip to content

Commit

Permalink
Badge: Support text truncation (#68107)
Browse files Browse the repository at this point in the history
* Badge: Support text truncation

* Stabilize tests against internal DOM changes

* Limit width to container

* Add changelog

Co-authored-by: mirka <[email protected]>
Co-authored-by: tyxla <[email protected]>
Co-authored-by: ciampo <[email protected]>
Co-authored-by: jameskoster <[email protected]>
  • Loading branch information
5 people authored Jan 2, 2025
1 parent a3e4c79 commit 27a4bcb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

- Add new `Badge` component ([#66555](https://github.com/WordPress/gutenberg/pull/66555)).
- `Menu`: refactor to more granular sub-components ([#67422](https://github.com/WordPress/gutenberg/pull/67422)).
- `Badge`: Support text truncation ([#68107](https://github.com/WordPress/gutenberg/pull/68107)).

### Internal

Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/badge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ function Badge( {
icon={ contextBasedIcon() }
size={ 16 }
fill="currentColor"
className="components-badge__icon"
/>
) }
{ children }
<span className="components-badge__content">{ children }</span>
</span>
);
}
Expand Down
17 changes: 14 additions & 3 deletions packages/components/src/badge/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ $badge-colors: (
);

.components-badge {
@include reset;

background-color: color-mix(in srgb, $white 90%, var(--base-color));
color: color-mix(in srgb, $black 50%, var(--base-color));
padding: 0 $grid-unit-10;
min-height: $grid-unit-30;
max-width: 100%;
border-radius: $radius-small;
font-size: $font-size-small;
font-weight: 400;
flex-shrink: 0;
line-height: $font-line-height-small;
width: fit-content;
display: flex;
display: inline-flex;
align-items: center;
gap: 2px;

Expand All @@ -36,3 +37,13 @@ $badge-colors: (
}
}
}

.components-badge__icon {
flex-shrink: 0;
}

.components-badge__content {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
13 changes: 9 additions & 4 deletions packages/components/src/badge/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@ import { render, screen } from '@testing-library/react';
/**
* Internal dependencies
*/
import Badge from '..';
import _Badge from '..';

const testid = 'my-badge';
const Badge = ( props: React.ComponentProps< typeof _Badge > ) => (
<_Badge data-testid={ testid } { ...props } />
);

describe( 'Badge', () => {
it( 'should render correctly with default props', () => {
render( <Badge>Code is Poetry</Badge> );
const badge = screen.getByText( 'Code is Poetry' );
const badge = screen.getByTestId( testid );
expect( badge ).toBeInTheDocument();
expect( badge.tagName ).toBe( 'SPAN' );
expect( badge ).toHaveClass( 'components-badge' );
} );

it( 'should render as per its intent and contain an icon', () => {
render( <Badge intent="error">Code is Poetry</Badge> );
const badge = screen.getByText( 'Code is Poetry' );
const badge = screen.getByTestId( testid );
expect( badge ).toHaveClass( 'components-badge', 'is-error' );
expect( badge ).toHaveClass( 'has-icon' );
} );

it( 'should combine custom className with default class', () => {
render( <Badge className="custom-class">Code is Poetry</Badge> );
const badge = screen.getByText( 'Code is Poetry' );
const badge = screen.getByTestId( testid );
expect( badge ).toHaveClass( 'components-badge' );
expect( badge ).toHaveClass( 'custom-class' );
} );
Expand Down

1 comment on commit 27a4bcb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 27a4bcb.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12582638030
📝 Reported issues:

Please sign in to comment.