|
2 | 2 | import React from 'react'; |
3 | 3 | import { customClassSwitcher } from '~/core'; |
4 | 4 | import { clsx } from 'clsx'; |
| 5 | + |
5 | 6 | const COMPONENT_NAME = 'Skeleton'; |
6 | 7 |
|
7 | | -export type SkeletonProps = { |
8 | | - loading:boolean; |
9 | | - className?:string; |
10 | | - customRootClass?:string; |
11 | | - children:React.ReactNode; |
12 | | - height:string; |
13 | | - width:string; |
14 | | - radius?:string; |
| 8 | +export type SkeletonProps = React.ComponentPropsWithoutRef<'div'> & { |
| 9 | + loading: boolean; |
| 10 | + customRootClass?: string; |
| 11 | + height: string; |
| 12 | + width: string; |
| 13 | + radius?: string; |
| 14 | +}; |
15 | 15 |
|
16 | | -} |
| 16 | +const Skeleton = React.forwardRef<React.ElementRef<'div'>, SkeletonProps>( |
| 17 | + ( |
| 18 | + { |
| 19 | + loading = true, |
| 20 | + className = '', |
| 21 | + customRootClass = '', |
| 22 | + children, |
| 23 | + height, |
| 24 | + width, |
| 25 | + radius, |
| 26 | + style, |
| 27 | + ...props |
| 28 | + }, |
| 29 | + ref |
| 30 | + ) => { |
| 31 | + if (!loading) return <>{children}</>; |
17 | 32 |
|
18 | | -const Skeleton = ({ loading = true, className = '', customRootClass = '', children, height, width, radius, ...props }:SkeletonProps) => { |
19 | | - // If loading is false, return the children |
20 | | - if (!loading) return children; |
| 33 | + const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME); |
21 | 34 |
|
22 | | - const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME); |
23 | | - return <div |
24 | | - className={clsx(rootClass, className)} {...props} style={{ |
25 | | - |
26 | | - ['--skeleton-height' as any]: height, |
27 | | - ['--skeleton-width' as any]: width, |
28 | | - ['--skeleton-radius' as any]: radius |
29 | | - }} |
30 | | - > |
31 | | - </div>; |
32 | | -}; |
| 35 | + return ( |
| 36 | + <div |
| 37 | + ref={ref} |
| 38 | + className={clsx(rootClass, className)} |
| 39 | + style={{ |
| 40 | + ...style, |
| 41 | + ['--skeleton-height' as any]: height, |
| 42 | + ['--skeleton-width' as any]: width, |
| 43 | + ['--skeleton-radius' as any]: radius |
| 44 | + }} |
| 45 | + {...props} |
| 46 | + /> |
| 47 | + ); |
| 48 | + } |
| 49 | +); |
33 | 50 |
|
34 | 51 | Skeleton.displayName = COMPONENT_NAME; |
| 52 | + |
35 | 53 | export default Skeleton; |
0 commit comments