Skip to content

Commit

Permalink
fix: this is better
Browse files Browse the repository at this point in the history
  • Loading branch information
denkristoffer committed Apr 25, 2024
1 parent e40e301 commit 55deb37
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
35 changes: 18 additions & 17 deletions packages/components/badge/src/Badge/Badge.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import tokens from '@contentful/f36-tokens';
import { css } from 'emotion';
import type { BadgeVariant, BadgeSize, BadgeStylesProps } from '../types';
import type { CSSObject } from '@emotion/serialize';
import { CSSProperties } from 'react';
import type { BadgeInternalProps } from './Badge';

const variantToStyles = ({ variant }: { variant: BadgeVariant }): CSSObject => {
switch (variant) {
Expand Down Expand Up @@ -63,11 +63,7 @@ const sizeToStyles = ({ size }: { size: BadgeSize }): CSSObject => {
}
};

export const getBadgeStyles = (
textTransform:
| Extract<CSSProperties['textTransform'], 'none'>
| undefined = undefined,
) => ({
export const getBadgeStyles = () => ({
badge: ({ variant, size }: BadgeStylesProps) =>
css({
columnGap: tokens.spacing2Xs,
Expand All @@ -85,16 +81,21 @@ export const getBadgeStyles = (
width: '0.875rem',
height: '0.875rem',
}),
badgeText: css([
{
color: 'currentcolor',
lineHeight: 'inherit',
},
textTransform !== 'none' && {
textTransform: 'lowercase',
'&::first-letter': {
textTransform: 'uppercase',
badgeText: ({
textTransform,
}: {
textTransform: BadgeInternalProps['textTransform'];
}) =>
css([
{
color: 'currentcolor',
lineHeight: 'inherit',
},
},
]),
textTransform !== 'none' && {
textTransform: 'lowercase',
'&::first-letter': {
textTransform: 'uppercase',
},
},
]),
});
18 changes: 12 additions & 6 deletions packages/components/badge/src/Badge/Badge.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React, { CSSProperties } from 'react';
import React from 'react';
import { cx } from 'emotion';
import {
Box,
type CommonProps,
type PropsWithHTMLElement,
type ExpandProps,
} from '@contentful/f36-core';
import { Caption } from '@contentful/f36-typography';
import type * as CSS from 'csstype';

import type { BadgeSize, BadgeVariant } from '../types';
import { getBadgeStyles } from './Badge.styles';
import { Caption } from '@contentful/f36-typography';

export type BadgeInternalProps = CommonProps & {
/**
Expand All @@ -32,16 +33,20 @@ export type BadgeInternalProps = CommonProps & {
*/
endIcon?: React.ReactNode;
/**
* Expects any valid CSS text-transform value. If not provided, will default to 'capitalize'
* By default the Badge uses CSS to capitalize only the first letter of the
* badge text. This CSS is hit by a bug in Firefox that results in the badge
* being rendered slightly too wide. To avoid the bug, set this property to
* `none` to disable the text transformation. Please be sure the initial
* letter of the badge text is already capitalized!
*/
textTransform?: CSSProperties['textTransform'];
textTransform?: Extract<CSS.Property.TextTransform, 'none'> | undefined;
};

export type BadgeProps = PropsWithHTMLElement<BadgeInternalProps, 'div'>;

export const Badge = React.forwardRef<HTMLDivElement, ExpandProps<BadgeProps>>(
(props, ref) => {
const styles = getBadgeStyles(props.textTransform);
const styles = getBadgeStyles();
const {
children,
variant = 'primary',
Expand All @@ -50,6 +55,7 @@ export const Badge = React.forwardRef<HTMLDivElement, ExpandProps<BadgeProps>>(
startIcon,
endIcon,
className,
textTransform = undefined,
...otherProps
} = props;

Expand All @@ -75,7 +81,7 @@ export const Badge = React.forwardRef<HTMLDivElement, ExpandProps<BadgeProps>>(
<Caption
fontWeight="fontWeightMedium"
isTruncated
className={styles.badgeText}
className={styles.badgeText({ textTransform })}
>
{children}
</Caption>
Expand Down

0 comments on commit 55deb37

Please sign in to comment.