Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Badge: Support text truncation #68107

Merged
merged 4 commits into from
Jan 2, 2025
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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;
Copy link
Contributor

@ciampo ciampo Dec 19, 2024

Choose a reason for hiding this comment

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

An inline outer display value seems more appropriate ✅

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 );
Copy link
Member Author

Choose a reason for hiding this comment

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

Tweaked to be more resilient against internal DOM changes.

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
Loading