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

Components: Fix icon condition for Badge #68588

Merged
merged 1 commit into from
Jan 10, 2025
Merged
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
55 changes: 28 additions & 27 deletions packages/components/src/badge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,46 @@ import type { BadgeProps } from './types';
import type { WordPressComponentProps } from '../context';
import Icon from '../icon';

/**
* Returns an icon based on the badge context.
*
* @return The corresponding icon for the provided context.
*/
function contextBasedIcon( intent: BadgeProps[ 'intent' ] = 'default' ) {
switch ( intent ) {
case 'info':
return info;
case 'success':
return published;
case 'warning':
return caution;
case 'error':
return error;
default:
return null;
}
}

function Badge( {
className,
intent = 'default',
children,
...props
}: WordPressComponentProps< BadgeProps, 'span', false > ) {
/**
* Returns an icon based on the badge context.
*
* @return The corresponding icon for the provided context.
*/
function contextBasedIcon() {
switch ( intent ) {
case 'info':
return info;
case 'success':
return published;
case 'warning':
return caution;
case 'error':
return error;
default:
return null;
}
}
const icon = contextBasedIcon( intent );
const hasIcon = !! icon;

return (
<span
className={ clsx(
'components-badge',
`is-${ intent }`,
intent !== 'default' && 'has-icon',
className
) }
className={ clsx( 'components-badge', className, {
[ `is-${ intent }` ]: intent,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also apply the is-intent classname only if hasIcon ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not a big difference. IMO, this method is more future-proof and will avoid is-undefined if we ever remove the default for the intent prop.

'has-icon': hasIcon,
} ) }
{ ...props }
>
{ intent !== 'default' && (
{ hasIcon && (
<Icon
icon={ contextBasedIcon() }
icon={ icon }
size={ 16 }
fill="currentColor"
className="components-badge__icon"
Expand Down
Loading