-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Badge component with cva and tailwind
- Loading branch information
1 parent
b44ecaa
commit 4e558d4
Showing
4 changed files
with
146 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,50 @@ | ||
import { clsx } from 'clsx' | ||
import { ReactElement } from 'react' | ||
import { cva, VariantProps } from 'class-variance-authority' | ||
import { CSSProperties, ReactElement } from 'react' | ||
|
||
import { Box, BoxProps } from '~/components/Box' | ||
import { textVariants } from '~/tokens/typography' | ||
|
||
import * as styles from './styles.css' | ||
const badgeVariants = cva( | ||
[ | ||
'inline-flex', | ||
'flex-shrink-0', | ||
'place-items-center', | ||
'rounded-full', | ||
'text-white', | ||
'whitespace-nowrap', | ||
], | ||
{ | ||
variants: { | ||
variant: { | ||
info: ['bg-info'], | ||
warning: ['bg-warning'], | ||
success: ['bg-positive'], | ||
error: ['bg-negative'], | ||
}, | ||
|
||
type BadgeProps = BoxProps & | ||
styles.BadgeVariants & { | ||
value: ReactElement | string | number | ||
size: { | ||
sm: [textVariants({ variant: 'small' }), 'h-4', 'min-w-4', 'px-2'], | ||
md: [textVariants({ variant: 'normal' }), 'h-5', 'min-w-5', 'px-3'], | ||
lg: [textVariants({ variant: 'medium' }), 'h-6', 'min-w-6', 'px-4'], | ||
}, | ||
}, | ||
defaultVariants: { | ||
size: 'sm', | ||
}, | ||
} | ||
) | ||
|
||
interface BadgeProps extends VariantProps<typeof badgeVariants> { | ||
className?: string | ||
style?: CSSProperties | ||
value: ReactElement | string | number | ||
} | ||
|
||
export const Badge = (props: BadgeProps) => { | ||
const { className, value, variant = 'info', size = 'md', ...rest } = props | ||
|
||
return ( | ||
<Box | ||
className={clsx(className, styles.badgeVariants({ variant, size }))} | ||
display="inline-flex" | ||
flexShrink="0" | ||
placeItems="center" | ||
borderRadius="circle" | ||
color="white" | ||
whiteSpace="nowrap" | ||
{...rest} | ||
> | ||
<div className={badgeVariants({ variant, size, className })} {...rest}> | ||
{value} | ||
</Box> | ||
</div> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters