1- import { cx } from 'class-variance-authority'
1+ import { cva } from 'class-variance-authority'
22import styled from 'styled-components'
33
4- import { theme } from '~/styles'
4+ import { tw } from '~/styles/utils '
55
6- import { AvatarSize , mapAvatarSize } from './Avatar'
6+ import { AvatarSize , avatarSizeStyles , mapAvatarSize } from './Avatar'
77
88enum SkeletonVariantEnum {
99 connectorAvatar = 'connectorAvatar' , // squared with rounded corners
@@ -22,7 +22,13 @@ type TSkeletonVariant = keyof typeof SkeletonVariantEnum
2222interface SkeletonConnectorProps {
2323 variant : Extract < TSkeletonVariant , 'userAvatar' | 'connectorAvatar' >
2424 size : AvatarSize
25+ /**
26+ * @deprecated Use `className` and TailwindCSS instead
27+ */
2528 width ?: never
29+ /**
30+ * @deprecated Use `className` and TailwindCSS instead
31+ */
2632 height ?: never
2733 className ?: string
2834 /**
@@ -42,7 +48,13 @@ interface SkeletonConnectorProps {
4248
4349interface SkeletonGenericProps {
4450 variant : Extract < TSkeletonVariant , 'text' | 'circular' >
51+ /**
52+ * @deprecated Use `className` and TailwindCSS instead
53+ */
4554 width ?: number | string
55+ /**
56+ * @deprecated Use `className` and TailwindCSS instead
57+ */
4658 height ?: number | string
4759 size ?: never
4860 className ?: string
@@ -61,80 +73,76 @@ interface SkeletonGenericProps {
6173 color ?: keyof typeof SkeletonColorEnum
6274}
6375
76+ const skeletonStyles = cva ( 'w-full animate-pulse bg-grey-100' , {
77+ variants : {
78+ size : avatarSizeStyles ,
79+ variant : {
80+ connectorAvatar : '' , // defined in avatarSizeStyles
81+ userAvatar : '' , // defined in avatarSizeStyles
82+ text : 'h-3 rounded-3xl' ,
83+ circular : 'rounded-full' ,
84+ } ,
85+ color : {
86+ dark : 'bg-grey-300' ,
87+ light : 'bg-grey-100' ,
88+ } ,
89+ defaultVariants : {
90+ color : 'light' ,
91+ } ,
92+ } ,
93+ } )
94+
6495export const Skeleton = ( {
6596 className,
6697 variant,
67- marginRight,
98+ color,
99+ size,
68100 marginBottom,
101+ marginRight,
69102 marginTop,
70- size,
71- height,
72103 width,
73- color = SkeletonColorEnum . light ,
104+ height ,
74105} : SkeletonConnectorProps | SkeletonGenericProps ) => {
75106 return (
76107 < SkeletonContainer
77108 $marginRight = { marginRight }
78109 $marginBottom = { marginBottom }
79110 $marginTop = { marginTop }
80- $height = { ( size ? mapAvatarSize ( size ) : height ) || 12 }
81- $width = { ( size ? mapAvatarSize ( size ) : width ) || 90 }
82- className = { cx ( className , {
83- 'skeleton-variant--circular' : [
84- SkeletonVariantEnum . circular ,
85- SkeletonVariantEnum . userAvatar ,
86- ] . includes ( SkeletonVariantEnum [ variant ] ) ,
87- 'skeleton-variant--text' : variant === SkeletonVariantEnum . text ,
88- 'skeleton-variant--rounded' : variant === SkeletonVariantEnum . connectorAvatar ,
89- 'skeleton-color--dark' : color === SkeletonColorEnum . dark ,
90- } ) }
111+ $height = { size ? mapAvatarSize ( size ) : height }
112+ $width = { size ? mapAvatarSize ( size ) : width }
113+ className = { tw ( skeletonStyles ( { variant, color, size } ) , className ) }
91114 />
92115 )
93116}
94117
95118const SkeletonContainer = styled . div < {
96- $height : number | string
97- $width : number | string
119+ $height ? : number | string
120+ $width ? : number | string
98121 $marginRight ?: number | string
99122 $marginBottom ?: number | string
100123 $marginTop ?: number | string
101124} > `
102- animation: pulse 1.5s ease-in-out 0.5s infinite;
103- background-color: ${ theme . palette . grey [ 100 ] } ;
104125 height: ${ ( { $height } ) =>
105126 ! $height ? 0 : typeof $height === 'number' ? `${ $height } px` : $height } ;
106- width: 100%;
107- max-width: ${ ( { $width } ) => ( ! $width ? 0 : typeof $width === 'number' ? `${ $width } px` : $width ) } ;
127+
128+ width: ${ ( { $width } ) =>
129+ ! $width ? 0 : typeof $width === 'number' ? `${ $width } px !important` : `${ $width } !important` } ;
108130 margin-right: ${ ( { $marginRight } ) =>
109- ! $marginRight ? 0 : typeof $marginRight === 'number' ? `${ $marginRight } px` : $marginRight } ;
131+ ! $marginRight
132+ ? 0
133+ : typeof $marginRight === 'number'
134+ ? `${ $marginRight } px ! important`
135+ : `${ $marginRight } !important` } ;
110136 margin-bottom: ${ ( { $marginBottom } ) =>
111- ! $marginBottom ? 0 : typeof $marginBottom === 'number' ? `${ $marginBottom } px` : $marginBottom } ;
137+ ! $marginBottom
138+ ? 0
139+ : typeof $marginBottom === 'number'
140+ ? `${ $marginBottom } px ! important`
141+ : `${ $marginBottom } !important` } ;
112142 margin-top: ${ ( { $marginTop } ) =>
113- ! $marginTop ? 0 : typeof $marginTop === 'number' ? `${ $marginTop } px` : $marginTop } ;
114-
115- &.skeleton-color--dark {
116- background-color: ${ theme . palette . grey [ 300 ] } ;
117- }
118-
119- &.skeleton-variant--circular {
120- border-radius: 50%;
121- }
122- &.skeleton-variant--text {
123- border-radius: 32px;
124- }
125- &.skeleton-variant--rounded {
126- border-radius: 12px;
127- }
128-
129- @keyframes pulse {
130- 0% {
131- opacity: 1;
132- }
133- 50% {
134- opacity: 0.4;
135- }
136- 100% {
137- opacity: 1;
138- }
139- }
143+ ! $marginTop
144+ ? 0
145+ : typeof $marginTop === 'number'
146+ ? `${ $marginTop } px ! important`
147+ : `${ $marginTop } !important` } ;
140148`
0 commit comments