Skip to content

Commit c8506af

Browse files
authored
misc(Skeleton): start TW migration but allow backward compatibility (#1845)
1 parent 5e39e66 commit c8506af

File tree

3 files changed

+66
-57
lines changed

3 files changed

+66
-57
lines changed

src/components/designSystem/Avatar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const getBackgroundColorKey = (identifier?: string): keyof typeof colors.avatar
7777
return colorKeys[colorIndex]
7878
}
7979

80-
const avatarSizeStyles: Record<AvatarSize, string> = {
80+
export const avatarSizeStyles: Record<AvatarSize, string> = {
8181
small: cx('w-4 min-w-4 h-4 rounded'),
8282
intermediate: cx('w-6 min-w-6 h-6 rounded-lg'),
8383
medium: cx('w-8 min-w-8 h-8 rounded-xl'),
Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { cx } from 'class-variance-authority'
1+
import { cva } from 'class-variance-authority'
22
import 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

88
enum SkeletonVariantEnum {
99
connectorAvatar = 'connectorAvatar', // squared with rounded corners
@@ -22,7 +22,13 @@ type TSkeletonVariant = keyof typeof SkeletonVariantEnum
2222
interface 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

4349
interface 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+
6495
export 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

95118
const 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
`

src/pages/__devOnly/DesignSystem.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,10 @@ const DesignSystem = () => {
483483
<Skeleton variant="userAvatar" size="large" />
484484
</Block>
485485
<div>
486-
<Skeleton variant="circular" width="60px" height="60px" marginBottom="16px" />
487-
<Skeleton variant="text" width={120} height={12} marginBottom="16px" />
488-
<Skeleton variant="text" width="50%" height={12} marginBottom="16px" />
486+
<Skeleton className="mb-4 size-15" variant="circular" />
487+
<Skeleton className="mb-4 h-3 w-30" variant="text" />
488+
<Skeleton className="mb-4 h-3 w-1/2" variant="text" />
489+
<Skeleton className="mb-4 h-3" variant="text" color="dark" />
489490
</div>
490491
</Container>
491492
),

0 commit comments

Comments
 (0)